/
country_timezone.rb
93 lines (83 loc) · 3.4 KB
/
country_timezone.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# encoding: UTF-8
module TZInfo
# Information about a time zone used by a {Country}.
class CountryTimezone
# @return [String] the identifier of the {Timezone} being described.
attr_reader :identifier
# The latitude of this time zone in degrees. Positive numbers are degrees
# north and negative numbers are degrees south.
#
# Note that depending on the data source, the position given by {#latitude}
# and {#longitude} may not be within the country.
#
# @return [Rational] the latitude in degrees.
attr_reader :latitude
# The longitude of this time zone in degrees. Positive numbers are degrees
# east and negative numbers are degrees west.
#
# Note that depending on the data source, the position given by {#latitude}
# and {#longitude} may not be within the country.
#
# @return [Rational] the longitude in degrees.
attr_reader :longitude
# A description of this time zone in relation to the country, e.g. "Eastern
# Time". This is usually `nil` for countries that have a single time zone.
#
# @return [String] an optional description of the time zone.
attr_reader :description
# Creates a new {CountryTimezone}.
#
# The passed in identifier and description instances will be frozen.
#
# {CountryTimezone} instances should normally only be constructed
# by implementations of {DataSource}.
#
# @param identifier [String] the {Timezone} identifier.
# @param latitude [Rational] the latitude of the time zone.
# @param longitude [Rational] the longitude of the time zone.
# @param description [String] an optional description of the time zone.
def initialize(identifier, latitude, longitude, description = nil)
@identifier = identifier.freeze
@latitude = latitude
@longitude = longitude
@description = description && description.freeze
end
# Returns the associated {Timezone}.
#
# The result is actually an instance of {TimezoneProxy} in order to defer
# loading of the time zone transition data until it is first needed.
#
# @return [Timezone] the associated {Timezone}.
def timezone
Timezone.get_proxy(@identifier)
end
# @return [String] the {description} if present, otherwise a human-readable
# representation of the identifier (using {Timezone#friendly_identifier}).
def description_or_friendly_identifier
description || timezone.friendly_identifier(true)
end
# Tests if the given object is equal to the current instance (has the same
# identifier, latitude, longitude and description).
#
# @param ct [Object] the object to be compared.
# @return [TrueClass] `true` if `ct` is equal to the current instance.
def ==(ct)
ct.kind_of?(CountryTimezone) &&
identifier == ct.identifier && latitude == ct.latitude &&
longitude == ct.longitude && description == ct.description
end
# Tests if the given object is equal to the current instance (has the same
# identifier, latitude, longitude and description).
#
# @param ct [Object] the object to be compared.
# @return [Boolean] `true` if `ct` is equal to the current instance.
def eql?(ct)
self == ct
end
# @return [Integer] a hash based on the {identifier}, {latitude},
# {longitude} and {description}.
def hash
[@identifier, @latitude, @longitude, @description].hash
end
end
end