Method: WorkingTime::Interval#hours_between

Defined in:
lib/working_time/interval.rb

#hours_betweenObject



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/working_time/interval.rb', line 44

def hours_between

  #LOG.puts "Calculating: #{@start} to #{@stop}" if DEBUG

  # Our placeholder in seconds   
  duration = 0.0

  start_date = Date.new(@start.year, @start.mon, @start.day)
  stop_date  = Date.new(@stop.year, @stop.mon, @stop.day)

  if start_date == stop_date

    # If we closed it in the same day, we need to do a simple calculation
    result = seconds_between(@start,@stop)
    duration += result
    #LOG.puts "Same day: #{@start} to #{@stop} (#{result / 3600})" if DEBUG

  else 

    # Loop over each day
    start_date.upto(stop_date) do |date|
      
      #LOG.puts "Looking at: #{date}"
      start_work_time = DateTime.new(date.year, date.mon, date.day, WORKING_HOURS[0])
      stop_work_time  = DateTime.new(date.year, date.mon, date.day, WORKING_HOURS[-1])

      # If we are looking at the first day, we only want to count from the create time, to the end of the working day
      if date == start_date
        result = seconds_between(@start,stop_work_time)
        duration += result
        #LOG.puts "Start date: #{@start} to #{stop_work_time} (#{result / 3600})" if DEBUG
      # If we are looking at the end date we only want to count from the beginning of the day to the close time
      elsif date == stop_date
        result = seconds_between(start_work_time,@stop)
        duration += result
        #LOG.puts "Stop date: #{start_work_time} to #{@stop} (#{result / 3600})" if DEBUG
      # Otherwise we assume this date falls between start and end
      else
        result = seconds_between(start_work_time,stop_work_time)
        duration += result
        #LOG.puts "In-between date: #{start_work_time} to #{stop_work_time} (#{result / 3600 })" if DEBUG 
      end

    end

  end

  #LOG.puts ""
  return duration / 3600
end