class TimeRange

Constants

VERSION
WEEKDAYS

Public Class Methods

new(start_time, end_time, exclusive = false) click to toggle source
Calls superclass method
# File lib/3scale_time_range.rb, line 9
def initialize(start_time, end_time, exclusive = false)
  raise ArgumentError, 'start and end must act like Time' unless start_time.acts_like?(:time) && end_time.acts_like?(:time)

  super
end

Public Instance Methods

+(interval) click to toggle source
# File lib/3scale_time_range.rb, line 45
def +(interval)
  # TODO - check it
  # raise ArgumentError.new('Only Fixnum is allowed') unless Fixnum === interval
  TimeRange.new(self.begin + interval, self.end + interval)
end
-(interval) click to toggle source
# File lib/3scale_time_range.rb, line 51
def -(interval)
  self + (-interval)
end
each(step = nil, &block) click to toggle source
Calls superclass method
# File lib/3scale_time_range.rb, line 23
def each(step = nil, &block)
  if step
    enumerator = if WEEKDAYS[step]
                   WeekdayEnumerator
                 elsif step == :end_of_month # the simple enumerator already works for the beginning, every month has a 1st
                   MonthEnumerator
                 else
                   SimpleEnumerator
                 end.new(self, step)

    enumerator.each(&block) if block_given?
    enumerator
  else
    super(&block)
  end
end
in_time_zone(timezone) click to toggle source

Shifts the time range to given timezone via Time#in_time_zone method.

# File lib/3scale_time_range.rb, line 41
def in_time_zone(timezone)
  TimeRange.new(self.begin.in_time_zone(timezone), self.end.in_time_zone(timezone))
end
inspect() click to toggle source
# File lib/3scale_time_range.rb, line 115
def inspect
  "#{self.class.name}(#{super})"
end
length() click to toggle source
# File lib/3scale_time_range.rb, line 82
def length
  self.end - self.begin
end
month?() click to toggle source

Does this range cover a whole month?

# File lib/3scale_time_range.rb, line 108
def month?
  self.begin.year == self.end.year &&
  self.begin.month == self.end.month &&
  self.begin == self.begin.beginning_of_month &&
  self.end == self.end.end_of_month
end
previous() click to toggle source

Previous time range, that is, a range with the same length as this range, but which ends when this range starts. Excludes the last element.

# File lib/3scale_time_range.rb, line 95
def previous
  self.class.new(self.begin - length, self.begin, true)
end
round(cycle) click to toggle source

“round” range, so it starts at the beginning of the cycle and end at it’s end. TODO: maybe round is not the best name for this method.

# File lib/3scale_time_range.rb, line 88
def round(cycle)
  self.class.new(self.begin.beginning_of_cycle(cycle),
                 self.end.end_of_cycle(cycle))
end
shift_in_hours() click to toggle source
# File lib/3scale_time_range.rb, line 71
def shift_in_hours
  self.utc_offset / 3600
end
to_s() click to toggle source
# File lib/3scale_time_range.rb, line 103
def to_s
  "#{self.begin.strftime("%B %e, %Y (%k:%M)")} - #{self.end.strftime("%B %e, %Y (%k:%M)")}"
end
to_time_range() click to toggle source
# File lib/3scale_time_range.rb, line 99
def to_time_range
  self
end
utc() click to toggle source
# File lib/3scale_time_range.rb, line 55
def utc
  self.in_time_zone('UTC')
end
utc_offset() click to toggle source
# File lib/3scale_time_range.rb, line 66
def utc_offset
  assert_time_with_zone
  self.begin.utc_offset
end
zone() click to toggle source

TODO: TimeRange should check if both ‘begin’ and ‘end’ are in the same timezone?

# File lib/3scale_time_range.rb, line 61
def zone
  assert_time_with_zone
  self.begin.zone
end

Private Instance Methods

assert_time_with_zone() click to toggle source
# File lib/3scale_time_range.rb, line 76
def assert_time_with_zone
  raise TypeError.new('Has to be TimeWithZone') unless ActiveSupport::TimeWithZone === self.begin
end