Module: Lazier::TimeZone::ClassMethods

Defined in:
lib/lazier/datetime.rb

Overview

General methods.

Instance Method Summary collapse

Instance Method Details

#compare(left, right) ⇒ Fixnum

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

Parameters:

Returns:

  • (Fixnum)

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



391
392
393
394
395
# File 'lib/lazier/datetime.rb', line 391

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.

Parameters:

  • name (String)

    The zone name.

  • dst_label (String) (defaults to: nil)

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

Returns:

  • (TimeZone)

    A timezone or nil if no zone was found.



315
316
317
318
319
320
321
322
323
324
325
# File 'lib/lazier/datetime.rb', line 315

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.

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.



305
306
307
308
# File 'lib/lazier/datetime.rb', line 305

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.

Parameters:

  • with_dst (Boolean) (defaults to: true)

    If include DST version of the zones.

  • dst_label (String) (defaults to: nil)

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

Returns:

  • (Array)

    A list of names of timezones.



332
333
334
335
336
337
338
339
# File 'lib/lazier/datetime.rb', line 332

def list_all(with_dst = true, dst_label = nil)
  dst_label ||= "(DST)"

  @zones_names ||= { "STANDARD" => ::ActiveSupport::TimeZone.all.collect(&:to_s) }
  @zones_names["DST[#{dst_label}]-STANDARD"] ||= ::ActiveSupport::TimeZone.all.collect { |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

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

Parameters:

  • tz (TimeZone)

    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.



350
351
352
353
354
355
356
357
358
359
360
# File 'lib/lazier/datetime.rb', line 350

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.

Parameters:

  • offset (Fixnum)

    The offset to convert.

Returns:

  • (Rational)

    The converted offset.



295
296
297
298
# File 'lib/lazier/datetime.rb', line 295

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.

Parameters:

  • tz (String)

    The zone to unparameterize.

  • as_string (Boolean) (defaults to: false)

    If return just the zone name.

  • dst_label (String) (defaults to: nil)

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

Returns:

  • (String|TimeZone)

    The found timezone or nil if the zone is not valid.

See Also:

  • DateTime#parameterize_zone


369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
# File 'lib/lazier/datetime.rb', line 369

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