time_diff 0.2.0 → 0.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/README CHANGED
@@ -1,34 +1,63 @@
1
- Installing
2
- ----------
1
+ = time_diff
2
+ Gem which calculates the difference between two time
3
3
 
4
- gem install time_diff
4
+ == Installation
5
5
 
6
- Usage
7
- -----
6
+ gem install time_diff
8
7
 
9
- require 'time_diff'
8
+ == Usage
10
9
 
11
- diff = Time.diff(start_date_time, end_date_time)
10
+ require 'time_diff'
11
+
12
+ time_diff_components = Time.diff(start_date_time, end_date_time)
12
13
 
13
14
  This will return the hash of time difference in terms of years, month, week, day, hour, minute and second.
14
15
 
15
16
  You can use the difference like:
16
17
 
17
- diff[:year], diff[:month], diff[:week]
18
+ time_diff_components[:year], time_diff_components[:month], time_diff_components[:week]
19
+
20
+ == Formatted Time difference
21
+
22
+ %y - year
23
+
24
+ %M - month
25
+
26
+ %w - week
27
+
28
+ %d - day
29
+
30
+ %H - hour
31
+
32
+ %N - minute
18
33
 
34
+ %S - second
19
35
 
20
- Formatted Time difference
21
- -------------------------
36
+ %h - hour (without adding 'hour' text to the hours. eg: 3 for 3 hours)
22
37
 
23
- %y - year
24
- %M - month
25
- %w - week
26
- %d - day
27
- %h - hour
28
- %m - minute
29
- %s - second
38
+ %m - minute (without adding 'minute' text)
39
+
40
+ %s - second (without adding 'second' text)
41
+
42
+ By default the format is
43
+
44
+ '%y, %M, %w, %d and %h:%m:%s'
45
+
46
+ this will return
47
+
48
+ '1 year, 2 months, 3 weeks, 4 days and 12:05:52'.
30
49
 
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
50
  You will get the result from the output hash, time_diff_components[:diff]
33
51
 
34
52
  You can pass your own format as third parameter to this function.
53
+
54
+ If you give '%d %h' as the third parameter to the Time.diff() method, then the difference(time_diff_components[:diff]) will be calculated only in days and hours.
55
+
56
+ == Examples
57
+
58
+ > Time.diff(Time.parse('2011-03-06'), Time.parse('2011-03-07'))
59
+ => {:year => 0, :month => 0, :week => 0, :day => 1, :hour => 0, :minute => 0, :second => 0, :diff => '1 day and 00:00:00'}
60
+ > Time.diff(Time.parse('2010-03-06 12:30:00'), Time.parse('2011-03-07 12:30:30'), '%y, %d and %h:%m:%s')
61
+ => {:year => 1, :month => 0, :week => 0, :day => 0, :hour => 18, :minute => 0, :second => 30, :diff => '1 year and 18:00:30'}
62
+ > Time.diff(Time.parse('2011-03-06 12:30:00'), Time.parse('2011-03-07 12:30:30'), '%H %N %S')
63
+ => {:year => 0, :month => 0, :week => 0, :day => 1, :hour => 0, :minute => 0, :second => 30, :diff => '24 hours 0 minute 30 seconds'}
@@ -1,36 +1,67 @@
1
1
  = time_diff
2
+ Gem which calculates the difference between two time
2
3
 
3
- ==Installing
4
+ == Installation
4
5
 
5
- gem install time_diff
6
+ gem install time_diff
6
7
 
7
- ==Usage
8
+ == Usage
8
9
 
9
- require 'time_diff'
10
+ require 'time_diff'
10
11
 
11
- time_diff_components = Time.diff(start_date_time, end_date_time)
12
+ time_diff_components = Time.diff(start_date_time, end_date_time)
12
13
 
13
14
  This will return the hash of time difference in terms of years, month, week, day, hour, minute and second.
14
15
 
15
16
  You can use the difference like:
16
17
 
