Module: Lazier::TimeZone
- Extended by:
- ActiveSupport::Concern
- Defined in:
- lib/lazier/timezone.rb
Overview
Extensions for timezone objects.
Defined Under Namespace
Modules: ClassMethods
Constant Summary collapse
- ALREADY_PARAMETERIZED =
Pattern for a parameterized timezone.
/^[+-]\d{4}@[a-z-]+/
- PARAMETERIZER =
Pattern for a unparameterized timezone.
/^( \( [a-z]+ # UTC Label (?<offset>([+-])(\d{2})(:?)(\d{2})) \) \s(?<label>.+) )$/xi
Instance Method Summary collapse
-
#aliases ⇒ Array
Returns a list of valid aliases (city names) for this timezone (basing on offset).
-
#current_alias ⇒ String
Returns the current alias for this timezone.
-
#current_alias=(new_alias) ⇒ Object
Sets the current alias.
-
#current_name(dst = false, dst_label: " (DST)", year: nil) ⇒ String
Returns the current name.
-
#current_offset(rational = false, date = nil) ⇒ Fixnum|Rational
Returns the current offset for this timezone, taking care of Daylight Saving Time (DST).
-
#dst_correction(rational = false, year = nil) ⇒ Fixnum|Rational
Return the correction applied to the standard offset the timezone when the Daylight Saving Time (DST) is active.
-
#dst_period(year = nil) ⇒ TimezonePeriod
Gets a period for this timezone when the Daylight Saving Time (DST) is active (it takes care of different hemispheres).
-
#offset(rational: false, dst: false, year: nil) ⇒ Fixnum|Rational
Returns the standard offset for this timezone.
-
#to_str(dst = false, **args) ⇒ String
Formats this zone as a string.
-
#uses_dst?(reference = nil) ⇒ Boolean
Checks if the timezone uses Daylight Saving Time (DST) for that date or year.
Instance Method Details
#aliases ⇒ Array
Returns a list of valid aliases (city names) for this timezone (basing on offset).
152 153 154 155 |
# File 'lib/lazier/timezone.rb', line 152 def aliases reference = self.class::MAPPING.fetch(name, name).gsub("_", " ") @aliases ||= ([reference] + self.class::MAPPING.map { |name, zone| format_alias(name, zone, reference) }).uniq.compact.sort end |
#current_alias ⇒ String
Returns the current alias for this timezone.
170 171 172 173 174 175 176 177 |
# File 'lib/lazier/timezone.rb', line 170 def current_alias if @current_alias @current_alias else identifier = name aliases.find { |a| a == identifier } || aliases.first end end |
#current_alias=(new_alias) ⇒ Object
Sets the current alias.
182 183 184 |
# File 'lib/lazier/timezone.rb', line 182 def current_alias=(new_alias) @current_alias = new_alias.ensure_string end |
#current_name(dst = false, dst_label: " (DST)", year: nil) ⇒ String
Returns the current name.
192 193 194 195 196 197 |
# File 'lib/lazier/timezone.rb', line 192 def current_name(dst = false, dst_label: " (DST)", year: nil) year ||= Date.current.year rv = current_alias rv += dst_label if dst && uses_dst?(year) rv end |
#current_offset(rational = false, date = nil) ⇒ Fixnum|Rational
Returns the current offset for this timezone, taking care of Daylight Saving Time (DST).
162 163 164 165 |
# File 'lib/lazier/timezone.rb', line 162 def current_offset(rational = false, date = nil) date = (date || ::DateTime.current).in_time_zone offset(rational: rational, dst: date.dst?, year: date.year) end |
#dst_correction(rational = false, year = nil) ⇒ Fixnum|Rational
Return the correction applied to the standard offset the timezone when the Daylight Saving Time (DST) is active.
248 249 250 251 |
# File 'lib/lazier/timezone.rb', line 248 def dst_correction(rational = false, year = nil) rv = dst_offset(year, :std_offset) rational ? self.class.rationalize_offset(rv) : rv end |
#dst_period(year = nil) ⇒ TimezonePeriod
Gets a period for this timezone when the Daylight Saving Time (DST) is active (it takes care of different hemispheres).
233 234 235 236 237 238 239 240 241 |
# File 'lib/lazier/timezone.rb', line 233 def dst_period(year = nil) year ||= ::Date.current.year period = period_for_utc(::DateTime.civil(year, 7, 15, 12).utc) # Summer for the northern hemisphere period = period_for_utc(::DateTime.civil(year, 1, 15, 12).utc) unless period.dst? # Summer for the southern hemisphere period.dst? ? period : nil rescue nil end |
#offset(rational: false, dst: false, year: nil) ⇒ Fixnum|Rational
Returns the standard offset for this timezone.
205 206 207 208 209 210 211 212 213 214 215 |
# File 'lib/lazier/timezone.rb', line 205 def offset(rational: false, dst: false, year: nil) rv = if dst period = dst_period(year) period ? period.utc_total_offset : 0 else utc_offset end rational ? self.class.rationalize_offset(rv) : rv end |
#to_str(dst = false, **args) ⇒ String
Formats this zone as a string.
268 269 270 271 272 273 274 275 276 277 278 |
# File 'lib/lazier/timezone.rb', line 268 def to_str(dst = false, **args) # PI: Ignore reek on this. label, dst_label, utc_label, year, parameterized, with_offset, colon, offset_position = prepare_to_str_arguments(args) if parameterized self.class.parameterize(to_str(dst, label: label, dst_label: dst_label, utc_label: utc_label, year: year, parameterized: false), with_offset) else offset_label = self.class.seconds_to_utc_offset(offset(rational: false, dst: dst, year: year), colon) to_str_unparameterized(dst ? dst_label : "", label, offset_label, offset_position, utc_label, with_offset) end end |
#uses_dst?(reference = nil) ⇒ Boolean
Checks if the timezone uses Daylight Saving Time (DST) for that date or year.
221 222 223 224 225 226 227 |
# File 'lib/lazier/timezone.rb', line 221 def uses_dst?(reference = nil) if reference.is_a?(Date) || reference.is_a?(DateTime) || reference.is_a?(Time) period_for_utc(reference).dst? else dst_period(reference) end end |