Class: TZInfo::TimezonePeriod
- Inherits:
-
Object
- Object
- TZInfo::TimezonePeriod
- Defined in:
- lib/tzinfo/timezone_period.rb
Overview
A period of time in a timezone where the same offset from UTC applies.
Constant Summary collapse
- @@rational_cache =
A cache of pre-reduced Rationals for each half-hour between -12 and +12. This is used to save time when loading a TimezonePeriod.
{ -43200 => Rational.new!(-1,2), -41400 => Rational.new!(-23,48), -39600 => Rational.new!(-11,24), -37800 => Rational.new!(-7,16), -36000 => Rational.new!(-5,12), -34200 => Rational.new!(-19,48), -32400 => Rational.new!(-3,8), -30600 => Rational.new!(-17,48), -28800 => Rational.new!(-1,3), -27000 => Rational.new!(-5,16), -25200 => Rational.new!(-7,24), -23400 => Rational.new!(-13,48), -21600 => Rational.new!(-1,4), -19800 => Rational.new!(-11,48), -18000 => Rational.new!(-5,24), -16200 => Rational.new!(-3,16), -14400 => Rational.new!(-1,6), -12600 => Rational.new!(-7,48), -10800 => Rational.new!(-1,8), -9000 => Rational.new!(-5,48), -7200 => Rational.new!(-1,12), -5400 => Rational.new!(-1,16), -3600 => Rational.new!(-1,24), -1800 => Rational.new!(-1,48), 0 => Rational.new!(0,1), 1800 => Rational.new!(1,48), 3600 => Rational.new!(1,24), 5400 => Rational.new!(1,16), 7200 => Rational.new!(1,12), 9000 => Rational.new!(5,48), 10800 => Rational.new!(1,8), 12600 => Rational.new!(7,48), 14400 => Rational.new!(1,6), 16200 => Rational.new!(3,16), 18000 => Rational.new!(5,24), 19800 => Rational.new!(11,48), 21600 => Rational.new!(1,4), 23400 => Rational.new!(13,48), 25200 => Rational.new!(7,24), 27000 => Rational.new!(5,16), 28800 => Rational.new!(1,3), 30600 => Rational.new!(17,48), 32400 => Rational.new!(3,8), 34200 => Rational.new!(19,48), 36000 => Rational.new!(5,12), 37800 => Rational.new!(7,16), 39600 => Rational.new!(11,24), 41400 => Rational.new!(23,48), 43200 => Rational.new!(1,2)}
Instance Attribute Summary collapse
-
#local_end ⇒ Object
readonly
End time of the period (local time).
-
#local_start ⇒ Object
readonly
Start time of the period (local time).
-
#std_offset ⇒ Object
readonly
Offset from the local time where daylight savings is in effect (seconds).
-
#utc_end ⇒ Object
readonly
End time of the period (UTC).
-
#utc_offset ⇒ Object
readonly
Base offset of the timezone from UTC (seconds).
-
#utc_start ⇒ Object
readonly
Start time of the period (UTC).
-
#zone_identifier ⇒ Object
readonly
The identifier of this period, e.g.
Instance Method Summary collapse
-
#dst? ⇒ Boolean
true if daylight savings is in effect for this period; otherwise false.
-
#initialize(utc_start, utc_end, utc_offset, std_offset, zone_identifier) ⇒ TimezonePeriod
constructor
Initializes a new TimezonePeriod.
-
#local_after_start?(local) ⇒ Boolean
true if the given local DateTime is after the start of the period (inclusive); otherwise false.
-
#local_before_end?(local) ⇒ Boolean
true if the given local DateTime is before the end of the period (exclusive); otherwise false.
-
#to_local(utc) ⇒ Object
Converts a UTC DateTime to local time based on the offset of this period.
-
#to_utc(local) ⇒ Object
Converts a local DateTime to UTC based on the offset of this period.
-
#utc_after_start?(utc) ⇒ Boolean
true if the given utc DateTime is after the start of the period (inclusive); otherwise false.
-
#utc_before_end?(utc) ⇒ Boolean
true if the given utc DateTime is before the end of the period (exclusive); otherwise false.
-
#utc_total_offset ⇒ Object
Total offset from UTC (seconds).
-
#utc_total_offset_rational ⇒ Object
Total offset from UTC (days).
-
#valid_for_local?(local) ⇒ Boolean
true if this period is valid for the given local DateTime; otherwise false.
-
#valid_for_utc?(utc) ⇒ Boolean
true if this period is valid for the given utc DateTime; otherwise false.
Constructor Details
#initialize(utc_start, utc_end, utc_offset, std_offset, zone_identifier) ⇒ TimezonePeriod
Initializes a new TimezonePeriod.
106 107 108 109 110 111 112 113 114 115 116 117 118 |
# File 'lib/tzinfo/timezone_period.rb', line 106 def initialize(utc_start, utc_end, utc_offset, std_offset, zone_identifier) @utc_start = utc_start @utc_end = utc_end @utc_offset = utc_offset @std_offset = std_offset @zone_identifier = zone_identifier @utc_total_offset = utc_offset + std_offset @utc_total_offset_rational = @@rational_cache[utc_total_offset] @utc_total_offset_rational = Rational(utc_total_offset, 86400) if @utc_total_offset_rational.nil? @local_start = utc_start.nil? ? nil : to_local(utc_start) @local_end = utc_end.nil? ? nil : to_local(utc_end) end |
Instance Attribute Details
#local_end ⇒ Object (readonly)
End time of the period (local time). May be nil if unbounded.
103 104 105 |
# File 'lib/tzinfo/timezone_period.rb', line 103 def local_end @local_end end |
#local_start ⇒ Object (readonly)
Start time of the period (local time). May be nil if unbounded.
100 101 102 |
# File 'lib/tzinfo/timezone_period.rb', line 100 def local_start @local_start end |
#std_offset ⇒ Object (readonly)
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 become +1 hours.
92 93 94 |
# File 'lib/tzinfo/timezone_period.rb', line 92 def std_offset @std_offset end |
#utc_end ⇒ Object (readonly)
End time of the period (UTC). May be nil if unbounded.
84 85 86 |
# File 'lib/tzinfo/timezone_period.rb', line 84 def utc_end @utc_end end |
#utc_offset ⇒ Object (readonly)
Base offset of the timezone from UTC (seconds).
87 88 89 |
# File 'lib/tzinfo/timezone_period.rb', line 87 def utc_offset @utc_offset end |
#utc_start ⇒ Object (readonly)
Start time of the period (UTC). May be nil if unbounded.
81 82 83 |
# File 'lib/tzinfo/timezone_period.rb', line 81 def utc_start @utc_start end |
#zone_identifier ⇒ Object (readonly)
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.
97 98 99 |
# File 'lib/tzinfo/timezone_period.rb', line 97 def zone_identifier @zone_identifier end |
Instance Method Details
#dst? ⇒ Boolean
true if daylight savings is in effect for this period; otherwise false.
131 132 133 |
# File 'lib/tzinfo/timezone_period.rb', line 131 def dst? std_offset != 0 end |
#local_after_start?(local) ⇒ Boolean
true if the given local DateTime is after the start of the period (inclusive); otherwise false.
156 157 158 |
# File 'lib/tzinfo/timezone_period.rb', line 156 def local_after_start?(local) @local_start.nil? || @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.
161 162 163 |
# File 'lib/tzinfo/timezone_period.rb', line 161 def local_before_end?(local) @local_end.nil? || @local_end > local end |
#to_local(utc) ⇒ Object
Converts a UTC DateTime to local time based on the offset of this period.
166 167 168 |
# File 'lib/tzinfo/timezone_period.rb', line 166 def to_local(utc) utc + utc_total_offset_rational end |
#to_utc(local) ⇒ Object
Converts a local DateTime to UTC based on the offset of this period.
171 172 173 |
# File 'lib/tzinfo/timezone_period.rb', line 171 def to_utc(local) local - utc_total_offset_rational end |
#utc_after_start?(utc) ⇒ Boolean
true if the given utc DateTime is after the start of the period (inclusive); otherwise false.
141 142 143 |
# File 'lib/tzinfo/timezone_period.rb', line 141 def utc_after_start?(utc) @utc_start.nil? || @utc_start <= utc end |
#utc_before_end?(utc) ⇒ Boolean
true if the given utc DateTime is before the end of the period (exclusive); otherwise false.
146 147 148 |
# File 'lib/tzinfo/timezone_period.rb', line 146 def utc_before_end?(utc) @utc_end.nil? || @utc_end > utc end |
#utc_total_offset ⇒ Object
Total offset from UTC (seconds). Equal to utc_offset + std_offset.
121 122 123 |
# File 'lib/tzinfo/timezone_period.rb', line 121 def utc_total_offset @utc_total_offset end |
#utc_total_offset_rational ⇒ Object
Total offset from UTC (days). Result is a Rational.
126 127 128 |
# File 'lib/tzinfo/timezone_period.rb', line 126 def utc_total_offset_rational @utc_total_offset_rational end |
#valid_for_local?(local) ⇒ Boolean
true if this period is valid for the given local DateTime; otherwise false.
151 152 153 |
# File 'lib/tzinfo/timezone_period.rb', line 151 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.
136 137 138 |
# File 'lib/tzinfo/timezone_period.rb', line 136 def valid_for_utc?(utc) utc_after_start?(utc) && utc_before_end?(utc) end |