humantime 0.2.1 → 0.2.2

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.
Files changed (3) hide show
  1. data/README.md +50 -1
  2. data/lib/humantime.rb +35 -14
  3. metadata +8 -8
data/README.md CHANGED
@@ -21,6 +21,10 @@ Output a number of seconds in human readable form:
21
21
  >> HumanTime.output 5452302
22
22
  => "2 months"
23
23
 
24
+ Output the number of seconds in human readable form, rounding up to a minimum unit:
25
+
26
+ >> HumanTime.output 53005, { :round_to => HumanTime::DAY }
27
+
24
28
  Get the difference between two integer values of seconds:
25
29
 
26
30
  >> HumanTime.between 3600, 60
@@ -30,10 +34,55 @@ Get the difference between two integer values of seconds:
30
34
 
31
35
  >> HumanTime.between Time.now, Time.now
32
36
  => "0 seconds"
37
+
38
+ >> t1 = Time.utc( 2011, 12, 1 )
39
+ => 2011-12-01 00:00:00 UTC
40
+ >> t2 = Time.utc( 2011, 12, 8 )
41
+ => 2011-12-08 00:00:00 UTC
42
+ >> HumanTime.between t1, t2
43
+ => "1 week"
44
+
45
+ >> t1 = Time.now
46
+ => 2012-04-03 11:35:22 +0100
47
+ >> t2 = Time.now + 3600
48
+ => 2012-04-03 12:35:26 +0100
49
+ >> HumanTime.between t1, t2
50
+ => "1 hour"
51
+
52
+ You can also additional options to #output or #between.
53
+
54
+ To round up times:
33
55
 
56
+ >> HumanTime.output 23
57
+ => "23 seconds"
58
+ >> HumanTime.output 23, { :round_to => HumanTime::MINUTE }
59
+ => "1 minute"
60
+ >> HumanTime.output 234
61
+ => "3 minutes 54 seconds"
62
+ >> HumanTime.output 234, { :round_to => HumanTime::HOUR }
63
+ => "1 hour"
64
+ >> HumanTime.output 53005
65
+ => "14 hours 43 minutes"
66
+ >> HumanTime.output 53005, { :round_to => HumanTime::DAY }
67
+ => "1 day"
68
+ >> HumanTime.output 530052
69
+ => "6 days"
70
+ >> HumanTime.output 530052, { :round_to => HumanTime::WEEK }
71
+ => "6 days"
72
+ >> HumanTime.output 1209600
73
+ => "2 weeks"
74
+ >> HumanTime.output 1209600, { :round_to => HumanTime::MONTH }
75
+ => "1 month"
76
+ >> HumanTime.output 5452302
77
+ => "2 months"
78
+ >> HumanTime.output 5452302, { :round_to => HumanTime::YEAR }
79
+ => "2 months"
80
+
34
81
  >> t1 = Time.now
35
82
  => 2012-04-03 11:35:22 +0100
36
83
  >> t2 = Time.now + 3600
37
84
  => 2012-04-03 12:35:26 +0100
38
85
  >> HumanTime.between t1, t2
39
- => "1 hour"
86
+ => "1 hour"
87
+ >> HumanTime.between t1, t2, { :round_to => HumanTime::DAY }
88
+ => "1 day"
data/lib/humantime.rb CHANGED
@@ -4,11 +4,12 @@ class HumanTime
4
4
  MINUTE = 60
5
5
  HOUR = 3600
6
6
  DAY = 86400
7
+ WEEK = 604800
7
8
  MONTH = 2629743
8
9
  YEAR = 31556926
9
10
 
10
11
  # Parse an integer and return the string representation of a time
11
- def self.output int
12
+ def self.output int, options = {}
12
13
  str = []
13
14
  display = ['years', 'months', 'days', 'hours', 'minutes', 'seconds']
14
15
 
@@ -31,13 +32,18 @@ class HumanTime
31
32
  end
32
33
 
33
34
  # Months
34
- if int < YEAR && int > ( MONTH - 1 ) && display.include?('months')
35
+ if ( int < YEAR && int > ( MONTH - 1 ) && display.include?('months') ) || options[:round_to] == MONTH
35
36
  months = int / MONTH
36
37
  int = int - ( months * MONTH )
37
38
  display.delete 'minutes'
38
39
  display.delete 'seconds'
39
40
  display.delete 'hours'
40
41
 
42
+ if options[:round_to] == MONTH
43
+ months = months.round + 1
44
+ int = 0
45
+ end
46
+
41
47
  if months.round == 1
42
48
  str << "#{months.round} month"
43
49
  elsif months.round == 12
@@ -49,12 +55,17 @@ class HumanTime
49
55
  end
