Class: TZInfo::CountryTimezone

Inherits:
Object
  • Object
show all
Defined in:
lib/tzinfo/country_timezone.rb

Overview

A Timezone within a Country. This contains extra information about the Timezone that is specific to the Country (a Timezone could be used by multiple countries).

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(identifier, latitude_numerator, latitude_denominator, longitude_numerator, longitude_denominator, description = nil) ⇒ CountryTimezone

Creates a new CountryTimezone with a timezone identifier, latitude, longitude and description. The latitude and longitude are specified as rationals - a numerator and denominator. For performance reasons, the numerators and denominators must be specified in their lowest form.

CountryTimezone instances should not normally be constructed manually.



42
43
44
45
46
47
48
49
50
# File 'lib/tzinfo/country_timezone.rb', line 42

def initialize(identifier, latitude_numerator, latitude_denominator, 
               longitude_numerator, longitude_denominator, description = nil) #:nodoc:
  @identifier = identifier
  @latitude_numerator = latitude_numerator
  @latitude_denominator = latitude_denominator
  @longitude_numerator = longitude_numerator
  @longitude_denominator = longitude_denominator      
  @description = description
end

Instance Attribute Details

#descriptionObject (readonly)

A description of this timezone in relation to the country, e.g. “Eastern Time”. This is usually nil for countries having only a single Timezone.



34
35
36
# File 'lib/tzinfo/country_timezone.rb', line 34

def description
  @description
end

#identifierObject (readonly)

The zone identifier.



29
30
31
# File 'lib/tzinfo/country_timezone.rb', line 29

def identifier
  @identifier
end

Instance Method Details

#==(ct) ⇒ Object

Returns true if and only if the given CountryTimezone is equal to the current CountryTimezone (has the same identifer, latitude, longitude and description).



84
85
86
87
88
# File 'lib/tzinfo/country_timezone.rb', line 84

def ==(ct)
  ct.kind_of?(CountryTimezone) &&
    identifier == ct.identifier  && latitude == ct.latitude &&
    longitude == ct.longitude   && description == ct.description         
end

#description_or_friendly_identifierObject

if description is not nil, this method returns description; otherwise it returns timezone.friendly_identifier(true).



59
60
61
# File 'lib/tzinfo/country_timezone.rb', line 59

def description_or_friendly_identifier
  description || timezone.friendly_identifier(true)
end

#eql?(ct) ⇒ Boolean

Returns true if and only if the given CountryTimezone is equal to the current CountryTimezone (has the same identifer, latitude, longitude and description).

Returns:

  • (Boolean)


93
94
95
# File 'lib/tzinfo/country_timezone.rb', line 93

def eql?(ct)
  self == ct
end

#hashObject

Returns a hash of this CountryTimezone.



98
99
100
101
# File 'lib/tzinfo/country_timezone.rb', line 98

def hash
  @identifier.hash ^ @latitude_numerator.hash ^ @latitude_denominator.hash ^ 
    @longitude_numerator.hash ^ @longitude_denominator.hash ^ @description.hash
end

#inspectObject

Returns internal object state as a programmer-readable string.



104
105
106
# File 'lib/tzinfo/country_timezone.rb', line 104

def inspect
  "#<#{self.class}: #@identifier>"
end

#latitudeObject

The latitude of this timezone in degrees as a Rational.



64
65
66
67
68
69
70
# File 'lib/tzinfo/country_timezone.rb', line 64

def latitude
  # Thread-safey: It is possible that the value of @latitude may be 
  # calculated multiple times in concurrently executing threads. It is not 
  # worth the overhead of locking to ensure that @latitude is only 
  # calculated once.
  @latitude ||= RubyCoreSupport.rational_new!(@latitude_numerator, @latitude_denominator)
end

#longitudeObject

The longitude of this timezone in degrees as a Rational.



73
74
75
76
77
78
79
# File 'lib/tzinfo/country_timezone.rb', line 73

def longitude
  # Thread-safey: It is possible that the value of @longitude may be 
  # calculated multiple times in concurrently executing threads. It is not 
  # worth the overhead of locking to ensure that @longitude is only 
  # calculated once.
  @longitude ||= RubyCoreSupport.rational_new!(@longitude_numerator, @longitude_denominator)
end

#timezoneObject

The Timezone (actually a TimezoneProxy for performance reasons).



53
54
55
# File 'lib/tzinfo/country_timezone.rb', line 53

def timezone
  Timezone.get_proxy(@identifier)
end