nextday 0.0.1 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +3 -9
- data/lib/nextday/config.rb +23 -0
- data/lib/nextday/core_ext/time.rb +3 -0
- data/lib/nextday/date_extension.rb +29 -4
- data/lib/nextday/version.rb +1 -1
- data/lib/nextday.rb +8 -2
- data/spec/lib/nextday/config_spec.rb +22 -0
- data/spec/lib/nextday/core_ext/date_spec.rb +41 -0
- data/spec/spec_helper.rb +10 -0
- metadata +11 -7
data/README.md
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
|
3
3
|
Finds the Next Working Day even if holidays are in the way.
|
4
4
|
|
5
|
-
Extends ```Date``` to allow you to find the next working day
|
5
|
+
Extends ```Date```, ```Time``` and ```DateTime``` to allow you to find the next working day
|
6
6
|
|
7
7
|
Nextday only checks for English public holidays at the moment, I aim to extend it to support locales in the near future.
|
8
8
|
|
@@ -10,16 +10,10 @@ Nextday only checks for English public holidays at the moment, I aim to extend i
|
|
10
10
|
|
11
11
|
```ruby
|
12
12
|
Date.today.next_working_day
|
13
|
+
DateTime.today.next_working_day
|
14
|
+
Time.now.next_working_day
|
13
15
|
```
|
14
16
|
|
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
17
|
## Installation
|
24
18
|
|
25
19
|
To use with bundler just add nextday to your gem file.
|
@@ -0,0 +1,23 @@
|
|
1
|
+
module Nextday
|
2
|
+
|
3
|
+
CUT_OFF_TIME = "16:00"
|
4
|
+
|
5
|
+
class Config
|
6
|
+
|
7
|
+
class << self
|
8
|
+
attr_writer :cut_off_time
|
9
|
+
|
10
|
+
def cut_off_time
|
11
|
+
@cut_off_time || CUT_OFF_TIME
|
12
|
+
end
|
13
|
+
|
14
|
+
def cut_off_hour
|
15
|
+
cut_off_time.split(":")[0].to_i
|
16
|
+
end
|
17
|
+
|
18
|
+
def cut_off_minute
|
19
|
+
cut_off_time.split(":")[1].to_i
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
@@ -1,12 +1,37 @@
|
|
1
1
|
module Nextday
|
2
2
|
|
3
3
|
module DateExtension
|
4
|
+
|
5
|
+
##
|
6
|
+
#
|
7
|
+
def despatch_day
|
8
|
+
if working_day?
|
9
|
+
if to_time < cut_off_time
|
10
|
+
to_date
|
11
|
+
else
|
12
|
+
next_working_day
|
13
|
+
end
|
14
|
+
else
|
15
|
+
next_working_day
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
def delivery_day
|
20
|
+
despatch_day.next_working_day
|
21
|
+
end
|
22
|
+
|
23
|
+
def cut_off_time
|
24
|
+
hour, minute = Config.cut_off_hour, Config.cut_off_minute
|
25
|
+
|
26
|
+
Time.new(year, month, day, hour, minute)
|
27
|
+
end
|
28
|
+
|
4
29
|
##
|
5
30
|
# The next working day after the current date
|
6
31
|
#
|
7
32
|
# @return [Date] Next Working Day
|
8
33
|
def next_working_day
|
9
|
-
next_day =
|
34
|
+
next_day = to_date + 1
|
10
35
|
|
11
36
|
# keep going until the next day is a working day
|
12
37
|
while !next_day.working_day?
|
@@ -29,7 +54,7 @@ module Nextday
|
|
29
54
|
#
|
30
55
|
# @return [Boolean]
|
31
56
|
def public_holiday?
|
32
|
-
Holidays.dates.include?(
|
57
|
+
Holidays.dates.include?(to_date)
|
33
58
|
end
|
34
59
|
|
35
60
|
##
|
@@ -45,7 +70,7 @@ module Nextday
|
|
45
70
|
#
|
46
71
|
# @return [Boolean]
|
47
72
|
def saturday?
|
48
|
-
|
73
|
+
to_date.wday == 6
|
49
74
|
end if RUBY_VERSION < '1.9'
|
50
75
|
|
51
76
|
##
|
@@ -53,7 +78,7 @@ module Nextday
|
|
53
78
|
#
|
54
79
|
# @return [Boolean]
|
55
80
|
def sunday?
|
56
|
-
|
81
|
+
to_date.wday == 0
|
57
82
|
end if RUBY_VERSION < '1.9'
|
58
83
|
end
|
59
84
|
end
|
data/lib/nextday/version.rb
CHANGED
data/lib/nextday.rb
CHANGED
@@ -1,7 +1,13 @@
|
|
1
|
+
require 'date'
|
2
|
+
require 'time'
|
3
|
+
|
1
4
|
require "nextday/version"
|
5
|
+
require "nextday/config"
|
6
|
+
|
2
7
|
require "nextday/holiday"
|
3
|
-
require "nextday/public_holidays"
|
4
8
|
require "nextday/holidays"
|
9
|
+
require "nextday/public_holidays"
|
5
10
|
|
6
11
|
require "nextday/date_extension"
|
7
|
-
require "nextday/core_ext/
|
12
|
+
require "nextday/core_ext/time"
|
13
|
+
require "nextday/core_ext/date"
|
@@ -0,0 +1,22 @@
|
|
1
|
+
require_relative '../../spec_helper.rb'
|
2
|
+
|
3
|
+
describe Nextday::Config do
|
4
|
+
|
5
|
+
subject { Nextday::Config }
|
6
|
+
|
7
|
+
describe "cut_off_time" do
|
8
|
+
|
9
|
+
describe "default" do
|
10
|
+
it { subject.cut_off_time.should eql(Nextday::CUT_OFF_TIME)}
|
11
|
+
end
|
12
|
+
|
13
|
+
describe "with a new time" do
|
14
|
+
|
15
|
+
before do
|
16
|
+
Nextday::Config.cut_off_time = "18:47"
|
17
|
+
end
|
18
|
+
|
19
|
+
it { subject.cut_off_time.should eql("18:47")}
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
@@ -2,6 +2,47 @@ require_relative '../../../spec_helper.rb'
|
|
2
2
|
|
3
3
|
describe Date do
|
4
4
|
|
5
|
+
describe "#despatch_day" do
|
6
|
+
|
7
|
+
describe "before cut_off_time" do
|
8
|
+
|
9
|
+
weekdays.each do |date|
|
10
|
+
# on a Monday..Friday
|
11
|
+
describe "on a #{date.strftime('%A')}" do
|
12
|
+
|
13
|
+
subject { Time.new(date.year, date.month, date.day, 0, 0).despatch_day.to_date }
|
14
|
+
|
15
|
+
it "should be the same day" do
|
16
|
+
should eql(date)
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
describe "after_cut_off_time" do
|
23
|
+
weekdays.each do |date|
|
24
|
+
# on a Monday..Friday
|
25
|
+
describe "on a #{date.strftime('%A')}" do
|
26
|
+
|
27
|
+
subject { Time.new(date.year, date.month, date.day, Nextday::Config.cut_off_hour + 1) }
|
28
|
+
|
29
|
+
it "should be the next working day" do
|
30
|
+
subject.despatch_day.to_date.should eql(subject.next_working_day)
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
describe "#delivery_day" do
|
38
|
+
|
39
|
+
subject { Time.now.despatch_day }
|
40
|
+
|
41
|
+
it "should be the next working day after the despatch day" do
|
42
|
+
subject.delivery_day.should eql(subject.despatch_day.next_working_day)
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
5
46
|
describe "#working_day?" do
|
6
47
|
|
7
48
|
describe "on a saturday" do
|
data/spec/spec_helper.rb
CHANGED
@@ -1,5 +1,15 @@
|
|
1
1
|
require 'nextday'
|
2
2
|
|
3
|
+
def weekdays
|
4
|
+
today = Date.today
|
5
|
+
|
6
|
+
today += 1 while (today.wday != 1) # step forward until it's monday
|
7
|
+
monday = today
|
8
|
+
|
9
|
+
# monday, tuesday, wednesday, thursday, friday
|
10
|
+
[monday, monday+1, monday+2, monday+3, monday+4]
|
11
|
+
end
|
12
|
+
|
3
13
|
def weekends
|
4
14
|
saturdays + sundays
|
5
15
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: nextday
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 0.1.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,11 +9,11 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2011-10-
|
12
|
+
date: 2011-10-02 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rspec
|
16
|
-
requirement: &
|
16
|
+
requirement: &2152939220 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ~>
|
@@ -21,10 +21,10 @@ dependencies:
|
|
21
21
|
version: 2.6.0
|
22
22
|
type: :development
|
23
23
|
prerelease: false
|
24
|
-
version_requirements: *
|
24
|
+
version_requirements: *2152939220
|
25
25
|
- !ruby/object:Gem::Dependency
|
26
26
|
name: yard
|
27
|
-
requirement: &
|
27
|
+
requirement: &2152938800 !ruby/object:Gem::Requirement
|
28
28
|
none: false
|
29
29
|
requirements:
|
30
30
|
- - ! '>='
|
@@ -32,7 +32,7 @@ dependencies:
|
|
32
32
|
version: '0'
|
33
33
|
type: :development
|
34
34
|
prerelease: false
|
35
|
-
version_requirements: *
|
35
|
+
version_requirements: *2152938800
|
36
36
|
description: ! "\n Finds the Next Working Day even if holidays are in the way.\n
|
37
37
|
\ Extends Date with a .next_working_day instance method.\n "
|
38
38
|
email:
|
@@ -48,13 +48,16 @@ files:
|
|
48
48
|
- README.md
|
49
49
|
- Rakefile
|
50
50
|
- lib/nextday.rb
|
51
|
+
- lib/nextday/config.rb
|
51
52
|
- lib/nextday/core_ext/date.rb
|
53
|
+
- lib/nextday/core_ext/time.rb
|
52
54
|
- lib/nextday/date_extension.rb
|
53
55
|
- lib/nextday/holiday.rb
|
54
56
|
- lib/nextday/holidays.rb
|
55
57
|
- lib/nextday/public_holidays.rb
|
56
58
|
- lib/nextday/version.rb
|
57
59
|
- nextday.gemspec
|
60
|
+
- spec/lib/nextday/config_spec.rb
|
58
61
|
- spec/lib/nextday/core_ext/date_spec.rb
|
59
62
|
- spec/lib/nextday/holiday_spec.rb
|
60
63
|
- spec/lib/nextday/holidays_spec.rb
|
@@ -79,11 +82,12 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
79
82
|
version: '0'
|
80
83
|
requirements: []
|
81
84
|
rubyforge_project: nextday
|
82
|
-
rubygems_version: 1.8.
|
85
|
+
rubygems_version: 1.8.6
|
83
86
|
signing_key:
|
84
87
|
specification_version: 3
|
85
88
|
summary: Finds the Next Working Day even if holidays are in the way.
|
86
89
|
test_files:
|
90
|
+
- spec/lib/nextday/config_spec.rb
|
87
91
|
- spec/lib/nextday/core_ext/date_spec.rb
|
88
92
|
- spec/lib/nextday/holiday_spec.rb
|
89
93
|
- spec/lib/nextday/holidays_spec.rb
|