time_duration_humanizer 0.1.6 → 2.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +37 -33
- data/lib/time_duration_humanizer.rb +56 -47
- data/lib/time_duration_humanizer/version.rb +1 -1
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 6dc5cfeb08e314bd679827105e34469f282f551d
|
4
|
+
data.tar.gz: 69ddae6813267280ba8eca00bec1213428385513
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 433d18188f3a68944ffb9727847665c2e1c096c8e9ee8e96d936612d21c073bbc94a0e4c699e9e2dc8c5c1ecc61724981b0b529950fee07e4801c218d58768ca
|
7
|
+
data.tar.gz: dd1d1f12ce0e547e8de922b117f1f87358c64287b7e4aff1cad001b998e6472a46cb828881d6ed8b618dad4d1279c98263b10ed462cbebeaf7f3b3f0957abbd4
|
data/README.md
CHANGED
@@ -23,39 +23,36 @@ Or install it yourself as:
|
|
23
23
|
## Usage
|
24
24
|
|
25
25
|
```ruby
|
26
|
-
irb(main):001:0>
|
26
|
+
irb(main):001:0> 12345.to_duration
|
27
27
|
=> "3 hours, 25 minutes and 45 seconds"
|
28
28
|
|
29
|
-
irb(main):002:0>
|
29
|
+
irb(main):002:0> 12345.to_duration(and_at_end: false)
|
30
30
|
=> "3 hours, 25 minutes, 45 seconds"
|
31
31
|
|
32
|
-
irb(main):003:0>
|
32
|
+
irb(main):003:0> 1234567890.to_duration
|
33
33
|
=> "39 years, 1 month, 14 days, 5 hours, 31 minutes and 30 seconds"
|
34
34
|
|
35
|
-
irb(main):004:0>
|
35
|
+
irb(main):004:0> 1234567890.to_duration(weeks: true)
|
36
36
|
=> "39 years, 1 month, 2 weeks, 5 hours, 31 minutes and 30 seconds"
|
37
37
|
|
38
|
-
irb(main):005:0>
|
38
|
+
irb(main):005:0> 62208000.to_duration
|
39
39
|
=> "1 year, 11 months, 24 days and 18 hours"
|
40
40
|
|
41
|
-
irb(main):006:0>
|
41
|
+
irb(main):006:0> 62208000.to_duration(days_in_year: 360)
|
42
42
|
=> "2 years"
|
43
43
|
```
|
44
44
|
|
45
|
-
## Options
|
45
|
+
## Options
|
46
46
|
|
47
|
-
* and_at_end - default `true`
|
48
|
-
* days_in_year - default `365.25`
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
*
|
53
|
-
*
|
54
|
-
*
|
55
|
-
*
|
56
|
-
* hours - default `true`
|
57
|
-
* minutes - default `true`
|
58
|
-
* seconds - default `true`
|
47
|
+
* `and_at_end` - default `true`
|
48
|
+
* `days_in_year` - default `365.25`
|
49
|
+
* `year` - default `true`
|
50
|
+
* `month` - default `true`
|
51
|
+
* `week` - default `false`
|
52
|
+
* `day` - default `true`
|
53
|
+
* `hour` - default `true`
|
54
|
+
* `minute` - default `true`
|
55
|
+
* `second` - default `true`
|
59
56
|
|
60
57
|
## Localization
|
61
58
|
|
@@ -65,20 +62,27 @@ In your Rails application edit `config/locales/en.yml`:
|
|
65
62
|
en:
|
66
63
|
time_duration_humanizer:
|
67
64
|
and: and
|
68
|
-
year:
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
65
|
+
year:
|
66
|
+
one: year
|
67
|
+
other: years
|
68
|
+
month:
|
69
|
+
one: month
|
70
|
+
other: months
|
71
|
+
week:
|
72
|
+
one: week
|
73
|
+
other: weeks
|
74
|
+
day:
|
75
|
+
one: day
|
76
|
+
other: days
|
77
|
+
hour:
|
78
|
+
one: hour
|
79
|
+
other: hours
|
80
|
+
minute:
|
81
|
+
one: minute
|
82
|
+
other: minutes
|
83
|
+
second:
|
84
|
+
one: second
|
85
|
+
other: seconds
|
82
86
|
```
|
83
87
|
|
84
88
|
## Contributing
|
@@ -1,52 +1,61 @@
|
|
1
|
-
require
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
}
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
value = seconds >= u ? seconds / u : 0
|
37
|
-
if value > 0
|
38
|
-
values << { name: k.to_s, value: value }
|
39
|
-
seconds -= value * u
|
40
|
-
end
|
41
|
-
end
|
42
|
-
end
|
1
|
+
require 'time_duration_humanizer/version'
|
2
|
+
|
3
|
+
class Numeric
|
4
|
+
UNITS = {
|
5
|
+
year: 315_576_00,
|
6
|
+
month: 259_200_0,
|
7
|
+
week: 604_800,
|
8
|
+
day: 864_00,
|
9
|
+
hour: 3600,
|
10
|
+
minute: 60,
|
11
|
+
second: 1
|
12
|
+
}.freeze
|
13
|
+
|
14
|
+
DEFAULTS = {
|
15
|
+
and_at_end: true,
|
16
|
+
days_in_year: 365.25,
|
17
|
+
year: true,
|
18
|
+
month: true,
|
19
|
+
week: false,
|
20
|
+
day: true,
|
21
|
+
hour: true,
|
22
|
+
minute: true,
|
23
|
+
second: true
|
24
|
+
}.freeze
|
25
|
+
|
26
|
+
def to_duration(options = {})
|
27
|
+
options = DEFAULTS.dup.merge!(options)
|
28
|
+
units = to_duration_units(options).map { |v| t_duration_unit(v) }
|
29
|
+
last = if units.length > 1 && options[:and_at_end] == true
|
30
|
+
" #{I18n.t('time_duration_humanizer.and')} #{units.pop}"
|
31
|
+
else
|
32
|
+
''
|
33
|
+
end
|
34
|
+
units.join(', ') + last
|
35
|
+
end
|
43
36
|
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
37
|
+
private
|
38
|
+
|
39
|
+
def to_duration_units(options)
|
40
|
+
units = []
|
41
|
+
seconds = to_i
|
42
|
+
|
43
|
+
UNITS.each do |k, v|
|
44
|
+
next if options[k] != true
|
45
|
+
|
46
|
+
secs = k == :year ? (options[:days_in_year] * UNITS[:day]).to_i : v
|
47
|
+
value = seconds >= secs ? seconds / secs : 0
|
48
|
+
if value > 0
|
49
|
+
units << { name: k, value: value }
|
50
|
+
seconds -= value * secs
|
51
|
+
end
|
48
52
|
end
|
53
|
+
units
|
54
|
+
end
|
49
55
|
|
50
|
-
|
56
|
+
def t_duration_unit(unit)
|
57
|
+
"#{unit[:value]} " + I18n.t(
|
58
|
+
"time_duration_humanizer.#{unit[:name]}", count: unit[:value]
|
59
|
+
)
|
51
60
|
end
|
52
61
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: time_duration_humanizer
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 2.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Nikolay Digaev
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2016-11-15 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -77,7 +77,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
77
77
|
version: '0'
|
78
78
|
requirements: []
|
79
79
|
rubyforge_project:
|
80
|
-
rubygems_version: 2.
|
80
|
+
rubygems_version: 2.5.1
|
81
81
|
signing_key:
|
82
82
|
specification_version: 4
|
83
83
|
summary: A Ruby gem for converting seconds into human-readable format.
|