Module: Lazier::TimeZone
- Extended by:
- ActiveSupport::Concern
- Defined in:
- lib/lazier/datetime.rb
Overview
Extensions for timezone objects.
Defined Under Namespace
Modules: ClassMethods
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_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_name(dst_label = nil, year = nil, name = nil) ⇒ String
Returns the name for this zone with Daylight Saving Time (DST) active.
-
#dst_offset(rational = false, year = nil, method = :utc_total_offset) ⇒ Fixnum|Rational
Returns the standard offset for this timezone 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) ⇒ Fixnum|Rational
Returns the standard offset for this timezone.
-
#to_str(name = nil, colon = true) ⇒ String
Returns the name for this zone with Daylight Saving Time (DST) active.
-
#to_str_parameterized(with_offset = true, name = nil) ⇒ String
Returns a parameterized string representation for this zone.
-
#to_str_with_dst(dst_label = nil, year = nil, name = nil) ⇒ String
Returns a string representation for this zone with Daylight Saving Time (DST) active.
-
#to_str_with_dst_parameterized(dst_label = nil, year = nil, name = nil) ⇒ String
Returns a parameterized string representation for this zone with Daylight Saving Time (DST) active.
-
#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).
416 417 418 419 |
# File 'lib/lazier/datetime.rb', line 416 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.
435 436 437 438 439 440 441 442 443 444 445 |
# File 'lib/lazier/datetime.rb', line 435 def current_alias identifier = tzinfo.identifier catch(:alias) do aliases.each do |a| throw(:alias, a) if a == identifier end aliases.first end end |
#current_offset(rational = false, date = nil) ⇒ Fixnum|Rational
Returns the current offset for this timezone, taking care of Daylight Saving Time (DST).
426 427 428 429 430 |
# File 'lib/lazier/datetime.rb', line 426 def current_offset(rational = false, date = nil) date ||= ::DateTime.now rv = (period_for_utc(date.utc).dst? ? dst_offset : offset) rational ? self.class.rationalize_offset(rv) : rv 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.
487 488 489 |
# File 'lib/lazier/datetime.rb', line 487 def dst_correction(rational = false, year = nil) dst_offset(rational, year, :std_offset) end |
#dst_name(dst_label = nil, year = nil, name = nil) ⇒ String
Returns the name for this zone with Daylight Saving Time (DST) active.
509 510 511 |
# File 'lib/lazier/datetime.rb', line 509 def dst_name(dst_label = nil, year = nil, name = nil) uses_dst?(year) ? "#{name || self.name} #{dst_label || "(DST)"}" : nil end |
#dst_offset(rational = false, year = nil, method = :utc_total_offset) ⇒ Fixnum|Rational
Returns the standard offset for this timezone timezone when the Daylight Saving Time (DST) is active.
497 498 499 500 501 |
# File 'lib/lazier/datetime.rb', line 497 def dst_offset(rational = false, year = nil, method = :utc_total_offset) period = dst_period(year) rv = period ? period.send(method) : 0 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).
459 460 461 462 463 464 465 466 467 468 |
# File 'lib/lazier/datetime.rb', line 459 def dst_period(year = nil) year ||= ::Date.today.year northern_summer = ::DateTime.civil(year, 7, 15).utc # This is a representation of a summer period in the Northern Hemisphere. southern_summer = ::DateTime.civil(year, 1, 15).utc # This is a representation of a summer period in the Southern Hemisphere. period = period_for_utc(northern_summer) period = period_for_utc(southern_summer) unless period.dst? period.dst? ? period : nil end |
#offset(rational = false) ⇒ Fixnum|Rational
Returns the standard offset for this timezone.
451 452 453 |
# File 'lib/lazier/datetime.rb', line 451 def offset(rational = false) rational ? self.class.rationalize_offset(utc_offset) : utc_offset end |
#to_str(name = nil, colon = true) ⇒ String
Returns the name for this zone with Daylight Saving Time (DST) active.
518 519 520 |
# File 'lib/lazier/datetime.rb', line 518 def to_str(name = nil, colon = true) "(GMT#{formatted_offset(colon)}) #{name || current_alias}" end |
#to_str_parameterized(with_offset = true, name = nil) ⇒ String
Returns a parameterized string representation for this zone.
537 538 539 |
# File 'lib/lazier/datetime.rb', line 537 def to_str_parameterized(with_offset = true, name = nil) ::ActiveSupport::TimeZone.parameterize_zone(name || to_str, with_offset) end |
#to_str_with_dst(dst_label = nil, year = nil, name = nil) ⇒ String
Returns a string representation for this zone with Daylight Saving Time (DST) active.
528 529 530 |
# File 'lib/lazier/datetime.rb', line 528 def to_str_with_dst(dst_label = nil, year = nil, name = nil) uses_dst?(year) ? "(GMT#{self.class.seconds_to_utc_offset(dst_period(year).utc_total_offset)}) #{name || current_alias} #{dst_label || "(DST)"}" : nil end |
#to_str_with_dst_parameterized(dst_label = nil, year = nil, name = nil) ⇒ String
Returns a parameterized string representation for this zone with Daylight Saving Time (DST) active.
547 548 549 550 |
# File 'lib/lazier/datetime.rb', line 547 def to_str_with_dst_parameterized(dst_label = nil, year = nil, name = nil) rv = to_str_with_dst(dst_label, year, name) rv ? ::ActiveSupport::TimeZone.parameterize_zone(rv) : nil end |
#uses_dst?(reference = nil) ⇒ Boolean
Checks if the timezone uses Daylight Saving Time (DST) for that date or year.
474 475 476 477 478 479 480 |
# File 'lib/lazier/datetime.rb', line 474 def uses_dst?(reference = nil) if reference.respond_to?(:year) && reference.respond_to?(:utc) # This is a date like object dst_period(reference.year).present? && period_for_utc(reference.utc).dst? else dst_period(reference).present? end end |