Class: TwitterCldr::Timezones::Timezone

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/twitter_cldr/timezones/timezone.rb

Constant Summary collapse

ALL_FORMATS =
(
  GenericLocation::FORMATS +
  GmtLocation::FORMATS +
  Iso8601Location::FORMATS +
  [:zone_id, :zone_id_short]
).freeze
GENERIC_TO_GMT_MAP =
{
  generic_location: :long_gmt,
  generic_short:    :short_gmt,
  generic_long:     :long_gmt,
  specific_short:   :short_gmt,
  specific_long:    :long_gmt
}.freeze
UNKNOWN =
'unk'.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(tz_id, locale) ⇒ Timezone

Returns a new instance of Timezone.



44
45
46
47
48
# File 'lib/twitter_cldr/timezones/timezone.rb', line 44

def initialize(tz_id, locale)
  @orig_tz = TZInfo::Timezone.get(tz_id)
  @tz = TZInfo::Timezone.get(ZoneMeta.normalize(tz_id))
  @locale = locale
end

Instance Attribute Details

#localeObject (readonly)

Returns the value of attribute locale.



42
43
44
# File 'lib/twitter_cldr/timezones/timezone.rb', line 42

def locale
  @locale
end

#orig_tzObject (readonly)

Returns the value of attribute orig_tz.



42
43
44
# File 'lib/twitter_cldr/timezones/timezone.rb', line 42

def orig_tz
  @orig_tz
end

#tzObject (readonly)

Returns the value of attribute tz.



42
43
44
# File 'lib/twitter_cldr/timezones/timezone.rb', line 42

def tz
  @tz
end

Class Method Details

.instance(tz_id, locale = TwitterCldr.locale) ⇒ Object



31
32
33
# File 'lib/twitter_cldr/timezones/timezone.rb', line 31

def instance(tz_id, locale = TwitterCldr.locale)
  cache["#{tz_id}:#{locale}"] ||= new(tz_id, locale)
end

Instance Method Details

#display_name_for(date, format = :generic_location, dst = TZInfo::Timezone.default_dst, &block) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/twitter_cldr/timezones/timezone.rb', line 50

def display_name_for(date, format = :generic_location, dst = TZInfo::Timezone.default_dst, &block)
  case format
    when *GenericLocation::FORMATS
      generic_location.display_name_for(date, format, dst, &block) ||
        gmt_location.display_name_for(date, GENERIC_TO_GMT_MAP[format], dst, &block)

    when *GmtLocation::FORMATS
      gmt_location.display_name_for(date, format, dst, &block)

    when *Iso8601Location::FORMATS
      iso_location.display_name_for(date, format, dst, &block)

    when :zone_id
      identifier

    when :zone_id_short
      ZoneMeta.short_name_for(identifier) || UNKNOWN

    else
      raise ArgumentError, "'#{format}' is not a valid timezone format, "\
        "must be one of #{ALL_FORMATS.join(', ')}"
  end
end

#generic_locationObject



98
99
100
# File 'lib/twitter_cldr/timezones/timezone.rb', line 98

def generic_location
  @generic_location ||= GenericLocation.new(self)
end

#gmt_locationObject



102
103
104
# File 'lib/twitter_cldr/timezones/timezone.rb', line 102

def gmt_location
  @gmt_location ||= GmtLocation.new(self)
end

#identifierObject



74
75
76
# File 'lib/twitter_cldr/timezones/timezone.rb', line 74

def identifier
  tz.identifier
end

#iso_locationObject



106
107
108
# File 'lib/twitter_cldr/timezones/timezone.rb', line 106

def iso_location
  @iso_location ||= Iso8601Location.new(self)
end

#max_localeObject



94
95
96
# File 'lib/twitter_cldr/timezones/timezone.rb', line 94

def max_locale
  @max_locale ||= orig_locale.maximize
end

#orig_localeObject



90
91
92
# File 'lib/twitter_cldr/timezones/timezone.rb', line 90

def orig_locale
  @orig_locale ||= TwitterCldr::Shared::Locale.new(locale)
end

#period_for_local(*args, &block) ⇒ Object



78
79
80
# File 'lib/twitter_cldr/timezones/timezone.rb', line 78

def period_for_local(*args, &block)
  tz.period_for_local(*args, &block)
end

#period_for_utc(time) ⇒ Object



82
83
84
# File 'lib/twitter_cldr/timezones/timezone.rb', line 82

def period_for_utc(time)
  tz.period_for_utc(time)
end

#transitions_up_to(date) ⇒ Object



86
87
88
# File 'lib/twitter_cldr/timezones/timezone.rb', line 86

def transitions_up_to(date)
  tz.transitions_up_to(date)
end