17
- time_diff_components[:year], time_diff_components[:month], time_diff_components[:week]
18
+ time_diff_components[:year], time_diff_components[:month], time_diff_components[:week]
18
19
 
19
- ==Formatted Time difference
20
+ == Formatted Time difference
20
21
 
21
- %y - year
22
- %M - month
23
- %w - week
24
- %d - day
25
- %h - hour
26
- %m - minute
27
- %s - second
22
+ %y - year
23
+
24
+ %M - month
25
+
26
+ %w - week
27
+
28
+ %d - day
29
+
30
+ %H - hour
31
+
32
+ %N - minute
33
+
34
+ %S - second
35
+
36
+ %h - hour (without adding 'hour' text to the hours. eg: 3 for 3 hours)
37
+
38
+ %m - minute (without adding 'minute' text)
39
+
40
+ %s - second (without adding 'second' text)
41
+
42
+ By default the format is
43
+
44
+ '%y, %M, %w, %d and %h:%m:%s'
45
+
46
+ this will return
47
+
48
+ '1 year, 2 months, 3 weeks, 4 days and 12:05:52'.
28
49
 
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
50
  You will get the result from the output hash, time_diff_components[:diff]
31
51
 
32
52
  You can pass your own format as third parameter to this function.
33
53
 
54
+ If you give '%d %h' as the third parameter to the Time.diff() method, then the difference(time_diff_components[:diff]) will be calculated only in days and hours.
55
+
56
+ == Examples
57
+
58
+ > Time.diff(Time.parse('2011-03-06'), Time.parse('2011-03-07'))
59
+ => {:year => 0, :month => 0, :week => 0, :day => 1, :hour => 0, :minute => 0, :second => 0, :diff => '1 day and 00:00:00'}
60
+ > Time.diff(Time.parse('2010-03-06 12:30:00'), Time.parse('2011-03-07 12:30:30'), '%y, %d and %h:%m:%s')
61
+ => {:year => 1, :month => 0, :week => 0, :day => 0, :hour => 18, :minute => 0, :second => 30, :diff => '1 year and 18:00:30'}
62
+ > Time.diff(Time.parse('2011-03-06 12:30:00'), Time.parse('2011-03-07 12:30:30'), '%H %N %S')
63
+ => {:year => 0, :month => 0, :week => 0, :day => 1, :hour => 0, :minute => 0, :second => 30, :diff => '24 hours 0 minute 30 seconds'}
64
+
34
65
  == Contributing to time_diff
35
66
 
36
67
  * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
@@ -43,6 +74,6 @@ You can pass your own format as third parameter to this function.
43
74
 
44
75
  == Copyright
45
76
 
46
- Copyright (c) 2011 abhilash. See LICENSE.txt for
77
+ Copyright (c) 2011 abhidsm. See LICENSE.txt for
47
78
  further details.
48
79
 
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.2.0
1
+ 0.2.1
@@ -6,27 +6,61 @@ class Time
6
6
  start_time = start_date.to_time if start_date.respond_to?(:to_time)
7
7
  end_time = end_date.to_time if end_date.respond_to?(:to_time)
8
8
  distance_in_seconds = ((end_time - start_time).abs).round
9
- components = []
10
9
 
