From: Keith Fahlgren Date: 2007-04-06T23:13:31+09:00 Subject: Re: Calculating Business Hours Left On 4/5/07, Phrogz wrote: > current_time = Time.new > due_time = # ... some Time instance in the future > work_hours_left_before_I_am_screwed = # ... calculate > > I'm writing a little task-tracking app to help me put out the most > urgent fires. As above, given any two Time instances, I'd like to know > how many hours are left to perform the task. This should skip weekends > and hours outside of specified start- and end-hours for a work day. > > Anyone got such code handy? Or feel like writing it for me? :) Hmm... no idea if this works, but it might be a start: #!/usr/bin/env ruby class WorkHours SECONDS_IN_HOUR = 3600 HOURS_I_WORK = (7..16) DAYS_I_WORK = (1..5) def self.diff_times(a, b) hours = [] (a.to_i..b.to_i).step(SECONDS_IN_HOUR) {|t| time = Time.at(t) hours << t if DAYS_I_WORK.include?(time.wday) && HOURS_I_WORK.include?(time.hour) } return hours.length end end nil now = Time.now => Fri Apr 06 07:12:08 PDT 2007 twenty_days_from_now = now + 1728000 => Thu Apr 26 07:12:08 PDT 2007 one_day_from_now = now + 86400 => Sat Apr 07 07:12:08 PDT 2007 WorkHours.diff_times(now, twenty_days_from_now) => 141 WorkHours.diff_times(now, one_day_from_now) => 10 HTH, Keith