diff options
author | francis <francis> | 2009-03-16 21:06:07 +0000 |
---|---|---|
committer | francis <francis> | 2009-03-16 21:06:07 +0000 |
commit | 160f6768dcc20f00bec7fdf8d81382dc2cf2ca25 (patch) | |
tree | e576d2c98d786ec31830ea547df03203288b873e | |
parent | 3a3924462ae25759608b12d1066a424e1efe4480 (diff) |
Convert date/times into dates.
-rw-r--r-- | app/models/holiday.rb | 60 |
1 files changed, 31 insertions, 29 deletions
diff --git a/app/models/holiday.rb b/app/models/holiday.rb index 5837753f1..025d6013b 100644 --- a/app/models/holiday.rb +++ b/app/models/holiday.rb @@ -21,37 +21,39 @@ # Copyright (c) 2009 UK Citizens Online Democracy. All rights reserved. # Email: francis@mysociety.org; WWW: http://www.mysociety.org/ # -# $Id: holiday.rb,v 1.3 2009-03-10 08:16:43 tony Exp $ +# $Id: holiday.rb,v 1.4 2009-03-16 21:06:07 francis Exp $ class Holiday < ActiveRecord::Base - # Calculate the date on which a request made on a given date falls due. - def Holiday.due_date_from(start_date) - - # TODO only fetch holidays after the start_date - holidays = self.all.collect { |h| h.day }.to_set - - # Count forward 20 working days. We start with today (or if not a working day, - # the next working day*) as "day zero". The first of the twenty full - # working days is the next day. We return the date of the last of the twenty. - # - # * See this response for example of a public authority complaining when we got - # that detail wrong: http://www.whatdotheyknow.com/request/policy_regarding_body_scans#incoming-1100 - - # We have to skip non-working days at start to find day zero, so start at - # day -1 and at yesterday, so we can do that. - days_passed = -1 - response_required_by = start_date - 1.day - - # Now step forward into day zero, and then each of the 20 days. - while days_passed < 20 - response_required_by += 1.day - next if response_required_by.wday == 0 || response_required_by.wday == 6 # weekend - next if holidays.include?(response_required_by) - days_passed += 1 - end - - return response_required_by - end + # Calculate the date on which a request made on a given date falls due. + def Holiday.due_date_from(start_date) + # convert date/times into dates + start_date = start_date.to_date + + # TODO only fetch holidays after the start_date + holidays = self.all.collect { |h| h.day }.to_set + + # Count forward 20 working days. We start with today (or if not a working day, + # the next working day*) as "day zero". The first of the twenty full + # working days is the next day. We return the date of the last of the twenty. + # + # * See this response for example of a public authority complaining when we got + # that detail wrong: http://www.whatdotheyknow.com/request/policy_regarding_body_scans#incoming-1100 + + # We have to skip non-working days at start to find day zero, so start at + # day -1 and at yesterday, so we can do that. + days_passed = -1 + response_required_by = start_date - 1.day + + # Now step forward into day zero, and then each of the 20 days. + while days_passed < 20 + response_required_by += 1.day + next if response_required_by.wday == 0 || response_required_by.wday == 6 # weekend + next if holidays.include?(response_required_by) + days_passed += 1 + end + + return response_required_by + end end |