|
require 'rubygems' |
|
require 'active_support/core_ext/date/calculations.rb' |
|
|
|
WORKDAY_STOP = 16 |
|
|
|
module DateCalculation |
|
def weekend? |
|
self.wday.to_s =~ /[6,0]/ |
|
end |
|
|
|
# Counts workdays between two Dates |
|
def count_weekdays(startdate,stopdate) |
|
raise "Expected both to be Date: (#{startdate.class},#{stopdate.class})" unless (startdate.class == Date && stopdate.class == Date) |
|
@workdays = 0 |
|
startdate.to_date.upto(stopdate.to_date) do |date| |
|
@workdays +=1 if date.wday.to_s =~ /[1,2,3,5]/ |
|
end |
|
@workdays |
|
end |
|
end |
|
|
|
|
|
|
|
module DateNavigation |
|
|
|
def one_week_before(start) |
|
current_date = start |
|
7.times do |d| |
|
current_date = current_date.yesterday |
|
end |
|
current_date |
|
end |
|
|
|
# Returns the previous monday, calculated from current date. |
|
def previous_monday(calculation_day = DateTime.now) |
|
until calculation_day.wday == 1 |
|
calculation_day = calculation_day.yesterday |
|
end |
|
calculation_day |
|
end |
|
|
|
# The monday we´re counting hours from till friday. This monday is not the monday the calculation is executed on. |
|
def start_of_calculation_week |
|
monday = one_week_before(previous_monday) |
|
end |
|
|
|
def end_of_calculation_week |
|
friday = find_end_of_week(start_of_calculation_week) |
|
end |
|
|
|
def find_end_of_week(date) |
|
yr,mon,day,hr,min = ParseDate.parsedate date.to_s |
|
end_of_week = Time.utc(yr,mon,day,WORKDAY_STOP) |
|
until end_of_week.wday == 5 |
|
end_of_week = end_of_week.tomorrow |
|
end |
|
end_of_week |
|
end |
|
|
|
def start_of_previous_week |
|
one_week_before(previous_monday) |
|
end |
|
|
|
def end_of_previous_week |
|
find_end_of_week(start_of_previous_week) |
|
end |
|
end |
|
|
|
|
|
class DateTime |
|
def time_to_date |
|
Date.new(self.year,self.month,self.day) |
|
end |
|
end |
|
|
|
class Time |
|
include DateCalculation |
|
def time_to_date |
|
Date.new(self.year,self.month,self.day) |
|
end |
|
end |
|
|
|
class Date |
|
include DateCalculation |
|
end |
|
|
|
class Fixnum |
|
include DateCalculation |
|
def weekend? |
|
date = Date.new(self) |
|
date.wday.to_s =~ /[6,0]/ |
|
end |
|
end |