Module: Lazier::TimeZone::ClassMethods
- Defined in:
- lib/lazier/timezone.rb
Overview
General methods.
Instance Method Summary (collapse)
-
- (Fixnum) compare(left, right)
Compares two timezones.
-
- (TimeZone) find(name, dst_label = " (DST)")
Find a zone by its name.
-
- (String) format_offset(offset, colon = true)
Returns a +HH:MM formatted representation of the offset.
-
- (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.
-
- (String) parameterize(tz, with_offset = true)
Returns a string representation of a timezone.
-
- (Rational) rationalize_offset(offset)
Expression to parameterize a zone Returns an offset in rational value.
-
- (TimeZone) unparameterize(tz, dst_label = " (DST)")
Finds a parameterized timezone.
Instance Method Details
- (Fixnum) compare(left, right)
Compares two timezones. They are sorted by the location name.
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 |
- (TimeZone) find(name, dst_label = " (DST)")
Find a zone by its name.
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 |
- (String) format_offset(offset, colon = true)
Returns a +HH:MM formatted representation of the 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 |
- (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.
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 |
- (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"
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 |
- (Rational) rationalize_offset(offset)
Expression to parameterize a zone Returns an offset in rational value.
30 31 32 |
# File 'lib/lazier/timezone.rb', line 30 def rationalize_offset(offset) ::TZInfo::OffsetRationals.rational_for_offset(offset) end |
- (TimeZone) unparameterize(tz, dst_label = " (DST)")
Finds a parameterized timezone.
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 |