Class: TimeCrisis::TZInfo::TimezonePeriod

Inherits:
Object
  • Object
show all
Defined in:
lib/time_crisis/tzinfo/timezone_period.rb

Overview

A period of time in a timezone where the same offset from UTC applies.

All the methods that take times accept instances of Time, DateTime or integer timestamps.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(start_transition, end_transition, offset = nil) ⇒ TimezonePeriod

Initializes a new TimezonePeriod.



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/time_crisis/tzinfo/timezone_period.rb', line 23

def initialize(start_transition, end_transition, offset = nil)
  @start_transition = start_transition
  @end_transition = end_transition

  if offset
    raise ArgumentError, 'Offset specified with transitions' if @start_transition || @end_transition
    @offset = offset
  else
    if @start_transition
      @offset = @start_transition.offset
    elsif @end_transition
      @offset = @end_transition.previous_offset
    else
      raise ArgumentError, 'No offset specified and no transitions to determine it from'
    end
  end

  @utc_total_offset_rational = nil
end

Instance Attribute Details

#end_transitionObject (readonly)

The TimezoneTransitionInfo that defines the end of this TimezonePeriod (may be nil if unbounded).



17
18
19
# File 'lib/time_crisis/tzinfo/timezone_period.rb', line 17

def end_transition
  @end_transition
end

#offsetObject (readonly)

The TimezoneOffsetInfo for this period.



20
21
22
# File 'lib/time_crisis/tzinfo/timezone_period.rb', line 20

def offset
  @offset
end

#start_transitionObject (readonly)

The TimezoneTransitionInfo that defines the start of this TimezonePeriod (may be nil if unbounded).



13
14
15
# File 'lib/time_crisis/tzinfo/timezone_period.rb', line 13

def start_transition
  @start_transition
end

Instance Method Details

#==(p) ⇒ Object

Returns true if this TimezonePeriod is equal to p. This compares the start_transition, end_transition and offset using ==.



150
151
152
153
154
# File 'lib/time_crisis/tzinfo/timezone_period.rb', line 150

def ==(p)
  p.respond_to?(:start_transition) && p.respond_to?(:end_transition) &&
          p.respond_to?(:offset) && start_transition == p.start_transition &&
          end_transition == p.end_transition && offset == p.offset
end

#abbreviationObject Also known as: zone_identifier

The identifier of this period, e.g. “GMT” (Greenwich Mean Time) or “BST” (British Summer Time) for “Europe/London”. The returned identifier is a symbol.



58
59
60
# File 'lib/time_crisis/tzinfo/timezone_period.rb', line 58

def abbreviation
  @offset.abbreviation
end

#dst?Boolean

true if daylight savings is in effect for this period; otherwise false.

Returns:

  • (Boolean)


100
101
102
# File 'lib/time_crisis/tzinfo/timezone_period.rb', line 100

def dst?
  @offset.dst?
end

#eql?(p) ⇒ Boolean

Returns true if this TimezonePeriods is equal to p. This compares the start_transition, end_transition and offset using eql?

Returns:

  • (Boolean)


158
159
160
161
162
# File 'lib/time_crisis/tzinfo/timezone_period.rb', line 158

def eql?(p)
  p.respond_to?(:start_transition) && p.respond_to?(:end_transition) &&
          p.respond_to?(:offset) && start_transition.eql?(p.start_transition) &&
          end_transition.eql?(p.end_transition) && offset.eql?(p.offset)
end

#hashObject

Returns a hash of this TimezonePeriod.



165
166
167
168
169
# File 'lib/time_crisis/tzinfo/timezone_period.rb', line 165

def hash
  result = @start_transition.hash ^ @end_transition.hash
  result ^= @offset.hash unless @start_transition || @end_transition
  result
end

#inspectObject

Returns internal object state as a programmer-readable string.



172
173
174
175
176
# File 'lib/time_crisis/tzinfo/timezone_period.rb', line 172

def inspect
  result = "#<#{self.class}: #{@start_transition.inspect},#{@end_transition.inspect}"
  result << ",#{@offset.inspect}>" unless @start_transition || @end_transition
  result + '>'
end

#local_after_start?(local) ⇒ Boolean

true if the given local DateTime is after the start of the period (inclusive); otherwise false.

Returns:

  • (Boolean)


128
129
130
# File 'lib/time_crisis/tzinfo/timezone_period.rb', line 128

def local_after_start?(local)
  !@start_transition || @start_transition.local_start <= local
end

#local_before_end?(local) ⇒ Boolean