11
- %w(year month week day hour minute second).each do |interval|
10
+ components = get_time_diff_components(%w(year month week day hour minute second), distance_in_seconds)
11
+ time_diff_components = {:year => components[0], :month => components[1], :week => components[2], :day => components[3], :hour => components[4], :minute => components[5], :second => components[6]}
12
+
13
+ formatted_intervals = get_formatted_intervals(format_string)
14
+ components = get_time_diff_components(formatted_intervals, distance_in_seconds)
15
+ formatted_components = create_formatted_component_hash(components, formatted_intervals)
16
+ format_string = remove_format_string_for_zero_components(formatted_components, format_string)
17
+ time_diff_components[:diff] = format_date_time(formatted_components, format_string) unless format_string.nil?
18
+ return time_diff_components
19
+ end
20
+
21
+ def self.get_formatted_intervals(format_string)
22
+ intervals = []
23
+ intervals << 'year' if format_string.include?('%y')
24
+ intervals << 'month' if format_string.include?('%M')
25
+ intervals << 'week' if format_string.include?('%w')
26
+ intervals << 'day' if format_string.include?('%d')
27
+ intervals << 'hour' if format_string.include?('%h') || format_string.include?('%H')
28
+ intervals << 'minute' if format_string.include?('%m') || format_string.include?('%N')
29
+ intervals << 'second' if format_string.include?('%s') || format_string.include?('%S')
30
+ intervals
31
+ end
32
+
33
+ def self.create_formatted_component_hash(components, formatted_intervals)
34
+ formatted_components = {}
35
+ index = 0
36
+ components.each do |component|
37
+ formatted_components[:"#{formatted_intervals[index]}"] = component
38
+ index = index + 1
39
+ end
40
+ formatted_components
41
+ end
42
+
43
+ def self.get_time_diff_components(intervals, distance_in_seconds)
44
+ components = []
45
+ intervals.each do |interval|
12
46
  component = (distance_in_seconds / 1.send(interval)).floor
13
47
  distance_in_seconds -= component.send(interval)
14
48
  components << component
15
49
  end
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
50
+ components
20
51
  end
21
52
 
22
53
  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)
54
+ format_string.gsub!('%y', "#{time_diff_components[:year]} #{pluralize('year', time_diff_components[:year])}") if time_diff_components[:year]
55
+ format_string.gsub!('%M', "#{time_diff_components[:month]} #{pluralize('month', time_diff_components[:month])}") if time_diff_components[:month]
56
+ format_string.gsub!('%w', "#{time_diff_components[:week]} #{pluralize('week', time_diff_components[:week])}") if time_diff_components[:week]
57
+ format_string.gsub!('%d', "#{time_diff_components[:day]} #{pluralize('day', time_diff_components[:day])}") if time_diff_components[:day]
58
+ format_string.gsub!('%H', "#{time_diff_components[:hour]} #{pluralize('hour', time_diff_components[:hour])}") if time_diff_components[:hour]
59
+ format_string.gsub!('%N', "#{time_diff_components[:minute]} #{pluralize('minute', time_diff_components[:minute])}") if time_diff_components[:minute]
60
+ format_string.gsub!('%S', "#{time_diff_components[:second]} #{pluralize('second', time_diff_components[:second])}") if time_diff_components[:second]
61
+ format_string.gsub!('%h', format_digit(time_diff_components[:hour]).to_s) if time_diff_components[:hour]
62
+ format_string.gsub!('%m', format_digit(time_diff_components[:minute]).to_s) if time_diff_components[:minute]
63
+ format_string.gsub!('%s', format_digit(time_diff_components[:second]).to_s) if time_diff_components[:second]
30
64
  format_string
31
65
  end
32
66
 
@@ -13,6 +13,8 @@ class TestTimeDiff < Test::Unit::TestCase
13
13
 
14
14
  should "return the time difference in a formatted text" do
15
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')
16
+ assert_test_scenarios_for_formatted_diff(Time.parse('2010-03-06 12:30:00'), Time.parse('2011-03-07 12:30:30'), '%d %H %N %S', '366 days 0 hour 0 minute 30 seconds')
17
+ assert_test_scenarios_for_formatted_diff(Time.parse('2011-03-06 12:30:00'), Time.parse('2011-03-07 12:30:30'), '%H %N %S', '24 hours 0 minute 30 seconds')
16
18
  end
17
19
 
18
20
  def assert_test_scenarios(start_date, end_date, expected_result)
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{time_diff}
8
- s.version = "0.2.0"
8
+ s.version = "0.2.1"
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-04-04}
12
+ s.date = %q{2011-04-10}
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: 23
4
+ hash: 21
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 2
9
- - 0
10
- version: 0.2.0
9
+ - 1
10
+ version: 0.2.1
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-04-04 00:00:00 +05:30
18
+ date: 2011-04-10 00:00:00 +05:30
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency