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 = nil)
Find a zone by its name.
-
- (String) format_offset(offset, colon = true)
Returns a +HH:MM formatted representation of the offset.
-
- (Array) list_all(with_dst = true, dst_label = nil)
Returns a list of names of all timezones.
-
- (String) parameterize_zone(tz, with_offset = true)
Returns a string representation of a timezone.
-
- (Rational) rationalize_offset(offset)
Returns an offset in rational value.
-
- (String|TimeZone) unparameterize_zone(tz, as_string = false, dst_label = nil)
Finds a parameterized timezone.
Instance Method Details
- (Fixnum) compare(left, right)
Compares two timezones. They are sorted by the location name.
109 110 111 112 113 |
# File 'lib/lazier/timezone.rb', line 109 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 = nil)
Find a zone by its name.
36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/lazier/timezone.rb', line 36 def find(name, dst_label = nil) catch(:zone) do ::ActiveSupport::TimeZone.all.each do |zone| zone.aliases.each do |zone_alias| throw(:zone, zone) if [zone.to_str(zone_alias), zone.to_str_with_dst(dst_label, nil, zone_alias)].include?(name) end end nil end end |
- (String) format_offset(offset, colon = true)
Returns a +HH:MM formatted representation of the offset.
27 28 29 |
# File 'lib/lazier/timezone.rb', line 27 def format_offset(offset, colon = true) seconds_to_utc_offset(offset.is_a?(::Rational) ? (offset * 86_400).to_i : offset, colon) end |
- (Array) list_all(with_dst = true, dst_label = nil)
Returns a list of names of all timezones.
53 54 55 56 57 58 59 60 61 62 |
# File 'lib/lazier/timezone.rb', line 53 def list_all(with_dst = true, dst_label = nil) dst_label ||= "(DST)" @zones_names ||= { "STANDARD" => ::ActiveSupport::TimeZone.all.map(&:to_s) } @zones_names["DST[#{dst_label}]-STANDARD"] ||= ::ActiveSupport::TimeZone.all .map { |zone| fetch_aliases(zone, dst_label) }.flatten.compact.uniq .sort { |a, b| ::ActiveSupport::TimeZone.compare(a, b) } # Sort by name @zones_names["#{with_dst ? "DST[#{dst_label}]-" : ""}STANDARD"] end |
- (String) parameterize_zone(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"
73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/lazier/timezone.rb', line 73 def parameterize_zone(tz, with_offset = true) tz = tz.to_s unless tz.is_a?(::String) mo = /^(\([a-z]+([+-])(\d{2})(:?)(\d{2})\)\s(.+))$/i.match(tz) if mo with_offset ? "#{mo[2]}#{mo[3]}#{mo[5]}@#{mo[6].to_s.parameterize}" : mo[6].to_s.parameterize elsif !with_offset then tz.gsub(/^([+-]?(\d{2})(:?)(\d{2})@)/, "") else tz.parameterize end end |
- (Rational) rationalize_offset(offset)
Returns an offset in rational value.
18 19 20 |
# File 'lib/lazier/timezone.rb', line 18 def rationalize_offset(offset) ::TZInfo::OffsetRationals.rational_for_offset(offset.is_a?(::Fixnum) ? offset : offset.offset) end |
- (String|TimeZone) unparameterize_zone(tz, as_string = false, dst_label = nil)
Finds a parameterized timezone.
93 94 95 96 97 98 99 100 101 102 |
# File 'lib/lazier/timezone.rb', line 93 def unparameterize_zone(tz, as_string = false, dst_label = nil) tz = parameterize_zone(tz, false) rv = find_parameterized_zone(dst_label, /(#{Regexp.quote(tz)})$/) if rv as_string ? rv : find(rv, dst_label) else nil end end |