true if the given local DateTime is before the end of the period (exclusive); otherwise false.

Returns:

  • (Boolean)


134
135
136
# File 'lib/time_crisis/tzinfo/timezone_period.rb', line 134

def local_before_end?(local)
  !@end_transition || @end_transition.local_end > local
end

#local_endObject

The end time of the period in local time as a DateTime. May be nil if unbounded.



95
96
97
# File 'lib/time_crisis/tzinfo/timezone_period.rb', line 95

def local_end
  @end_transition ? @end_transition.local_end.to_tc_datetime : nil
end

#local_startObject

The start time of the period in local time as a DateTime. May be nil if unbounded.



89
90
91
# File 'lib/time_crisis/tzinfo/timezone_period.rb', line 89

def local_start
  @start_transition ? @start_transition.local_start.to_tc_datetime : nil
end

#std_offsetObject

Offset from the local time where daylight savings is in effect (seconds). E.g.: utc_offset could be -5 hours. Normally, std_offset would be 0. During daylight savings, std_offset would typically become +1 hours.



51
52
53
# File 'lib/time_crisis/tzinfo/timezone_period.rb', line 51

def std_offset
  @offset.std_offset
end

#to_local(utc) ⇒ Object

Converts a UTC DateTime to local time based on the offset of this period.



139
140
141
# File 'lib/time_crisis/tzinfo/timezone_period.rb', line 139

def to_local(utc)
  @offset.to_local(utc)
end

#to_utc(local) ⇒ Object

Converts a local DateTime to UTC based on the offset of this period.



144
145
146
# File 'lib/time_crisis/tzinfo/timezone_period.rb', line 144

def to_utc(local)
  @offset.to_utc(local)
end

#utc_after_start?(utc) ⇒ Boolean

true if the given UTC DateTime is after the start of the period (inclusive); otherwise false.

Returns:

  • (Boolean)


111
112
113
# File 'lib/time_crisis/tzinfo/timezone_period.rb', line 111

def utc_after_start?(utc)
  !@start_transition || @start_transition.at <= utc
end

#utc_before_end?(utc) ⇒ Boolean

true if the given UTC DateTime is before the end of the period (exclusive); otherwise false.

Returns:

  • (Boolean)


117
118
119
# File 'lib/time_crisis/tzinfo/timezone_period.rb', line 117

def utc_before_end?(utc)
  !@end_transition || @end_transition.at > utc
end

#utc_endObject

The end time of the period in UTC as a DateTime. May be nil if unbounded.



83
84
85
# File 'lib/time_crisis/tzinfo/timezone_period.rb', line 83

def utc_end
  @end_transition ? @end_transition.at.to_tc_datetime : nil
end

#utc_offsetObject

Base offset of the timezone from UTC (seconds).



44
45
46
# File 'lib/time_crisis/tzinfo/timezone_period.rb', line 44

def utc_offset
  @offset.utc_offset
end

#utc_startObject

The start time of the period in UTC as a DateTime. May be nil if unbounded.



78
79
80
# File 'lib/time_crisis/tzinfo/timezone_period.rb', line 78

def utc_start
  @start_transition ? @start_transition.at.to_tc_datetime : nil
end

#utc_total_offsetObject

Total offset from UTC (seconds). Equal to utc_offset + std_offset.



65
66
67
# File 'lib/time_crisis/tzinfo/timezone_period.rb', line 65

def utc_total_offset
  @offset.utc_total_offset
end

#utc_total_offset_rationalObject

Total offset from UTC (days). Result is a Rational.



70
71
72
73
74
75
# File 'lib/time_crisis/tzinfo/timezone_period.rb', line 70

def utc_total_offset_rational
  unless @utc_total_offset_rational
    @utc_total_offset_rational = OffsetRationals.rational_for_offset(utc_total_offset)
  end
  @utc_total_offset_rational
end

#valid_for_local?(local) ⇒ Boolean

true if this period is valid for the given local DateTime; otherwise false.

Returns:

  • (Boolean)


122
123
124
# File 'lib/time_crisis/tzinfo/timezone_period.rb', line 122

def valid_for_local?(local)
  local_after_start?(local) && local_before_end?(local)
end

#valid_for_utc?(utc) ⇒ Boolean

true if this period is valid for the given UTC DateTime; otherwise false.

Returns:

  • (Boolean)


105
106
107
# File 'lib/time_crisis/tzinfo/timezone_period.rb', line 105

def valid_for_utc?(utc)
  utc_after_start?(utc) && utc_before_end?(utc)
end