time-elapsed 0.0.1
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 +18 -0
- data/Gemfile +4 -0
- data/Gemfile.lock +17 -0
- data/LICENSE +22 -0
- data/README.md +25 -0
- data/Rakefile +1 -0
- data/lib/time/elapsed/parser/parser.rb +41 -0
- data/lib/time/elapsed/version.rb +5 -0
- data/lib/time/elapsed.rb +37 -0
- data/lib/time-elapsed.rb +1 -0
- data/spec/elapsed.rb +29 -0
- data/spec/parser.rb +24 -0
- data/time-elapsed.gemspec +22 -0
- metadata +82 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 92bba8e887a6e34be29d882acb9bb277f0c29d36
|
4
|
+
data.tar.gz: 0ade0e672b19e5545c72e61276e7c043160e69f4
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: c160353b58fb8c35460ce4e858fc54204eda7ae2c99582c4f3281749e1d6deb0e78f86521a044f3fbaeb1a2f99645f8884725583ca833bf28e37c6e0f68b3846
|
7
|
+
data.tar.gz: 7e1d84f7eff3aae9a71dd41f3314b01336589a97f1ccc9cf175ca91f4082cd680164f54e4b39f686a35eeba27554875854a7013c4434f316f3889e38aabe675c
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 TODO: Write your name
|
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,25 @@
|
|
1
|
+
# Time::Elapsed
|
2
|
+
|
3
|
+
Adds elapsed time functionality to ruby Time objects
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
```
|
7
|
+
gem install time-elapsed
|
8
|
+
```
|
9
|
+
## Usage
|
10
|
+
```
|
11
|
+
require 'time-elapsed'
|
12
|
+
```
|
13
|
+
## Examples
|
14
|
+
```
|
15
|
+
Time.now.plus('2 minutes 32 seconds')
|
16
|
+
Time.now.minus('3 weeks and 3 days and 32 hours')
|
17
|
+
Time.now + Time::Elapsed.time('34m30s')
|
18
|
+
```
|
19
|
+
## Contributing
|
20
|
+
|
21
|
+
1. Fork it
|
22
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
23
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
24
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
25
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
@@ -0,0 +1,41 @@
|
|
1
|
+
class Time
|
2
|
+
module Elapsed
|
3
|
+
|
4
|
+
module Parser
|
5
|
+
LAST_REGEX = /
|
6
|
+
((?<years>[\d]+)y)?
|
7
|
+
((?<weeks>[\d]+)w)?
|
8
|
+
((?<days>[\d]+)d)?
|
9
|
+
((?<hours>[\d]+)h)?
|
10
|
+
((?<minutes>[\d]+)m)?
|
11
|
+
((?<seconds>[\d]+)s?)? #Defaults to seconds
|
12
|
+
/ix
|
13
|
+
|
14
|
+
TIME_TO_SECONDS = {
|
15
|
+
'years' => 31536000,
|
16
|
+
'weeks' => 604800,
|
17
|
+
'days' => 86400,
|
18
|
+
'hours' => 3600,
|
19
|
+
'minutes' => 60,
|
20
|
+
'seconds' => 1
|
21
|
+
}
|
22
|
+
|
23
|
+
# @param [String] time_str User string to parse
|
24
|
+
# @return [Fixnum] parsed time in seconds
|
25
|
+
def self.parse_time_str(time_str, last_regex = LAST_REGEX)
|
26
|
+
raise ArgumentError unless last_regex.is_a?(Regexp)
|
27
|
+
keys = last_regex.names
|
28
|
+
matches = time_str.gsub(/\s+/, '').scan(last_regex)
|
29
|
+
ago_hash = matches.inject({}) do |h, m|
|
30
|
+
m.map!(&:to_i) #Expect zeros
|
31
|
+
match_hash = Hash[*keys.zip(m).flatten]
|
32
|
+
h.merge(match_hash){|k, old_v, new_v| [old_v, new_v].max }
|
33
|
+
end
|
34
|
+
ago_hash.merge(TIME_TO_SECONDS) do |k, base, multiplier|
|
35
|
+
base*multiplier
|
36
|
+
end.values.inject(&:+)
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
end
|
41
|
+
end
|
data/lib/time/elapsed.rb
ADDED
@@ -0,0 +1,37 @@
|
|
1
|
+
require 'time'
|
2
|
+
|
3
|
+
class Time
|
4
|
+
# @param [String] time_str time string to be added
|
5
|
+
def plus(time_str)
|
6
|
+
self + Elapsed.time(time_str)
|
7
|
+
end
|
8
|
+
# @param [String] time_str time string to be subtracted
|
9
|
+
def minus(time_str)
|
10
|
+
self - Elapsed.time(time_str)
|
11
|
+
end
|
12
|
+
|
13
|
+
module Elapsed
|
14
|
+
# @param [String] time_str time string to be parsed
|
15
|
+
# @return [Fixnum] time_str in seconds
|
16
|
+
def self.time(time_str)
|
17
|
+
not_zero Parser.parse_time_str(time_str)
|
18
|
+
end
|
19
|
+
|
20
|
+
private
|
21
|
+
# Allows number Strings or Fixnums
|
22
|
+
# Raise ArgumentError unless the number is not zero
|
23
|
+
# always returns Integer
|
24
|
+
def self.not_zero(i)
|
25
|
+
i_dup = i.to_i #not really a dup since Fixnum is immutable
|
26
|
+
if i_dup.zero?
|
27
|
+
raise ArgumentError, 'Elapsed time not parsable'
|
28
|
+
else
|
29
|
+
return i_dup
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
end
|
35
|
+
|
36
|
+
require File.expand_path('../elapsed/version.rb', __FILE__)
|
37
|
+
require File.expand_path('../elapsed/parser/parser.rb', __FILE__)
|
data/lib/time-elapsed.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require File.expand_path('../time/elapsed.rb', __FILE__)
|
data/spec/elapsed.rb
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
require 'time-elapsed'
|
2
|
+
describe Time do
|
3
|
+
describe '.plus' do
|
4
|
+
it 'adds time to value' do
|
5
|
+
Time.at(0).plus('3days').to_i.should == 259200
|
6
|
+
end
|
7
|
+
end
|
8
|
+
|
9
|
+
describe '.minus' do
|
10
|
+
it 'subtracts time to value' do
|
11
|
+
Time.at(259200 + 1234).minus('3 days').to_i.should == 1234
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
describe Time::Elapsed do
|
17
|
+
describe '.time' do
|
18
|
+
it 'creates a parsed string' do
|
19
|
+
Time::Elapsed.time('3 weeks').should == 1814400
|
20
|
+
end
|
21
|
+
it 'raises an error on zero values' do
|
22
|
+
expect { Time::Elapsed.time('0 weeks') }.to raise_error
|
23
|
+
end
|
24
|
+
it 'defaults to seconds' do
|
25
|
+
Time::Elapsed.time('3m3').should == 183
|
26
|
+
Time::Elapsed.time('3').should == 3
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
data/spec/parser.rb
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
require 'time-elapsed'
|
2
|
+
describe Time::Elapsed::Parser do
|
3
|
+
p = Time::Elapsed::Parser
|
4
|
+
describe '.parse_time_str' do
|
5
|
+
it 'should correctly parse test cases' do
|
6
|
+
p.parse_time_str('32s').should == 32
|
7
|
+
p.parse_time_str('5 minutes 4seconds').should == 304
|
8
|
+
p.parse_time_str('6 weeks of vacation').should == 3628800
|
9
|
+
p.parse_time_str('12 years ago').should == 378432000
|
10
|
+
p.parse_time_str('12days 3 minutes and 6hrs2sec').should == 1058582
|
11
|
+
p.parse_time_str('13minutes').should == 780
|
12
|
+
p.parse_time_str('2 hours').should == 7200
|
13
|
+
p.parse_time_str('13').should == 13
|
14
|
+
p.parse_time_str('3 weeks 2days 4hrs 10minutes 2 secs').should == 2002202
|
15
|
+
end
|
16
|
+
|
17
|
+
it 'should return zero for no elapsed time and non-parsable garbage' do
|
18
|
+
p.parse_time_str('afea').should == 0
|
19
|
+
p.parse_time_str('0s').should == 0
|
20
|
+
p.parse_time_str('0 weeks of vacation').should == 0
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
@@ -0,0 +1,22 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'time/elapsed/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = 'time-elapsed'
|
8
|
+
spec.version = Time::Elapsed::VERSION
|
9
|
+
spec.authors = ['kcen']
|
10
|
+
spec.description = %q{Adds elapsed time functionality to rubys time}
|
11
|
+
spec.summary = %q{Adds elapsed time functionality to rubys time}
|
12
|
+
spec.homepage = 'https://github.com/kcen/time-elapsed'
|
13
|
+
spec.license = 'MIT'
|
14
|
+
|
15
|
+
spec.files = `git ls-files`.split($/)
|
16
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
17
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
18
|
+
spec.require_paths = ['lib']
|
19
|
+
|
20
|
+
spec.add_development_dependency 'bundler', '~> 1.3'
|
21
|
+
spec.add_development_dependency 'rake'
|
22
|
+
end
|
metadata
ADDED
@@ -0,0 +1,82 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: time-elapsed
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- kcen
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2013-08-14 00:00:00 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: bundler
|
16
|
+
prerelease: false
|
17
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
18
|
+
requirements:
|
19
|
+
- - ~>
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: "1.3"
|
22
|
+
type: :development
|
23
|
+
version_requirements: *id001
|
24
|
+
- !ruby/object:Gem::Dependency
|
25
|
+
name: rake
|
26
|
+
prerelease: false
|
27
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
28
|
+
requirements:
|
29
|
+
- &id003
|
30
|
+
- ">="
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: "0"
|
33
|
+
type: :development
|
34
|
+
version_requirements: *id002
|
35
|
+
description: Adds elapsed time functionality to rubys time
|
36
|
+
email:
|
37
|
+
executables: []
|
38
|
+
|
39
|
+
extensions: []
|
40
|
+
|
41
|
+
extra_rdoc_files: []
|
42
|
+
|
43
|
+
files:
|
44
|
+
- .gitignore
|
45
|
+
- Gemfile
|
46
|
+
- Gemfile.lock
|
47
|
+
- LICENSE
|
48
|
+
- README.md
|
49
|
+
- Rakefile
|
50
|
+
- lib/time-elapsed.rb
|
51
|
+
- lib/time/elapsed.rb
|
52
|
+
- lib/time/elapsed/parser/parser.rb
|
53
|
+
- lib/time/elapsed/version.rb
|
54
|
+
- spec/elapsed.rb
|
55
|
+
- spec/parser.rb
|
56
|
+
- time-elapsed.gemspec
|
57
|
+
homepage: https://github.com/kcen/time-elapsed
|
58
|
+
licenses:
|
59
|
+
- MIT
|
60
|
+
metadata: {}
|
61
|
+
|
62
|
+
post_install_message:
|
63
|
+
rdoc_options: []
|
64
|
+
|
65
|
+
require_paths:
|
66
|
+
- lib
|
67
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
68
|
+
requirements:
|
69
|
+
- *id003
|
70
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
71
|
+
requirements:
|
72
|
+
- *id003
|
73
|
+
requirements: []
|
74
|
+
|
75
|
+
rubyforge_project:
|
76
|
+
rubygems_version: 2.0.6
|
77
|
+
signing_key:
|
78
|
+
specification_version: 4
|
79
|
+
summary: Adds elapsed time functionality to rubys time
|
80
|
+
test_files:
|
81
|
+
- spec/elapsed.rb
|
82
|
+
- spec/parser.rb
|