class GBWorkDay::Duration
Constants
- SEC_IN_DAY
Attributes
Public Class Methods
# File lib/gb_work_day/duration.rb, line 8 def initialize(days, week=nil) @work_days = days @week = week || WorkWeek.current end
Public Instance Methods
Calculates a new Time
or Date
that is as far in the past as this Duration
represents. Time
has to be a work day, it is calculated starting from the next work day
# File lib/gb_work_day/duration.rb, line 85 def ago(time = ::Time.current) self.work_days > 0 ? subtract(time) : sum(time) end
Returns true
if other
is also a Duration
instance, which has the same parts as this one.
# File lib/gb_work_day/duration.rb, line 66 def eql?(other) Duration === other && other.work_days.eql?(work_days) && other.week.eql?(week) end
# File lib/gb_work_day/duration.rb, line 90 def in_time_zone(zone) self end
Calculates a new Time
or Date
that is as far in the future as this Duration
represents. If time is a free day, it is calculated starting from the next work day
# File lib/gb_work_day/duration.rb, line 77 def since(time = ::Time.current) self.work_days > 0 ? sum(time) : subtract(time) end
Returns the number of seconds that this Duration
represents.
# File lib/gb_work_day/duration.rb, line 55 def to_i(format = :seconds) if format == :days work_days else work_days * SEC_IN_DAY end end
Returns the amount of seconds a duration covers as a string. For more information check to_i
method.
1.work_day.to_s # => "86400"
# File lib/gb_work_day/duration.rb, line 50 def to_s to_i.to_s end
Private Instance Methods
@param time [Date, Time] @return distance_to_eof [Integer]
# File lib/gb_work_day/duration.rb, line 116 def distance_to_end_of_week(time) (@week.work_days_per_week - time.wday) % (@week.work_days_per_week) end
Return next working day, or today if today is a working day @param day [Date, Time] @return [Date, Time]
# File lib/gb_work_day/duration.rb, line 123 def next_work_day(day) if @week.free_day? day day.next_work_day(@week) else day end end
# File lib/gb_work_day/duration.rb, line 105 def subtract(time) time = next_work_day time distance_to_eof = distance_to_end_of_week(time) weekends_count = (distance_to_eof + self.work_days.abs) / @week.work_days_per_week weekends_length = weekends_count * @week.free_days_per_week sum_normal_days(sum_normal_days(time, -weekends_length), -self.work_days.abs) end
# File lib/gb_work_day/duration.rb, line 96 def sum(time) time = next_work_day time distance_to_last_monday = (time.wday - 1) % 7 weekends_count = (distance_to_last_monday + self.work_days.abs) / @week.work_days_per_week weekends_length = weekends_count * @week.free_days_per_week sum_normal_days(sum_normal_days(time, weekends_length), self.work_days.abs) end
@param time [Date, Time] @param days [Integer]
# File lib/gb_work_day/duration.rb, line 133 def sum_normal_days(time, days) time + if time.is_a? ::Date days * 1 else (days * SEC_IN_DAY) end end