to_duration 1.1.1 → 1.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/.travis.yml +5 -2
- data/README.md +47 -34
- data/lib/numeric.rb +1 -1
- data/lib/to_duration.rb +1 -4
- data/lib/to_duration/duration.rb +91 -0
- data/lib/to_duration/version.rb +1 -1
- data/to_duration.gemspec +3 -3
- metadata +12 -12
- data/lib/to_duration/converter.rb +0 -61
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 7c88aeaa5f019f1bf7a63698277624b3fb0406e2752344a19f5e9886ee2168f6
|
4
|
+
data.tar.gz: 6821895e880e484887c691806e383b3a1b1956bffd16d71a4a9ff1445e9ebec0
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: be3f3acb88afa570c9cf86e03f04c39e8afcc9d3b74ca499d7e904f4eb8ad9e58340718879c722691279a628ad35ff22cfdc87988ef6c495ebbff32d3e843e96
|
7
|
+
data.tar.gz: d31929e163e45b969a6097c5f0f89cebdd1dfc899564ca01306fa074fac454353568414ee78a8368e6410c41529ef3133b935421e1696a43ad33ad81004e63f1
|
data/.travis.yml
CHANGED
data/README.md
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
|
3
3
|
[![Gem Version](https://badge.fury.io/rb/to_duration.svg)](https://badge.fury.io/rb/to_duration) [![Build Status](https://travis-ci.org/digaev/to_duration.svg)](https://travis-ci.org/digaev/to_duration) [![Coverage Status](https://coveralls.io/repos/github/digaev/to_duration/badge.svg?branch=master)](https://coveralls.io/github/digaev/to_duration?branch=master)
|
4
4
|
|
5
|
-
A Ruby gem for converting seconds into human-readable format.
|
5
|
+
A Ruby gem for converting seconds into human-readable format.
|
6
6
|
|
7
7
|
## Installation
|
8
8
|
|
@@ -22,6 +22,8 @@ Or install it yourself as:
|
|
22
22
|
|
23
23
|
## Usage
|
24
24
|
|
25
|
+
Just call `#to_duration` method on any `Numeric` instance.
|
26
|
+
|
25
27
|
```ruby
|
26
28
|
irb(main):001:0> 12345.to_duration
|
27
29
|
=> "3 hours, 25 minutes and 45 seconds"
|
@@ -29,50 +31,61 @@ irb(main):001:0> 12345.to_duration
|
|
29
31
|
irb(main):002:0> 1234567890.to_duration
|
30
32
|
=> "39 years, 1 month, 14 days, 5 hours, 31 minutes and 30 seconds"
|
31
33
|
|
32
|
-
irb(main):003:0> 1234567890.to_duration(
|
34
|
+
irb(main):003:0> 1234567890.to_duration(weeks: true)
|
33
35
|
=> "39 years, 1 month, 2 weeks, 5 hours, 31 minutes and 30 seconds"
|
36
|
+
|
37
|
+
irb(main):004:0> 0.5.to_duration
|
38
|
+
=> "Less than one second"
|
34
39
|
```
|
35
40
|
|
36
|
-
|
41
|
+
### #to_duration(options = {})
|
37
42
|
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
43
|
+
| Option | Type | Default |
|
44
|
+
|---|:-:|:-:|
|
45
|
+
| `:years` | Boolean | `true` |
|
46
|
+
| `:months` | Boolean | `true` |
|
47
|
+
| `:weeks` | Boolean | `false` |
|
48
|
+
| `:days` | Boolean | `true` |
|
49
|
+
| `:hours` | Boolean | `true` |
|
50
|
+
| `:minutes` | Boolean | `true` |
|
51
|
+
| `:seconds` | Boolean | `true` |
|
52
|
+
|
53
|
+
**Notice:** Before version `1.2.0` these keys were singular.
|
45
54
|
|
46
55
|
## Localization
|
47
56
|
|
48
|
-
|
57
|
+
The gem uses [I18n](https://github.com/svenfuchs/i18n) for localization.
|
49
58
|
|
50
59
|
```yml
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
60
|
+
en: # Replace this key with your locale
|
61
|
+
to_duration:
|
62
|
+
and: and
|
63
|
+
less_than_one_second: Less than one second
|
64
|
+
year:
|
65
|
+
one: year
|
66
|
+
other: years
|
67
|
+
month:
|
68
|
+
one: month
|
69
|
+
other: months
|
70
|
+
week:
|
71
|
+
one: week
|
72
|
+
other: weeks
|
73
|
+
day:
|
74
|
+
one: day
|
75
|
+
other: days
|
76
|
+
hour:
|
77
|
+
one: hour
|
78
|
+
other: hours
|
79
|
+
minute:
|
80
|
+
one: minute
|
81
|
+
other: minutes
|
82
|
+
second:
|
83
|
+
one: second
|
84
|
+
other: seconds
|
74
85
|
```
|
75
86
|
|
87
|
+
If you're on Rails you may create `config/locales/to_duration_#{locale}.yml` file.
|
88
|
+
|
76
89
|
## Contributing
|
77
90
|
|
78
91
|
Bug reports and pull requests are welcome on GitHub at https://github.com/digaev/to_duration.
|
data/lib/numeric.rb
CHANGED
data/lib/to_duration.rb
CHANGED
@@ -0,0 +1,91 @@
|
|
1
|
+
module ToDuration
|
2
|
+
class Duration
|
3
|
+
UNITS = {
|
4
|
+
years: 315_576_00,
|
5
|
+
months: 259_200_0,
|
6
|
+
weeks: 604_800,
|
7
|
+
days: 864_00,
|
8
|
+
hours: 3600,
|
9
|
+
minutes: 60,
|
10
|
+
seconds: 1
|
11
|
+
}.freeze
|
12
|
+
|
13
|
+
attr_reader :seconds
|
14
|
+
|
15
|
+
def initialize(seconds)
|
16
|
+
@seconds = Integer(seconds)
|
17
|
+
end
|
18
|
+
|
19
|
+
def years
|
20
|
+
seconds / UNITS[:years]
|
21
|
+
end
|
22
|
+
|
23
|
+
def months
|
24
|
+
seconds / UNITS[:months]
|
25
|
+
end
|
26
|
+
|
27
|
+
def weeks
|
28
|
+
seconds / UNITS[:weeks]
|
29
|
+
end
|
30
|
+
|
31
|
+
def days
|
32
|
+
seconds / UNITS[:days]
|
33
|
+
end
|
34
|
+
|
35
|
+
def hours
|
36
|
+
seconds / UNITS[:hours]
|
37
|
+
end
|
38
|
+
|
39
|
+
def minutes
|
40
|
+
seconds / UNITS[:minutes]
|
41
|
+
end
|
42
|
+
|
43
|
+
def add(unit, count)
|
44
|
+
@seconds += UNITS[unit] * count
|
45
|
+
end
|
46
|
+
|
47
|
+
def sub(unit, count)
|
48
|
+
@seconds -= UNITS[unit] * count
|
49
|
+
end
|
50
|
+
|
51
|
+
def to_s(options = {})
|
52
|
+
return t('less_than_one_second') if seconds < 1
|
53
|
+
|
54
|
+
options = {}.tap do |defaults|
|
55
|
+
UNITS.keys.each { |k| defaults[k] = k != :weeks }
|
56
|
+
end.merge!(options)
|
57
|
+
|
58
|
+
units_to_s(to_units(options))
|
59
|
+
end
|
60
|
+
|
61
|
+
private
|
62
|
+
|
63
|
+
def t(key, options = {})
|
64
|
+
I18n.t("to_duration.#{key}", options)
|
65
|
+
end
|
66
|
+
|
67
|
+
def to_units(options)
|
68
|
+
duration = Duration.new(seconds)
|
69
|
+
|
70
|
+
{}.tap do |units|
|
71
|
+
UNITS.keys.each do |k|
|
72
|
+
next if options[k] != true
|
73
|
+
|
74
|
+
count = duration.public_send(k)
|
75
|
+
next if count.zero?
|
76
|
+
|
77
|
+
units[k] = count
|
78
|
+
duration.sub(k, count)
|
79
|
+
end
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
83
|
+
def units_to_s(units)
|
84
|
+
units = units.map { |k, v| "#{v} #{t(k.to_s.chop, count: v)}" }
|
85
|
+
|
86
|
+
s = units.join(', ')
|
87
|
+
s.gsub!(/,([^,]*)$/, " #{t('and')}\\1") if units.length > 1
|
88
|
+
s
|
89
|
+
end
|
90
|
+
end
|
91
|
+
end
|
data/lib/to_duration/version.rb
CHANGED
data/to_duration.gemspec
CHANGED
@@ -22,8 +22,8 @@ Gem::Specification.new do |spec|
|
|
22
22
|
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
23
23
|
spec.require_paths = ['lib']
|
24
24
|
|
25
|
-
spec.add_development_dependency 'bundler', '~> 1.
|
26
|
-
spec.add_development_dependency 'rake', '~> 10.0'
|
27
|
-
spec.add_development_dependency 'minitest', '~> 5.0'
|
25
|
+
spec.add_development_dependency 'bundler', '~> 1.16'
|
28
26
|
spec.add_development_dependency 'coveralls', '~> 0.8'
|
27
|
+
spec.add_development_dependency 'minitest', '~> 5.0'
|
28
|
+
spec.add_development_dependency 'rake', '~> 10.0'
|
29
29
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: to_duration
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.2.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: 2018-09-24 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -16,28 +16,28 @@ dependencies:
|
|
16
16
|
requirements:
|
17
17
|
- - "~>"
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version: '1.
|
19
|
+
version: '1.16'
|
20
20
|
type: :development
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
24
|
- - "~>"
|
25
25
|
- !ruby/object:Gem::Version
|
26
|
-
version: '1.
|
26
|
+
version: '1.16'
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
|
-
name:
|
28
|
+
name: coveralls
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
30
30
|
requirements:
|
31
31
|
- - "~>"
|
32
32
|
- !ruby/object:Gem::Version
|
33
|
-
version: '
|
33
|
+
version: '0.8'
|
34
34
|
type: :development
|
35
35
|
prerelease: false
|
36
36
|
version_requirements: !ruby/object:Gem::Requirement
|
37
37
|
requirements:
|
38
38
|
- - "~>"
|
39
39
|
- !ruby/object:Gem::Version
|
40
|
-
version: '
|
40
|
+
version: '0.8'
|
41
41
|
- !ruby/object:Gem::Dependency
|
42
42
|
name: minitest
|
43
43
|
requirement: !ruby/object:Gem::Requirement
|
@@ -53,19 +53,19 @@ dependencies:
|
|
53
53
|
- !ruby/object:Gem::Version
|
54
54
|
version: '5.0'
|
55
55
|
- !ruby/object:Gem::Dependency
|
56
|
-
name:
|
56
|
+
name: rake
|
57
57
|
requirement: !ruby/object:Gem::Requirement
|
58
58
|
requirements:
|
59
59
|
- - "~>"
|
60
60
|
- !ruby/object:Gem::Version
|
61
|
-
version: '0
|
61
|
+
version: '10.0'
|
62
62
|
type: :development
|
63
63
|
prerelease: false
|
64
64
|
version_requirements: !ruby/object:Gem::Requirement
|
65
65
|
requirements:
|
66
66
|
- - "~>"
|
67
67
|
- !ruby/object:Gem::Version
|
68
|
-
version: '0
|
68
|
+
version: '10.0'
|
69
69
|
description: A Ruby gem for converting seconds into human-readable format.
|
70
70
|
email:
|
71
71
|
- ffs.cmp@gmail.com
|
@@ -85,7 +85,7 @@ files:
|
|
85
85
|
- bin/setup
|
86
86
|
- lib/numeric.rb
|
87
87
|
- lib/to_duration.rb
|
88
|
-
- lib/to_duration/
|
88
|
+
- lib/to_duration/duration.rb
|
89
89
|
- lib/to_duration/version.rb
|
90
90
|
- to_duration.gemspec
|
91
91
|
homepage: https://github.com/digaev/to_duration
|
@@ -108,7 +108,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
108
108
|
version: '0'
|
109
109
|
requirements: []
|
110
110
|
rubyforge_project:
|
111
|
-
rubygems_version: 2.
|
111
|
+
rubygems_version: 2.7.6
|
112
112
|
signing_key:
|
113
113
|
specification_version: 4
|
114
114
|
summary: A Ruby gem for converting seconds into human-readable format.
|
@@ -1,61 +0,0 @@
|
|
1
|
-
module ToDuration
|
2
|
-
class Converter
|
3
|
-
def self.call(value, options = {})
|
4
|
-
new(value, options).call
|
5
|
-
end
|
6
|
-
|
7
|
-
def initialize(value, options = {})
|
8
|
-
@value = value
|
9
|
-
@options = DEFAULTS.dup.merge!(options).freeze
|
10
|
-
end
|
11
|
-
|
12
|
-
def call
|
13
|
-
units = to_units
|
14
|
-
last = if units.length > 1
|
15
|
-
" #{I18n.t('to_duration.and')} #{units.pop}"
|
16
|
-
else
|
17
|
-
''
|
18
|
-
end
|
19
|
-
"#{units.join(', ')}#{last}"
|
20
|
-
end
|
21
|
-
|
22
|
-
private
|
23
|
-
|
24
|
-
UNITS = {
|
25
|
-
year: 315_576_00,
|
26
|
-
month: 259_200_0,
|
27
|
-
week: 604_800,
|
28
|
-
day: 864_00,
|
29
|
-
hour: 3600,
|
30
|
-
minute: 60,
|
31
|
-
second: 1
|
32
|
-
}.freeze
|
33
|
-
|
34
|
-
DEFAULTS = {
|
35
|
-
year: true,
|
36
|
-
month: true,
|
37
|
-
week: false,
|
38
|
-
day: true,
|
39
|
-
hour: true,
|
40
|
-
minute: true,
|
41
|
-
second: true
|
42
|
-
}.freeze
|
43
|
-
|
44
|
-
def to_units
|
45
|
-
seconds = @value
|
46
|
-
|
47
|
-
UNITS.map do |k, v|
|
48
|
-
next if @options[k] != true
|
49
|
-
|
50
|
-
count = seconds >= v ? seconds / v : 0
|
51
|
-
seconds -= count * v
|
52
|
-
|
53
|
-
t_unit(k, count) if count > 0
|
54
|
-
end.compact
|
55
|
-
end
|
56
|
-
|
57
|
-
def t_unit(unit, count)
|
58
|
-
"#{count} #{I18n.t("to_duration.#{unit}", count: count)}"
|
59
|
-
end
|
60
|
-
end
|
61
|
-
end
|