Module: Lazier::TimeZone::ClassMethods

Defined in:
lib/lazier/timezone.rb

Overview

General methods.

Instance Method Summary collapse

Instance Method Details

#compare(left, right) ⇒ Fixnum

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

Parameters:

Returns:

  • (Fixnum)

    The result of comparison, like Ruby's operator <=>.



112
113
114
115
116
# 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)") ⇒ TimeZone

Find a zone by its name.

Parameters:

  • name (String)

    The zone name.

  • dst_label (String) (defaults to: " (DST)")

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

Returns:

  • (TimeZone)

    A timezone or nil if no zone was found.



48
49
50
51
52
# 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) ⇒ String

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

Parameters:

  • offset (Rational|Fixnum)

    The offset to represent, in seconds or as a rational.

  • colon (Boolean) (defaults to: true)

    Whether to put the colon in the output string.

Returns:

  • (String)

    The formatted offset.



39
40
41
# 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) ⇒ Array|Hash

Returns a list of names of all timezones.

Parameters:

  • with_dst (Boolean) (defaults to: true)

    If include DST version of the zones.

  • parameterized (Boolean) (defaults to: false)

    If parameterize zones.

  • dst_label (String) (defaults to: " (DST)")

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

  • as_hash (Hash) (defaults to: false)

    If return an hash.

Returns:

  • (Array|Hash)

    A list of names of timezones or a hash with labels and timezones as keys.



61
62
63
64
65
66
67
68
69
70
71
72
# 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) ⇒ String

Returns a string representation of a timezone.

DateTime.parameterize_zone(ActiveSupport::TimeZone["Pacific Time (US & Canada)"])
# => "-0800@pacific-time-us-canada"

Parameters:

  • tz (TimeZone|String)

    The zone to represent.

  • with_offset (Boolean) (defaults to: true)

    Whether to include offset into the representation.

Returns:

  • (String)

    A string representation which can be used for searches.



83
84
85
86
87
88
89
90
91
92
93
94
# 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) ⇒ Rational

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

Parameters:

  • offset (Fixnum)

    The offset to convert.

Returns:

  • (Rational)

    The converted offset.



30
31
32
# File 'lib/lazier/timezone.rb', line 30

def rationalize_offset(offset)
  ::TZInfo::OffsetRationals.rational_for_offset(offset)
end

#unparameterize(tz, dst_label = " (DST)") ⇒ TimeZone

Finds a parameterized timezone.

Parameters:

  • tz (String)

    The zone to unparameterize.

  • dst_label (String) (defaults to: " (DST)")

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

Returns:

  • (TimeZone)

    The found timezone or nil if the zone is not valid.

See Also:

  • DateTime#parameterize_zone


102
103
104
105
# 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