span 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +15 -0
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +55 -0
- data/Rakefile +10 -0
- data/lib/span.rb +126 -0
- data/lib/span/version.rb +3 -0
- data/span.gemspec +24 -0
- data/test/span_test.rb +73 -0
- data/test/test_helper.rb +7 -0
- metadata +98 -0
checksums.yaml
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
---
|
2
|
+
!binary "U0hBMQ==":
|
3
|
+
metadata.gz: !binary |-
|
4
|
+
NzVhYjA4MDBjNmExMDRmODlkNzU4MjAwMjRiMmYyMzZlZWVkODkxYQ==
|
5
|
+
data.tar.gz: !binary |-
|
6
|
+
YmEyMDhhNjk2YTMyYjlmMTNjMDI1MmU5MDIyYmU2OGUxYWQyMDU3NQ==
|
7
|
+
!binary "U0hBNTEy":
|
8
|
+
metadata.gz: !binary |-
|
9
|
+
M2NkZTgyZjM2NTg1Y2U5NjI5Mzc5ZGIzOWEyN2UyMDY0YzczY2IzMzMzYWZi
|
10
|
+
NWYxMDM2ODczZWVjZGQ5MGI1MWY3NDUwN2Y4ODcxYzhkZGQxY2E4NmQ3MjI5
|
11
|
+
MDBkNTgwMGExNTdjZWM0MDZmZmQwMGRkMTRiYWZjNGY2YmE0MjY=
|
12
|
+
data.tar.gz: !binary |-
|
13
|
+
MjdmMTYwZTBjMzQ4ZmZjNmNhMGU1YzUyYzgzYjY3ODU5MzMwNmEyYTFjOTE0
|
14
|
+
ZjIzY2EyOWQ5MmU5ZGU2YmYzNDFmNzllMjdjNmUyYjZiODJmNDY2ZWU1ZjNj
|
15
|
+
YmI5MzkyYWEwMDU1ZDViODEyYzliODQwOTU3YzJkNGU1Yjg3YjM=
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 Ryan Cook <cookrn@gmail.com>
|
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,55 @@
|
|
1
|
+
# Span
|
2
|
+
|
3
|
+
Calculate the differences between two dates. A few notes:
|
4
|
+
|
5
|
+
* It is calendar-correct, meaning that it respects non-30-day months
|
6
|
+
* It currently only calculates the composite span, meaning that each
|
7
|
+
quantity in the result hash contributes to the total span.
|
8
|
+
* No external dependencies except the standard library
|
9
|
+
|
10
|
+
## Installation
|
11
|
+
|
12
|
+
Add this line to your application's Gemfile:
|
13
|
+
|
14
|
+
gem 'span'
|
15
|
+
|
16
|
+
And then execute:
|
17
|
+
|
18
|
+
$ bundle
|
19
|
+
|
20
|
+
Or install it yourself as:
|
21
|
+
|
22
|
+
$ gem install span
|
23
|
+
|
24
|
+
## Usage
|
25
|
+
|
26
|
+
`Span` ships with a single entry point for its API currently: `compute`.
|
27
|
+
|
28
|
+
```ruby
|
29
|
+
require 'span'
|
30
|
+
end_date = Date.parse '25/03/2013'
|
31
|
+
start_date = Date.parse '01/01/2012'
|
32
|
+
result = Span.compute end_date , start_date
|
33
|
+
puts result[ :year ].inspect # => 1
|
34
|
+
puts result[ :month ].inspect # => 2
|
35
|
+
puts result[ :week ].inspect # => 3
|
36
|
+
puts result[ :day ].inspect # => 4
|
37
|
+
```
|
38
|
+
|
39
|
+
It returns a `Hash` that has four `Symbol` keys: `:year`, `:month`, `:week`,
|
40
|
+
and `:day`.
|
41
|
+
|
42
|
+
## TODOs
|
43
|
+
|
44
|
+
* Reconsider `end_date` before `start_date` in `compute` API arguments
|
45
|
+
* Add more API options
|
46
|
+
* Better tests?
|
47
|
+
* Clean up code to make it easier to contribute
|
48
|
+
|
49
|
+
## Contributing
|
50
|
+
|
51
|
+
1. Fork it ( http://github.com/cookrn/span/fork )
|
52
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
53
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
54
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
55
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
data/lib/span.rb
ADDED
@@ -0,0 +1,126 @@
|
|
1
|
+
require 'date'
|
2
|
+
|
3
|
+
require 'span/version'
|
4
|
+
|
5
|
+
module Span
|
6
|
+
def self.compute( *args , &block )
|
7
|
+
TimeDiff.compute *args , &block
|
8
|
+
end
|
9
|
+
|
10
|
+
class TimeDiff
|
11
|
+
attr_reader \
|
12
|
+
:end_date,
|
13
|
+
:start_date
|
14
|
+
|
15
|
+
def self.compute( *args , &block )
|
16
|
+
new( *args , &block ).to_hash
|
17
|
+
end
|
18
|
+
|
19
|
+
def initialize( end_date_input , start_date_input )
|
20
|
+
@end_date = end_date_input.to_date
|
21
|
+
@start_date = start_date_input.to_date
|
22
|
+
end
|
23
|
+
|
24
|
+
def build_diff_hash
|
25
|
+
check_from = start_date
|
26
|
+
|
27
|
+
[ :years , :months , :weeks , :days ].reduce Hash.new do | memo , part |
|
28
|
+
check_from , num_of_part , part_singular =
|
29
|
+
send \
|
30
|
+
:"diff_#{ part }",
|
31
|
+
check_from,
|
32
|
+
part
|
33
|
+
|
34
|
+
memo[ part_singular ] = num_of_part
|
35
|
+
memo
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
def default_diff( check_from , part , part_singular )
|
40
|
+
num_of_part = 0
|
41
|
+
part_next_method = :"next_#{ part_singular }"
|
42
|
+
|
43
|
+
loop do
|
44
|
+
check = check_from.send part_next_method
|
45
|
+
|
46
|
+
break if check > end_date
|
47
|
+
|
48
|
+
check_from = check
|
49
|
+
num_of_part += 1
|
50
|
+
end
|
51
|
+
|
52
|
+
[ check_from , num_of_part , part_singular ]
|
53
|
+
end
|
54
|
+
|
55
|
+
def diff_days( *args )
|
56
|
+
default_diff *args , :day
|
57
|
+
end
|
58
|
+
|
59
|
+
def diff_months( check_from , * )
|
60
|
+
num_of_part = 0
|
61
|
+
part_singular = :month
|
62
|
+
part_next_method = :"next_#{ part_singular }"
|
63
|
+
|
64
|
+
loop do
|
65
|
+
check = check_from.send part_next_method
|
66
|
+
|
67
|
+
if check > end_date
|
68
|
+
if start_date.day != check_from.day
|
69
|
+
day = start_date.strftime '%d'
|
70
|
+
month = check_from.strftime '%m'
|
71
|
+
year = check_from.strftime '%Y'
|
72
|
+
|
73
|
+
date_to_try =
|
74
|
+
begin
|
75
|
+
Date.parse "#{ day }/#{ month }/#{ year }"
|
76
|
+
rescue ArgumentError
|
77
|
+
false
|
78
|
+
end
|
79
|
+
|
80
|
+
check_from =
|
81
|
+
if date_to_try
|
82
|
+
date_to_try
|
83
|
+
else
|
84
|
+
next_month = check_from.next_month.strftime '%m'
|
85
|
+
Date.parse( "01/#{ next_month }/#{ year }" ).prev_day
|
86
|
+
end
|
87
|
+
end
|
88
|
+
|
89
|
+
break
|
90
|
+
end
|
91
|
+
|
92
|
+
check_from = check
|
93
|
+
num_of_part += 1
|
94
|
+
end
|
95
|
+
|
96
|
+
[ check_from , num_of_part , part_singular ]
|
97
|
+
end
|
98
|
+
|
99
|
+
def diff_weeks( check_from , * )
|
100
|
+
num_of_part = 0
|
101
|
+
part_singular = :week
|
102
|
+
part_next_method = :next_day
|
103
|
+
check_from = check_from.prev_day
|
104
|
+
|
105
|
+
loop do
|
106
|
+
check = check_from.send part_next_method
|
107
|
+
6.times { check = check.send part_next_method }
|
108
|
+
|
109
|
+
break if check > end_date
|
110
|
+
|
111
|
+
check_from = check
|
112
|
+
num_of_part += 1
|
113
|
+
end
|
114
|
+
|
115
|
+
[ check_from , num_of_part , part_singular ]
|
116
|
+
end
|
117
|
+
|
118
|
+
def diff_years( *args )
|
119
|
+
default_diff *args , :year
|
120
|
+
end
|
121
|
+
|
122
|
+
def to_hash
|
123
|
+
@_hash ||= build_diff_hash
|
124
|
+
end
|
125
|
+
end
|
126
|
+
end
|
data/lib/span/version.rb
ADDED
data/span.gemspec
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 'span/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = 'span'
|
8
|
+
spec.version = Span::VERSION
|
9
|
+
spec.authors = ['Ryan Cook']
|
10
|
+
spec.email = ['cookrn@gmail.com']
|
11
|
+
spec.summary = %q{Calculate the difference between two dates.}
|
12
|
+
# spec.description = %q{TODO: Write a longer description. Optional.}
|
13
|
+
spec.homepage = 'https://github.com/cookrn/span'
|
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 'minitest'
|
23
|
+
spec.add_development_dependency 'rake'
|
24
|
+
end
|
data/test/span_test.rb
ADDED
@@ -0,0 +1,73 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
describe Span do
|
4
|
+
[
|
5
|
+
[
|
6
|
+
'06/01/2014', # January 6th
|
7
|
+
'31/01/2012', # January 31st
|
8
|
+
{
|
9
|
+
:year => 1,
|
10
|
+
:month => 11,
|
11
|
+
:week => 1,
|
12
|
+
:day => 0
|
13
|
+
}
|
14
|
+
],
|
15
|
+
[
|
16
|
+
'06/01/2014', # January 6th
|
17
|
+
'22/02/2013', # February 22nd
|
18
|
+
{
|
19
|
+
:year => 0,
|
20
|
+
:month => 10,
|
21
|
+
:week => 2,
|
22
|
+
:day => 2
|
23
|
+
}
|
24
|
+
],
|
25
|
+
[
|
26
|
+
'06/01/2014', # January 6th
|
27
|
+
'24/02/2013', # February 24th
|
28
|
+
{
|
29
|
+
:year => 0,
|
30
|
+
:month => 10,
|
31
|
+
:week => 2,
|
32
|
+
:day => 0
|
33
|
+
}
|
34
|
+
],
|
35
|
+
[
|
36
|
+
'07/01/2014', # January 7th
|
37
|
+
'11/05/2013', # May 11th
|
38
|
+
{
|
39
|
+
:year => 0,
|
40
|
+
:month => 7,
|
41
|
+
:week => 4,
|
42
|
+
:day => 0
|
43
|
+
}
|
44
|
+
],
|
45
|
+
[
|
46
|
+
'25/03/2013', # March 25th
|
47
|
+
'01/01/2012', # January 1st
|
48
|
+
{
|
49
|
+
:year => 1,
|
50
|
+
:month => 2,
|
51
|
+
:week => 3,
|
52
|
+
:day => 4
|
53
|
+
}
|
54
|
+
]
|
55
|
+
].each do | example |
|
56
|
+
it "expected returns #{ example[ 1 ] }" do
|
57
|
+
check_date = Date.parse example[ 0 ]
|
58
|
+
birth_date = Date.parse example[ 1 ]
|
59
|
+
expected = example[ 2 ]
|
60
|
+
|
61
|
+
computed =
|
62
|
+
Span.compute \
|
63
|
+
check_date,
|
64
|
+
birth_date
|
65
|
+
|
66
|
+
computed.delete_if do | key , _ |
|
67
|
+
!expected.keys.include?( key )
|
68
|
+
end
|
69
|
+
|
70
|
+
computed.must_equal expected
|
71
|
+
end
|
72
|
+
end
|
73
|
+
end
|
data/test/test_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,98 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: span
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Ryan Cook
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-01-13 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: minitest
|
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: rake
|
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
|
+
description:
|
56
|
+
email:
|
57
|
+
- cookrn@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
|
+
- lib/span.rb
|
68
|
+
- lib/span/version.rb
|
69
|
+
- span.gemspec
|
70
|
+
- test/span_test.rb
|
71
|
+
- test/test_helper.rb
|
72
|
+
homepage: https://github.com/cookrn/span
|
73
|
+
licenses:
|
74
|
+
- MIT
|
75
|
+
metadata: {}
|
76
|
+
post_install_message:
|
77
|
+
rdoc_options: []
|
78
|
+
require_paths:
|
79
|
+
- lib
|
80
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
81
|
+
requirements:
|
82
|
+
- - ! '>='
|
83
|
+
- !ruby/object:Gem::Version
|
84
|
+
version: '0'
|
85
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ! '>='
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
requirements: []
|
91
|
+
rubyforge_project:
|
92
|
+
rubygems_version: 2.0.4
|
93
|
+
signing_key:
|
94
|
+
specification_version: 4
|
95
|
+
summary: Calculate the difference between two dates.
|
96
|
+
test_files:
|
97
|
+
- test/span_test.rb
|
98
|
+
- test/test_helper.rb
|