class GBWorkDay::Duration

Constants

SEC_IN_DAY

Attributes

week[RW]
work_days[RW]

Public Class Methods

new(days, week=nil) click to toggle source
# 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

ago(time = ::Time.current) click to toggle source

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
Also aliased as: until
eql?(other) click to toggle source

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
from_now(time = ::Time.current)
Alias for: since
in_time_zone(zone) click to toggle source
# File lib/gb_work_day/duration.rb, line 90
def in_time_zone(zone)
  self
end
since(time = ::Time.current) click to toggle source

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
Also aliased as: from_now
to_i(format = :seconds) click to toggle source

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
to_s() click to toggle source

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
until(time = ::Time.current)
Alias for: ago

Private Instance Methods

distance_to_end_of_week(time) click to toggle source

@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
next_work_day(day) click to toggle source

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
subtract(time) click to toggle source
# 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
sum(time) click to toggle source
# 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
sum_normal_days(time, days) click to toggle source

@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