tzinfo 1.2.3 → 2.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (144) hide show
  1. checksums.yaml +5 -5
  2. checksums.yaml.gz.sig +0 -0
  3. data.tar.gz.sig +0 -0
  4. data/.yardopts +3 -0
  5. data/CHANGES.md +501 -372
  6. data/LICENSE +13 -13
  7. data/README.md +368 -113
  8. data/lib/tzinfo.rb +64 -29
  9. data/lib/tzinfo/country.rb +141 -129
  10. data/lib/tzinfo/country_timezone.rb +71 -101
  11. data/lib/tzinfo/data_source.rb +389 -144
  12. data/lib/tzinfo/data_sources.rb +8 -0
  13. data/lib/tzinfo/data_sources/constant_offset_data_timezone_info.rb +56 -0
  14. data/lib/tzinfo/data_sources/country_info.rb +42 -0
  15. data/lib/tzinfo/data_sources/data_timezone_info.rb +91 -0
  16. data/lib/tzinfo/data_sources/linked_timezone_info.rb +33 -0
  17. data/lib/tzinfo/data_sources/ruby_data_source.rb +143 -0
  18. data/lib/tzinfo/data_sources/timezone_info.rb +47 -0
  19. data/lib/tzinfo/data_sources/transitions_data_timezone_info.rb +214 -0
  20. data/lib/tzinfo/data_sources/zoneinfo_data_source.rb +575 -0
  21. data/lib/tzinfo/data_sources/zoneinfo_reader.rb +286 -0
  22. data/lib/tzinfo/data_timezone.rb +33 -47
  23. data/lib/tzinfo/datetime_with_offset.rb +153 -0
  24. data/lib/tzinfo/format1.rb +10 -0
  25. data/lib/tzinfo/format1/country_definer.rb +17 -0
  26. data/lib/tzinfo/format1/country_index_definition.rb +64 -0
  27. data/lib/tzinfo/format1/timezone_definer.rb +64 -0
  28. data/lib/tzinfo/format1/timezone_definition.rb +39 -0
  29. data/lib/tzinfo/format1/timezone_index_definition.rb +77 -0
  30. data/lib/tzinfo/format2.rb +10 -0
  31. data/lib/tzinfo/format2/country_definer.rb +68 -0
  32. data/lib/tzinfo/format2/country_index_definer.rb +68 -0
  33. data/lib/tzinfo/format2/country_index_definition.rb +46 -0
  34. data/lib/tzinfo/format2/timezone_definer.rb +94 -0
  35. data/lib/tzinfo/format2/timezone_definition.rb +73 -0
  36. data/lib/tzinfo/format2/timezone_index_definer.rb +45 -0
  37. data/lib/tzinfo/format2/timezone_index_definition.rb +55 -0
  38. data/lib/tzinfo/info_timezone.rb +26 -21
  39. data/lib/tzinfo/linked_timezone.rb +33 -52
  40. data/lib/tzinfo/offset_timezone_period.rb +42 -0
  41. data/lib/tzinfo/string_deduper.rb +118 -0
  42. data/lib/tzinfo/time_with_offset.rb +128 -0
  43. data/lib/tzinfo/timestamp.rb +548 -0
  44. data/lib/tzinfo/timestamp_with_offset.rb +85 -0
  45. data/lib/tzinfo/timezone.rb +989 -498
  46. data/lib/tzinfo/timezone_offset.rb +90 -57
  47. data/lib/tzinfo/timezone_period.rb +155 -197
  48. data/lib/tzinfo/timezone_proxy.rb +71 -74
  49. data/lib/tzinfo/timezone_transition.rb +77 -99
  50. data/lib/tzinfo/transitions_timezone_period.rb +63 -0
  51. data/lib/tzinfo/untaint_ext.rb +18 -0
  52. data/lib/tzinfo/version.rb +7 -0
  53. data/lib/tzinfo/with_offset.rb +61 -0
  54. metadata +63 -120
  55. metadata.gz.sig +0 -0
  56. data/Rakefile +0 -107
  57. data/lib/tzinfo/country_index_definition.rb +0 -31
  58. data/lib/tzinfo/country_info.rb +0 -42
  59. data/lib/tzinfo/data_timezone_info.rb +0 -55
  60. data/lib/tzinfo/linked_timezone_info.rb +0 -26
  61. data/lib/tzinfo/offset_rationals.rb +0 -77
  62. data/lib/tzinfo/ruby_core_support.rb +0 -146
  63. data/lib/tzinfo/ruby_country_info.rb +0 -70
  64. data/lib/tzinfo/ruby_data_source.rb +0 -136
  65. data/lib/tzinfo/time_or_datetime.rb +0 -333
  66. data/lib/tzinfo/timezone_definition.rb +0 -36
  67. data/lib/tzinfo/timezone_index_definition.rb +0 -54
  68. data/lib/tzinfo/timezone_info.rb +0 -30
  69. data/lib/tzinfo/timezone_transition_definition.rb +0 -101
  70. data/lib/tzinfo/transition_data_timezone_info.rb +0 -274
  71. data/lib/tzinfo/zoneinfo_country_info.rb +0 -35
  72. data/lib/tzinfo/zoneinfo_data_source.rb +0 -487
  73. data/lib/tzinfo/zoneinfo_timezone_info.rb +0 -294
  74. data/test/tc_country.rb +0 -236
  75. data/test/tc_country_index_definition.rb +0 -69
  76. data/test/tc_country_info.rb +0 -16
  77. data/test/tc_country_timezone.rb +0 -161
  78. data/test/tc_data_source.rb +0 -218
  79. data/test/tc_data_timezone.rb +0 -99
  80. data/test/tc_data_timezone_info.rb +0 -18
  81. data/test/tc_info_timezone.rb +0 -34
  82. data/test/tc_linked_timezone.rb +0 -155
  83. data/test/tc_linked_timezone_info.rb +0 -23
  84. data/test/tc_offset_rationals.rb +0 -23
  85. data/test/tc_ruby_core_support.rb +0 -168
  86. data/test/tc_ruby_country_info.rb +0 -80
  87. data/test/tc_ruby_data_source.rb +0 -143
  88. data/test/tc_time_or_datetime.rb +0 -639
  89. data/test/tc_timezone.rb +0 -1350
  90. data/test/tc_timezone_definition.rb +0 -113
  91. data/test/tc_timezone_index_definition.rb +0 -73
  92. data/test/tc_timezone_info.rb +0 -11
  93. data/test/tc_timezone_london.rb +0 -143
  94. data/test/tc_timezone_melbourne.rb +0 -142
  95. data/test/tc_timezone_new_york.rb +0 -142
  96. data/test/tc_timezone_offset.rb +0 -126
  97. data/test/tc_timezone_period.rb +0 -548
  98. data/test/tc_timezone_proxy.rb +0 -127
  99. data/test/tc_timezone_transition.rb +0 -352
  100. data/test/tc_timezone_transition_definition.rb +0 -284
  101. data/test/tc_timezone_utc.rb +0 -27
  102. data/test/tc_transition_data_timezone_info.rb +0 -423
  103. data/test/tc_zoneinfo_country_info.rb +0 -64
  104. data/test/tc_zoneinfo_data_source.rb +0 -1193
  105. data/test/tc_zoneinfo_timezone_info.rb +0 -1128
  106. data/test/test_utils.rb +0 -134
  107. data/test/ts_all.rb +0 -7
  108. data/test/ts_all_ruby.rb +0 -5
  109. data/test/ts_all_zoneinfo.rb +0 -7
  110. data/test/tzinfo-data/tzinfo/data.rb +0 -8
  111. data/test/tzinfo-data/tzinfo/data/definitions/America/Argentina/Buenos_Aires.rb +0 -89
  112. data/test/tzinfo-data/tzinfo/data/definitions/America/New_York.rb +0 -315
  113. data/test/tzinfo-data/tzinfo/data/definitions/Australia/Melbourne.rb +0 -218
  114. data/test/tzinfo-data/tzinfo/data/definitions/EST.rb +0 -19
  115. data/test/tzinfo-data/tzinfo/data/definitions/Etc/GMT__m__1.rb +0 -21
  116. data/test/tzinfo-data/tzinfo/data/definitions/Etc/GMT__p__1.rb +0 -21
  117. data/test/tzinfo-data/tzinfo/data/definitions/Etc/UTC.rb +0 -21
  118. data/test/tzinfo-data/tzinfo/data/definitions/Europe/Amsterdam.rb +0 -261
  119. data/test/tzinfo-data/tzinfo/data/definitions/Europe/Andorra.rb +0 -186
  120. data/test/tzinfo-data/tzinfo/data/definitions/Europe/London.rb +0 -321
  121. data/test/tzinfo-data/tzinfo/data/definitions/Europe/Paris.rb +0 -265
  122. data/test/tzinfo-data/tzinfo/data/definitions/Europe/Prague.rb +0 -220
  123. data/test/tzinfo-data/tzinfo/data/definitions/UTC.rb +0 -16
  124. data/test/tzinfo-data/tzinfo/data/indexes/countries.rb +0 -927
  125. data/test/tzinfo-data/tzinfo/data/indexes/timezones.rb +0 -596
  126. data/test/tzinfo-data/tzinfo/data/version.rb +0 -14
  127. data/test/zoneinfo/America/Argentina/Buenos_Aires +0 -0
  128. data/test/zoneinfo/America/New_York +0 -0
  129. data/test/zoneinfo/Australia/Melbourne +0 -0
  130. data/test/zoneinfo/EST +0 -0
  131. data/test/zoneinfo/Etc/UTC +0 -0
  132. data/test/zoneinfo/Europe/Amsterdam +0 -0
  133. data/test/zoneinfo/Europe/Andorra +0 -0
  134. data/test/zoneinfo/Europe/London +0 -0
  135. data/test/zoneinfo/Europe/Paris +0 -0
  136. data/test/zoneinfo/Europe/Prague +0 -0
  137. data/test/zoneinfo/Factory +0 -0
  138. data/test/zoneinfo/iso3166.tab +0 -275
  139. data/test/zoneinfo/posix/Europe/London +0 -0
  140. data/test/zoneinfo/posixrules +0 -0
  141. data/test/zoneinfo/right/Europe/London +0 -0
  142. data/test/zoneinfo/zone.tab +0 -439
  143. data/test/zoneinfo/zone1970.tab +0 -369
  144. data/tzinfo.gemspec +0 -21
