Permalink
Browse files

Added next working day calculation

  • Loading branch information...
1 parent d25d53a commit 63bc6fc2c38d23d84d33e41af593bc1359c8bf4b @lhoeg lhoeg committed Mar 14, 2012
Showing with 63 additions and 0 deletions.
  1. +29 −0 lib/danske_helligdage/arbejdsdag.rb
  2. +34 −0 test/test_arbejdsdage.rb
@@ -0,0 +1,29 @@
+# -*- coding: utf-8 -*-
+
+require 'danske_helligdage'
+
+module DanskeHelligdage
+ module Arbejdsdag
+
+ # Calculate the following working day.
+ #
+ # ==== Parameters
+ # [<tt>num</tt>] Integer. Number of working days forward (>0).
+ #
+ # Returns Date of the next working day.
+ #
+ # Note: returns a new Date object and does not change current object.
+ def arbejdsdag(num=1)
+ d = self+1
+ until(d.arbejdsdag?) do
+ d = d+1
+ end
+
+ # recursively find next working day(s)
+ num > 1 ? d.arbejdsdag(num-1) : d
+ end
+
+ end
+end
+
+Date.send(:include, DanskeHelligdage::Arbejdsdag)
View
@@ -0,0 +1,34 @@
+# -*- coding: UTF-8 -*-
+require 'danske_helligdage'
+require 'danske_helligdage/arbejdsdag'
+
+class ArbejdsdageTests < Test::Unit::TestCase
+
+ def test_should_return_next_working_day
+ assert_equal Date.civil(2008, 5, 6), Date.civil(2008, 5, 5).arbejdsdag
+ end
+
+ def test_should_return_next_working_day_on_a_sunday
+ assert_equal Date.civil(2008, 5, 5), Date.civil(2008, 5, 4).arbejdsdag
+ end
+
+ def test_should_return_next_working_day_on_a_saturday
+ assert_equal Date.civil(2008, 5, 5), Date.civil(2008, 5, 3).arbejdsdag
+ end
+
+ def test_should_return_next_working_day_on_a_friday
+ assert_equal Date.civil(2008, 5, 5), Date.civil(2008, 5, 2).arbejdsdag
+ end
+
+ def test_should_return_next_working_day_on_a_holiday
+ assert_equal Date.civil(2008, 5, 2), Date.civil(2008, 5, 1).arbejdsdag
+ end
+
+ def test_should_return_next_working_day_before_a_holiday
+ assert_equal Date.civil(2008, 5, 2), Date.civil(2008, 4, 30).arbejdsdag
+ end
+
+ def test_should_return_next_3_working_day_before_holiday_and_weekend
+ assert_equal Date.civil(2008, 5, 6), Date.civil(2008, 4, 30).arbejdsdag(3)
+ end
+end

0 comments on commit 63bc6fc

Please sign in to comment.