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)
-
- (Array) aliases
Returns a list of valid aliases (city names) for this timezone (basing on offset).
-
- (Object) current_alias
Return the current alias for this timezone.
-
- (Fixnum|Rational) current_offset(rational = false, date = nil)
Returns the current offset for this timezone, taking care of Daylight Saving Time (DST).
-
- (Fixnum|Rational) dst_correction(rational = false, year = nil)
Return the correction applied to the standard offset the timezone when the Daylight Saving Time (DST) is active.
-
- (String) dst_name(dst_label = nil, year = nil, name = nil)
Returns the name for this zone with Daylight Saving Time (DST) active.
-
- (Fixnum|Rational) dst_offset(rational = false, year = nil, method = :utc_total_offset)
Returns the standard offset for this timezone timezone when the Daylight Saving Time (DST) is active.
-
- (TimezonePeriod) dst_period(year = nil)
Gets a period for this timezone when the Daylight Saving Time (DST) is active (it takes care of different hemispheres).
-
- (Fixnum|Rational) offset(rational = false)
Returns the standard offset for this timezone.
-
- (String) to_str(name = nil, colon = true)
Returns the name for this zone with Daylight Saving Time (DST) active.
-
- (String) to_str_parameterized(with_offset = true, name = nil)
Returns a parametized string representation for this zone.
-
- (String) to_str_with_dst(dst_label = nil, year = nil, name = nil)
Returns a string representation for this zone with Daylight Saving Time (DST) active.
-
- (String) to_str_with_dst_parameterized(dst_label = nil, with_offset = true, year = nil, name = nil)
Returns a parametized string representation for this zone with Daylight Saving Time (DST) active.
-
- (Boolean) uses_dst?(reference = nil)
Checks if the timezone uses Daylight Saving Time (DST) for that date or year.
Instance Method Details
- (Array) aliases
Returns a list of valid aliases (city names) for this timezone (basing on offset).
388 389 390 391 392 393 394 395 396 397 398 |
# File 'lib/lazier/datetime.rb', line 388 def aliases reference = self.class::MAPPING.fetch(self.name, self.name).gsub("_", " ") @aliases ||= ([reference] + self.class::MAPPING.collect { |name, zone| if zone.gsub("_", " ") == reference then (["International Date Line West", "UTC"].include?(name) || name.include?("(US & Canada)")) ? name : reference.gsub(/\/.*/, "/#{name}") else nil end }).uniq.compact.sort end |
- (Object) current_alias
Return the current alias for this timezone.
415 416 417 418 419 420 421 422 423 424 425 |
# File 'lib/lazier/datetime.rb', line 415 def current_alias identifier = self.tzinfo.identifier catch(:alias) do self.aliases.each do |a| throw(:alias, a) if a == identifier end self.aliases.first end end |
- (Fixnum|Rational) current_offset(rational = false, date = nil)
Returns the current offset for this timezone, taking care of Daylight Saving Time (DST).
405 406 407 408 409 410 411 412 |
# File 'lib/lazier/datetime.rb', line 405 def current_offset(rational = false, date = nil) date ||= ::DateTime.now dst_period = self.dst_period rv = (self.period_for_utc(date.utc).dst? ? self.dst_offset : self.offset) rational ? self.class.rationalize_offset(rv) : rv end |
- (Fixnum|Rational) dst_correction(rational = false, year = nil)
Return the correction applied to the standard offset the timezone when the Daylight Saving Time (DST) is active.
468 469 470 |
# File 'lib/lazier/datetime.rb', line 468 def dst_correction(rational = false, year = nil) self.dst_offset(rational, year, :std_offset) end |
- (String) dst_name(dst_label = nil, year = nil, name = nil)
Returns the name for this zone with Daylight Saving Time (DST) active.
490 491 492 493 494 495 |
# File 'lib/lazier/datetime.rb', line 490 def dst_name(dst_label = nil, year = nil, name = nil) dst_label ||= "(DST)" name ||= self.name self.uses_dst?(year) ? "#{name} #{dst_label}" : nil end |
- (Fixnum|Rational) dst_offset(rational = false, year = nil, method = :utc_total_offset)
Returns the standard offset for this timezone timezone when the Daylight Saving Time (DST) is active.
478 479 480 481 482 |
# File 'lib/lazier/datetime.rb', line 478 def dst_offset(rational = false, year = nil, method = :utc_total_offset) period = self.dst_period(year) rv = period ? period.send(method) : 0 rational ? self.class.rationalize_offset(rv) : rv end |
- (TimezonePeriod) dst_period(year = nil)
Gets a period for this timezone when the Daylight Saving Time (DST) is active (it takes care of different hemispheres).
440 441 442 443 444 445 446 447 448 449 |
# File 'lib/lazier/datetime.rb', line 440 def dst_period(year = nil) year ||= ::Date.today.year nothern_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 Northern Hemisphere. period = self.period_for_utc(nothern_summer) period = self.period_for_utc(southern_summer) if !period.dst? period.dst? ? period : nil end |
- (Fixnum|Rational) offset(rational = false)
Returns the standard offset for this timezone.
431 432 433 434 |
# File 'lib/lazier/datetime.rb', line 431 def offset(rational = false) rv = self.utc_offset rational ? self.class.rationalize_offset(rv) : rv end |
- (String) to_str(name = nil, colon = true)
Returns the name for this zone with Daylight Saving Time (DST) active.
502 503 504 505 |
# File 'lib/lazier/datetime.rb', line 502 def to_str(name = nil, colon = true) name ||= self.current_alias "(GMT#{self.formatted_offset(colon)}) #{name}" end |
- (String) to_str_parameterized(with_offset = true, name = nil)
Returns a parametized string representation for this zone.
531 532 533 |
# File 'lib/lazier/datetime.rb', line 531 def to_str_parameterized(with_offset = true, name = nil) ::ActiveSupport::TimeZone.parameterize_zone(name || self.to_str, with_offset) end |
- (String) to_str_with_dst(dst_label = nil, year = nil, name = nil)
Returns a string representation for this zone with Daylight Saving Time (DST) active.
513 514 515 516 517 518 519 520 521 522 523 524 |
# File 'lib/lazier/datetime.rb', line 513 def to_str_with_dst(dst_label = nil, year = nil, name = nil) dst_label ||= "(DST)" name ||= self.current_alias if self.uses_dst?(year) then period = self.dst_period(year) offset = self.class.seconds_to_utc_offset(period.utc_total_offset) "(GMT#{offset}) #{name} #{dst_label}" else nil end end |
- (String) to_str_with_dst_parameterized(dst_label = nil, with_offset = true, year = nil, name = nil)
Returns a parametized string representation for this zone with Daylight Saving Time (DST) active.
542 543 544 545 |
# File 'lib/lazier/datetime.rb', line 542 def to_str_with_dst_parameterized(dst_label = nil, with_offset = true, year = nil, name = nil) rv = self.to_str_with_dst(dst_label, year, name) rv ? ::ActiveSupport::TimeZone.parameterize_zone(rv) : nil end |
- (Boolean) uses_dst?(reference = nil)
Checks if the timezone uses Daylight Saving Time (DST) for that date or year.
455 456 457 458 459 460 461 |
# File 'lib/lazier/datetime.rb', line 455 def uses_dst?(reference = nil) if reference.respond_to?(:year) && reference.respond_to?(:utc) then # This is a date like object self.dst_period(reference.year).present? && self.period_for_utc(reference.utc).dst? else self.dst_period(reference).present? end end |