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

Instance Method Details

#aliasesArray

Returns a list of valid aliases (city names) for this timezone (basing on offset).

Returns:

  • (Array)

    A list of aliases for this timezone



411
412
413
414
# File 'lib/lazier/datetime.rb', line 411

def aliases
  reference = self.class::MAPPING.fetch(self.name, self.name).gsub("_", " ")
  @aliases ||= ([reference] + self.class::MAPPING.collect { |name, zone| format_alias(name, zone, reference) }).uniq.compact.sort
end

#current_aliasObject

Return the current alias for this timezone.



431
432
433
434
435
436
437
438
439
440
441
# File 'lib/lazier/datetime.rb', line 431

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

#current_offset(rational = false, date = nil) ⇒ Fixnum|Rational

Returns the current offset for this timezone, taking care of Daylight Saving Time (DST).

Parameters:

  • rational (Boolean) (defaults to: false)

    If to return the offset as a Rational.

  • date (DateTime) (defaults to: nil)

    The date to consider. Defaults to now.

Returns:

  • (Fixnum|Rational)

    The offset of this timezone.



421
422
423
424
425
426
427
428
# File 'lib/lazier/datetime.rb', line 421

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 Daylight Saving Time (DST) is active.

Parameters:

  • rational (Boolean) (defaults to: false)

    If to return the offset as a Rational.

  • year (Fixnum) (defaults to: nil)

    The year to which refer to. Defaults to the current year.

Returns:

  • (Fixnum|Rational)

    The correction for dst.



484
485
486
# File 'lib/lazier/datetime.rb', line 484

def dst_correction(rational = false, year = nil)
  self.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.

Parameters:

  • dst_label (String) (defaults to: nil)

    Label for the DST indication. Defaults to (DST).

  • year (Fixnum) (defaults to: nil)

    The year to which refer to. Defaults to the current year.

  • name (String) (defaults to: nil)

    The name to use for this zone. Defaults to the zone name.

Returns:

  • (String)

    The name for the zone with DST or nil, if the timezone doesn't use DST for that year.



506
507
508
509
510
511
# File 'lib/lazier/datetime.rb', line 506

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, method = :utc_total_offset) ⇒ Fixnum|Rational

Returns the standard offset for this timezone timezone when the Daylight Saving Time (DST) is active.

Parameters:

  • rational (Boolean) (defaults to: false)

    If to return the offset as a Rational.

  • year (Fixnum) (defaults to: nil)

    The year to which refer to. Defaults to the current year.

  • method (Symbol) (defaults to: :utc_total_offset)

    The method to use for getting the offset. Default is total offset from UTC.

Returns:

  • (Fixnum|Rational)

    The DST offset for this timezone or 0, if the timezone doesn't use DST for that year.



494
495
496
497
498
# File 'lib/lazier/datetime.rb', line 494

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

#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).

Parameters:

  • year (Fixnum) (defaults to: nil)

    The year to which refer to. Defaults to the current year.

Returns:

  • (TimezonePeriod)

    A period when the Daylight Saving Time (DST) is active or nil if the timezone doesn't use DST for that year.



456
457
458
459
460
461
462
463
464
465
# File 'lib/lazier/datetime.rb', line 456

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.

Parameters:

  • rational (Boolean) (defaults to: false)

    If to return the offset as a Rational.

Returns:

  • (Fixnum|Rational)

    The offset of this timezone.



447
448
449
450
# File 'lib/lazier/datetime.rb', line 447

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 Daylight Saving Time (DST) active.

Parameters:

  • name (String) (defaults to: nil)

    The name to use for this zone. Defaults to the zone name.

  • colon (Boolean) (defaults to: true)

    If to put the colon in the output string.

Returns:

  • (String)

    The name for this zone.



518
519
520
521
# File 'lib/lazier/datetime.rb', line 518

def to_str(name = nil, colon = true)
  name ||= self.current_alias
  "(GMT#{self.formatted_offset(colon)}) #{name}"
end

#to_str_parameterized(with_offset = true, name = nil) ⇒ String

Returns a parametized string representation for this zone.

Parameters:

  • with_offset (Boolean) (defaults to: true)

    If to include offset into the representation.

  • name (String) (defaults to: nil)

    The name to use for this zone. Defaults to the zone name.

Returns:

  • (String)

    The parametized string representation for this zone.



547
548
549
# File 'lib/lazier/datetime.rb', line 547

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 Daylight Saving Time (DST) active.

Parameters:

  • dst_label (String) (defaults to: nil)

    Label for the DST indication. Defaults to (DST).

  • year (Fixnum) (defaults to: nil)

    The year to which refer to. Defaults to the current year.

  • name (String) (defaults to: nil)

    The name to use for this zone. Defaults to the zone name.

Returns:

  • (String)

    The string representation for the zone with DST or nil, if the timezone doesn't use DST for that year.



529
530
531
532
533
534
535
536
537
538
539
540
# File 'lib/lazier/datetime.rb', line 529

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

#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 Daylight Saving Time (DST) active.

Parameters:

  • dst_label (String) (defaults to: nil)

    Label for the DST indication. Defaults to (DST).

  • with_offset (Boolean) (defaults to: true)

    If to include offset into the representation.

  • year (Fixnum) (defaults to: nil)

    The year to which refer to. Defaults to the current year.

  • name (String) (defaults to: nil)

    The name to use for this zone. Defaults to the zone name.

Returns:

  • (String)

    The parametized string representation for this zone with DST or nil, if the timezone doesn't use DST for that year.



558
559
560
561
# File 'lib/lazier/datetime.rb', line 558

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?(reference = nil) ⇒ Boolean

Checks if the timezone uses Daylight Saving Time (DST) for that date or year.

Parameters:

  • reference (Object) (defaults to: nil)

    The date or year to check. Defaults to the current year.

Returns:

  • (Boolean)

    true if the zone uses DST for that date or year, false otherwise.



471
472
473
474
475
476
477
# File 'lib/lazier/datetime.rb', line 471

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