Module: Lazier::TimeZone::ClassMethods

Defined in:
lib/lazier/timezone.rb

Overview

General methods.

Instance Method Summary (collapse)

Instance Method Details

- (Fixnum) compare(left, right)

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

Parameters:

Returns:

  • (Fixnum)

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



111
112
113
114
115
# File 'lib/lazier/timezone.rb', line 111

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

- (TimeZone) find(name, dst_label = " (DST)")

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.



49
50
51
# File 'lib/lazier/timezone.rb', line 49

def find(name, dst_label = " (DST)")
  list(true, dst_label: dst_label, as_hash: true)[name]
end

- (String) format_offset(offset, colon = true)

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)

    If to put the colon in the output string.

Returns:

  • (String)

    The formatted offset.



40
41
42
# File 'lib/lazier/timezone.rb', line 40

def format_offset(offset, colon = true)
  seconds_to_utc_offset(offset.is_a?(::Rational) ? (offset * 86_400).to_i : offset, colon)
end

- (Array|Hash) list(with_dst = true, dst_label: " (DST)", parameterized: false, sort_by_name: true, as_hash: false)

Returns a list of names of all timezones.

Parameters:

  • with_dst (Boolean) (defaults to: true)

    If include DST version of the zones.

  • parameterized (Boolean)

    If parameterize zones.

  • dst_label (String)

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

  • as_hash (Hash)

    If return an hash.

Returns:

  • (Array|Hash)

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



60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/lazier/timezone.rb', line 60

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

- (String) parameterize(tz, with_offset = true)

Returns a string representation of a timezone.

ruby 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)

    If to include offset into the representation.

Returns:

  • (String)

    A string representation which can be used for searches.



82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/lazier/timezone.rb', line 82

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

- (Rational) rationalize_offset(offset)

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

Parameters:

  • offset (Fixnum)

    The offset to convert.

Returns:

  • (Rational)

    The converted offset.



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

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

- (TimeZone) unparameterize(tz, dst_label = " (DST)")

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


101
102
103
104
# File 'lib/lazier/timezone.rb', line 101

def unparameterize(tz, dst_label = " (DST)")
  tz = parameterize(tz)
  list(true, dst_label: dst_label, parameterized: true, as_hash: true)[tz]
end