time_difference 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +17 -0
- data/.travis.yml +8 -0
- data/CHANGELOG.md +3 -0
- data/Gemfile +4 -0
- data/LICENSE +22 -0
- data/README.md +50 -0
- data/Rakefile +5 -0
- data/lib/time_difference.rb +36 -0
- data/spec/spec_helper.rb +7 -0
- data/spec/time_difference_spec.rb +22 -0
- data/time_difference.gemspec +19 -0
- metadata +94 -0
data/.gitignore
ADDED
data/.travis.yml
ADDED
data/CHANGELOG.md
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 TM Lee
|
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,50 @@
|
|
1
|
+
[![Build Status](https://travis-ci.org/tmlee/time_difference.png)](https://travis-ci.org/tmlee/time_difference)
|
2
|
+
|
3
|
+
# TimeDifference
|
4
|
+
|
5
|
+
TimeDifference is the missing Ruby method to calculate difference between two given time. You can do a Ruby time difference in year, month, week, day, hour, minute, and seconds.
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
gem 'time_difference', git: 'git@github.com:tmlee/time_difference.git'
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
## Usage
|
18
|
+
|
19
|
+
### Get the time difference in year
|
20
|
+
|
21
|
+
start_time = Time.new(2013,1)
|
22
|
+
end_time = Time.new(2014,1)
|
23
|
+
TimeDifference.between(start_time, end_time).in_years
|
24
|
+
=> 1.0
|
25
|
+
|
26
|
+
### Get the time difference in year
|
27
|
+
|
28
|
+
start_time = Time.new(2013,1)
|
29
|
+
end_time = Time.new(2014,1)
|
30
|
+
TimeDifference.between(start_time, end_time).in_months
|
31
|
+
=> 12.0
|
32
|
+
|
33
|
+
### Supported time difference includes
|
34
|
+
|
35
|
+
in_years
|
36
|
+
in_months
|
37
|
+
in_weeks
|
38
|
+
in_days
|
39
|
+
in_hours
|
40
|
+
in_minutes
|
41
|
+
in_seconds
|
42
|
+
|
43
|
+
|
44
|
+
## Contributing
|
45
|
+
|
46
|
+
1. Fork it
|
47
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
48
|
+
3. Commit your changes (`git commit -am 'Added some feature'`)
|
49
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
50
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,36 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require "active_support/all"
|
3
|
+
|
4
|
+
class TimeDifference
|
5
|
+
|
6
|
+
def self.between(start_time, end_time)
|
7
|
+
@time_diff = end_time - start_time
|
8
|
+
self
|
9
|
+
end
|
10
|
+
|
11
|
+
class << self
|
12
|
+
|
13
|
+
[:years, :months, :weeks, :days, :hours, :minutes, :seconds].each do |time_component|
|
14
|
+
self.class.instance_eval do
|
15
|
+
define_method("in_#{time_component}") do
|
16
|
+
if time_component == :months
|
17
|
+
(@time_diff/(1.days * 30.42)).round(2)
|
18
|
+
else
|
19
|
+
(@time_diff/1.send(time_component)).round(2)
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
end
|
26
|
+
|
27
|
+
def self.in_general
|
28
|
+
result = {}
|
29
|
+
[:years, :months, :weeks, :days, :hours, :minutes, :seconds].each do |time_component|
|
30
|
+
result[time_component] = (@time_diff/1.send(time_component)).floor
|
31
|
+
@time_diff = (@time_diff - result[time_component].send(time_component))
|
32
|
+
end
|
33
|
+
result
|
34
|
+
end
|
35
|
+
|
36
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe TimeDifference do
|
4
|
+
|
5
|
+
it "returns time difference in general that matches the total seconds" do
|
6
|
+
start_time = Time.new(2009,9)
|
7
|
+
end_time = Time.new(2010,11)
|
8
|
+
expect(TimeDifference.between(start_time, end_time).in_general).to eql({:years=>1, :months=>2, :weeks=>0, :days=>0, :hours=>18, :minutes=>0, :seconds=>0})
|
9
|
+
end
|
10
|
+
|
11
|
+
it "returns time difference based on Wolfram Alpha" do
|
12
|
+
start_time = Time.new(2011,1)
|
13
|
+
end_time = Time.new(2011,12)
|
14
|
+
expect(TimeDifference.between(start_time, end_time).in_years).to eql 0.91
|
15
|
+
expect(TimeDifference.between(start_time, end_time).in_months).to eql 10.98
|
16
|
+
expect(TimeDifference.between(start_time, end_time).in_weeks).to eql 47.71
|
17
|
+
expect(TimeDifference.between(start_time, end_time).in_days).to eql 334.0
|
18
|
+
expect(TimeDifference.between(start_time, end_time).in_hours).to eql 8016.0
|
19
|
+
expect(TimeDifference.between(start_time, end_time).in_minutes).to eql 480960.0
|
20
|
+
end
|
21
|
+
|
22
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
Gem::Specification.new do |gem|
|
3
|
+
gem.authors = ["TM Lee"]
|
4
|
+
gem.email = ["tmlee.ltm@gmail.com"]
|
5
|
+
gem.description = "TimeDifference is the missing Ruby method to calculate difference between two given time. You can do a Ruby time difference in year, month, week, day, hour, minute, and seconds."
|
6
|
+
gem.summary = "TimeDifference is the missing Ruby method to calculate difference between two given time. You can do a Ruby time difference in year, month, week, day, hour, minute, and seconds."
|
7
|
+
gem.homepage = ""
|
8
|
+
|
9
|
+
gem.files = `git ls-files`.split($\)
|
10
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
11
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
12
|
+
gem.name = "time_difference"
|
13
|
+
gem.require_paths = ["lib"]
|
14
|
+
gem.version = "0.1.0"
|
15
|
+
|
16
|
+
gem.add_runtime_dependency('activesupport')
|
17
|
+
gem.add_development_dependency('rspec', '~> 2.13.0')
|
18
|
+
|
19
|
+
end
|
metadata
ADDED
@@ -0,0 +1,94 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: time_difference
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- TM Lee
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-04-18 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: activesupport
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '0'
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: rspec
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ~>
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: 2.13.0
|
38
|
+
type: :development
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ~>
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: 2.13.0
|
46
|
+
description: TimeDifference is the missing Ruby method to calculate difference between
|
47
|
+
two given time. You can do a Ruby time difference in year, month, week, day, hour,
|
48
|
+
minute, and seconds.
|
49
|
+
email:
|
50
|
+
- tmlee.ltm@gmail.com
|
51
|
+
executables: []
|
52
|
+
extensions: []
|
53
|
+
extra_rdoc_files: []
|
54
|
+
files:
|
55
|
+
- .gitignore
|
56
|
+
- .travis.yml
|
57
|
+
- CHANGELOG.md
|
58
|
+
- Gemfile
|
59
|
+
- LICENSE
|
60
|
+
- README.md
|
61
|
+
- Rakefile
|
62
|
+
- lib/time_difference.rb
|
63
|
+
- spec/spec_helper.rb
|
64
|
+
- spec/time_difference_spec.rb
|
65
|
+
- time_difference.gemspec
|
66
|
+
homepage: ''
|
67
|
+
licenses: []
|
68
|
+
post_install_message:
|
69
|
+
rdoc_options: []
|
70
|
+
require_paths:
|
71
|
+
- lib
|
72
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
73
|
+
none: false
|
74
|
+
requirements:
|
75
|
+
- - ! '>='
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: '0'
|
78
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
79
|
+
none: false
|
80
|
+
requirements:
|
81
|
+
- - ! '>='
|
82
|
+
- !ruby/object:Gem::Version
|
83
|
+
version: '0'
|
84
|
+
requirements: []
|
85
|
+
rubyforge_project:
|
86
|
+
rubygems_version: 1.8.24
|
87
|
+
signing_key:
|
88
|
+
specification_version: 3
|
89
|
+
summary: TimeDifference is the missing Ruby method to calculate difference between
|
90
|
+
two given time. You can do a Ruby time difference in year, month, week, day, hour,
|
91
|
+
minute, and seconds.
|
92
|
+
test_files:
|
93
|
+
- spec/spec_helper.rb
|
94
|
+
- spec/time_difference_spec.rb
|