I’d like to calculate the working hours difference between two points in
time so that if for example is defined as Monday to Friday 0900-1730
then the difference between Monday 1000 and the previous Friday 1600 is
2h30m. If one could also identify public holidays to exclude then that
would be nice. Any suggestions? My first thoughts are I would need an
ugly long list and subtract mulitples of 15.5 hours depending on how far
apart the start adn stop are in terms of days, but I’m hoping there is
an elegant and short way of looking at the problem. Thanks in advance
for any suggestions.
Toby R. wrote in post #1007176:
I’d like to calculate the working hours difference between two points in
time so that if for example is defined as Monday to Friday 0900-1730
then the difference between Monday 1000 and the previous Friday 1600 is
2h30m. If one could also identify public holidays to exclude then that
would be nice. Any suggestions? My first thoughts are I would need an
ugly long list and subtract mulitples of 15.5 hours depending on how far
apart the start adn stop are in terms of days, but I’m hoping there is
an elegant and short way of looking at the problem. Thanks in advance
for any suggestions.
What version of ruby?
Ruby 1.9.1
Toby R. wrote in post #1007176:
If one could also identify public holidays to exclude then that
would be nice.
You seem to be under the impression that the whole world has the same
holidays. Or, is it that you think the only country with internet
access is the one you live in?
Toby R. wrote in post #1007389:
Ruby 1.9.1
require ‘date’
ONE_DAY = 246060 #in seconds
start_of_day = DateTime.new(2011,6,25,9,0,0,0)
end_of_day = DateTime.new(2011,6,24,17,30,0)
overnight = start_of_day.to_time - end_of_day.to_time
day1 = DateTime.new(2011, 6, 24, 16, 30, 0) #4:30 pm Friday
day2 = DateTime.new(2011, 6, 27, 10, 0, 0) #10:00 am Monday
day = day1.to_date #=> hour = 00:00
normalized_day2 = day2.to_time #datetime in seconds
while (day = day.next_day) < day2
case day.cwday
when 6,7
normalized_day2 -= ONE_DAY
else
normalized_day2 -= overnight
end
end
diff = (normalized_day2 - day1.to_time)
puts “#{diff/(60*60)} hours worked”
–output:–
2.0 hours worked
I’ll leave it to you to check that the initial datetimes are legal, i.e.
they fall within the specified weekday work hours, and that day1 is
earlier than day2.
7stud – wrote in post #1007782:
Toby R. wrote in post #1007176:
If one could also identify public holidays to exclude then that
would be nice.You seem to be under the impression that the whole world has the same
holidays. Or, is it that you think the only country with internet
access is the one you live in?
No, I’m pretty familiar with the extent of the Internet, and very aware
of the variety of public holidays (and indeed weekends). The public
holidays I would like to take account of are those not worked by my NOC
engineers, which is generally a subset of the public holidays of our
client networks. But now that you’ve shown me the way to approach the
problem I’m hopeful I’ll be able to expand your design to account for
those days (or indeed as there are so few of them manually exclude them
as and when they arise). Thanks again for that.
Ah, very nice, many thanks for your kind help.