Class: WorkingTime::Interval
- Inherits:
-
Object
- Object
- WorkingTime::Interval
- Defined in:
- lib/working_time/interval.rb
Instance Attribute Summary collapse
-
#duration ⇒ Object
Returns the value of attribute duration.
-
#log ⇒ Object
Returns the value of attribute log.
-
#start ⇒ Object
Returns the value of attribute start.
-
#stop ⇒ Object
Returns the value of attribute stop.
Instance Method Summary collapse
- #hours_between ⇒ Object
-
#initialize(start, stop) ⇒ Interval
constructor
A new instance of Interval.
-
#seconds_between(start, stop) ⇒ Object
Calculates the number of seconds in a work day between two times in the same day.
Constructor Details
#initialize(start, stop) ⇒ Interval
Returns a new instance of Interval.
12 13 14 15 16 |
# File 'lib/working_time/interval.rb', line 12 def initialize(start, stop) @start = start @stop = stop @duration = hours_between end |
Instance Attribute Details
#duration ⇒ Object
Returns the value of attribute duration.
9 10 11 |
# File 'lib/working_time/interval.rb', line 9 def duration @duration end |
#log ⇒ Object
Returns the value of attribute log.
10 11 12 |
# File 'lib/working_time/interval.rb', line 10 def log @log end |
#start ⇒ Object
Returns the value of attribute start.
7 8 9 |
# File 'lib/working_time/interval.rb', line 7 def start @start end |
#stop ⇒ Object
Returns the value of attribute stop.
8 9 10 |
# File 'lib/working_time/interval.rb', line 8 def stop @stop end |
Instance Method Details
#hours_between ⇒ Object
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 |
#seconds_between(start, stop) ⇒ Object
Calculates the number of seconds in a work day between two times in the same day
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/working_time/interval.rb', line 19 def seconds_between(start, stop) # we need this to be a float duration = 0.0 # we just return if the start comes after the stop return duration if start > stop # If it's a weekday, examine it. if WORKING_DAYS.include? start.wday # If it's inside working hours, calculate if WORKING_HOURS.include? start.hour # Figure out how many seconds between now and then hours, mins, secs, ignore_fractions = Date::day_fraction_to_time(stop - start) duration = hours * 60 * 60 + mins * 60 + secs end end return duration end |