nextday 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.
- data/.gitignore +6 -0
- data/.rspec +1 -0
- data/Gemfile +15 -0
- data/Guardfile +6 -0
- data/README.md +29 -0
- data/Rakefile +1 -0
- data/lib/nextday/core_ext/date.rb +3 -0
- data/lib/nextday/date_extension.rb +59 -0
- data/lib/nextday/holiday.rb +11 -0
- data/lib/nextday/holidays.rb +33 -0
- data/lib/nextday/public_holidays.rb +62 -0
- data/lib/nextday/version.rb +3 -0
- data/lib/nextday.rb +7 -0
- data/nextday.gemspec +28 -0
- data/spec/lib/nextday/core_ext/date_spec.rb +52 -0
- data/spec/lib/nextday/holiday_spec.rb +9 -0
- data/spec/lib/nextday/holidays_spec.rb +25 -0
- data/spec/spec_helper.rb +23 -0
- metadata +90 -0
data/.gitignore
ADDED
data/.rspec
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--color
|
data/Gemfile
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
source "http://rubygems.org"
|
2
|
+
|
3
|
+
# Specify your gem's dependencies in nextday.gemspec
|
4
|
+
gemspec
|
5
|
+
|
6
|
+
gem 'rake'
|
7
|
+
|
8
|
+
require 'rbconfig'
|
9
|
+
|
10
|
+
if RbConfig::CONFIG['target_os'] =~ /darwin/i
|
11
|
+
gem 'rb-fsevent', '>= 0.4.0', :require => false
|
12
|
+
gem 'growl_notify', :require => false
|
13
|
+
gem 'guard', '~> 0.6.0', :require => false
|
14
|
+
gem 'guard-rspec', '~> 0.4.0', :require => false
|
15
|
+
end
|
data/Guardfile
ADDED
data/README.md
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
# Nextday
|
2
|
+
|
3
|
+
Finds the Next Working Day even if holidays are in the way.
|
4
|
+
|
5
|
+
Extends ```Date``` to allow you to find the next working day
|
6
|
+
|
7
|
+
Nextday only checks for English public holidays at the moment, I aim to extend it to support locales in the near future.
|
8
|
+
|
9
|
+
## Usage
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
Date.today.next_working_day
|
13
|
+
```
|
14
|
+
|
15
|
+
To use this with ```DateTime``` and ```Time``` convert to ```Date``` first, for instance.
|
16
|
+
|
17
|
+
```ruby
|
18
|
+
Time.now.to_date.next_working_day
|
19
|
+
DateTime.new(2030,11,30).to_date.next_working_day
|
20
|
+
```
|
21
|
+
|
22
|
+
|
23
|
+
## Installation
|
24
|
+
|
25
|
+
To use with bundler just add nextday to your gem file.
|
26
|
+
|
27
|
+
```ruby
|
28
|
+
gem 'nextday'
|
29
|
+
```
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
@@ -0,0 +1,59 @@
|
|
1
|
+
module Nextday
|
2
|
+
|
3
|
+
module DateExtension
|
4
|
+
##
|
5
|
+
# The next working day after the current date
|
6
|
+
#
|
7
|
+
# @return [Date] Next Working Day
|
8
|
+
def next_working_day
|
9
|
+
next_day = self + 1
|
10
|
+
|
11
|
+
# keep going until the next day is a working day
|
12
|
+
while !next_day.working_day?
|
13
|
+
next_day = next_day + 1
|
14
|
+
end
|
15
|
+
|
16
|
+
next_day
|
17
|
+
end
|
18
|
+
|
19
|
+
##
|
20
|
+
# Is the current date a working day?
|
21
|
+
#
|
22
|
+
# @return [Boolean]
|
23
|
+
def working_day?
|
24
|
+
!weekend? and !public_holiday?
|
25
|
+
end
|
26
|
+
|
27
|
+
##
|
28
|
+
# Is the current date a public holiday?
|
29
|
+
#
|
30
|
+
# @return [Boolean]
|
31
|
+
def public_holiday?
|
32
|
+
Holidays.dates.include?(self)
|
33
|
+
end
|
34
|
+
|
35
|
+
##
|
36
|
+
# Is the current date on the weekend?
|
37
|
+
#
|
38
|
+
# @return [Boolean]
|
39
|
+
def weekend?
|
40
|
+
saturday? or sunday?
|
41
|
+
end
|
42
|
+
|
43
|
+
##
|
44
|
+
# Is the current date a saturday?
|
45
|
+
#
|
46
|
+
# @return [Boolean]
|
47
|
+
def saturday?
|
48
|
+
self.wday == 6
|
49
|
+
end if RUBY_VERSION < '1.9'
|
50
|
+
|
51
|
+
##
|
52
|
+
# Is the current date a saturday?
|
53
|
+
#
|
54
|
+
# @return [Boolean]
|
55
|
+
def sunday?
|
56
|
+
self.wday == 0
|
57
|
+
end if RUBY_VERSION < '1.9'
|
58
|
+
end
|
59
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
require 'singleton'
|
2
|
+
|
3
|
+
module Nextday
|
4
|
+
|
5
|
+
class Holidays
|
6
|
+
include Enumerable
|
7
|
+
include Singleton
|
8
|
+
|
9
|
+
def initialize
|
10
|
+
@holidays = PUBLIC_HOLIDAYS
|
11
|
+
end
|
12
|
+
|
13
|
+
def each(&block)
|
14
|
+
@holidays.each{|holiday| block.call(holiday)}
|
15
|
+
end
|
16
|
+
|
17
|
+
##
|
18
|
+
# Returning all the dates holidays occur
|
19
|
+
#
|
20
|
+
# @return [Array] of holidays as [Date] objects
|
21
|
+
def self.dates
|
22
|
+
instance.map(&:date)
|
23
|
+
end
|
24
|
+
|
25
|
+
##
|
26
|
+
# Find the first holiday for the supplied date
|
27
|
+
# @param [Date] format the format type, `:text` or `:html`
|
28
|
+
# @return [Holiday] the first matching holiday for the supplied date
|
29
|
+
def self.find(date)
|
30
|
+
instance.find{ |holiday| holiday.date == date }
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
@@ -0,0 +1,62 @@
|
|
1
|
+
module Nextday
|
2
|
+
##
|
3
|
+
# Bank and public holidays in England and Wales
|
4
|
+
# Includes substitute days if holiday lands on a weekend
|
5
|
+
# http://www.direct.gov.uk/en/Employment/Employees/Timeoffandholidays/DG_073741
|
6
|
+
#
|
7
|
+
# Be careful when adding new holidays, please specify the date as '1' not '01'
|
8
|
+
#
|
9
|
+
BANK_AND_PUBLIC_HOLIDAYS_ENGLAND_AND_WALES = [
|
10
|
+
Holiday.new("Spring Bank Holiday", Date.new(2011, 5, 30)),
|
11
|
+
Holiday.new("Spring Bank Holiday", Date.new(2012, 6, 4)),
|
12
|
+
Holiday.new("Spring Bank Holiday", Date.new(2013, 5, 27)),
|
13
|
+
Holiday.new("Spring Bank Holiday", Date.new(2014, 5, 26)),
|
14
|
+
Holiday.new("Spring Bank Holiday", Date.new(2015, 5, 25)),
|
15
|
+
|
16
|
+
Holiday.new("Early May Bank Holiday", Date.new(2011, 5, 2)),
|
17
|
+
Holiday.new("Early May Bank Holiday", Date.new(2012, 5, 7)),
|
18
|
+
Holiday.new("Early May Bank Holiday", Date.new(2013, 5, 6)),
|
19
|
+
Holiday.new("Early May Bank Holiday", Date.new(2014, 5, 5)),
|
20
|
+
Holiday.new("Early May Bank Holiday", Date.new(2015, 5, 4)),
|
21
|
+
|
22
|
+
Holiday.new("Queen's Diamond Jubilee", Date.new(2012, 6, 5)),
|
23
|
+
|
24
|
+
Holiday.new("Good Friday", Date.new(2011, 4, 22)),
|
25
|
+
Holiday.new("Good Friday", Date.new(2012, 4, 06)),
|
26
|
+
Holiday.new("Good Friday", Date.new(2013, 3, 29)),
|
27
|
+
Holiday.new("Good Friday", Date.new(2014, 4, 18)),
|
28
|
+
Holiday.new("Good Friday", Date.new(2015, 4, 3)),
|
29
|
+
|
30
|
+
Holiday.new("Summer Bank Holiday", Date.new(2011, 8, 29)),
|
31
|
+
Holiday.new("Summer Bank Holiday", Date.new(2012, 8, 27)),
|
32
|
+
Holiday.new("Summer Bank Holiday", Date.new(2013, 8, 26)),
|
33
|
+
Holiday.new("Summer Bank Holiday", Date.new(2014, 8, 25)),
|
34
|
+
Holiday.new("Summer Bank Holiday", Date.new(2015, 8, 31)),
|
35
|
+
|
36
|
+
Holiday.new("New Year's Day", Date.new(2011, 1, 3)),
|
37
|
+
Holiday.new("New Year's Day", Date.new(2012, 1, 2)),
|
38
|
+
Holiday.new("New Year's Day", Date.new(2013, 1, 1)),
|
39
|
+
Holiday.new("New Year's Day", Date.new(2014, 1, 1)),
|
40
|
+
Holiday.new("New Year's Day", Date.new(2015, 1, 1)),
|
41
|
+
|
42
|
+
Holiday.new("Christmas Day", Date.new(2011, 12, 26)),
|
43
|
+
Holiday.new("Christmas Day", Date.new(2012, 12, 25)),
|
44
|
+
Holiday.new("Christmas Day", Date.new(2013, 12, 25)),
|
45
|
+
Holiday.new("Christmas Day", Date.new(2014, 12, 25)),
|
46
|
+
Holiday.new("Christmas Day", Date.new(2015, 12, 25)),
|
47
|
+
|
48
|
+
Holiday.new("Easter Monday", Date.new(2011, 4, 25)),
|
49
|
+
Holiday.new("Easter Monday", Date.new(2012, 4, 9)),
|
50
|
+
Holiday.new("Easter Monday", Date.new(2013, 4, 1)),
|
51
|
+
Holiday.new("Easter Monday", Date.new(2014, 4, 21)),
|
52
|
+
Holiday.new("Easter Monday", Date.new(2015, 4, 6)),
|
53
|
+
|
54
|
+
Holiday.new("Boxing Day", Date.new(2011, 12, 27)),
|
55
|
+
Holiday.new("Boxing Day", Date.new(2012, 12, 26)),
|
56
|
+
Holiday.new("Boxing Day", Date.new(2013, 12, 26)),
|
57
|
+
Holiday.new("Boxing Day", Date.new(2014, 12, 26)),
|
58
|
+
Holiday.new("Boxing Day", Date.new(2015, 12, 28))
|
59
|
+
]
|
60
|
+
|
61
|
+
PUBLIC_HOLIDAYS = BANK_AND_PUBLIC_HOLIDAYS_ENGLAND_AND_WALES
|
62
|
+
end
|
data/lib/nextday.rb
ADDED
data/nextday.gemspec
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "nextday/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "nextday"
|
7
|
+
s.version = Nextday::VERSION
|
8
|
+
s.authors = ["Robert Williams"]
|
9
|
+
s.email = ["rob@r-williams.com"]
|
10
|
+
s.homepage = "https://github.com/robwilliams/nextday"
|
11
|
+
s.summary = %q{Finds the Next Working Day even if holidays are in the way.}
|
12
|
+
s.description = %q{
|
13
|
+
Finds the Next Working Day even if holidays are in the way.
|
14
|
+
Extends Date with a .next_working_day instance method.
|
15
|
+
}
|
16
|
+
|
17
|
+
s.rubyforge_project = "nextday"
|
18
|
+
|
19
|
+
s.files = `git ls-files`.split("\n")
|
20
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
21
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
22
|
+
s.require_paths = ["lib"]
|
23
|
+
|
24
|
+
# specify any dependencies here; for example:
|
25
|
+
s.add_development_dependency('rspec', '~> 2.6.0')
|
26
|
+
s.add_development_dependency('yard')
|
27
|
+
# s.add_runtime_dependency "rest-client"
|
28
|
+
end
|
@@ -0,0 +1,52 @@
|
|
1
|
+
require_relative '../../../spec_helper.rb'
|
2
|
+
|
3
|
+
describe Date do
|
4
|
+
|
5
|
+
describe "#working_day?" do
|
6
|
+
|
7
|
+
describe "on a saturday" do
|
8
|
+
it {
|
9
|
+
saturdays.each { |date| date.should_not be_a_working_day }
|
10
|
+
}
|
11
|
+
end
|
12
|
+
|
13
|
+
describe "on a sunday" do
|
14
|
+
it {
|
15
|
+
sundays.each { |date| date.should_not be_a_working_day }
|
16
|
+
}
|
17
|
+
end
|
18
|
+
|
19
|
+
describe "on a public holiday" do
|
20
|
+
it {
|
21
|
+
Nextday::Holidays.dates.each { |date| date.should_not be_a_working_day }
|
22
|
+
}
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
describe "#next_working_day" do
|
27
|
+
|
28
|
+
subject {Date.today.next_working_day}
|
29
|
+
|
30
|
+
it {should be_kind_of(Date)}
|
31
|
+
it {should_not eql(Date.today)}
|
32
|
+
it {should be_a_working_day }
|
33
|
+
|
34
|
+
describe "on a saturday" do
|
35
|
+
it {
|
36
|
+
saturdays.each { |date| date.next_working_day.should be_a_working_day }
|
37
|
+
}
|
38
|
+
end
|
39
|
+
|
40
|
+
describe "on a sunday" do
|
41
|
+
it {
|
42
|
+
sundays.each { |date| date.next_working_day.should be_a_working_day }
|
43
|
+
}
|
44
|
+
end
|
45
|
+
|
46
|
+
describe "on a public holiday" do
|
47
|
+
it {
|
48
|
+
Nextday::Holidays.dates.each { |date| date.next_working_day.should be_a_working_day }
|
49
|
+
}
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
require_relative '../../spec_helper.rb'
|
2
|
+
|
3
|
+
describe Nextday::Holidays do
|
4
|
+
|
5
|
+
describe "#find" do
|
6
|
+
|
7
|
+
Nextday::Holidays.dates.each do |date|
|
8
|
+
describe "when the date is a holiday (#{date})" do
|
9
|
+
subject {Nextday::Holidays.find(date)}
|
10
|
+
|
11
|
+
it { should be_kind_of(Nextday::Holiday) }
|
12
|
+
it { subject.date.should eql(date) }
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
describe "#dates" do
|
18
|
+
|
19
|
+
subject { Nextday::Holidays.dates }
|
20
|
+
|
21
|
+
it { should be_kind_of(Array) }
|
22
|
+
it { subject.should have_at_least(1).holiday }
|
23
|
+
it { subject.each {|date| date.should be_a_kind_of(Date)} }
|
24
|
+
end
|
25
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
require 'nextday'
|
2
|
+
|
3
|
+
def weekends
|
4
|
+
saturdays + sundays
|
5
|
+
end
|
6
|
+
|
7
|
+
def saturdays
|
8
|
+
today = Date.today
|
9
|
+
|
10
|
+
today += 1 while (today.wday != 6) # step forward until it's saturday
|
11
|
+
saturday = today
|
12
|
+
|
13
|
+
saturday.step(saturday + 365, 7).to_a
|
14
|
+
end
|
15
|
+
|
16
|
+
def sundays
|
17
|
+
today = Date.today
|
18
|
+
|
19
|
+
today += 1 while (today.wday != 0) # step forward until it's sunday
|
20
|
+
sunday = today
|
21
|
+
|
22
|
+
sunday.step(sunday + 365, 7).to_a
|
23
|
+
end
|
metadata
ADDED
@@ -0,0 +1,90 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: nextday
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Robert Williams
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2011-10-01 00:00:00.000000000Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rspec
|
16
|
+
requirement: &2151822900 !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ~>
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: 2.6.0
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: *2151822900
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: yard
|
27
|
+
requirement: &2152545660 !ruby/object:Gem::Requirement
|
28
|
+
none: false
|
29
|
+
requirements:
|
30
|
+
- - ! '>='
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '0'
|
33
|
+
type: :development
|
34
|
+
prerelease: false
|
35
|
+
version_requirements: *2152545660
|
36
|
+
description: ! "\n Finds the Next Working Day even if holidays are in the way.\n
|
37
|
+
\ Extends Date with a .next_working_day instance method.\n "
|
38
|
+
email:
|
39
|
+
- rob@r-williams.com
|
40
|
+
executables: []
|
41
|
+
extensions: []
|
42
|
+
extra_rdoc_files: []
|
43
|
+
files:
|
44
|
+
- .gitignore
|
45
|
+
- .rspec
|
46
|
+
- Gemfile
|
47
|
+
- Guardfile
|
48
|
+
- README.md
|
49
|
+
- Rakefile
|
50
|
+
- lib/nextday.rb
|
51
|
+
- lib/nextday/core_ext/date.rb
|
52
|
+
- lib/nextday/date_extension.rb
|
53
|
+
- lib/nextday/holiday.rb
|
54
|
+
- lib/nextday/holidays.rb
|
55
|
+
- lib/nextday/public_holidays.rb
|
56
|
+
- lib/nextday/version.rb
|
57
|
+
- nextday.gemspec
|
58
|
+
- spec/lib/nextday/core_ext/date_spec.rb
|
59
|
+
- spec/lib/nextday/holiday_spec.rb
|
60
|
+
- spec/lib/nextday/holidays_spec.rb
|
61
|
+
- spec/spec_helper.rb
|
62
|
+
homepage: https://github.com/robwilliams/nextday
|
63
|
+
licenses: []
|
64
|
+
post_install_message:
|
65
|
+
rdoc_options: []
|
66
|
+
require_paths:
|
67
|
+
- lib
|
68
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
69
|
+
none: false
|
70
|
+
requirements:
|
71
|
+
- - ! '>='
|
72
|
+
- !ruby/object:Gem::Version
|
73
|
+
version: '0'
|
74
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
75
|
+
none: false
|
76
|
+
requirements:
|
77
|
+
- - ! '>='
|
78
|
+
- !ruby/object:Gem::Version
|
79
|
+
version: '0'
|
80
|
+
requirements: []
|
81
|
+
rubyforge_project: nextday
|
82
|
+
rubygems_version: 1.8.10
|
83
|
+
signing_key:
|
84
|
+
specification_version: 3
|
85
|
+
summary: Finds the Next Working Day even if holidays are in the way.
|
86
|
+
test_files:
|
87
|
+
- spec/lib/nextday/core_ext/date_spec.rb
|
88
|
+
- spec/lib/nextday/holiday_spec.rb
|
89
|
+
- spec/lib/nextday/holidays_spec.rb
|
90
|
+
- spec/spec_helper.rb
|