@@ -1,42 +0,0 @@
1
- module TZInfo
2
- # Represents a country and references to its timezones as returned by a
3
- # DataSource.
4
- class CountryInfo
5
- # The ISO 3166 country code.
6
- attr_reader :code
7
-
8
- # The name of the country.
9
- attr_reader :name
10
-
11
- # Constructs a new CountryInfo with an ISO 3166 country code and name
12
- def initialize(code, name)
13
- @code = code
14
- @name = name
15
- end
16
-
17
- # Returns internal object state as a programmer-readable string.
18
- def inspect
19
- "#<#{self.class}: #@code>"
20
- end
21
-
22
- # Returns a frozen array of all the zone identifiers for the country.
23
- # The identifiers are ordered by importance according to the DataSource.
24
- def zone_identifiers
25
- raise_not_implemented('zone_identifiers')
26
- end
27
-
28
- # Returns a frozen array of all the timezones for the for the country as
29
- # CountryTimezone instances.
30
- #
31
- # The timezones are ordered by importance according to the DataSource.
32
- def zones
33
- raise_not_implemented('zones')
34
- end
35
-
36
- private
37
-
38
- def raise_not_implemented(method_name)
39
- raise NotImplementedError, "Subclasses must override #{method_name}"
40
- end
41
- end
42
- end
@@ -1,55 +0,0 @@
1
- module TZInfo
2
- # Represents a defined timezone containing transition data.
3
- class DataTimezoneInfo < TimezoneInfo
4
-
5
- # Returns the TimezonePeriod for the given UTC time.
6
- def period_for_utc(utc)
7
- raise_not_implemented('period_for_utc')
8
- end
9
-
10
- # Returns the set of TimezonePeriods for the given local time as an array.
11
- # Results returned are ordered by increasing UTC start date.
12
- # Returns an empty array if no periods are found for the given time.
13
- def periods_for_local(local)
14
- raise_not_implemented('periods_for_local')
15
- end
16
-
17
- # Returns an Array of TimezoneTransition instances representing the times
18
- # where the UTC offset of the timezone changes.
19
- #
20
- # Transitions are returned up to a given date and time up to a given date
21
- # and time, specified in UTC (utc_to).
22
- #
23
- # A from date and time may also be supplied using the utc_from parameter
24
- # (also specified in UTC). If utc_from is not nil, only transitions from
25
- # that date and time onwards will be returned.
26
- #
27
- # Comparisons with utc_to are exclusive. Comparisons with utc_from are
28
- # inclusive. If a transition falls precisely on utc_to, it will be excluded.
29
- # If a transition falls on utc_from, it will be included.
30
- #
31
- # Transitions returned are ordered by when they occur, from earliest to
32
- # latest.
33
- #
34
- # utc_to and utc_from can be specified using either DateTime, Time or
35
- # integer timestamps (Time.to_i).
36
- #
37
- # If utc_from is specified and utc_to is not greater than utc_from, then
38
- # transitions_up_to raises an ArgumentError exception.
39
- def transitions_up_to(utc_to, utc_from = nil)
40
- raise_not_implemented('transitions_up_to')
41
- end
42
-
43
- # Constructs a Timezone instance for the timezone represented by this
44
- # DataTimezoneInfo.
45
- def create_timezone
46
- DataTimezone.new(self)
47
- end
48
-
49
- private
50
-
51
- def raise_not_implemented(method_name)
52
- raise NotImplementedError, "Subclasses must override #{method_name}"
53
- end
54
- end
55
- end
@@ -1,26 +0,0 @@
1
- module TZInfo
2
- # Represents a timezone that is defined as a link or alias to another zone.
3
- class LinkedTimezoneInfo < TimezoneInfo
4
-
5
- # The zone that provides the data (that this zone is an alias for).
6
- attr_reader :link_to_identifier
7
-
8
- # Constructs a new LinkedTimezoneInfo with an identifier and the identifier
9
- # of the zone linked to.
10
- def initialize(identifier, link_to_identifier)
11
- super(identifier)
12
- @link_to_identifier = link_to_identifier
13
- end
14
-
15
- # Returns internal object state as a programmer-readable string.
16
- def inspect
17
- "#<#{self.class}: #@identifier,#@link_to_identifier>"
18
- end
19
-
20
- # Constructs a Timezone instance for the timezone represented by this
21
- # DataTimezoneInfo.
22
- def create_timezone
23
- LinkedTimezone.new(self)
24
- end
25
- end
26
- end
@@ -1,77 +0,0 @@
1
- require 'rational' unless defined?(Rational)
2
-
3
- module TZInfo
4
-
5
- # Provides a method for getting Rationals for a timezone offset in seconds.
6
- # Pre-reduced rationals are returned for all the half-hour intervals between
7
- # -14 and +14 hours to avoid having to call gcd at runtime.
8
- #
9
- # @private
10
- module OffsetRationals #:nodoc:
11
- @@rational_cache = {
12
- -50400 => RubyCoreSupport.rational_new!(-7,12),
13
- -48600 => RubyCoreSupport.rational_new!(-9,16),
14
- -46800 => RubyCoreSupport.rational_new!(-13,24),
15
- -45000 => RubyCoreSupport.rational_new!(-25,48),
16
- -43200 => RubyCoreSupport.rational_new!(-1,2),
17
- -41400 => RubyCoreSupport.rational_new!(-23,48),
18
- -39600 => RubyCoreSupport.rational_new!(-11,24),
19
- -37800 => RubyCoreSupport.rational_new!(-7,16),
20
- -36000 => RubyCoreSupport.rational_new!(-5,12),
21
- -34200 => RubyCoreSupport.rational_new!(-19,48),
22
- -32400 => RubyCoreSupport.rational_new!(-3,8),
23
- -30600 => RubyCoreSupport.rational_new!(-17,48),
24
- -28800 => RubyCoreSupport.rational_new!(-1,3),
25
- -27000 => RubyCoreSupport.rational_new!(-5,16),
26
- -25200 => RubyCoreSupport.rational_new!(-7,24),
27
- -23400 => RubyCoreSupport.rational_new!(-13,48),
28
- -21600 => RubyCoreSupport.rational_new!(-1,4),
29
- -19800 => RubyCoreSupport.rational_new!(-11,48),
30
- -18000 => RubyCoreSupport.rational_new!(-5,24),
31
- -16200 => RubyCoreSupport.rational_new!(-3,16),
32
- -14400 => RubyCoreSupport.rational_new!(-1,6),
33
- -12600 => RubyCoreSupport.rational_new!(-7,48),
34
- -10800 => RubyCoreSupport.rational_new!(-1,8),
35
- -9000 => RubyCoreSupport.rational_new!(-5,48),
36
- -7200 => RubyCoreSupport.rational_new!(-1,12),
37
- -5400 => RubyCoreSupport.rational_new!(-1,16),
38
- -3600 => RubyCoreSupport.rational_new!(-1,24),
39
- -1800 => RubyCoreSupport.rational_new!(-1,48),
40
- 0 => RubyCoreSupport.rational_new!(0,1),
41
- 1800 => RubyCoreSupport.rational_new!(1,48),
42
- 3600 => RubyCoreSupport.rational_new!(1,24),
43
- 5400 => RubyCoreSupport.rational_new!(1,16),
44
- 7200 => RubyCoreSupport.rational_new!(1,12),
45
- 9000 => RubyCoreSupport.rational_new!(5,48),
46
- 10800 => RubyCoreSupport.rational_new!(1,8),
47
- 12600 => RubyCoreSupport.rational_new!(7,48),
48
- 14400 => RubyCoreSupport.rational_new!(1,6),
49
- 16200 => RubyCoreSupport.rational_new!(3,16),
50
- 18000 => RubyCoreSupport.rational_new!(5,24),
51
- 19800 => RubyCoreSupport.rational_new!(11,48),
52
- 21600 => RubyCoreSupport.rational_new!(1,4),
53
- 23400 => RubyCoreSupport.rational_new!(13,48),
54
- 25200 => RubyCoreSupport.rational_new!(7,24),
55
- 27000 => RubyCoreSupport.rational_new!(5,16),
56
- 28800 => RubyCoreSupport.rational_new!(1,3),
57
- 30600 => RubyCoreSupport.rational_new!(17,48),
58
- 32400 => RubyCoreSupport.rational_new!(3,8),
59
- 34200 => RubyCoreSupport.rational_new!(19,48),
60
- 36000 => RubyCoreSupport.rational_new!(5,12),
61
- 37800 => RubyCoreSupport.rational_new!(7,16),
62
- 39600 => RubyCoreSupport.rational_new!(11,24),
63
- 41400 => RubyCoreSupport.rational_new!(23,48),
64
- 43200 => RubyCoreSupport.rational_new!(1,2),
65
- 45000 => RubyCoreSupport.rational_new!(25,48),
66
- 46800 => RubyCoreSupport.rational_new!(13,24),
67
- 48600 => RubyCoreSupport.rational_new!(9,16),
68
- 50400 => RubyCoreSupport.rational_new!(7,12)}.freeze
69
-
70
- # Returns a Rational expressing the fraction of a day that offset in
71
- # seconds represents (i.e. equivalent to Rational(offset, 86400)).
72
- def rational_for_offset(offset)
73
- @@rational_cache[offset] || Rational(offset, 86400)
74
- end
75
- module_function :rational_for_offset
76
- end
77
- end
@@ -1,146 +0,0 @@
1
- require 'date'
2
- require 'rational' unless defined?(Rational)
3
-
4
- module TZInfo
5
-
6
- # Methods to support different versions of Ruby.
7
- #
8
- # @private
9
- module RubyCoreSupport #:nodoc:
10
-
11
- # Use Rational.new! for performance reasons in Ruby 1.8.
12
- # This has been removed from 1.9, but Rational performs better.
13
- if Rational.respond_to? :new!
14
- def self.rational_new!(numerator, denominator = 1)
15
- Rational.new!(numerator, denominator)
16
- end
17
- else
18
- def self.rational_new!(numerator, denominator = 1)
19
- Rational(numerator, denominator)
20
- end
21
- end
22
-
23
- # Ruby 1.8.6 introduced new! and deprecated new0.
24
- # Ruby 1.9.0 removed new0.
25
- # Ruby trunk revision 31668 removed the new! method.
26
- # Still support new0 for better performance on older versions of Ruby (new0 indicates
27
- # that the rational has already been reduced to its lowest terms).
28
- # Fallback to jd with conversion from ajd if new! and new0 are unavailable.
29
- if DateTime.respond_to? :new!
30
- def self.datetime_new!(ajd = 0, of = 0, sg = Date::ITALY)
31
- DateTime.new!(ajd, of, sg)
32
- end
33
- elsif DateTime.respond_to? :new0
34
- def self.datetime_new!(ajd = 0, of = 0, sg = Date::ITALY)
35
- DateTime.new0(ajd, of, sg)
36
- end
37
- else
38
- HALF_DAYS_IN_DAY = rational_new!(1, 2)
39
-
40
- def self.datetime_new!(ajd = 0, of = 0, sg = Date::ITALY)
41
- # Convert from an Astronomical Julian Day number to a civil Julian Day number.
42
- jd = ajd + of + HALF_DAYS_IN_DAY
43
-
44
- # Ruby trunk revision 31862 changed the behaviour of DateTime.jd so that it will no
45
- # longer accept a fractional civil Julian Day number if further arguments are specified.
46
- # Calculate the hours, minutes and seconds to pass to jd.
47
-
48
- jd_i = jd.to_i
49
- jd_i -= 1 if jd < 0
50
- hours = (jd - jd_i) * 24
51
- hours_i = hours.to_i
52
- minutes = (hours - hours_i) * 60
53
- minutes_i = minutes.to_i
54
- seconds = (minutes - minutes_i) * 60
55
-
56
- DateTime.jd(jd_i, hours_i, minutes_i, seconds, of, sg)
57
- end
58
- end
59
-
60
- # DateTime in Ruby 1.8.6 doesn't consider times within the 60th second to be
61
- # valid. When attempting to specify such a DateTime, subtract the fractional
62
- # part and then add it back later
63
- if Date.respond_to?(:valid_time?) && !Date.valid_time?(0, 0, rational_new!(59001, 1000)) # 0:0:59.001
64
- def self.datetime_new(y=-4712, m=1, d=1, h=0, min=0, s=0, of=0, sg=Date::ITALY)
65
- if !s.kind_of?(Integer) && s > 59
66
- dt = DateTime.new(y, m, d, h, min, 59, of, sg)
67
- dt + (s - 59) / 86400
68
- else
69
- DateTime.new(y, m, d, h, min, s, of, sg)
70
- end
71
- end
72
- else
73
- def self.datetime_new(y=-4712, m=1, d=1, h=0, min=0, s=0, of=0, sg=Date::ITALY)
74
- DateTime.new(y, m, d, h, min, s, of, sg)
75
- end
76
- end
77
-
78
- # Returns true if Time on the runtime platform supports Times defined
79
- # by negative 32-bit timestamps, otherwise false.
80
- begin
81
- Time.at(-1)
82
- Time.at(-2147483648)
83
-
84
- def self.time_supports_negative
85
- true
86
- end
87
- rescue ArgumentError
88
- def self.time_supports_negative
89
- false
90
- end
91
- end
92
-
93
- # Returns true if Time on the runtime platform supports Times defined by
94
- # 64-bit timestamps, otherwise false.
95
- begin
96
- Time.at(-2147483649)
97
- Time.at(2147483648)
98
-
99
- def self.time_supports_64bit
100
- true
101
- end
102
- rescue RangeError
103
- def self.time_supports_64bit
104
- false
105
- end
106
- end
107
-
108
- # Return the result of Time#nsec if it exists, otherwise return the
109
- # result of Time#usec * 1000.
110
- if Time.method_defined?(:nsec)
111
- def self.time_nsec(time)
112
- time.nsec
113
- end
114
- else
115
- def self.time_nsec(time)
116
- time.usec * 1000
117
- end
118
- end
119
-
120
- # Call String#force_encoding if this version of Ruby has encoding support
121
- # otherwise treat as a no-op.
122
- if String.method_defined?(:force_encoding)
123
- def self.force_encoding(str, encoding)
124
- str.force_encoding(encoding)
125
- end
126
- else
127
- def self.force_encoding(str, encoding)
128
- str
129
- end
130
- end
131
-
132
-
133
- # Wrapper for File.open that supports passing hash options for specifying
134
- # encodings on Ruby 1.9+. The options are ignored on earlier versions of
135
- # Ruby.
136
- if RUBY_VERSION =~ /\A1\.[0-8]\./
137
- def self.open_file(file_name, mode, opts, &block)
138
- File.open(file_name, mode, &block)
139
- end
140
- else
141
- def self.open_file(file_name, mode, opts, &block)
142
- File.open(file_name, mode, opts, &block)
143
- end
144
- end
145
- end
146
- end
@@ -1,70 +0,0 @@
1
- module TZInfo
2
- # Represents information about a country returned by RubyDataSource.
3
- #
4
- # @private
5
- class RubyCountryInfo < CountryInfo #:nodoc:
6
- # Constructs a new CountryInfo with an ISO 3166 country code, name and
7
- # block. The block will be evaluated to obtain the timezones for the
8
- # country when the zones are first needed.
9
- def initialize(code, name, &block)
10
- super(code, name)
11
- @block = block
12
- @zones = nil
13
- @zone_identifiers = nil
14
- end
15
-
16
- # Returns a frozen array of all the zone identifiers for the country. These
17
- # are in the order they were added using the timezone method.
18
- def zone_identifiers
19
- # Thread-safety: It is possible that the value of @zone_identifiers may be
20
- # calculated multiple times in concurrently executing threads. It is not
21
- # worth the overhead of locking to ensure that @zone_identifiers is only
22
- # calculated once.
23
-
24
- unless @zone_identifiers
25
- @zone_identifiers = zones.collect {|zone| zone.identifier}.freeze
26
- end
27
-
28
- @zone_identifiers
29
- end
30
-
31
- # Returns a frozen array of all the timezones for the for the country as
32
- # CountryTimezone instances. These are in the order they were added using
33
- # the timezone method.
34
- def zones
35
- # Thread-safety: It is possible that the value of @zones may be
36
- # calculated multiple times in concurrently executing threads. It is not
37
- # worth the overhead of locking to ensure that @zones is only
38
- # calculated once.
39
-
40
- unless @zones
41
- zones = Zones.new
42
- @block.call(zones) if @block
43
- @block = nil
44
- @zones = zones.list.freeze
45
- end
46
-
47
- @zones
48
- end
49
-
50
- # An instance of the Zones class is passed to the block used to define
51
- # timezones.
52
- #
53
- # @private
54
- class Zones #:nodoc:
55
- attr_reader :list
56
-
57
- def initialize
58
- @list = []
59
- end
60
-
61
- # Called by the index data to define a timezone for the country.
62
- def timezone(identifier, latitude_numerator, latitude_denominator,
63
- longitude_numerator, longitude_denominator, description = nil)
64
- @list << CountryTimezone.new!(identifier, latitude_numerator,
65
- latitude_denominator, longitude_numerator, longitude_denominator,
66
- description)
67
- end
68
- end
69
- end
70
- end
@@ -1,136 +0,0 @@
1
- module TZInfo
2
- # A DataSource that loads data from the set of Ruby modules included in the
3
- # TZInfo::Data library (tzinfo-data gem).
4
- #
5
- # To have TZInfo use this DataSource, call TZInfo::DataSource.set as follows:
6
- #
7
- # TZInfo::DataSource.set(:ruby)
8
- class RubyDataSource < DataSource
9
- # Base path for require.
10
- REQUIRE_PATH = File.join('tzinfo', 'data', 'definitions')
11
-
12
- # Whether the timezone index has been loaded yet.
13
- @@timezone_index_loaded = false
14
-
15
- # Whether the country index has been loaded yet.
16
- @@country_index_loaded = false
17
-
18
- # Returns a TimezoneInfo instance for a given identifier.
19
- # Raises InvalidTimezoneIdentifier if the timezone is not found or the
20
- # identifier is invalid.
21
- def load_timezone_info(identifier)
22
- raise InvalidTimezoneIdentifier, 'Invalid identifier' if identifier !~ /^[A-Za-z0-9\+\-_]+(\/[A-Za-z0-9\+\-_]+)*$/
23
-
24
- identifier = identifier.gsub(/-/, '__m__').gsub(/\+/, '__p__')
25
-
26
- # Untaint identifier after it has been reassigned to a new string. We
27
- # don't want to modify the original identifier. identifier may also be
28
- # frozen and therefore cannot be untainted.
29
- identifier.untaint
30
-
31
- identifier = identifier.split('/')
32
- begin
33
- require_definition(identifier)
34
-
35
- m = Data::Definitions
36
- identifier.each {|part|
37
- m = m.const_get(part)
38
- }
39
-
40
- m.get
41
- rescue LoadError, NameError => e
42
- raise InvalidTimezoneIdentifier, e.message
43
- end
44
- end
45
-
46
- # Returns an array of all the available timezone identifiers.
47
- def timezone_identifiers
48
- load_timezone_index
49
- Data::Indexes::Timezones.timezones
50
- end
51
-
52
- # Returns an array of all the available timezone identifiers for
53
- # data timezones (i.e. those that actually contain definitions).
54
- def data_timezone_identifiers
55
- load_timezone_index
56
- Data::Indexes::Timezones.data_timezones
57
- end
58
-
59
- # Returns an array of all the available timezone identifiers that
60
- # are links to other timezones.
61
- def linked_timezone_identifiers
62
- load_timezone_index
63
- Data::Indexes::Timezones.linked_timezones
64
- end
65
-
66
- # Returns a CountryInfo instance for the given ISO 3166-1 alpha-2
67
- # country code. Raises InvalidCountryCode if the country could not be found
68
- # or the code is invalid.
69
- def load_country_info(code)
70
- load_country_index
71
- info = Data::Indexes::Countries.countries[code]
72
- raise InvalidCountryCode, 'Invalid country code' unless info
73
- info
74
- end
75
-
76
- # Returns an array of all the available ISO 3166-1 alpha-2
77
- # country codes.
78
- def country_codes
79
- load_country_index
80
- Data::Indexes::Countries.countries.keys.freeze
81
- end
82
-
83
- # Returns the name of this DataSource.
84
- def to_s
85
- "Ruby DataSource"
86
- end
87
-
88
- private
89
-
90
- # Requires a zone definition by its identifier (split on /).
91
- def require_definition(identifier)
92
- require_data(*(['definitions'] + identifier))
93
- end
94
-
95
- # Requires an index by its name.
96
- def self.require_index(name)
97
- require_data(*['indexes', name])
98
- end
99
-
100
- # Requires a file from tzinfo/data.
101
- def require_data(*file)
102
- self.class.require_data(*file)
103
- end
104
-
105
- # Requires a file from tzinfo/data.
106
- def self.require_data(*file)
107
- require File.join('tzinfo', 'data', *file)
108
- end
109
-
110
- # Loads in the index of timezones if it hasn't already been loaded.
111
- def load_timezone_index
112
- self.class.load_timezone_index
113
- end
114
-
115
- # Loads in the index of timezones if it hasn't already been loaded.
116
- def self.load_timezone_index
117
- unless @@timezone_index_loaded
118
- require_index('timezones')
119
- @@timezone_index_loaded = true
120
- end
121
- end
122
-
123
- # Loads in the index of countries if it hasn't already been loaded.
124
- def load_country_index
125
- self.class.load_country_index
126
- end
127
-
128
- # Loads in the index of countries if it hasn't already been loaded.
129
- def self.load_country_index
130
- unless @@country_index_loaded
131
- require_index('countries')
132
- @@country_index_loaded = true
133
- end
134
- end
135
- end
136
- end