class TwitterCldr::Timezones::GmtLocation

Constants

DEFAULT_FORMAT
DEFAULT_GMT_ZERO_FORMAT
FORMATS

Public Instance Methods

display_name_for(date, format = DEFAULT_FORMAT) click to toggle source
# File lib/twitter_cldr/timezones/gmt_location.rb, line 13
def display_name_for(date, format = DEFAULT_FORMAT)
  offset = tz.period_for_local(date).offset
  offset_secs = offset.utc_offset + offset.std_offset
  return gmt_zero_format if offset_secs == 0

  gmt_format.sub('{0}', format_offset(offset_secs, format))
end

Private Instance Methods

format_offset(offset_secs, format) click to toggle source
# File lib/twitter_cldr/timezones/gmt_location.rb, line 23
def format_offset(offset_secs, format)
  positive = offset_secs >= 0
  offset_secs = offset_secs.abs
  offset_hour ||= offset_secs / 60 / 60
  offset_min ||= (offset_secs / 60) % 60
  offset_sec ||= offset_secs % 60

  tokens = hour_format(positive ? :positive : :negative)
  format_tokens(tokens, format, offset_hour, offset_min, offset_sec)
end
format_tokens(tokens, format, hour, min, sec) click to toggle source
# File lib/twitter_cldr/timezones/gmt_location.rb, line 34
def format_tokens(tokens, format, hour, min, sec)
  ''.tap do |result|
    tokens.each do |token|
      case token.type
        when :plaintext
          result << token.value
        when :pattern
          case token.value[0]
            when 'H'
              result << offset_digits(hour, format == :short_gmt ? 1 : 2)
              break if min == 0 && sec == 0 && format == :short_gmt
            when 'm'
              result << offset_digits(min, 2)
              break if sec == 0 && format == :short_gmt
            when 's'
              result << offset_digits(sec, 2)
          end
      end
    end
  end
end
gmt_format() click to toggle source
# File lib/twitter_cldr/timezones/gmt_location.rb, line 86
def gmt_format
  @gmt_format ||= resource[:formats][:gmt_formats][:generic]
end
gmt_zero_format() click to toggle source
# File lib/twitter_cldr/timezones/gmt_location.rb, line 81
def gmt_zero_format
  @gmt_zero_format ||= resource[:formats][:gmt_zero_formats][:generic] ||
    DEFAULT_GMT_ZERO_FORMAT
end
hour_format(type) click to toggle source
# File lib/twitter_cldr/timezones/gmt_location.rb, line 64
def hour_format(type)
  case type
    when :positive
      hour_formats.first
    else
      hour_formats.last
  end
end
hour_formats() click to toggle source
# File lib/twitter_cldr/timezones/gmt_location.rb, line 73
def hour_formats
  @hour_formats ||= resource[:formats][:hour_formats][:generic]
    .split(';')
    .map do |pat|
      TwitterCldr::Tokenizers::TimeTokenizer.tokenizer.tokenize(pat)
    end
end
number_system() click to toggle source
# File lib/twitter_cldr/timezones/gmt_location.rb, line 60
def number_system
  @number_system ||= TwitterCldr::Shared::NumberingSystem.for_locale(tz.locale)
end
offset_digits(n, min_digits) click to toggle source
# File lib/twitter_cldr/timezones/gmt_location.rb, line 56
def offset_digits(n, min_digits)
  number_system.transliterate(n.to_s.rjust(min_digits, '0'))
end