human_seconds 0.0.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.
- checksums.yaml +7 -0
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +38 -0
- data/Rakefile +8 -0
- data/human_seconds.gemspec +24 -0
- data/lib/human_seconds.rb +57 -0
- data/lib/human_seconds/version.rb +3 -0
- data/spec/human_seconds_spec.rb +73 -0
- metadata +96 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 6a7031f3711775b80787d09d6e4948a6d689b90c
|
4
|
+
data.tar.gz: a09fe209cbe8aba89760904259e5b1a22478b74f
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: c7bd3ab9ccba5f4d7cf5367583af607913735ff53970b10a8a887612a8bf602e31086c8f2392b198f422ff8de963dcfc4efbc88a09b002ee0e1786864d18cbb4
|
7
|
+
data.tar.gz: 106cb44ecf4261b2b6ed50dfd009365fadd058a4f3a916f0dbae11a1a65078ce662d4c056a0a74de9c0ea1e4d6c49e21f1bd8faa37c97bd336b9c72194ee887f
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 Brendon Murphy
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,38 @@
|
|
1
|
+
# HumanSeconds
|
2
|
+
|
3
|
+
Converts seconds like 7261 into a human readable string '2h1m1s'.
|
4
|
+
|
5
|
+
Also can parse that format back to integer seconds.
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
gem 'human_seconds'
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install human_seconds
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
```ruby
|
24
|
+
hs = HumanSeconds.new(61)
|
25
|
+
hs.to_i #=> 61
|
26
|
+
hs.to_s #=> 1m1s
|
27
|
+
|
28
|
+
HumanSeconds.parse('1m1s').to_i # => 61
|
29
|
+
```
|
30
|
+
|
31
|
+
## Contributing
|
32
|
+
|
33
|
+
1. Fork it ( http://github.com/<my-github-username>/human_seconds/fork )
|
34
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
35
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
36
|
+
4. Make sure specs pass (`bundle exec rake spec`)
|
37
|
+
5. Push to the branch (`git push origin my-new-feature`)
|
38
|
+
6. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'human_seconds/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "human_seconds"
|
8
|
+
spec.version = HumanSeconds::VERSION
|
9
|
+
spec.authors = ["Brendon Murphy"]
|
10
|
+
spec.email = ["xternal1+github@gmail.com"]
|
11
|
+
spec.summary = %q{Converts seconds like 7261 into a human readable string '2h1m1s'}
|
12
|
+
spec.description = spec.summary
|
13
|
+
spec.homepage = ""
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0")
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_development_dependency "bundler", "~> 1.5"
|
22
|
+
spec.add_development_dependency "rake"
|
23
|
+
spec.add_development_dependency "minitest", "~> 5.4.0"
|
24
|
+
end
|
@@ -0,0 +1,57 @@
|
|
1
|
+
require "human_seconds/version"
|
2
|
+
|
3
|
+
class HumanSeconds
|
4
|
+
attr_reader :total_seconds
|
5
|
+
|
6
|
+
def initialize(total_seconds)
|
7
|
+
@total_seconds = total_seconds
|
8
|
+
end
|
9
|
+
|
10
|
+
def self.parse(str)
|
11
|
+
m = str.match(/(\d+d)?(\d+h)?(\d+m)?(\d+s)?/)
|
12
|
+
|
13
|
+
if m[0].empty?
|
14
|
+
raise ArgumentError, "Unparseable string #{str}"
|
15
|
+
else
|
16
|
+
seconds = (m[1].to_i * 86_400) + (m[2].to_i * 3600) +
|
17
|
+
(m[3].to_i * 60) + m[4].to_i
|
18
|
+
|
19
|
+
new(seconds)
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
def to_i
|
24
|
+
total_seconds
|
25
|
+
end
|
26
|
+
|
27
|
+
def to_s
|
28
|
+
[
|
29
|
+
"#{days}d",
|
30
|
+
"#{hours}h",
|
31
|
+
"#{minutes}m",
|
32
|
+
"#{seconds}s",
|
33
|
+
].reject { |t| t.start_with?('0') }.join
|
34
|
+
end
|
35
|
+
|
36
|
+
private
|
37
|
+
|
38
|
+
def ref_time
|
39
|
+
@ref_time ||= Time.at(0).utc + total_seconds
|
40
|
+
end
|
41
|
+
|
42
|
+
def days
|
43
|
+
total_seconds / 86_400
|
44
|
+
end
|
45
|
+
|
46
|
+
def hours
|
47
|
+
ref_time.hour
|
48
|
+
end
|
49
|
+
|
50
|
+
def minutes
|
51
|
+
ref_time.min
|
52
|
+
end
|
53
|
+
|
54
|
+
def seconds
|
55
|
+
ref_time.sec
|
56
|
+
end
|
57
|
+
end
|
@@ -0,0 +1,73 @@
|
|
1
|
+
require 'minitest/spec'
|
2
|
+
require 'minitest/autorun'
|
3
|
+
|
4
|
+
require_relative '../lib/human_seconds'
|
5
|
+
|
6
|
+
describe HumanSeconds do
|
7
|
+
it "is initialized with integer total_seconds" do
|
8
|
+
human_seconds = HumanSeconds.new(61)
|
9
|
+
assert_equal 61, human_seconds.to_i
|
10
|
+
end
|
11
|
+
|
12
|
+
it "can convert seconds like 59 to '59s'" do
|
13
|
+
human_seconds = HumanSeconds.new(59)
|
14
|
+
assert_equal "59s", human_seconds.to_s
|
15
|
+
end
|
16
|
+
|
17
|
+
it "can convert seconds like 60 to '1m'" do
|
18
|
+
human_seconds = HumanSeconds.new(60)
|
19
|
+
assert_equal "1m", human_seconds.to_s
|
20
|
+
end
|
21
|
+
|
22
|
+
it "can convert seconds like 7212 to '2h12s'" do
|
23
|
+
human_seconds = HumanSeconds.new(7212)
|
24
|
+
assert_equal "2h12s", human_seconds.to_s
|
25
|
+
end
|
26
|
+
|
27
|
+
it "can convert seconds like 7261 to '2h1m1s'" do
|
28
|
+
human_seconds = HumanSeconds.new(7261)
|
29
|
+
assert_equal "2h1m1s", human_seconds.to_s
|
30
|
+
end
|
31
|
+
|
32
|
+
it "can convert seconds like 7261 to '2h1m1s'" do
|
33
|
+
human_seconds = HumanSeconds.new(7261)
|
34
|
+
assert_equal "2h1m1s", human_seconds.to_s
|
35
|
+
end
|
36
|
+
|
37
|
+
# it "can convert seconds like 176461 to '49h1m1s'" do
|
38
|
+
it "can convert seconds like 176461 to '2d1h1m1s'" do
|
39
|
+
human_seconds = HumanSeconds.new(176461)
|
40
|
+
assert_equal "2d1h1m1s", human_seconds.to_s
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
describe HumanSeconds, '.parse' do
|
45
|
+
it "can parse '59s' to an instance with 59 seconds" do
|
46
|
+
human_seconds = HumanSeconds.parse('59s')
|
47
|
+
assert_equal 59, human_seconds.to_i
|
48
|
+
end
|
49
|
+
|
50
|
+
it "can parse '1m' to an instance with 60 seconds" do
|
51
|
+
human_seconds = HumanSeconds.parse('1m')
|
52
|
+
assert_equal 60, human_seconds.to_i
|
53
|
+
end
|
54
|
+
|
55
|
+
it "can parse '2h12s' to an instance with 7212 seconds" do
|
56
|
+
human_seconds = HumanSeconds.parse('2h12s')
|
57
|
+
assert_equal 7212, human_seconds.to_i
|
58
|
+
end
|
59
|
+
|
60
|
+
it "can parse '2h1m1s' to an instance with 7261 seconds" do
|
61
|
+
human_seconds = HumanSeconds.parse('2h1m1s')
|
62
|
+
assert_equal 7261, human_seconds.to_i
|
63
|
+
end
|
64
|
+
|
65
|
+
it "can parse '2d1h1m1s' to an instance with 176461 seconds" do
|
66
|
+
human_seconds = HumanSeconds.parse('2d1h1m1s')
|
67
|
+
assert_equal 176461, human_seconds.to_i
|
68
|
+
end
|
69
|
+
|
70
|
+
it "raises an ArgumentError exception if given something unparseable" do
|
71
|
+
assert_raises(ArgumentError) { HumanSeconds.parse('1zl1k2j3z') }
|
72
|
+
end
|
73
|
+
end
|
metadata
ADDED
@@ -0,0 +1,96 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: human_seconds
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.2
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Brendon Murphy
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-07-14 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.5'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.5'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: minitest
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: 5.4.0
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: 5.4.0
|
55
|
+
description: Converts seconds like 7261 into a human readable string '2h1m1s'
|
56
|
+
email:
|
57
|
+
- xternal1+github@gmail.com
|
58
|
+
executables: []
|
59
|
+
extensions: []
|
60
|
+
extra_rdoc_files: []
|
61
|
+
files:
|
62
|
+
- ".gitignore"
|
63
|
+
- Gemfile
|
64
|
+
- LICENSE.txt
|
65
|
+
- README.md
|
66
|
+
- Rakefile
|
67
|
+
- human_seconds.gemspec
|
68
|
+
- lib/human_seconds.rb
|
69
|
+
- lib/human_seconds/version.rb
|
70
|
+
- spec/human_seconds_spec.rb
|
71
|
+
homepage: ''
|
72
|
+
licenses:
|
73
|
+
- MIT
|
74
|
+
metadata: {}
|
75
|
+
post_install_message:
|
76
|
+
rdoc_options: []
|
77
|
+
require_paths:
|
78
|
+
- lib
|
79
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
80
|
+
requirements:
|
81
|
+
- - ">="
|
82
|
+
- !ruby/object:Gem::Version
|
83
|
+
version: '0'
|
84
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
85
|
+
requirements:
|
86
|
+
- - ">="
|
87
|
+
- !ruby/object:Gem::Version
|
88
|
+
version: '0'
|
89
|
+
requirements: []
|
90
|
+
rubyforge_project:
|
91
|
+
rubygems_version: 2.2.2
|
92
|
+
signing_key:
|
93
|
+
specification_version: 4
|
94
|
+
summary: Converts seconds like 7261 into a human readable string '2h1m1s'
|
95
|
+
test_files:
|
96
|
+
- spec/human_seconds_spec.rb
|