50
56
 
51
57
  # Days
52
- if int < MONTH && int > ( DAY - 1 ) && display.include?('hours')
58
+ if ( int < MONTH && int > ( DAY - 1 ) && display.include?('hours')) || options[:round_to] == DAY
53
59
  days = int / DAY
54
60
  int = int - ( days * DAY )
55
61
  display.delete 'minutes'
56
62
  display.delete 'seconds'
57
63
 
64
+ if options[:round_to] == DAY
65
+ days = days.round + 1
66
+ int = 0
67
+ end
68
+
58
69
  if days.round == 1
59
70
  str << "#{days.round} day"
60
71
  else
@@ -74,32 +85,42 @@ class HumanTime
74
85
  end
75
86
 
76
87
  # Hours
77
- if int < DAY && int > ( HOUR - 1 ) && display.include?('hours')
88
+ if ( int < DAY && int > ( HOUR - 1 ) && display.include?('hours') ) || options[:round_to] == HOUR
78
89
  hours = int / HOUR
79
90
  int = int - ( hours * HOUR )
80
91
  display.delete 'seconds'
81
92
 
82
- if hours.round == 1
83
- str << "#{hours.round} hour"
93
+ if options[:round_to] == HOUR
94
+ hours = hours.round + 1
95
+ int = 0
96
+ end
97
+
98
+ if hours == 1
99
+ str << "#{hours} hour"
84
100
  else
85
- str << "#{hours.round} hours"
101
+ str << "#{hours} hours"
86
102
  end
87
103
  end
88
104
 
89
105
  # Minutes
90
- if int < HOUR && int > ( MINUTE - 1 ) && display.include?('minutes')
106
+ if ( int < HOUR && int > ( MINUTE - 1 ) && display.include?('minutes') ) || options[:round_to] == MINUTE
91
107
  mins = int / MINUTE
92
108
  int = int - ( mins * MINUTE )
93
109
 
94
- if mins.round == 1
95
- str << "#{mins.round} minute"
110
+ if options[:round_to] == MINUTE
111
+ mins = mins.round + 1
112
+ int = 0
113
+ end
114
+
115
+ if mins == 1
116
+ str << "#{mins} minute"
96
117
  else
97
- str << "#{mins.round} minutes"
118
+ str << "#{mins} minutes"
98
119
  end
99
120
  end
100
121
 
101
122
  # Seconds
102
- if int < MINUTE && int > 0 && display.include?('seconds')
123
+ if int < MINUTE && int > 0 && display.include?('seconds')
103
124
  if int.round == 1
104
125
  str << "#{int.round} second"
105
126
  else
@@ -111,7 +132,7 @@ class HumanTime
111
132
  end
112
133
 
113
134
  # Output the difference betwen two integer / DateTime / Time values
114
- def self.between start_time, end_time
135
+ def self.between start_time, end_time, options = {}
115
136
  et = end_time.to_i
116
137
  st = start_time.to_i
117
138
 
@@ -122,7 +143,7 @@ class HumanTime
122
143
  else
123
144
  diff = 0
124
145
  end
125
- self.output diff
146
+ self.output diff, options
126
147
  end
127
148
 
128
149
  # From https://github.com/hpoydar/chronic_duration
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: humantime
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.2.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -13,7 +13,7 @@ date: 2012-04-03 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: simplecov
16
- requirement: &70092746521980 !ruby/object:Gem::Requirement
16
+ requirement: &70274888367980 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: '0'
22
22
  type: :development
23
23
  prerelease: false
24
- version_requirements: *70092746521980
24
+ version_requirements: *70274888367980
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: bundler
27
- requirement: &70092746520160 !ruby/object:Gem::Requirement
27
+ requirement: &70274888366260 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: '0'
33
33
  type: :development
34
34
  prerelease: false
35
- version_requirements: *70092746520160
35
+ version_requirements: *70274888366260
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: jeweler
38
- requirement: &70092746518240 !ruby/object:Gem::Requirement
38
+ requirement: &70274888363980 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ~>
@@ -43,7 +43,7 @@ dependencies:
43
43
  version: 1.8.3
44
44
  type: :development
45
45
  prerelease: false
46
- version_requirements: *70092746518240
46
+ version_requirements: *70274888363980
47
47
  description: Outputs human readable times
48
48
  email: james@jamesinman.co.uk
49
49
  executables: []
@@ -70,7 +70,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
70
70
  version: '0'
71
71
  segments:
72
72
  - 0
73
- hash: 2380798774758535066
73
+ hash: 2626903473191386984
74
74
  required_rubygems_version: !ruby/object:Gem::Requirement
75
75
  none: false
76
76
  requirements: