Class: TimeRange

Inherits:
Range show all
Includes:
Granulate
Defined in:
lib/3scale_time_range.rb,
lib/3scale_time_range/version.rb

Defined Under Namespace

Classes: MonthEnumerator, SimpleEnumerator, WeekdayEnumerator

Constant Summary collapse

WEEKDAYS =
{:monday    => 0,
:tuesday   => 1,
:wednesday => 2,
:thursday  => 3,
:friday    => 4,
:saturday  => 5,
:sunday    => 6}
VERSION =
'0.3.0'

Instance Method Summary collapse

Methods included from Granulate

included

Constructor Details

#initialize(start_time, end_time, exclusive = false) ⇒ TimeRange

Returns a new instance of TimeRange.

Raises:

  • (ArgumentError)


9
10
11
12
13
# 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

Instance Method Details

#+(interval) ⇒ Object



45
46
47
48
49
# 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) ⇒ Object



51
52
53
# File 'lib/3scale_time_range.rb', line 51

def -(interval)
  self + (-interval)
end

#each(step = nil, &block) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# 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) ⇒ Object

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



41
42
43
# 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

#inspectObject



115
116
117
# File 'lib/3scale_time_range.rb', line 115

def inspect
  "#{self.class.name}(#{super})"
end

#lengthObject



82
83
84
# File 'lib/3scale_time_range.rb', line 82

def length
  self.end - self.begin
end

#month?Boolean

Does this range cover a whole month?

Returns:

  • (Boolean)


108
109
110
111
112
113
# 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

#previousObject

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.



95
96
97
# File 'lib/3scale_time_range.rb', line 95

def previous
  self.class.new(self.begin - length, self.begin, true)
end

#round(cycle) ⇒ Object

“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.



88
89
90
91
# 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_hoursObject



71
72
73
# File 'lib/3scale_time_range.rb', line 71

def shift_in_hours
  self.utc_offset / 3600
end

#to_sObject



103
104
105
# 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_rangeObject



99
100
101
# File 'lib/3scale_time_range.rb', line 99

def to_time_range
  self
end

#utcObject



55
56
57
# File 'lib/3scale_time_range.rb', line 55

def utc
  self.in_time_zone('UTC')
end

#utc_offsetObject



66
67
68
69
# File 'lib/3scale_time_range.rb', line 66

def utc_offset
  assert_time_with_zone
  self.begin.utc_offset
end

#zoneObject

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



61
62
63
64
# File 'lib/3scale_time_range.rb', line 61

def zone
  assert_time_with_zone
  self.begin.zone
end