time_units 0.0.1 → 0.1.0
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.md +11 -2
- data/lib/time_units.rb +1 -0
- data/lib/time_units/duration.rb +55 -0
- data/lib/time_units/version.rb +1 -1
- data/specs/unit/duration_spec.rb +22 -0
- metadata +7 -5
data/README.md
CHANGED
data/lib/time_units.rb
CHANGED
@@ -0,0 +1,55 @@
|
|
1
|
+
|
2
|
+
module TimeDuration
|
3
|
+
class << self
|
4
|
+
attr_accessor :units
|
5
|
+
end
|
6
|
+
|
7
|
+
self.units = [
|
8
|
+
[1.week, :weeks, "week", "weeks"],
|
9
|
+
[1.day, :days, "day", 'days'],
|
10
|
+
[1.hour, :hours, "hour", "hours"],
|
11
|
+
[1.minute, :minutes, "min", "mins"],
|
12
|
+
[1, :seconds, "s", "s"]
|
13
|
+
]
|
14
|
+
|
15
|
+
def human_duration
|
16
|
+
diff = {
|
17
|
+
:weeks => 0,
|
18
|
+
:days => 0,
|
19
|
+
:hours => 0,
|
20
|
+
:minutes => 0,
|
21
|
+
:seconds => 0
|
22
|
+
}
|
23
|
+
|
24
|
+
ret = []
|
25
|
+
|
26
|
+
pluralize = proc do |count, arr|
|
27
|
+
unit = (count == 1) ? arr[2] : arr[3]
|
28
|
+
"#{count} #{unit}"
|
29
|
+
end
|
30
|
+
|
31
|
+
t = self.to_i
|
32
|
+
|
33
|
+
while t > 0
|
34
|
+
TimeDuration.units.each do |(limit, field)|
|
35
|
+
if t >= limit
|
36
|
+
diff[field] += 1
|
37
|
+
t -= limit
|
38
|
+
break
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
end
|
43
|
+
|
44
|
+
ret = []
|
45
|
+
|
46
|
+
TimeDuration.units.each.with_index do |unit|
|
47
|
+
unit_name = unit[1]
|
48
|
+
ret << pluralize.(diff[unit_name], unit) if diff[unit_name] > 0
|
49
|
+
end
|
50
|
+
|
51
|
+
ret.join(' ')
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
Numeric.send(:include, TimeDuration)
|
data/lib/time_units/version.rb
CHANGED
@@ -0,0 +1,22 @@
|
|
1
|
+
require_relative '../spec_helper'
|
2
|
+
|
3
|
+
describe 'TimeDuration' do
|
4
|
+
should 'works' do
|
5
|
+
min1 = 60
|
6
|
+
hour1 = 60*min1
|
7
|
+
day1 = 24*hour1
|
8
|
+
|
9
|
+
12.human_duration.should == "12 s"
|
10
|
+
60.human_duration.should == "1 min"
|
11
|
+
120.human_duration.should == "2 mins"
|
12
|
+
124.human_duration.should == "2 mins 4 s"
|
13
|
+
|
14
|
+
(20*min1 + 36).human_duration.should == "20 mins 36 s"
|
15
|
+
|
16
|
+
(2*hour1).human_duration.should == "2 hours"
|
17
|
+
|
18
|
+
(2*day1 + 21*hour1 + 3).human_duration.should == "2 days 21 hours 3 s"
|
19
|
+
|
20
|
+
(2.weeks + 36.hours).human_duration.should == "2 weeks 1 day 12 hours"
|
21
|
+
end
|
22
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: time_units
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 0.1.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-
|
12
|
+
date: 2012-06-29 00:00:00.000000000 Z
|
13
13
|
dependencies: []
|
14
14
|
description:
|
15
15
|
email:
|
@@ -25,9 +25,11 @@ files:
|
|
25
25
|
- README.md
|
26
26
|
- Rakefile
|
27
27
|
- lib/time_units.rb
|
28
|
+
- lib/time_units/duration.rb
|
28
29
|
- lib/time_units/units.rb
|
29
30
|
- lib/time_units/version.rb
|
30
31
|
- specs/spec_helper.rb
|
32
|
+
- specs/unit/duration_spec.rb
|
31
33
|
- specs/unit/units_spec.rb
|
32
34
|
- time_units.gemspec
|
33
35
|
homepage: https://github.com/schmurfy/time_units
|
@@ -44,7 +46,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
44
46
|
version: '0'
|
45
47
|
segments:
|
46
48
|
- 0
|
47
|
-
hash:
|
49
|
+
hash: -2833575916128421829
|
48
50
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
49
51
|
none: false
|
50
52
|
requirements:
|
@@ -53,10 +55,10 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
53
55
|
version: '0'
|
54
56
|
segments:
|
55
57
|
- 0
|
56
|
-
hash:
|
58
|
+
hash: -2833575916128421829
|
57
59
|
requirements: []
|
58
60
|
rubyforge_project:
|
59
|
-
rubygems_version: 1.8.
|
61
|
+
rubygems_version: 1.8.24
|
60
62
|
signing_key:
|
61
63
|
specification_version: 3
|
62
64
|
summary: Some time helpers
|