module Lazier::TimeZone::ClassMethods

General methods.

Public Instance Methods

compare(left, right) click to toggle source

Compares two timezones. They are sorted by the location name.

@param left [String|TimeZone] The first zone name to compare. @param right [String|TimeZone] The second zone name to compare. @return [Fixnum] The result of comparison, like Ruby's operator `<=>`.

# File lib/lazier/timezone.rb, line 112
def compare(left, right)
  left = left.to_str if left.is_a?(::ActiveSupport::TimeZone)
  right = right.to_str if right.is_a?(::ActiveSupport::TimeZone)
  left.ensure_string.split(" ", 2)[1] <=> right.ensure_string.split(" ", 2)[1]
end
find(name, dst_label = " (DST)") click to toggle source

Find a zone by its name.

@param name [String] The zone name. @param dst_label [String] Label for the DST indication. Defaults to ` (DST)`. @return [TimeZone] A timezone or `nil` if no zone was found.

# File lib/lazier/timezone.rb, line 48
def find(name, dst_label = " (DST)")
  rv = list(true, dst_label: dst_label, as_hash: true)[name]
  rv.current_alias = name.gsub(/\(GMT(.{6})\) (.+)(#{Regexp.quote(dst_label)})$/, "\\2") if rv
  rv
end
format_offset(offset, colon = true) click to toggle source

Returns a +HH:MM formatted representation of the offset.

@param offset [Rational|Fixnum] The offset to represent, in seconds or as a rational. @param colon [Boolean] Whether to put the colon in the output string. @return [String] The formatted offset.

# File lib/lazier/timezone.rb, line 39
def format_offset(offset, colon = true)
  seconds_to_utc_offset(offset.is_a?(::Rational) ? (offset * 86_400).to_i : offset, colon)
end
list(with_dst = true, dst_label: " (DST)", parameterized: false, sort_by_name: true, as_hash: false) click to toggle source

Returns a list of names of all timezones.

@param with_dst [Boolean] If include DST version of the zones. @param parameterized [Boolean] If parameterize zones. @param dst_label [String] Label for the DST indication. Defaults to ` (DST)`. @param as_hash [Hash] If return an hash. @return [Array|Hash] A list of names of timezones or a hash with labels and timezones as keys.

# File lib/lazier/timezone.rb, line 61
def list(with_dst = true, dst_label: " (DST)", parameterized: false, sort_by_name: true, as_hash: false)
  dst_label = nil unless with_dst
  key = [dst_label, sort_by_name, as_hash, parameterized].join(":")
  @zones_names ||= {}

  unless @zones_names[key]
    all = ::ActiveSupport::TimeZone.all
    @zones_names[key] = send("finalize_list_as_#{as_hash ? "hash" : "list"}", all, dst_label, parameterized, sort_by_name)
  end

  @zones_names[key]
end
parameterize(tz, with_offset = true) click to toggle source

Returns a string representation of a timezone.

“`ruby DateTime.parameterize_zone(ActiveSupport::TimeZone[“Pacific Time (US & Canada)”]) # => “-0800@pacific-time-us-canada” “` @param tz [TimeZone|String] The zone to represent. @param with_offset [Boolean] Whether to include offset into the representation. @return [String] A string representation which can be used for searches.

# File lib/lazier/timezone.rb, line 83
def parameterize(tz, with_offset = true)
  tz = tz.to_str unless tz.is_a?(::String)

  if tz =~ ::Lazier::TimeZone::ALREADY_PARAMETERIZED
    tz
  elsif tz =~ ::Lazier::TimeZone::PARAMETERIZER
    mo = $LAST_MATCH_INFO
    [(with_offset ? mo[:offset].gsub(":", "") : nil), mo[:label].parameterize].compact.join("@")
  else
    tz.parameterize
  end
end
rationalize_offset(offset) click to toggle source

Expression to parameterize a zone Returns an offset in rational value.

@param offset [Fixnum] The offset to convert. @return [Rational] The converted offset.

# File lib/lazier/timezone.rb, line 30
def rationalize_offset(offset)
  ::TZInfo::OffsetRationals.rational_for_offset(offset)
end
unparameterize(tz, dst_label = " (DST)") click to toggle source

Finds a parameterized timezone. @see DateTime#parameterize_zone

@param tz [String] The zone to unparameterize. @param dst_label [String] Label for the DST indication. Defaults to `(DST)`. @return [TimeZone] The found timezone or `nil` if the zone is not valid.

# File lib/lazier/timezone.rb, line 102
def unparameterize(tz, dst_label = " (DST)")
  tz = parameterize(tz)
  list(true, dst_label: dst_label, parameterized: true, as_hash: true)[tz]
end