uk_working_days 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
data/.gitignore ADDED
@@ -0,0 +1,21 @@
1
+ ## MAC OS
2
+ .DS_Store
3
+
4
+ ## TEXTMATE
5
+ *.tmproj
6
+ tmtags
7
+
8
+ ## EMACS
9
+ *~
10
+ \#*
11
+ .\#*
12
+
13
+ ## VIM
14
+ *.swp
15
+
16
+ ## PROJECT::GENERAL
17
+ coverage
18
+ rdoc
19
+ pkg
20
+
21
+ ## PROJECT::SPECIFIC
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2010 Notonthehighstreet Enterprises Ltd
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,40 @@
1
+ = UK Working Days
2
+
3
+ Provides mixins to the Date, Time and DateTime clasess to provide functionality around UK working days / public holidays.
4
+
5
+ == Basic Usage
6
+
7
+ If Easter monday is on the 5th April 2010:
8
+
9
+ Date.new(2010, 4, 5).public_holiday? # => true
10
+ Date.new(2010, 4, 5).working_day? # => false
11
+ Date.new(2010, 4, 5).weekday? # => true
12
+ Date.new(2010, 4, 5) == Date.easter_monday(2010) # => true
13
+ Date.public_holidays(2010).include?( Date.new(2010, 4, 5) ) # => true
14
+
15
+ Will return DateTimes or Times instead as appropriate. See rdoc for more information.
16
+
17
+ == Dependencies
18
+
19
+ * ActiveSupport
20
+
21
+ == Authors
22
+
23
+ * Gregory Becker
24
+ * Joe Simms
25
+ * Roland Swingler
26
+ * Jonathan Viney
27
+
28
+ == Note on Patches/Pull Requests
29
+
30
+ * Fork the project.
31
+ * Make your feature addition or bug fix.
32
+ * Add tests for it. This is important so I don't break it in a
33
+ future version unintentionally.
34
+ * Commit, do not mess with rakefile, version, or history.
35
+ (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
36
+ * Send me a pull request. Bonus points for topic branches.
37
+
38
+ == Copyright
39
+
40
+ Copyright (c) 2010 Notonthehighstreet Enterprises Ltd. See LICENSE for details.
data/Rakefile ADDED
@@ -0,0 +1,54 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "uk_working_days"
8
+ gem.summary = %Q{Provide date helpers for UK working days}
9
+ gem.description = %Q{Provide date helpers for UK working days}
10
+ gem.email = "roland.swingler@gmail.com"
11
+ gem.homepage = "http://github.com/knaveofdiamonds/uk_working_days"
12
+ gem.authors = ["Roland Swingler"]
13
+ gem.add_dependency "activesupport", ">= 2"
14
+ gem.add_development_dependency "shoulda", ">= 0"
15
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
16
+ end
17
+ Jeweler::GemcutterTasks.new
18
+ rescue LoadError
19
+ puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
20
+ end
21
+
22
+ require 'rake/testtask'
23
+ Rake::TestTask.new(:test) do |test|
24
+ test.libs << 'lib' << 'test'
25
+ test.pattern = 'test/**/test_*.rb'
26
+ test.verbose = true
27
+ end
28
+
29
+ begin
30
+ require 'rcov/rcovtask'
31
+ Rcov::RcovTask.new do |test|
32
+ test.libs << 'test'
33
+ test.pattern = 'test/**/test_*.rb'
34
+ test.verbose = true
35
+ end
36
+ rescue LoadError
37
+ task :rcov do
38
+ abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
39
+ end
40
+ end
41
+
42
+ task :test => :check_dependencies
43
+
44
+ task :default => :test
45
+
46
+ require 'rake/rdoctask'
47
+ Rake::RDocTask.new do |rdoc|
48
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
49
+
50
+ rdoc.rdoc_dir = 'rdoc'
51
+ rdoc.title = "uk_working_days #{version}"
52
+ rdoc.rdoc_files.include('README*')
53
+ rdoc.rdoc_files.include('lib/**/*.rb')
54
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
@@ -0,0 +1,100 @@
1
+ module UkWorkingDays
2
+ # Extensions to the Date and DateTime classes
3
+ module DateExtensions
4
+ # Returns true if this day is in the week
5
+ def weekday?
6
+ ! weekend?
7
+ end
8
+
9
+ # Returns true if this day is on the weekend
10
+ def weekend?
11
+ wday == 0 || wday == 6
12
+ end
13
+
14
+ # Returns true if this day is a bank holiday
15
+ def public_holiday?
16
+ Date.public_holidays(year).include?(to_date)
17
+ end
18
+
19
+ # Returns true if this day is a normal weekday
20
+ def working_day?
21
+ weekday? && ! public_holiday?
22
+ end
23
+
24
+ # Returns the next (or count'th) working day
25
+ def next_working_day(count = 1)
26
+ negative = count < 0
27
+ count = count.abs
28
+ date = negative ? yesterday : tomorrow
29
+
30
+ loop do
31
+ count -= 1 if date.working_day?
32
+ return date if count.zero?
33
+
34
+ date += negative ? -1 : 1
35
+ end
36
+ end
37
+
38
+ # The previous (or count'th) working day
39
+ def previous_working_day(count = 1)
40
+ next_working_day(-count)
41
+ end
42
+
43
+ module ClassMethods
44
+ # New year's day, or the next weekday following if on a weekend
45
+ def new_years_day_holiday(year)
46
+ nyd = new(year, 1, 1)
47
+ nyd.weekday?() ? nyd : nyd.next_week
48
+ end
49
+
50
+ # Christmas day, or the next weekday following if on a weekend
51
+ def christmas_day_holiday(year)
52
+ xmas_day = new(year, 12, 25)
53
+ xmas_day.weekday? ? xmas_day : xmas_day.next_week
54
+ end
55
+
56
+ # Boxing day, or the next working day following
57
+ def boxing_day_holiday(year)
58
+ ([0,5,6].include? christmas_day_holiday(year).wday) ? christmas_day_holiday(year).next_week : christmas_day_holiday(year).tomorrow
59
+ end
60
+
61
+ # The monday after Easter Sunday
62
+ def easter_monday(year)
63
+ easter(year) + 1
64
+ end
65
+
66
+ # The friday before Easter Sunday
67
+ def good_friday(year)
68
+ easter(year) - 2
69
+ end
70
+
71
+ # The first monday in May
72
+ def may_bank_holiday(year)
73
+ first_day_may = new(year, 5, 1)
74
+ first_day_may.wday == 1 ? first_day_may : first_day_may.next_week
75
+ end
76
+
77
+ # The last monday in May
78
+ def spring_bank_holiday(year)
79
+ new(year, 5, 31).beginning_of_week
80
+ end
81
+
82
+ # The last monday in August
83
+ def summer_bank_holiday(year)
84
+ new(year, 8, 31).beginning_of_week
85
+ end
86
+
87
+ # An Array of all public holidays for the given year
88
+ def public_holidays(year)
89
+ [new_years_day_holiday(year),
90
+ good_friday(year),
91
+ easter_monday(year),
92
+ may_bank_holiday(year),
93
+ spring_bank_holiday(year),
94
+ summer_bank_holiday(year),
95
+ christmas_day_holiday(year),
96
+ boxing_day_holiday(year)]
97
+ end
98
+ end
99
+ end
100
+ end
@@ -0,0 +1,43 @@
1
+ # The easter calculation is largely derived from
2
+ # reusablecode.blogspot.com under the following license
3
+ #
4
+ # Copyright (c) 2008, reusablecode.blogspot.com; some rights reserved.
5
+ #
6
+ # This work is licensed under the Creative Commons Attribution License. To view
7
+ # a copy of this license, visit http://creativecommons.org/licenses/by/3.0/ or
8
+ # send a letter to Creative Commons, 559 Nathan Abbott Way, Stanford, California
9
+ # 94305, USA.
10
+ #
11
+
12
+ module UkWorkingDays
13
+ module Easter
14
+ # Calculate easter sunday for the given year
15
+ def easter(year)
16
+ golden_number = (year % 19) + 1
17
+
18
+ if year <= 1752 then
19
+ # Julian calendar
20
+ dominical_number = (year + (year / 4) + 5) % 7
21
+ paschal_full_moon = (3 - (11 * golden_number) - 7) % 30
22
+ else
23
+ # Gregorian calendar
24
+ dominical_number = (year + (year / 4) - (year / 100) + (year / 400)) % 7
25
+ solar_correction = (year - 1600) / 100 - (year - 1600) / 400
26
+ lunar_correction = (((year - 1400) / 100) * 8) / 25
27
+ paschal_full_moon = (3 - 11 * golden_number + solar_correction - lunar_correction) % 30
28
+ end
29
+
30
+ dominical_number += 7 until dominical_number > 0
31
+
32
+ paschal_full_moon += 30 until paschal_full_moon > 0
33
+ paschal_full_moon -= 1 if paschal_full_moon == 29 or (paschal_full_moon == 28 and golden_number > 11)
34
+
35
+ difference = (4 - paschal_full_moon - dominical_number) % 7
36
+ difference += 7 if difference < 0
37
+
38
+ day_easter = paschal_full_moon + difference + 1
39
+
40
+ day_easter < 11 ? new(year, 3, day_easter + 21) : new(year, 4, day_easter - 10)
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,14 @@
1
+ require 'date'
2
+ require 'activesupport'
3
+ require File.dirname(__FILE__) + '/uk_working_days/easter'
4
+ require File.dirname(__FILE__) + '/uk_working_days/date_extensions'
5
+
6
+ class Date #:nodoc:
7
+ extend UkWorkingDays::Easter
8
+ extend UkWorkingDays::DateExtensions::ClassMethods
9
+ include UkWorkingDays::DateExtensions
10
+ end
11
+
12
+ class Time #:nodoc:
13
+ include UkWorkingDays::DateExtensions
14
+ end
data/test/helper.rb ADDED
@@ -0,0 +1,10 @@
1
+ require 'rubygems'
2
+ require 'test/unit'
3
+ require 'shoulda'
4
+
5
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
6
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
7
+ require 'uk_working_days'
8
+
9
+ class Test::Unit::TestCase
10
+ end
@@ -0,0 +1,7 @@
1
+ require 'helper'
2
+
3
+ class TestEaster < Test::Unit::TestCase
4
+ should "be on the 4th of April in 2010" do
5
+ assert_equal Date.new(2010,4,4), Date.easter(2010)
6
+ end
7
+ end
@@ -0,0 +1,158 @@
1
+ require 'helper'
2
+
3
+ class TestUkWorkingDays < Test::Unit::TestCase
4
+ should "return true for #weekend? if day is a Saturday or Sunday" do
5
+ assert Date.new(2010, 2, 27).weekend?
6
+ assert Date.new(2010, 2, 28).weekend?
7
+ assert Time.local(2010, 2, 28).weekend?
8
+ end
9
+
10
+ should "return true for #weekday? if day is not on the Weekend" do
11
+ assert Date.new(2010, 2, 26).weekday?
12
+ assert Time.local(2010, 2, 26).weekday?
13
+ end
14
+
15
+ should "return true for #public_holiday? if day is a public holiday" do
16
+ assert Date.new(2010, 4, 5).public_holiday?
17
+ assert DateTime.new(2010, 4, 5).public_holiday?
18
+ assert Time.local(2010, 4, 5).public_holiday?
19
+ end
20
+
21
+ should "return false for #public_holiday? if day not a public holiday" do
22
+ assert ! Date.new(2010, 4, 6).public_holiday?
23
+ assert ! DateTime.new(2010, 4, 6).public_holiday?
24
+ end
25
+
26
+ should "return true for #working_day? on a normal weekday" do
27
+ assert Date.new(2010, 4, 1).working_day?
28
+ assert Time.local(2010, 4, 1).working_day?
29
+ end
30
+
31
+ should "return false for #working_day? if day is a public holiday or on the weekend" do
32
+ assert ! Date.new(2010, 4, 5).working_day?
33
+ assert ! Date.new(2010, 4, 4).working_day?
34
+ assert ! Time.local(2010, 4, 4).working_day?
35
+ end
36
+
37
+ should "return the next working day for #next_working_day" do
38
+ assert_equal Date.new(2010, 4, 6), Date.new(2010, 4, 2).next_working_day
39
+ assert_equal Date.new(2010, 4, 7), Date.new(2010, 4, 6).next_working_day
40
+ assert_equal Time.local(2010, 4, 7), Time.local(2010, 4, 6).next_working_day
41
+ end
42
+
43
+ should "return the previous working day for #previous_working_day" do
44
+ assert_equal Date.new(2010, 4, 1), Date.new(2010, 4, 6).previous_working_day
45
+ assert_equal Date.new(2010, 3, 31), Date.new(2010, 4, 1).previous_working_day
46
+ assert_equal Time.local(2010, 3, 31), Time.local(2010, 4, 1).previous_working_day
47
+ end
48
+
49
+ context "Date#new_years_day_holiday" do
50
+ should "return new year's day if it is a weekday" do
51
+ assert_equal Date.new(2010, 1, 1), Date.new_years_day_holiday(2010)
52
+ end
53
+
54
+ should "return the next monday after new year's day if on a weekend" do
55
+ assert_equal Date.new(2011, 1, 3), Date.new_years_day_holiday(2011)
56
+ end
57
+
58
+ should "return a DateTime if called from DateTime" do
59
+ assert_kind_of DateTime, DateTime.new_years_day_holiday(2011)
60
+ end
61
+ end
62
+
63
+ context "Date#christmas_day_holiday" do
64
+ should "return christmas day if it is a weekday" do
65
+ assert_equal Date.new(2009, 12, 25), Date.christmas_day_holiday(2009)
66
+ end
67
+
68
+ should "return the next monday after new year's day if on a weekend" do
69
+ assert_equal Date.new(2010, 12, 27), Date.christmas_day_holiday(2010)
70
+ end
71
+
72
+ should "return a DateTime if called from DateTime" do
73
+ assert_kind_of DateTime, DateTime.christmas_day_holiday(2010)
74
+ end
75
+ end
76
+
77
+ context "Date#boxing_day_holiday" do
78
+ should "return following monday if boxing day is on saturday" do
79
+ assert_equal Date.new(2009, 12, 28), Date.boxing_day_holiday(2009)
80
+ end
81
+
82
+ should "return the following tuesday if boxing day is on sunday" do
83
+ assert_equal Date.new(2010, 12, 28), Date.boxing_day_holiday(2010)
84
+ end
85
+
86
+ should "return the following tuesday if boxing day is on monday" do
87
+ assert_equal Date.new(2011, 12, 27), Date.boxing_day_holiday(2011)
88
+ end
89
+
90
+ should "return boxing day itself in other cases" do
91
+ assert_equal Date.new(2008, 12, 26), Date.boxing_day_holiday(2008)
92
+ end
93
+
94
+ should "return a DateTime if called from DateTime" do
95
+ assert_kind_of DateTime, DateTime.boxing_day_holiday(2010)
96
+ end
97
+ end
98
+
99
+ context "Date#easter_monday" do
100
+ should "return the monday after easter" do
101
+ assert_equal Date.new(2010, 4, 5), Date.easter_monday(2010)
102
+ end
103
+
104
+ should "return a DateTime if called from DateTime" do
105
+ assert_kind_of DateTime, DateTime.easter_monday(2010)
106
+ end
107
+ end
108
+
109
+ context "Date#good_friday" do
110
+ should "return the friday before easter sunday" do
111
+ assert_equal Date.new(2010, 4, 2), Date.good_friday(2010)
112
+ end
113
+
114
+ should "return a DateTime if called from DateTime" do
115
+ assert_kind_of DateTime, DateTime.good_friday(2010)
116
+ end
117
+ end
118
+
119
+ context "Date#may_bank_holiday" do
120
+ should "return the first monday in may" do
121
+ assert_equal Date.new(2010, 5, 3), Date.may_bank_holiday(2010)
122
+ end
123
+
124
+ should "return a DateTime if called from DateTime" do
125
+ assert_kind_of DateTime, DateTime.may_bank_holiday(2010)
126
+ end
127
+ end
128
+
129
+ context "Date#spring_bank_holiday" do
130
+ should "return the last monday in may" do
131
+ assert_equal Date.new(2010, 5, 31), Date.spring_bank_holiday(2010)
132
+ end
133
+
134
+ should "return a DateTime if called from DateTime" do
135
+ assert_kind_of DateTime, DateTime.spring_bank_holiday(2010)
136
+ end
137
+ end
138
+
139
+ context "Date#summer_bank_holiday" do
140
+ should "return the last monday in august" do
141
+ assert_equal Date.new(2010, 8, 30), Date.summer_bank_holiday(2010)
142
+ end
143
+
144
+ should "return a DateTime if called from DateTime" do
145
+ assert_kind_of DateTime, DateTime.summer_bank_holiday(2010)
146
+ end
147
+ end
148
+
149
+ context "Date#public_holidays" do
150
+ should "return the eight public holidays in the year" do
151
+ assert_equal 8, Date.public_holidays(2010).uniq.size
152
+ end
153
+
154
+ should "return datetimes if called from DateTime" do
155
+ assert DateTime.public_holidays(2010).all? {|d| d.kind_of? DateTime }
156
+ end
157
+ end
158
+ end
metadata ADDED
@@ -0,0 +1,99 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: uk_working_days
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 1
8
+ - 0
9
+ version: 0.1.0
10
+ platform: ruby
11
+ authors:
12
+ - Roland Swingler
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2010-03-05 00:00:00 +00:00
18
+ default_executable:
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: activesupport
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - ">="
26
+ - !ruby/object:Gem::Version
27
+ segments:
28
+ - 2
29
+ version: "2"
30
+ type: :runtime
31
+ version_requirements: *id001
32
+ - !ruby/object:Gem::Dependency
33
+ name: shoulda
34
+ prerelease: false
35
+ requirement: &id002 !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ">="
38
+ - !ruby/object:Gem::Version
39
+ segments:
40
+ - 0
41
+ version: "0"
42
+ type: :development
43
+ version_requirements: *id002
44
+ description: Provide date helpers for UK working days
45
+ email: roland.swingler@gmail.com
46
+ executables: []
47
+
48
+ extensions: []
49
+
50
+ extra_rdoc_files:
51
+ - LICENSE
52
+ - README.rdoc
53
+ files:
54
+ - .document
55
+ - .gitignore
56
+ - LICENSE
57
+ - README.rdoc
58
+ - Rakefile
59
+ - VERSION
60
+ - lib/uk_working_days.rb
61
+ - lib/uk_working_days/date_extensions.rb
62
+ - lib/uk_working_days/easter.rb
63
+ - test/helper.rb
64
+ - test/test_easter.rb
65
+ - test/test_uk_working_days.rb
66
+ has_rdoc: true
67
+ homepage: http://github.com/knaveofdiamonds/uk_working_days
68
+ licenses: []
69
+
70
+ post_install_message:
71
+ rdoc_options:
72
+ - --charset=UTF-8
73
+ require_paths:
74
+ - lib
75
+ required_ruby_version: !ruby/object:Gem::Requirement
76
+ requirements:
77
+ - - ">="
78
+ - !ruby/object:Gem::Version
79
+ segments:
80
+ - 0
81
+ version: "0"
82
+ required_rubygems_version: !ruby/object:Gem::Requirement
83
+ requirements:
84
+ - - ">="
85
+ - !ruby/object:Gem::Version
86
+ segments:
87
+ - 0
88
+ version: "0"
89
+ requirements: []
90
+
91
+ rubyforge_project:
92
+ rubygems_version: 1.3.6
93
+ signing_key:
94
+ specification_version: 3
95
+ summary: Provide date helpers for UK working days
96
+ test_files:
97
+ - test/helper.rb
98
+ - test/test_easter.rb
99
+ - test/test_uk_working_days.rb