Module: Cowtech::Extensions::TimeZone
- Extended by:
- ActiveSupport::Concern
- Defined in:
- lib/cowtech-extensions/datetime.rb
Overview
Extensions for timezone objects.
Defined Under Namespace
Modules: ClassMethods
Instance Method Summary collapse
-
#aliases(dst_label = nil) ⇒ Array
Returns a list of valid aliases (city names) for this timezone (basing on offset).
-
#current_offset(rational = false, date = nil) ⇒ Fixnum|Rational
Returns the current offset for this timezone, taking care of DST (DST).
-
#dst_correction(rational = false, year = nil) ⇒ Fixnum|Rational
Return the correction applied to the standard offset the timezone when the DST (DST) is active.
-
#dst_name(dst_label = nil, year = nil, name = nil) ⇒ String
Returns the name for this zone with DST (DST) active.
-
#dst_offset(rational = false, year = nil) ⇒ Fixnum|Rational
Returns the standard offset for this timezone timezone when the DST (DST) is active.
-
#dst_period(year = nil) ⇒ TimezonePeriod
Gets a period for this timezone when the DST (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 DST (DST) active.
-
#to_str_parameterized(with_offset = true, name = nil) ⇒ String
Returns a parametized 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 DST (DST) active.
-
#to_str_with_dst_parameterized(dst_label = nil, with_offset = true, year = nil, name = nil) ⇒ String
Returns a parametized string representation for this zone with DST (DST) active.
-
#uses_dst?(year = nil) ⇒ Boolean
Checks if the timezone uses DST (DST) for that year.
Instance Method Details
#aliases(dst_label = nil) ⇒ Array
Returns a list of valid aliases (city names) for this timezone (basing on offset).
389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 |
# File 'lib/cowtech-extensions/datetime.rb', line 389 def aliases(dst_label = nil) reference = self.name reference = self.class::MAPPING[self.name] if self.class::MAPPING.has_key?(self.name) # We are an alias reference = reference.gsub("_", " ") if @aliases.blank? then # First we search for aliases by name @aliases = [reference] self.class::MAPPING.each do |name, zone| if zone.gsub("_", " ") == reference then if name == "International Date Line West" || name == "UTC" || name.include?("(US & Canada)") @aliases << name else @aliases << reference.gsub(/\/.*/, "/" + name) end end end @aliases = @aliases.uniq.compact.sort end @aliases end |
#current_offset(rational = false, date = nil) ⇒ Fixnum|Rational
Returns the current offset for this timezone, taking care of DST (DST).
419 420 421 422 423 424 425 426 |
# File 'lib/cowtech-extensions/datetime.rb', line 419 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 |
#dst_correction(rational = false, year = nil) ⇒ Fixnum|Rational
Return the correction applied to the standard offset the timezone when the DST (DST) is active.
465 466 467 468 469 |
# File 'lib/cowtech-extensions/datetime.rb', line 465 def dst_correction(rational = false, year = nil) period = self.dst_period(year) rv = period ? period.std_offset : 0 rational ? self.class.rationalize_offset(rv) : rv end |
#dst_name(dst_label = nil, year = nil, name = nil) ⇒ String
Returns the name for this zone with DST (DST) active.
488 489 490 491 492 493 |
# File 'lib/cowtech-extensions/datetime.rb', line 488 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 |
#dst_offset(rational = false, year = nil) ⇒ Fixnum|Rational
Returns the standard offset for this timezone timezone when the DST (DST) is active.
476 477 478 479 480 |
# File 'lib/cowtech-extensions/datetime.rb', line 476 def dst_offset(rational = false, year = nil) period = self.dst_period(year) rv = period ? period.utc_total_offset : 0 rational ? self.class.rationalize_offset(rv) : rv end |
#dst_period(year = nil) ⇒ TimezonePeriod
Gets a period for this timezone when the DST (DST) is active (it takes care of different hemispheres).
441 442 443 444 445 446 447 448 449 450 |
# File 'lib/cowtech-extensions/datetime.rb', line 441 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 |
#offset(rational = false) ⇒ Fixnum|Rational
Returns the standard offset for this timezone.
432 433 434 435 |
# File 'lib/cowtech-extensions/datetime.rb', line 432 def offset(rational = false) rv = self.utc_offset rational ? self.class.rationalize_offset(rv) : rv end |
#to_str(name = nil, colon = true) ⇒ String
Returns the name for this zone with DST (DST) active.
500 501 502 503 |
# File 'lib/cowtech-extensions/datetime.rb', line 500 def to_str(name = nil, colon = true) name ||= self.aliases.first "(GMT#{self.formatted_offset(colon)}) #{name}" end |
#to_str_parameterized(with_offset = true, name = nil) ⇒ String
Returns a parametized string representation for this zone.
529 530 531 |
# File 'lib/cowtech-extensions/datetime.rb', line 529 def to_str_parameterized(with_offset = true, name = nil) ::ActiveSupport::TimeZone.parameterize_zone(name || self.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 DST (DST) active.
511 512 513 514 515 516 517 518 519 520 521 522 |
# File 'lib/cowtech-extensions/datetime.rb', line 511 def to_str_with_dst(dst_label = nil, year = nil, name = nil) dst_label ||= "(DST)" name ||= self.aliases.first 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 |
#to_str_with_dst_parameterized(dst_label = nil, with_offset = true, year = nil, name = nil) ⇒ String
Returns a parametized string representation for this zone with DST (DST) active.
540 541 542 543 |
# File 'lib/cowtech-extensions/datetime.rb', line 540 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 |
#uses_dst?(year = nil) ⇒ Boolean
Checks if the timezone uses DST (DST) for that year.
456 457 458 |
# File 'lib/cowtech-extensions/datetime.rb', line 456 def uses_dst?(year = nil) self.dst_period(year).present? end |