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.
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)
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 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
|
#inspect ⇒ Object
115
116
117
|
# File 'lib/3scale_time_range.rb', line 115
def inspect
"#{self.class.name}(#{super})"
end
|
#length ⇒ Object
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?
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
|
#previous ⇒ Object
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_hours ⇒ Object
71
72
73
|
# File 'lib/3scale_time_range.rb', line 71
def shift_in_hours
self.utc_offset / 3600
end
|
#to_s ⇒ Object
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_range ⇒ Object
99
100
101
|
# File 'lib/3scale_time_range.rb', line 99
def to_time_range
self
end
|
#utc ⇒ Object
55
56
57
|
# File 'lib/3scale_time_range.rb', line 55
def utc
self.in_time_zone('UTC')
end
|
#utc_offset ⇒ Object
66
67
68
69
|
# File 'lib/3scale_time_range.rb', line 66
def utc_offset
assert_time_with_zone
self.begin.utc_offset
end
|
#zone ⇒ Object
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
|