time_diff 0.1.1 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README +16 -0
- data/README.rdoc +17 -2
- data/VERSION +1 -1
- data/lib/time_diff.rb +43 -8
- data/test/test_time_diff.rb +15 -5
- data/time_diff.gemspec +2 -2
- metadata +5 -5
data/README
CHANGED
@@ -16,3 +16,19 @@ You can use the difference like:
|
|
16
16
|
|
17
17
|
diff[:year], diff[:month], diff[:week]
|
18
18
|
|
19
|
+
|
20
|
+
Formatted Time difference
|
21
|
+
-------------------------
|
22
|
+
|
23
|
+
%y - year
|
24
|
+
%M - month
|
25
|
+
%w - week
|
26
|
+
%d - day
|
27
|
+
%h - hour
|
28
|
+
%m - minute
|
29
|
+
%s - second
|
30
|
+
|
31
|
+
By default the format is '%y, %M, %w, %d and %h:%m:%s' this will return '1 year, 2 months, 3 weeks, 4 days and 12:05:52'.
|
32
|
+
You will get the result from the output hash, time_diff_components[:diff]
|
33
|
+
|
34
|
+
You can pass your own format as third parameter to this function.
|
data/README.rdoc
CHANGED
@@ -8,13 +8,28 @@ gem install time_diff
|
|
8
8
|
|
9
9
|
require 'time_diff'
|
10
10
|
|
11
|
-
|
11
|
+
time_diff_components = Time.diff(start_date_time, end_date_time)
|
12
12
|
|
13
13
|
This will return the hash of time difference in terms of years, month, week, day, hour, minute and second.
|
14
14
|
|
15
15
|
You can use the difference like:
|
16
16
|
|
17
|
-
|
17
|
+
time_diff_components[:year], time_diff_components[:month], time_diff_components[:week]
|
18
|
+
|
19
|
+
==Formatted Time difference
|
20
|
+
|
21
|
+
%y - year
|
22
|
+
%M - month
|
23
|
+
%w - week
|
24
|
+
%d - day
|
25
|
+
%h - hour
|
26
|
+
%m - minute
|
27
|
+
%s - second
|
28
|
+
|
29
|
+
By default the format is '%y, %M, %w, %d and %h:%m:%s' this will return '1 year, 2 months, 3 weeks, 4 days and 12:05:52'.
|
30
|
+
You will get the result from the output hash, time_diff_components[:diff]
|
31
|
+
|
32
|
+
You can pass your own format as third parameter to this function.
|
18
33
|
|
19
34
|
== Contributing to time_diff
|
20
35
|
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.
|
1
|
+
0.2.0
|
data/lib/time_diff.rb
CHANGED
@@ -2,17 +2,52 @@ require 'rubygems'
|
|
2
2
|
require 'active_support/all'
|
3
3
|
|
4
4
|
class Time
|
5
|
-
def self.diff(start_date, end_date)
|
6
|
-
|
7
|
-
|
8
|
-
distance_in_seconds = ((
|
5
|
+
def self.diff(start_date, end_date, format_string='%y, %M, %w, %d and %h:%m:%s')
|
6
|
+
start_time = start_date.to_time if start_date.respond_to?(:to_time)
|
7
|
+
end_time = end_date.to_time if end_date.respond_to?(:to_time)
|
8
|
+
distance_in_seconds = ((end_time - start_time).abs).round
|
9
9
|
components = []
|
10
10
|
|
11
11
|
%w(year month week day hour minute second).each do |interval|
|
12
|
-
|
13
|
-
distance_in_seconds -=
|
14
|
-
components <<
|
12
|
+
component = (distance_in_seconds / 1.send(interval)).floor
|
13
|
+
distance_in_seconds -= component.send(interval)
|
14
|
+
components << component
|
15
15
|
end
|
16
|
-
|
16
|
+
time_diff_components = {:year => components[0], :month => components[1], :week => components[2], :day => components[3], :hour => components[4], :minute => components[5], :second => components[6]}
|
17
|
+
format_string = remove_format_string_for_zero_components(time_diff_components, format_string)
|
18
|
+
time_diff_components[:diff] = format_date_time(time_diff_components, format_string) unless format_string.nil?
|
19
|
+
return time_diff_components
|
20
|
+
end
|
21
|
+
|
22
|
+
def Time.format_date_time(time_diff_components, format_string)
|
23
|
+
format_string.gsub!('%y', "#{time_diff_components[:year]} #{pluralize('year', time_diff_components[:year])}")
|
24
|
+
format_string.gsub!('%M', "#{time_diff_components[:month]} #{pluralize('month', time_diff_components[:month])}")
|
25
|
+
format_string.gsub!('%w', "#{time_diff_components[:week]} #{pluralize('week', time_diff_components[:week])}")
|
26
|
+
format_string.gsub!('%d', "#{time_diff_components[:day]} #{pluralize('day', time_diff_components[:day])}")
|
27
|
+
format_string.gsub!('%h', format_digit(time_diff_components[:hour]).to_s)
|
28
|
+
format_string.gsub!('%m', format_digit(time_diff_components[:minute]).to_s)
|
29
|
+
format_string.gsub!('%s', format_digit(time_diff_components[:second]).to_s)
|
30
|
+
format_string
|
31
|
+
end
|
32
|
+
|
33
|
+
def Time.pluralize(word, count)
|
34
|
+
return count > 1 ? word.pluralize : word
|
35
|
+
end
|
36
|
+
|
37
|
+
def Time.remove_format_string_for_zero_components(time_diff_components, format_string)
|
38
|
+
format_string.gsub!('%y, ','') if time_diff_components[:year] == 0
|
39
|
+
format_string.gsub!('%M, ','') if time_diff_components[:month] == 0
|
40
|
+
format_string.gsub!('%w, ','') if time_diff_components[:week] == 0
|
41
|
+
if format_string.slice(0..1) == '%d'
|
42
|
+
format_string.gsub!('%d ','') if time_diff_components[:day] == 0
|
43
|
+
else
|
44
|
+
format_string.gsub!(', %d','') if time_diff_components[:day] == 0
|
45
|
+
end
|
46
|
+
format_string.slice!(0..3) if format_string.slice(0..3) == 'and '
|
47
|
+
format_string
|
48
|
+
end
|
49
|
+
|
50
|
+
def Time.format_digit(number)
|
51
|
+
return '%02d' % number
|
17
52
|
end
|
18
53
|
end
|
data/test/test_time_diff.rb
CHANGED
@@ -3,15 +3,25 @@ require 'time'
|
|
3
3
|
|
4
4
|
class TestTimeDiff < Test::Unit::TestCase
|
5
5
|
should "return the time differnce in displayable format" do
|
6
|
-
assert_test_scenarios(Time.parse('2011-03-06'), Time.parse('2011-03-07'), {:year => 0, :month => 0, :week => 0, :day => 1, :hour => 0, :minute => 0, :second => 0})
|
7
|
-
assert_test_scenarios(Time.parse('2011-03-06'), Time.parse('2011-04-08'), {:year => 0, :month => 1, :week => 0, :day => 3, :hour => 0, :minute => 0, :second => 0})
|
8
|
-
assert_test_scenarios(Time.parse('2011-03-06 12:30:00'), Time.parse('2011-03-07 12:30:30'), {:year => 0, :month => 0, :week => 0, :day => 1, :hour => 0, :minute => 0, :second => 30})
|
9
|
-
assert_test_scenarios(Time.parse('2011-03-06'), Time.parse('2013-03-07'), {:year => 2, :month => 0, :week => 0, :day => 1, :hour => 12, :minute => 0, :second => 0})
|
10
|
-
assert_test_scenarios(Time.parse('2011-03-06'), Time.parse('2011-03-14'), {:year => 0, :month => 0, :week => 1, :day => 1, :hour => 0, :minute => 0, :second => 0})
|
6
|
+
assert_test_scenarios(Time.parse('2011-03-06'), Time.parse('2011-03-07'), {:year => 0, :month => 0, :week => 0, :day => 1, :hour => 0, :minute => 0, :second => 0, :diff => '1 day and 00:00:00'})
|
7
|
+
assert_test_scenarios(Time.parse('2011-03-06'), Time.parse('2011-04-08'), {:year => 0, :month => 1, :week => 0, :day => 3, :hour => 0, :minute => 0, :second => 0, :diff => '1 month, 3 days and 00:00:00'})
|
8
|
+
assert_test_scenarios(Time.parse('2011-03-06 12:30:00'), Time.parse('2011-03-07 12:30:30'), {:year => 0, :month => 0, :week => 0, :day => 1, :hour => 0, :minute => 0, :second => 30, :diff => '1 day and 00:00:30'})
|
9
|
+
assert_test_scenarios(Time.parse('2011-03-06'), Time.parse('2013-03-07'), {:year => 2, :month => 0, :week => 0, :day => 1, :hour => 12, :minute => 0, :second => 0, :diff => '2 years, 1 day and 12:00:00'})
|
10
|
+
assert_test_scenarios(Time.parse('2011-03-06'), Time.parse('2011-03-14'), {:year => 0, :month => 0, :week => 1, :day => 1, :hour => 0, :minute => 0, :second => 0, :diff => '1 week, 1 day and 00:00:00'})
|
11
|
+
assert_test_scenarios(Time.parse('2011-03-06 12:30:00'), Time.parse('2011-03-06 12:30:30'), {:year => 0, :month => 0, :week => 0, :day => 0, :hour => 0, :minute => 0, :second => 30, :diff => '00:00:30'})
|
12
|
+
end
|
13
|
+
|
14
|
+
should "return the time difference in a formatted text" do
|
15
|
+
assert_test_scenarios_for_formatted_diff(Time.parse('2010-03-06 12:30:00'), Time.parse('2011-03-07 12:30:30'), '%y, %d and %h:%m:%s', '1 year and 18:00:30')
|
11
16
|
end
|
12
17
|
|
13
18
|
def assert_test_scenarios(start_date, end_date, expected_result)
|
14
19
|
date_diff = Time.diff(start_date, end_date)
|
15
20
|
assert_equal(date_diff, expected_result)
|
16
21
|
end
|
22
|
+
|
23
|
+
def assert_test_scenarios_for_formatted_diff(start_date, end_date, format_string, expected_result)
|
24
|
+
date_diff = Time.diff(start_date, end_date, format_string)
|
25
|
+
assert_equal(date_diff[:diff], expected_result)
|
26
|
+
end
|
17
27
|
end
|
data/time_diff.gemspec
CHANGED
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{time_diff}
|
8
|
-
s.version = "0.
|
8
|
+
s.version = "0.2.0"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["abhilash"]
|
12
|
-
s.date = %q{2011-
|
12
|
+
s.date = %q{2011-04-04}
|
13
13
|
s.description = %q{It returns a hash file with the difference in terms of year, month, week, day, hour, minute and second}
|
14
14
|
s.email = %q{abhidsm@gmail.com}
|
15
15
|
s.extra_rdoc_files = [
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: time_diff
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 23
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
|
-
-
|
9
|
-
-
|
10
|
-
version: 0.
|
8
|
+
- 2
|
9
|
+
- 0
|
10
|
+
version: 0.2.0
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- abhilash
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2011-
|
18
|
+
date: 2011-04-04 00:00:00 +05:30
|
19
19
|
default_executable:
|
20
20
|
dependencies:
|
21
21
|
- !ruby/object:Gem::Dependency
|