time_duration_humanizer 0.1.3
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.
- checksums.yaml +7 -0
- data/.gitignore +10 -0
- data/.travis.yml +4 -0
- data/Gemfile +4 -0
- data/README.md +86 -0
- data/Rakefile +1 -0
- data/bin/console +14 -0
- data/bin/setup +7 -0
- data/lib/time_duration_humanizer.rb +44 -0
- data/lib/time_duration_humanizer/version.rb +3 -0
- data/time_duration_humanizer.gemspec +28 -0
- metadata +97 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: d7ed4da9e5a159f1bffef3af39ad92ff00ba2d58
|
4
|
+
data.tar.gz: 4d4f818658ea9d33f0feca93bd255292b22d5f4f
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 7b5b256445406de2868b4bf3478b548f880e68bbbd47e86e3a48e244599bcc42e7ce22a0731c260c503b256c43a4c3f33970601951fe71e28d04c8a347489b12
|
7
|
+
data.tar.gz: e747dca5f31c31cc03338be075cc213875d65048e9dafdd49f4780fecbf469b080b1237536346cc360372058452f98f050457f0936279fbf107a35c61ede1fed
|
data/.gitignore
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/README.md
ADDED
@@ -0,0 +1,86 @@
|
|
1
|
+
# TimeDurationHumanizer
|
2
|
+
|
3
|
+
[](https://badge.fury.io/rb/time_duration_humanizer)
|
4
|
+
|
5
|
+
An extremely simple gem for converting seconds to human-readable format.
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
gem 'time_duration_humanizer'
|
13
|
+
```
|
14
|
+
|
15
|
+
And then execute:
|
16
|
+
|
17
|
+
$ bundle
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
|
21
|
+
$ gem install time_duration_humanizer
|
22
|
+
|
23
|
+
## Usage
|
24
|
+
|
25
|
+
```ruby
|
26
|
+
irb(main):001:0> TimeDurationHumanizer.humanize(12345)
|
27
|
+
=> "3 hours, 25 minutes and 45 seconds"
|
28
|
+
|
29
|
+
irb(main):002:0> TimeDurationHumanizer.humanize(12345, { and_at_end: false })
|
30
|
+
=> "3 hours, 25 minutes, 45 seconds"
|
31
|
+
|
32
|
+
irb(main):003:0> TimeDurationHumanizer.humanize(1234567890)
|
33
|
+
=> "39 years, 1 month, 14 days, 5 hours, 31 minutes and 30 seconds"
|
34
|
+
|
35
|
+
irb(main):004:0> TimeDurationHumanizer.humanize(1234567890, {}, { weeks: true })
|
36
|
+
=> "39 years, 1 month, 2 weeks, 5 hours, 31 minutes and 30 seconds"
|
37
|
+
|
38
|
+
irb(main):005:0> TimeDurationHumanizer.humanize(62208000)
|
39
|
+
=> "1 year, 11 months, 24 days and 18 hours"
|
40
|
+
|
41
|
+
irb(main):006:0> TimeDurationHumanizer.humanize(62208000, { days_in_year: 360 })
|
42
|
+
=> "2 years"
|
43
|
+
```
|
44
|
+
|
45
|
+
## Options (seconds parameter)
|
46
|
+
|
47
|
+
* end_at_end - default `true`
|
48
|
+
* days_in_year - default `365.25`
|
49
|
+
|
50
|
+
## Units (third parameter)
|
51
|
+
|
52
|
+
* years - default `true`
|
53
|
+
* months - default `true`
|
54
|
+
* weeks - default `false`
|
55
|
+
* days - default `true`
|
56
|
+
* hours - default `true`
|
57
|
+
* minutes - default `true`
|
58
|
+
* seconds - default `true`
|
59
|
+
|
60
|
+
## Localization
|
61
|
+
|
62
|
+
In your Rails application edit `config/locales/en.yml`:
|
63
|
+
|
64
|
+
```yml
|
65
|
+
en:
|
66
|
+
time_duration_humanizer:
|
67
|
+
and: and
|
68
|
+
year: year
|
69
|
+
years: years
|
70
|
+
month: month
|
71
|
+
months: months
|
72
|
+
week: week
|
73
|
+
weeks: weeks
|
74
|
+
day: day
|
75
|
+
days: days
|
76
|
+
hour: hour
|
77
|
+
hours: hours
|
78
|
+
minute: minute
|
79
|
+
minutes: minutes
|
80
|
+
second: second
|
81
|
+
seconds: seconds
|
82
|
+
```
|
83
|
+
|
84
|
+
## Contributing
|
85
|
+
|
86
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/digaev/time_duration_humanizer.
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "time_duration_humanizer"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start
|
data/bin/setup
ADDED
@@ -0,0 +1,44 @@
|
|
1
|
+
require "time_duration_humanizer/version"
|
2
|
+
|
3
|
+
module TimeDurationHumanizer
|
4
|
+
@@known_units = [:years, :months, :weeks, :days, :hours, :minutes, :seconds]
|
5
|
+
|
6
|
+
def self.humanize(seconds, options = {}, units = {})
|
7
|
+
options = {
|
8
|
+
and_at_end: true,
|
9
|
+
days_in_year: 365.25
|
10
|
+
}.merge!(options.symbolize_keys)
|
11
|
+
|
12
|
+
units = {
|
13
|
+
years: true,
|
14
|
+
months: true,
|
15
|
+
weeks: false,
|
16
|
+
days: true,
|
17
|
+
hours: true,
|
18
|
+
minutes: true,
|
19
|
+
seconds: true
|
20
|
+
}.merge!(units.symbolize_keys)
|
21
|
+
|
22
|
+
values = []
|
23
|
+
duration = ''
|
24
|
+
|
25
|
+
units.each do |k, v|
|
26
|
+
if v == true && @@known_units.include?(k)
|
27
|
+
u = (k == :years ? options[:days_in_year].days : 1.send(k)).to_i
|
28
|
+
value = seconds >= u ? seconds / u : 0
|
29
|
+
if value > 0
|
30
|
+
values << { name: k.to_s, value: value }
|
31
|
+
seconds -= value * u
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
l = values.length
|
37
|
+
values.each_with_index do |v, i|
|
38
|
+
separator = i == l - 1 ? '' : (i == l - 2 && options[:and_at_end] == true ? " #{I18n.t('time_duration_humanizer.and')} " : ', ')
|
39
|
+
duration += "#{v[:value]} #{I18n.t("time_duration_humanizer.#{v[:value] == 1 ? v[:name].singularize : v[:name]}")}#{separator}"
|
40
|
+
end
|
41
|
+
|
42
|
+
duration
|
43
|
+
end
|
44
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'time_duration_humanizer/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "time_duration_humanizer"
|
8
|
+
spec.version = TimeDurationHumanizer::VERSION
|
9
|
+
spec.authors = ["Nikolay Digaev"]
|
10
|
+
spec.email = ["ffs.cmp@gmail.com"]
|
11
|
+
|
12
|
+
spec.summary = %q{An extremely simple gem for converting seconds to human-readable format.}
|
13
|
+
spec.description = %q{An extremely simple gem for converting seconds to human-readable format (12345 => "3 hours, 25 minutes and 45 seconds").}
|
14
|
+
spec.homepage = "https://github.com/digaev/time_duration_humanier"
|
15
|
+
spec.license = "MIT"
|
16
|
+
|
17
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
18
|
+
spec.bindir = "exe"
|
19
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
20
|
+
spec.require_paths = ["lib"]
|
21
|
+
|
22
|
+
spec.required_ruby_version = ">= 1.9.3"
|
23
|
+
|
24
|
+
spec.add_runtime_dependency "i18n", "~> 0.6"
|
25
|
+
|
26
|
+
spec.add_development_dependency "bundler", "~> 1.10"
|
27
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
28
|
+
end
|
metadata
ADDED
@@ -0,0 +1,97 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: time_duration_humanizer
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.3
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Nikolay Digaev
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-12-07 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: i18n
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0.6'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0.6'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: bundler
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '1.10'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '1.10'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rake
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '10.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '10.0'
|
55
|
+
description: An extremely simple gem for converting seconds to human-readable format
|
56
|
+
(12345 => "3 hours, 25 minutes and 45 seconds").
|
57
|
+
email:
|
58
|
+
- ffs.cmp@gmail.com
|
59
|
+
executables: []
|
60
|
+
extensions: []
|
61
|
+
extra_rdoc_files: []
|
62
|
+
files:
|
63
|
+
- ".gitignore"
|
64
|
+
- ".travis.yml"
|
65
|
+
- Gemfile
|
66
|
+
- README.md
|
67
|
+
- Rakefile
|
68
|
+
- bin/console
|
69
|
+
- bin/setup
|
70
|
+
- lib/time_duration_humanizer.rb
|
71
|
+
- lib/time_duration_humanizer/version.rb
|
72
|
+
- time_duration_humanizer.gemspec
|
73
|
+
homepage: https://github.com/digaev/time_duration_humanier
|
74
|
+
licenses:
|
75
|
+
- MIT
|
76
|
+
metadata: {}
|
77
|
+
post_install_message:
|
78
|
+
rdoc_options: []
|
79
|
+
require_paths:
|
80
|
+
- lib
|
81
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
82
|
+
requirements:
|
83
|
+
- - ">="
|
84
|
+
- !ruby/object:Gem::Version
|
85
|
+
version: 1.9.3
|
86
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
87
|
+
requirements:
|
88
|
+
- - ">="
|
89
|
+
- !ruby/object:Gem::Version
|
90
|
+
version: '0'
|
91
|
+
requirements: []
|
92
|
+
rubyforge_project:
|
93
|
+
rubygems_version: 2.4.5.1
|
94
|
+
signing_key:
|
95
|
+
specification_version: 4
|
96
|
+
summary: An extremely simple gem for converting seconds to human-readable format.
|
97
|
+
test_files: []
|