Module: Cowtech::Extensions::TimeZone::ClassMethods
- Defined in:
- lib/cowtech-extensions/datetime.rb
Overview
General methods.
Instance Method Summary collapse
-
#compare(left, right) ⇒ Fixnum
Compares two timezones.
-
#find(name, dst_label = nil) ⇒ TimeZone
Find a zone by its name.
-
#format_offset(offset, colon = true) ⇒ String
Returns a +HH:MM formatted representation of the offset.
-
#list_all(with_dst = true, dst_label = nil) ⇒ Array
Returns a list of names of all timezones.
-
#parameterize_zone(tz, with_offset = true) ⇒ String
Returns a string representation of a timezone.
-
#rationalize_offset(offset) ⇒ Rational
Returns an offset in rational value.
-
#unparameterize_zone(tz, as_string = false, dst_label = nil) ⇒ String|TimeZone
Finds a parameterized timezone.
Instance Method Details
#compare(left, right) ⇒ Fixnum
Compares two timezones. They are sorted by the location name.
378 379 380 381 382 |
# File 'lib/cowtech-extensions/datetime.rb', line 378 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 = nil) ⇒ TimeZone
Find a zone by its name.
290 291 292 293 294 295 296 297 298 299 300 |
# File 'lib/cowtech-extensions/datetime.rb', line 290 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 |
#format_offset(offset, colon = true) ⇒ String
Returns a +HH:MM formatted representation of the offset.
280 281 282 283 |
# File 'lib/cowtech-extensions/datetime.rb', line 280 def format_offset(offset, colon = true) offset = (offset * 86400).to_i if offset.is_a?(::Rational) offset.is_a?(::Fixnum) ? self.seconds_to_utc_offset(offset, colon) : nil end |
#list_all(with_dst = true, dst_label = nil) ⇒ Array
Returns a list of names of all timezones.
307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 |
# File 'lib/cowtech-extensions/datetime.rb', line 307 def list_all(with_dst = true, dst_label = nil) dst_label ||= "(DST)" dst_key = "DST-#{dst_label}" @zones_names ||= { "STANDARD" => ::ActiveSupport::TimeZone.all.collect(&:to_s) } if with_dst && @zones_names[dst_key].blank? then @zones_names[dst_key] = [] ::ActiveSupport::TimeZone.all.each do |zone| zone.aliases.each do |zone_alias| @zones_names[dst_key] << zone.to_str(zone_alias) @zones_names[dst_key] << zone.to_str_with_dst(dst_label, nil, zone_alias) if zone.uses_dst? && zone_alias !~ /(#{Regexp.quote(dst_label)})$/ end end @zones_names[dst_key]= @zones_names[dst_key].uniq.compact.sort { |a,b| ::ActiveSupport::TimeZone.compare(a, b) } # Sort by name end @zones_names[with_dst ? dst_key : "STANDARD"] end |
#parameterize_zone(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"
337 338 339 340 341 342 343 344 345 346 347 |
# File 'lib/cowtech-extensions/datetime.rb', line 337 def parameterize_zone(tz, with_offset = true) tz = tz.to_s if !tz.is_a?(::String) if tz =~ /^(\([a-z]+([+-])(\d{2})(:?)(\d{2})\)\s(.+))$/i then with_offset ? "#{$2}#{$3}#{$5}@#{$6.parameterize}" : $6.parameterize elsif !with_offset then tz.gsub(/^([+-]?(\d{2})(:?)(\d{2})@)/, "") else tz.parameterize end end |
#rationalize_offset(offset) ⇒ Rational
Returns an offset in rational value.
270 271 272 273 |
# File 'lib/cowtech-extensions/datetime.rb', line 270 def rationalize_offset(offset) offset = offset.try(:offset) if !offset.is_a?(::Fixnum) ::TZInfo::OffsetRationals.rational_for_offset(offset.to_integer) end |
#unparameterize_zone(tz, as_string = false, dst_label = nil) ⇒ String|TimeZone
Finds a parameterized timezone.
356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 |
# File 'lib/cowtech-extensions/datetime.rb', line 356 def unparameterize_zone(tz, as_string = false, dst_label = nil) tz = self.parameterize_zone(tz, false) rv = catch(:zone) do self.list_all(true, dst_label).each do |zone| throw(:zone, zone) if self.parameterize_zone(zone, false) =~ /(#{Regexp.quote(tz)})$/ end nil end if rv then (as_string ? rv : self.find(rv, dst_label)) else nil end end |