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.
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.
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.
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.
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"
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.
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.
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 |