Module: Bizhours

Defined in:
lib/bizhours.rb

Constant Summary collapse

VERSION =
'1.1.0'
BIZHOURS =
{  
  :mon => [8,20],
  :tue => [8,20],
  :wed => [8,20],
  :thu => [8,20],
  :fri => [8,20],
  :sat => [9,18],
  :sun => [8,8]
}

Instance Method Summary collapse

Instance Method Details

#get_duration(startt, endt) ⇒ Object

Takes 2 timestamps and returns a duration object of hours that match BIZHOURS for day



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/bizhours.rb', line 21

def get_duration(startt, endt)
  #if start and end time is on same day do the following
  if (startt.yday == endt.yday && startt.year == endt.year)
    openhr, closehr = business_hours_for_day(startt)
    openhr = Time.local(startt.year, startt.month, startt.day, openhr)
    closehr = Time.local(startt.year, startt.month, startt.day, closehr)
    
    #to solve start hour before open
    if startt < openhr
      startt = Time.local(startt.year, startt.month, startt.day, openhr.hour)
    end
    
    #solves close hour after closing
    if endt > closehr
      endt = Time.local(endt.year, endt.month, endt.day, closehr.hour)
    end
    dur = Duration.new(endt - startt)
    
    #to solve same time in and out past business hours
    if dur < 0
      return Duration.new
    else
      return dur 
    end
  end
 
  duration = first_day_time(startt) + last_day_time(endt) + inbetween_day_time(startt, endt)

end