2
# Copyright (c) 2006 Philip Ross
4
# Permission is hereby granted, free of charge, to any person obtaining a copy
5
# of this software and associated documentation files (the "Software"), to deal
6
# in the Software without restriction, including without limitation the rights
7
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8
# copies of the Software, and to permit persons to whom the Software is
9
# furnished to do so, subject to the following conditions:
11
# The above copyright notice and this permission notice shall be included in all
12
# copies or substantial portions of the Software.
14
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24
require 'tzinfo/time_or_datetime'
27
# Represents an offset defined in a Timezone data file.
28
class TimezoneTransitionInfo #:nodoc:
29
# The offset this transition changes to (a TimezoneOffsetInfo instance).
32
# The offset this transition changes from (a TimezoneOffsetInfo instance).
33
attr_reader :previous_offset
35
# The numerator of the DateTime if the transition time is defined as a
36
# DateTime, otherwise the transition time as a timestamp.
37
attr_reader :numerator_or_time
38
protected :numerator_or_time
40
# Either the denominotor of the DateTime if the transition time is defined
41
# as a DateTime, otherwise nil.
42
attr_reader :denominator
43
protected :denominator
45
# Creates a new TimezoneTransitionInfo with the given offset,
46
# previous_offset (both TimezoneOffsetInfo instances) and UTC time.
47
# if denominator is nil, numerator_or_time is treated as a number of
48
# seconds since the epoch. If denominator is specified numerator_or_time
49
# and denominator are used to create a DateTime as follows:
51
# DateTime.new!(Rational.send(:new!, numerator_or_time, denominator), 0, Date::ITALY)
53
# For performance reasons, the numerator and denominator must be specified
54
# in their lowest form.
55
def initialize(offset, previous_offset, numerator_or_time, denominator = nil)
57
@previous_offset = previous_offset
58
@numerator_or_time = numerator_or_time
59
@denominator = denominator
66
# A TimeOrDateTime instance representing the UTC time when this transition
71
@at = TimeOrDateTime.new(@numerator_or_time)
73
r = RubyCoreSupport.rational_new!(@numerator_or_time, @denominator)
74
dt = RubyCoreSupport.datetime_new!(r, 0, Date::ITALY)
75
@at = TimeOrDateTime.new(dt)
82
# A TimeOrDateTime instance representing the local time when this transition
83
# causes the previous observance to end (calculated from at using
86
@local_end = at.add_with_convert(@previous_offset.utc_total_offset) unless @local_end
90
# A TimeOrDateTime instance representing the local time when this transition
91
# causes the next observance to start (calculated from at using offset).
93
@local_start = at.add_with_convert(@offset.utc_total_offset) unless @local_start
97
# Returns true if this TimezoneTransitionInfo is equal to the given
98
# TimezoneTransitionInfo. Two TimezoneTransitionInfo instances are
99
# considered to be equal by == if offset, previous_offset and at are all
102
tti.respond_to?(:offset) && tti.respond_to?(:previous_offset) && tti.respond_to?(:at) &&
103
offset == tti.offset && previous_offset == tti.previous_offset && at == tti.at
106
# Returns true if this TimezoneTransitionInfo is equal to the given
107
# TimezoneTransitionInfo. Two TimezoneTransitionInfo instances are
108
# considered to be equal by eql? if offset, previous_offset,
109
# numerator_or_time and denominator are all equal. This is stronger than ==,
110
# which just requires the at times to be equal regardless of how they were
111
# originally specified.
113
tti.respond_to?(:offset) && tti.respond_to?(:previous_offset) &&
114
tti.respond_to?(:numerator_or_time) && tti.respond_to?(:denominator) &&
115
offset == tti.offset && previous_offset == tti.previous_offset &&
116
numerator_or_time == tti.numerator_or_time && denominator == tti.denominator
119
# Returns a hash of this TimezoneTransitionInfo instance.
121
@offset.hash ^ @previous_offset.hash ^ @numerator_or_time.hash ^ @denominator.hash
124
# Returns internal object state as a programmer-readable string.
126
"#<#{self.class}: #{at.inspect},#{@offset.inspect}>"