From: Phrogz Date: 2007-04-06T23:25:09+09:00 Subject: Re: Calculating Business Hours Left On Apr 6, 8:13 am, "Keith Fahlgren" wrote: > Hmm... no idea if this works, but it might be a start: That feels like it might be good, thanks! I'll have to investigate more. It certainly looks cleaner than what I came up with: class Time def at( hrs, mins=0 ) self.class.parse strftime("%Y-%b-%d #{hrs}:#{mins}" ) end end # Fails for tasks one year or farther in the future def work_hours_until( due_time, work_starts_at=9, work_ends_at=17 ) start_time = [ Time.now, Time.parse( "#{work_starts_at}:00" ) ].max end_time = [ due_time, due_time.at( work_ends_at ) ].min weekdays = 1..5 one_hour = 3600.0 one_day = one_hour * 24 if start_time.yday == end_time.yday if weekdays.include?( start_time.wday ) ( end_time - start_time ) / one_hour else 0 end else business_hours = work_ends_at - work_starts_at # Handle partial hours on the first day hours = ( start_time.at( work_ends_at ) - start_time ) / one_hour t = start_time + one_day until t.yday == due_time.yday hours += business_hours if weekdays.include?( t.wday ) t += one_day end # Handle partial hours on the last day if weekdays.include?( t.wday ) hours += ( end_time - end_time.at( work_starts_at ) ) / one_hour end hours end end