dateoperations 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/LICENSE.txt +21 -0
- data/README.md +9 -0
- data/lib/dateoperations.rb +20 -1
- metadata +21 -8
- data/Rakefile +0 -13
- data/test/dateoperations_test.rb +0 -46
- data/test/test_helper.rb +0 -8
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: cfd90d8293a749a6fd266508ca029f366a0b08e0
|
4
|
+
data.tar.gz: 74dcfa904f08b166e10dd3c1d294d83b6082ba2e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f9cb3fc95767df4055e7c7c942e3fb334bd171a5810f694d511a7006c4e6c5b487d8e15d6b78f596726ef6659db0ac273a4bc36c896b7107ad9c566e8188f6d9
|
7
|
+
data.tar.gz: 4eb2a8cb59bba7d0906fe79e36f02091e0b712ab03189b2cfa88ffd9bf4cb89bd4dffea27180f3be622835736cec5916eb071d2cfd87e42e77315a1bbd1b2314
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
MIT License
|
2
|
+
|
3
|
+
Copyright (c) 2016 Michael Schwarze
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
13
|
+
copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
21
|
+
SOFTWARE.
|
data/README.md
CHANGED
@@ -13,10 +13,19 @@ Download the gem, then install it:
|
|
13
13
|
require 'dateoperations'
|
14
14
|
...
|
15
15
|
DateOperations.country #=> :de
|
16
|
+
|
16
17
|
DateOperations.holiday?(Date.new(2016, 10, 3)) #=> true
|
18
|
+
|
17
19
|
DateOperations.country = :us #=> :us
|
18
20
|
DateOperations.holiday?(Date.new(2016, 10, 3)) # => false
|
21
|
+
|
19
22
|
DateOperations.country = :de #=> :de
|
20
23
|
DateOperations.number_of_business_days_between(Date.new(2016, 10, 1), Date.new(2016, 10, -1)) #=> 20
|
24
|
+
|
21
25
|
DateOperations.country = :au #=> :au
|
22
26
|
DateOperations.number_of_business_days_between(Date.new(2016, 10, 1), Date.new(2016, 10, -1)) #=> 21
|
27
|
+
|
28
|
+
DateOperations.weekend?(DateOperations.weekend?(Date.new(2016, 6, 18)) #=> true
|
29
|
+
|
30
|
+
## Documentation
|
31
|
+
[Documentation](http://dateoperations.schwarze-web.de/DateOperations.html)
|
data/lib/dateoperations.rb
CHANGED
@@ -1,38 +1,57 @@
|
|
1
1
|
require 'date'
|
2
2
|
require 'holidays'
|
3
3
|
|
4
|
-
|
4
|
+
##
|
5
|
+
# Different date operations, primarily around business days, based on the Holidays[https://github.com/holidays/holidays] Gem.
|
6
|
+
# - Home[]https://github.com/msc01/dateoperations
|
7
|
+
# - Documentation[http://dateoperations.schwarze-web.de/DateOperations.html]
|
5
8
|
class DateOperations
|
6
9
|
@country = :de
|
7
10
|
|
8
11
|
class << self
|
12
|
+
# Switches to the calendar of the respective country:
|
13
|
+
# - :de = Germany
|
14
|
+
# - :us = USA
|
15
|
+
# - ...
|
9
16
|
attr_accessor :country
|
10
17
|
end
|
11
18
|
|
19
|
+
# Returns an array with dates for the "week days" (= Monday to Friday) between a given start and end date.
|
12
20
|
def self.week_days_between(start_date, end_date)
|
13
21
|
week_day_nbrs = (1..5)
|
14
22
|
(start_date..end_date).select { |w| week_day_nbrs.include?(w.wday) }
|
15
23
|
end
|
16
24
|
|
25
|
+
# returns the number of "week days" (= Monday to Friday) between a given start and end date.
|
17
26
|
def self.number_of_week_days_between(start_date, end_date)
|
18
27
|
week_days = week_days_between(start_date, end_date)
|
19
28
|
week_days.length
|
20
29
|
end
|
21
30
|
|
31
|
+
# Returns an array with dates
|
32
|
+
# for the "business days" = "week days" (= Monday to Friday)
|
33
|
+
# without <tt>holidays</tt>
|
34
|
+
# between a given start and end date.
|
22
35
|
def self.business_days_between(start_date, end_date)
|
23
36
|
week_days = week_days_between(start_date, end_date)
|
24
37
|
week_days.reject { |b| holiday?(b) }
|
25
38
|
end
|
26
39
|
|
40
|
+
# Returns the number of "business days"
|
41
|
+
# = "week days" (= Monday to Friday)
|
42
|
+
# without <tt>holidays</tt>
|
43
|
+
# between a given start and end date.
|
27
44
|
def self.number_of_business_days_between(start_date, end_date)
|
28
45
|
business_days = business_days_between(start_date, end_date)
|
29
46
|
business_days.length
|
30
47
|
end
|
31
48
|
|
49
|
+
# Checks whether a given date is a weekend (Saturday, Sunday).
|
32
50
|
def self.weekend?(date)
|
33
51
|
date.saturday? || date.sunday?
|
34
52
|
end
|
35
53
|
|
54
|
+
# Checks whether a given date is a holiday based on the calendar for the respective <tt>country</tt>.
|
36
55
|
def self.holiday?(date)
|
37
56
|
return false if Holidays.on(date, DateOperations.country, :informal).empty?
|
38
57
|
true
|
metadata
CHANGED
@@ -1,27 +1,40 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: dateoperations
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Michael Schwarze
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-06-
|
12
|
-
dependencies:
|
13
|
-
|
11
|
+
date: 2016-06-21 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: holidays
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - '='
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 4.3.0
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - '='
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 4.3.0
|
27
|
+
description: Simple date operations to determine weekends, holidays, and esp. business
|
28
|
+
days. Based on the Holidays Gem.
|
14
29
|
email: michael@schwarze-web.de
|
15
30
|
executables: []
|
16
31
|
extensions: []
|
17
32
|
extra_rdoc_files: []
|
18
33
|
files:
|
34
|
+
- LICENSE.txt
|
19
35
|
- README.md
|
20
|
-
- Rakefile
|
21
36
|
- lib/dateoperations.rb
|
22
|
-
|
23
|
-
- test/test_helper.rb
|
24
|
-
homepage: http://rubygems.org/gems/dateoperations
|
37
|
+
homepage: https://github.com/msc01/dateoperations
|
25
38
|
licenses:
|
26
39
|
- MIT
|
27
40
|
metadata: {}
|
data/Rakefile
DELETED
data/test/dateoperations_test.rb
DELETED
@@ -1,46 +0,0 @@
|
|
1
|
-
#!/usr/bin/env ruby
|
2
|
-
|
3
|
-
require_relative 'test_helper'
|
4
|
-
|
5
|
-
# Test class for Minitest Unit Tests for class DateOperations
|
6
|
-
class DateOperationsTest < Minitest::Test
|
7
|
-
def test_december_2016_has_22_week_days
|
8
|
-
d1 = Date.new(2016, 12, 1)
|
9
|
-
d2 = Date.new(2016, 12, -1)
|
10
|
-
assert_equal 22, DateOperations.number_of_week_days_between(d1, d2)
|
11
|
-
end
|
12
|
-
|
13
|
-
def test_december_2016_has_21_business_days
|
14
|
-
d1 = Date.new(2016, 12, 1)
|
15
|
-
d2 = Date.new(2016, 12, -1)
|
16
|
-
assert_equal 21, DateOperations.number_of_business_days_between(d1, d2)
|
17
|
-
end
|
18
|
-
|
19
|
-
def test_24th_of_december_2016_is_a_holiday
|
20
|
-
assert DateOperations.holiday?(Date.new(2016, 12, 24))
|
21
|
-
end
|
22
|
-
|
23
|
-
def test_23th_of_december_2016_is_not_a_holiday
|
24
|
-
assert !DateOperations.holiday?(Date.new(2016, 12, 23))
|
25
|
-
end
|
26
|
-
|
27
|
-
def test_11th_of_june_2016_is_weekend
|
28
|
-
assert DateOperations.weekend?(Date.new(2016, 6, 11))
|
29
|
-
end
|
30
|
-
|
31
|
-
def test_10th_of_june_2016_is_not_weekend
|
32
|
-
assert !DateOperations.weekend?(Date.new(2016, 6, 10))
|
33
|
-
end
|
34
|
-
|
35
|
-
def test_june_2016_has_22_week_days
|
36
|
-
d1 = Date.new(2016, 6, 1)
|
37
|
-
d2 = Date.new(2016, 6, 30)
|
38
|
-
assert_equal 22, DateOperations.number_of_week_days_between(d1, d2)
|
39
|
-
end
|
40
|
-
|
41
|
-
def test_startdate_greater_enddate_returns_0_week_days
|
42
|
-
d1 = Date.new(2016, 6, 1)
|
43
|
-
d2 = Date.new(2016, 6, 30)
|
44
|
-
assert_equal 0, DateOperations.number_of_week_days_between(d2, d1)
|
45
|
-
end
|
46
|
-
end
|