time_subtract 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 +15 -0
- data/.gitignore +22 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +69 -0
- data/Rakefile +2 -0
- data/lib/time_subtract.rb +13 -0
- data/lib/time_subtract/seconds_to_hash.rb +16 -0
- data/lib/time_subtract/time_intervals_to_string.rb +28 -0
- data/lib/time_subtract/version.rb +3 -0
- data/spec/spec_helper.rb +9 -0
- data/spec/time_subtract_spec.rb +143 -0
- data/time_subtract.gemspec +26 -0
- metadata +114 -0
checksums.yaml
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
---
|
2
|
+
!binary "U0hBMQ==":
|
3
|
+
metadata.gz: !binary |-
|
4
|
+
YWZjYjI4YmViMjI1NTAxN2UxYmJlNWQ1NGEyOWQ0MmIyMjY1YjQ1ZQ==
|
5
|
+
data.tar.gz: !binary |-
|
6
|
+
NWJjODQ1Y2ZhOGQ5NGQ3NzUxY2I4NDZiZTEzMjQ3Yzg1YzViYjk4OA==
|
7
|
+
SHA512:
|
8
|
+
metadata.gz: !binary |-
|
9
|
+
ZDAzNWMyZDFiMjVjODI1MDQ2ZjMzNjVlNWQzMzBmNTMxOGEyNGZkYzRmYWNk
|
10
|
+
MDZhMjk5NTliYmEzZTM5MmJhZGVkNjQ3NjNiMGQ0ODlhNzEzYWZiZmU1ZjIy
|
11
|
+
YmRlYzEzYjU3YjE1MWIyNzMxNWQxM2Y0MWIzZDZiZjE5YzExZjc=
|
12
|
+
data.tar.gz: !binary |-
|
13
|
+
MjI1YjFkODVhZDIxYTczNDMwZDRjZjdiN2UyOGI4MDE2MTI4ZTA4YzExNDdi
|
14
|
+
ZDcxNmEzODdhOGVjYWQxM2Y1YWU2MTBlZmZmYzllZGQ0NjUwZGI2MjgxMzdi
|
15
|
+
MzA4Y2JlMzk2MjUyZGYyYjJmMzM5MDBhZmExMTc0ZDFmMWI1MDE=
|
data/.gitignore
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
*.gem
|
2
|
+
*.rbc
|
3
|
+
.bundle
|
4
|
+
.config
|
5
|
+
.yardoc
|
6
|
+
Gemfile.lock
|
7
|
+
InstalledFiles
|
8
|
+
_yardoc
|
9
|
+
coverage
|
10
|
+
doc/
|
11
|
+
lib/bundler/man
|
12
|
+
pkg
|
13
|
+
rdoc
|
14
|
+
spec/reports
|
15
|
+
test/tmp
|
16
|
+
test/version_tmp
|
17
|
+
tmp
|
18
|
+
*.bundle
|
19
|
+
*.so
|
20
|
+
*.o
|
21
|
+
*.a
|
22
|
+
mkmf.log
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 Ponnusamy
|
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,69 @@
|
|
1
|
+
# TimeSubtract [](https://codeclimate.com/github/ponnusamygit/time_subtract)
|
2
|
+
|
3
|
+
Get the difference between two time
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'time_subtract'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install time_subtract
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
time.smart_subtract time2
|
22
|
+
|
23
|
+
# This will return
|
24
|
+
|
25
|
+
'2 years 1 month 8 days 10 hours 5 minutes 23 seconds'.
|
26
|
+
|
27
|
+
If you want result as hash
|
28
|
+
|
29
|
+
(time - time2).time_diff_hash
|
30
|
+
|
31
|
+
# or
|
32
|
+
|
33
|
+
seconds = 123456
|
34
|
+
|
35
|
+
seconds.time_diff_hash
|
36
|
+
|
37
|
+
# This will return
|
38
|
+
|
39
|
+
#=> {:year => 0, :month => 0, :day => 1, :hour=> 10, :minute => 17, :second => 36}
|
40
|
+
|
41
|
+
|
42
|
+
## Contributing
|
43
|
+
|
44
|
+
1. Fork it ( https://github.com/ponnusamygit/time_subtract/fork )
|
45
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
46
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
47
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
48
|
+
5. Create a new Pull Request
|
49
|
+
|
50
|
+
|
51
|
+
## Maintainer
|
52
|
+
|
53
|
+
* [Ponnusamy](https://github.com/ponnusamygit)
|
54
|
+
|
55
|
+
## Todo
|
56
|
+
|
57
|
+
- Get format from user
|
58
|
+
|
59
|
+
## Testing
|
60
|
+
|
61
|
+
Time Subtract uses RSpec for test coverage. Inside the gem
|
62
|
+
directory, you can run the specs with:
|
63
|
+
|
64
|
+
rpsec spec/time_subtract_spec.rb
|
65
|
+
|
66
|
+
License
|
67
|
+
----
|
68
|
+
|
69
|
+
MIT
|
data/Rakefile
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
require "time_subtract/version"
|
2
|
+
require "time_subtract/seconds_to_hash"
|
3
|
+
require "time_subtract/time_intervals_to_string"
|
4
|
+
require 'active_support/all'
|
5
|
+
|
6
|
+
module TimeSubtract
|
7
|
+
def smart_subtract _time
|
8
|
+
_seconds = self - _time
|
9
|
+
TimeIntervalsToString.interval_to_string _seconds.time_diff_hash
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
Time.send :include, TimeSubtract
|
@@ -0,0 +1,16 @@
|
|
1
|
+
require 'active_support/all'
|
2
|
+
|
3
|
+
module SecondsToHash
|
4
|
+
def time_diff_hash
|
5
|
+
number = self.abs
|
6
|
+
intervals = [:year, :month, :day, :hour, :minute, :second]
|
7
|
+
date_time_hs = {}
|
8
|
+
intervals.each do |interval|
|
9
|
+
date_time_hs[interval] = (number / (1.send interval)).to_i
|
10
|
+
number = number % (1.send interval)
|
11
|
+
end
|
12
|
+
date_time_hs
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
Numeric.send :include, SecondsToHash
|
@@ -0,0 +1,28 @@
|
|
1
|
+
module TimeIntervalsToString
|
2
|
+
def self.interval_to_string arg_hash
|
3
|
+
# Raise Wrong Argument Type Error
|
4
|
+
if !(arg_hash.is_a? Hash)
|
5
|
+
raise 'WrongArgumentType'
|
6
|
+
end
|
7
|
+
|
8
|
+
order = [:year, :month, :day, :hour, :minute, :second]
|
9
|
+
|
10
|
+
# Keep only selected key values
|
11
|
+
arg_hash.keep_if{|k, v| order.include? k}
|
12
|
+
|
13
|
+
# No Key match exception
|
14
|
+
if arg_hash.blank?
|
15
|
+
raise 'NoKeyMatch'
|
16
|
+
end
|
17
|
+
|
18
|
+
# Order the hash with our order
|
19
|
+
ordered_hash = Hash[arg_hash.sort_by {|k, _| order.index k}]
|
20
|
+
|
21
|
+
out_string = []
|
22
|
+
ordered_hash.each do |interval, value|
|
23
|
+
out_string << "#{value} #{interval.to_s.pluralize(value)}" unless value.zero?
|
24
|
+
end
|
25
|
+
out_string.join(' ')
|
26
|
+
end
|
27
|
+
|
28
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,143 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe TimeSubtract do
|
4
|
+
it 'Module TimeSubtract included Time' do
|
5
|
+
expect(Time.included_modules).to include(TimeSubtract)
|
6
|
+
end
|
7
|
+
|
8
|
+
describe '12/10/2001 subtract 27/06/2014' do
|
9
|
+
it 'Have a method named smart_subtract' do
|
10
|
+
time1 = Time.new(2001, 10, 12, 10, 12, 30)
|
11
|
+
expect(time1.respond_to? 'smart_subtract').to eq(true)
|
12
|
+
end
|
13
|
+
|
14
|
+
describe 'Diffenence' do
|
15
|
+
begin
|
16
|
+
time1 = Time.now
|
17
|
+
end
|
18
|
+
|
19
|
+
it '10 seconds' do
|
20
|
+
time2 = time1 + 10.seconds
|
21
|
+
expect(time1.smart_subtract time2).to eq('10 seconds')
|
22
|
+
end
|
23
|
+
|
24
|
+
it '20 minutes 5 seconds' do
|
25
|
+
time2 = time1 + 1205.seconds
|
26
|
+
expect(time1.smart_subtract time2).to eq('20 minutes 5 seconds')
|
27
|
+
end
|
28
|
+
|
29
|
+
it '2 hours 3 minutes 4 seconds' do
|
30
|
+
time2 = time1 + 7384.seconds
|
31
|
+
expect(time1.smart_subtract time2).to eq('2 hours 3 minutes 4 seconds')
|
32
|
+
end
|
33
|
+
|
34
|
+
it '2 days 3 hours 4 minutes 5 seconds' do
|
35
|
+
time2 = time1 + 183845.seconds
|
36
|
+
expect(time1.smart_subtract time2).to eq('2 days 3 hours 4 minutes 5 seconds')
|
37
|
+
end
|
38
|
+
|
39
|
+
it '7 month 2 days 3 hours 4 minutes 5 seconds' do
|
40
|
+
time2 = time1 + 18327845.seconds
|
41
|
+
expect(time1.smart_subtract time2).to eq('7 months 2 days 3 hours 4 minutes 5 seconds')
|
42
|
+
end
|
43
|
+
|
44
|
+
it '3 years 2 days 3 hours 4 minutes 5 seconds' do
|
45
|
+
time2 = time1 + 94856645.seconds
|
46
|
+
expect(time1.smart_subtract time2).to eq('3 years 2 days 3 hours 4 minutes 5 seconds')
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
describe SecondsToHash do
|
53
|
+
begin
|
54
|
+
num = 123456
|
55
|
+
negative_num = 0-num
|
56
|
+
expected = {:year => 0, :month => 0, :day => 1, :hour=> 10, :minute => 17, :second => 36}
|
57
|
+
end
|
58
|
+
|
59
|
+
it 'Inclue SecondsToHash to Numeric class' do
|
60
|
+
expect(Numeric.included_modules).to include(SecondsToHash)
|
61
|
+
end
|
62
|
+
|
63
|
+
it 'Have instance method time_diff_hash' do
|
64
|
+
expect(num.respond_to? 'time_diff_hash').to eq(true)
|
65
|
+
end
|
66
|
+
|
67
|
+
describe 'should retrun' do
|
68
|
+
it 'value as hash' do
|
69
|
+
expect(num.time_diff_hash).to be_a(Hash)
|
70
|
+
end
|
71
|
+
|
72
|
+
it 'values only as fixnum' do
|
73
|
+
date_hash = num.time_diff_hash
|
74
|
+
date_hash.each do |interval, value|
|
75
|
+
expect(value).to be_a Fixnum
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
it 'values as unsigned positive number' do
|
80
|
+
date_hash = negative_num.time_diff_hash
|
81
|
+
date_hash.each do |interval, value|
|
82
|
+
expect(value).to be >= 0
|
83
|
+
end
|
84
|
+
end
|
85
|
+
|
86
|
+
it 'value in order [:year, :month, :day, :hour, :minute, :second]' do
|
87
|
+
orders = {:year => 0, :month => 1, :day => 2, :hour => 3, :minute => 4, :second => 5}
|
88
|
+
date_hash = num.time_diff_hash.to_a
|
89
|
+
orders.each do |interval, order|
|
90
|
+
expect(date_hash[order]).to include(interval)
|
91
|
+
end
|
92
|
+
end
|
93
|
+
|
94
|
+
it 'Must be 0 year 0 month 1 day 10 hour 17 minute 36 second' do
|
95
|
+
expect(num.time_diff_hash).to eq(expected)
|
96
|
+
end
|
97
|
+
end
|
98
|
+
|
99
|
+
end
|
100
|
+
|
101
|
+
describe TimeIntervalsToString do
|
102
|
+
it 'should have method interval_to_string' do
|
103
|
+
expect(TimeIntervalsToString.respond_to? 'interval_to_string').to eq(true)
|
104
|
+
end
|
105
|
+
|
106
|
+
describe 'Accept [:year, :month, :day, :hour, :minute, :second] keys' do
|
107
|
+
begin
|
108
|
+
intervals = {:year => 10, :month => 0, :day => 13, :hour => 3, :minute => 14, :second => 25}
|
109
|
+
wrong_order = {:second => 25, :hour => 3, :day => 13, :month => 0, :minute => 14, :year => 10}
|
110
|
+
with_other_key = {:other => 'key', :second => 25, :hour => 3, :day => 13, :month => 0, :minute => 14, :year => 10}
|
111
|
+
no_key_match = {:weight => 80, :height => 166 , :age => 22}
|
112
|
+
expected = '10 years 13 days 3 hours 14 minutes 25 seconds'
|
113
|
+
end
|
114
|
+
|
115
|
+
it 'And return string' do
|
116
|
+
expect(TimeIntervalsToString.interval_to_string(intervals)).to eq(expected)
|
117
|
+
end
|
118
|
+
|
119
|
+
it 'should have with pluralized form' do
|
120
|
+
expect(TimeIntervalsToString.interval_to_string(intervals)).to eq(expected)
|
121
|
+
end
|
122
|
+
|
123
|
+
it 'with out Zero values' do
|
124
|
+
expect(TimeIntervalsToString.interval_to_string(intervals)).to eq(expected)
|
125
|
+
end
|
126
|
+
|
127
|
+
it 'Re-order the worng parameter' do
|
128
|
+
expect(TimeIntervalsToString.interval_to_string(wrong_order)).to eq(expected)
|
129
|
+
end
|
130
|
+
|
131
|
+
it "Any other keys occured don't parse other keys" do
|
132
|
+
expect(TimeIntervalsToString.interval_to_string(with_other_key)).to eq(expected)
|
133
|
+
end
|
134
|
+
|
135
|
+
it 'Are else raise no key is matched, expected [:year, :month, :day, :hour, :minute, :second] keys' do
|
136
|
+
expect {TimeIntervalsToString.interval_to_string(no_key_match) }.to raise_error 'NoKeyMatch'
|
137
|
+
end
|
138
|
+
|
139
|
+
it "Should raise 'Argument miss match expected Hash' when differnet type argumenr" do
|
140
|
+
expect {TimeIntervalsToString.interval_to_string 'Raise Error' }.to raise_error 'WrongArgumentType'
|
141
|
+
end
|
142
|
+
end
|
143
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'time_subtract/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "time_subtract"
|
8
|
+
spec.version = TimeSubtract::VERSION
|
9
|
+
spec.authors = ["Ponnusamy"]
|
10
|
+
spec.email = ["ponnusamy.gac@gmail.com"]
|
11
|
+
spec.summary = %q{Subtract two time and get a smart string as a return value}
|
12
|
+
spec.description = %q{Time object subtraction method available for easy use}
|
13
|
+
spec.homepage = "https://github.com/ponnusamygit/time_subtract"
|
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.6"
|
22
|
+
spec.add_development_dependency "rake"
|
23
|
+
spec.add_development_dependency "rspec"
|
24
|
+
spec.add_dependency "activesupport"
|
25
|
+
|
26
|
+
end
|
metadata
ADDED
@@ -0,0 +1,114 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: time_subtract
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.2
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Ponnusamy
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-06-28 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.6'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ~>
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.6'
|
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: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ! '>='
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ! '>='
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: activesupport
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ! '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :runtime
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ! '>='
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
description: Time object subtraction method available for easy use
|
70
|
+
email:
|
71
|
+
- ponnusamy.gac@gmail.com
|
72
|
+
executables: []
|
73
|
+
extensions: []
|
74
|
+
extra_rdoc_files: []
|
75
|
+
files:
|
76
|
+
- .gitignore
|
77
|
+
- Gemfile
|
78
|
+
- LICENSE.txt
|
79
|
+
- README.md
|
80
|
+
- Rakefile
|
81
|
+
- lib/time_subtract.rb
|
82
|
+
- lib/time_subtract/seconds_to_hash.rb
|
83
|
+
- lib/time_subtract/time_intervals_to_string.rb
|
84
|
+
- lib/time_subtract/version.rb
|
85
|
+
- spec/spec_helper.rb
|
86
|
+
- spec/time_subtract_spec.rb
|
87
|
+
- time_subtract.gemspec
|
88
|
+
homepage: https://github.com/ponnusamygit/time_subtract
|
89
|
+
licenses:
|
90
|
+
- MIT
|
91
|
+
metadata: {}
|
92
|
+
post_install_message:
|
93
|
+
rdoc_options: []
|
94
|
+
require_paths:
|
95
|
+
- lib
|
96
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
97
|
+
requirements:
|
98
|
+
- - ! '>='
|
99
|
+
- !ruby/object:Gem::Version
|
100
|
+
version: '0'
|
101
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
102
|
+
requirements:
|
103
|
+
- - ! '>='
|
104
|
+
- !ruby/object:Gem::Version
|
105
|
+
version: '0'
|
106
|
+
requirements: []
|
107
|
+
rubyforge_project:
|
108
|
+
rubygems_version: 2.3.0
|
109
|
+
signing_key:
|
110
|
+
specification_version: 4
|
111
|
+
summary: Subtract two time and get a smart string as a return value
|
112
|
+
test_files:
|
113
|
+
- spec/spec_helper.rb
|
114
|
+
- spec/time_subtract_spec.rb
|