tzinfo 0.0.3 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of tzinfo might be problematic. Click here for more details.

data/CHANGES ADDED
@@ -0,0 +1,58 @@
1
+ Version 0.0.4 (tzdata v2005m) - 18-Sep-2005
2
+ -------------------------------------------
3
+
4
+ * Removed debug output accidently included in the previous release.
5
+ * Fixed a bug in the generation of friendly zone identifiers (was inserting
6
+ apostrophes into UTC, GMT, etc).
7
+ * Fixed Country <=> operator (was comparing non-existant attribute)
8
+ * Fixed Timezone.period_for_local error when period not found.
9
+ * Added testcases for Timezone, TimezoneProxy, TimezonePeriod, Country and
10
+ some selected timezones.
11
+
12
+ Version 0.0.3 (tzdata v2005m) - 17-Sep-2005
13
+ -------------------------------------------
14
+
15
+ * Reduced visibility of some methods added in Timezone#setup and Country#setup.
16
+ * Added name method to Timezone (returns the identifier).
17
+ * Added friendly_identifier method to Timezone. Returns a more friendly version
18
+ of the identifier.
19
+ * Added to_s method to Timezone. Returns the friendly identifier.
20
+ * Added == and <=> operators to Timezone (compares identifiers).
21
+ * Timezone now includes Comparable.
22
+ * Added to_s method to Country.
23
+ * Added == and <=> operators to Country (compares ISO 3166 country codes).
24
+ * Country now includes Comparable.
25
+ * New TimezoneProxy class that behaves the same as a Timezone but doesn't
26
+ actually load in its definition until it is actually required.
27
+ * Modified Timezone and Country methods that return Timezones to return
28
+ TimezoneProxy instances instead. This makes these methods much quicker.
29
+
30
+ In Ruby on Rails, you can now show a drop-down list of all timezones using the
31
+ Rails time_zone_select helper method:
32
+
33
+ <%= time_zone_select 'user', 'time_zone', TZInfo::Timezone.all.sort, :model => TZInfo::Timezone %>
34
+
35
+
36
+ Version 0.0.2 (tzdata v2005m) - 13-Sep-2005
37
+ -------------------------------------------
38
+
39
+ * Country and Timezone data is now loaded into class rather than instance
40
+ variables. This makes Timezone links more efficient and saves memory if
41
+ creating specific Timezone and Country classes directly.
42
+ * TimezonePeriod zone_identifier is now defined as a symbol to save memory
43
+ (was previously a string).
44
+ * TimezonePeriod zone_identifiers that were previously '' are now :Unknown.
45
+ * Timezones and Countries can now be returned using Timezone.new(identifier)
46
+ and Country.new(identifier). When passed an identifier, the new method
47
+ calls get to return an instance of the specified timezone or country.
48
+ * Added new class methods to Timezone to return sets of zones and identifiers.
49
+
50
+ Thanks to Scott Barron of Lunchbox Software for the suggestions in his
51
+ article about using TZInfo with Rails
52
+ (http://lunchroom.lunchboxsoftware.com/pages/tzinfo_rails)
53
+
54
+
55
+ Version 0.0.1 (tzdata v2005m) - 29-Aug-2005
56
+ -------------------------------------------
57
+
58
+ * First release.
data/README CHANGED
@@ -64,7 +64,7 @@ Documentation can be found at
64
64
 
65
65
  The preferred method of installing TZInfo is through its GEM file (RubyGems[http://docs.rubygems.org/] required):
66
66
 
67
- % gem install tzinfo-0.0.3.gem
67
+ % gem install tzinfo-0.0.4.gem
68
68
 
69
69
  or to automatically download and install:
70
70
 
@@ -76,11 +76,6 @@ or to automatically download and install:
76
76
  TZInfo is released under the MIT[http://opensource.org/licenses/mit-license.html] license.
77
77
 
78
78
 
79
- == Issues
80
-
81
- * It currently takes an extremely long time to generate all the Ruby Timezone classes from the tz data. This process could do with some optimization.
82
- * Test cases need to be written.
83
-
84
79
  == Support
85
80
 
86
81
  Please contact Philip Ross (phil.ross@gmail.com) if you require assistance or have
@@ -1,6 +1,10 @@
1
1
  require 'tzinfo/timezone'
2
2
 
3
3
  module TZInfo
4
+ # Thrown by Country#get if the code given is not valid.
5
+ class InvalidCountryCode < StandardError
6
+ end
7
+
4
8
  # An ISO 3166 country. Can be used to get a list of Timezones for a country.
5
9
  # For example:
6
10
  #
@@ -13,9 +17,13 @@ module TZInfo
13
17
  # Gets a Country by its ISO 3166 code. Raising an exception if it couldn't
14
18
  # be found.
15
19
  def self.get(identifier)
16
- raise 'Invalid identifier' if identifier !~ /^[A-Z]{2}$/
17
- require "tzinfo/countries/#{identifier}"
18
- Countries.const_get(identifier).instance
20
+ raise InvalidCountryCode.new, 'Invalid identifier' if identifier !~ /^[A-Z]{2}$/
21
+ begin
22
+ require "tzinfo/countries/#{identifier}"
23
+ Countries.const_get(identifier).instance
24
+ rescue LoadError, NameError => e
25
+ raise InvalidCountryCode, e
26
+ end
19
27
  end
20
28
 
21
29
  # If identifier is nil calls super(), else calls get(identifier).
@@ -38,12 +46,7 @@ module TZInfo
38
46
  all_codes.collect {|code|
39
47
  get(code)
40
48
  }
41
- end
42
-
43
- # Initializes the Country.
44
- def initialize
45
-
46
- end
49
+ end
47
50
 
48
51
  # The ISO 3166 country code.
49
52
  def code
@@ -87,7 +90,7 @@ module TZInfo
87
90
  # Compare two Countries based on their code. Returns -1 if tz is less
88
91
  # than self, 0 if tz is equal to self and +1 if tz is greater than self.
89
92
  def <=>(tz)
90
- identifier <=> tz.identifier
93
+ code <=> tz.code
91
94
  end
92
95
 
93
96
  protected
@@ -1,9 +1,14 @@
1
1
  require 'tzinfo/country'
2
2
 
3
3
  module TZInfo
4
+ # Thrown to indicate that no TimezonePeriod matching a given time could be found.
4
5
  class PeriodNotFound < StandardError
5
6
  end
6
7
 
8
+ # Thrown by Timezone#get if the identifier given is not valid.
9
+ class InvalidTimezoneIdentifier < StandardError
10
+ end
11
+
7
12
  # Timezone is the base class of all timezones. It provides a factory method
8
13
  # get to access timezones by identifier. Once a specific Timezone has been
9
14
  # retrieved, DateTimes and Times can be converted between the UTC and the
@@ -23,21 +28,25 @@ module TZInfo
23
28
  #
24
29
  # Raises an exception of the timezone couldn't be found.
25
30
  def self.get(identifier)
26
- raise 'Invalid identifier' if identifier !~ /^[A-z0-9\+\-_]+(\/[A-z0-9\+\-_]+)*$/
31
+ raise InvalidTimezoneIdentifier, 'Invalid identifier' if identifier !~ /^[A-z0-9\+\-_]+(\/[A-z0-9\+\-_]+)*$/
27
32
  identifier = identifier.gsub(/-/, '__m__').gsub(/\+/, '__p__')
28
- require "tzinfo/definitions/#{identifier}"
29
-
30
- m = Definitions
31
- identifier.split(/\//).each {|part|
32
- m = m.const_get(part)
33
- }
34
- m.instance
33
+ begin
34
+ require "tzinfo/definitions/#{identifier}"
35
+
36
+ m = Definitions
37
+ identifier.split(/\//).each {|part|
38
+ m = m.const_get(part)
39
+ }
40
+ m.instance
41
+ rescue LoadError, NameError => e
42
+ raise InvalidTimezoneIdentifier, e
43
+ end
35
44
  end
36
45
 
37
- # If identifier is nil calls super(), else calls get(identifier).
46
+ # If identifier is nil calls super(), else calls get(identifier). An
47
+ # identfier should always be passed in when called externally.
38
48
  def self.new(identifier = nil)
39
- if identifier
40
- puts 'getting'
49
+ if identifier
41
50
  get(identifier)
42
51
  else
43
52
  super()
@@ -131,13 +140,15 @@ module TZInfo
131
140
  parts[1, parts.length - 1].reverse_each {|part|
132
141
  part.gsub!(/_/, ' ')
133
142
 
134
- # Missing a space if a lower case followed by an upper case and the
135
- # name isn't McXxxx.
136
- part.gsub!(/[^M]([a-z])([A-Z])/, '\1 \2')
137
- part.gsub!(/[M]([a-bd-z])([A-Z])/, '\1 \2')
138
-
139
- # Missing an apostrophe if two consecutive upper case characters.
140
- part.gsub!(/([A-Z])([A-Z])/, '\1\'\2')
143
+ if part.index(/[a-z]/)
144
+ # Missing a space if a lower case followed by an upper case and the
145
+ # name isn't McXxxx.
146
+ part.gsub!(/([^M][a-z])([A-Z])/, '\1 \2')
147
+ part.gsub!(/([M][a-bd-z])([A-Z])/, '\1 \2')
148
+
149
+ # Missing an apostrophe if two consecutive upper case characters.
150
+ part.gsub!(/([A-Z])([A-Z])/, '\1\'\2')
151
+ end
141
152
 
142
153
  result << part
143
154
  result << ', '
@@ -208,11 +219,11 @@ module TZInfo
208
219
  if first.local_before_end?(local)
209
220
  first
210
221
  else
211
- raise PeriodNotFound, "No time period found for #{utc}. This could be because a change to daylight savings time caused an hour to be skipped."
222
+ raise PeriodNotFound, "No time period found for #{local}. This could be because a change to daylight savings time caused an hour to be skipped."
212
223
  end
213
224
  end
214
225
  else
215
- raise PeriodNotFound, "No time period found for #{utc}. This could be because a change to daylight savings time caused an hour to be skipped."
226
+ raise PeriodNotFound, "No time period found for #{local}. This could be because a change to daylight savings time caused an hour to be skipped."
216
227
  end
217
228
  }
218
229
  end
@@ -252,12 +263,14 @@ module TZInfo
252
263
  period_for_utc(Time.now.utc)
253
264
  end
254
265
 
255
- # Returns the current time and TimezonePeriod as an array.
266
+ # Returns the current Time and TimezonePeriod as an array.
256
267
  def current_period_and_time
257
268
  utc = Time.now.utc
258
269
  [utc_to_local(utc), period_for_utc(utc)]
259
270
  end
260
271
 
272
+ alias :current_time_and_period :current_period_and_time
273
+
261
274
  # Two Timezones are considered to be equal if their identifiers are the same.
262
275
  def ==(tz)
263
276
  identifier == tz.identifier
@@ -408,12 +421,12 @@ CODE
408
421
  utc_after_start?(utc) && utc_before_end?(utc)
409
422
  end
410
423
 
411
- # true if the given utc DateTime is after the start of the period; otherwise false.
424
+ # true if the given utc DateTime is after the start of the period (inclusive); otherwise false.
412
425
  def utc_after_start?(utc)
413
426
  @utc_start.nil? || @utc_start <= utc
414
427
  end
415
428
 
416
- # true if the given utc DateTime is before the end of the period; otherwise false.
429
+ # true if the given utc DateTime is before the end of the period (exclusive); otherwise false.
417
430
  def utc_before_end?(utc)
418
431
  @utc_end.nil? || @utc_end > utc
419
432
  end
@@ -423,12 +436,12 @@ CODE
423
436
  local_after_start?(local) && local_before_end?(local)
424
437
  end
425
438
 
426
- # true if the given local DateTime is after the start of the period; otherwise false.
439
+ # true if the given local DateTime is after the start of the period (inclusive); otherwise false.
427
440
  def local_after_start?(local)
428
441
  @local_start.nil? || @local_start <= local
429
442
  end
430
443
 
431
- # true if the given local DateTime is before the end of the period; otherwise false.
444
+ # true if the given local DateTime is before the end of the period (exclusive); otherwise false.
432
445
  def local_before_end?(local)
433
446
  @local_end.nil? || @local_end > local
434
447
  end
@@ -0,0 +1,127 @@
1
+ $:.unshift File.join(File.dirname(__FILE__), "..", "lib")
2
+ require 'test/unit'
3
+ require 'tzinfo/country'
4
+
5
+ include TZInfo
6
+
7
+ class TCCountry < Test::Unit::TestCase
8
+ def test_get_valid
9
+ c = Country.get('GB')
10
+
11
+ assert_not_nil(c)
12
+ require 'tzinfo/countries/GB'
13
+ assert_same(Countries::GB.instance, c)
14
+ assert_equal('GB', c.code)
15
+ end
16
+
17
+ def test_get_not_exist
18
+ assert_raise(InvalidCountryCode) {
19
+ Country.get('ZZ')
20
+ }
21
+ end
22
+
23
+ def test_get_invalid
24
+ assert_raise(InvalidCountryCode) {
25
+ Country.get('../Countries/GB')
26
+ }
27
+ end
28
+
29
+ def test_get_nil
30
+ assert_raise(InvalidCountryCode) {
31
+ Country.get(nil)
32
+ }
33
+ end
34
+
35
+ def test_get_case
36
+ assert_raise(InvalidCountryCode) {
37
+ Country.get('gb')
38
+ }
39
+ end
40
+
41
+ def test_new_no_args
42
+ c = Country.new
43
+
44
+ assert_equal('Unknown', c.code)
45
+ end
46
+
47
+ def test_new_nil
48
+ c = Country.new(nil)
49
+
50
+ assert_equal('Unknown', c.code)
51
+ end
52
+
53
+ def test_new_arg
54
+ c = Country.new('GB')
55
+ assert_same(Country.get('GB'), c)
56
+ end
57
+
58
+ def test_new_arg_not_exist
59
+ assert_raise(InvalidCountryCode) {
60
+ Country.new('ZZ')
61
+ }
62
+ end
63
+
64
+ def test_all_codes
65
+ all_codes = Country.all_codes
66
+ assert_kind_of(Array, all_codes)
67
+ end
68
+
69
+ def test_all
70
+ all = Country.all
71
+ assert_equal(Country.all_codes, all.collect {|c| c.code})
72
+ end
73
+
74
+ def test_code
75
+ assert_equal('Unknown', Country.new.code)
76
+ assert_equal('US', Country.get('US').code)
77
+ end
78
+
79
+ def test_name
80
+ assert_kind_of(String, Country.get('US').name)
81
+ end
82
+
83
+ def test_to_s
84
+ assert_equal(Country.get('US').name, Country.get('US').to_s)
85
+ assert_equal(Country.get('GB').name, Country.get('GB').to_s)
86
+ end
87
+
88
+ def test_zone_names
89
+ zone_names = Country.get('US').zone_names
90
+ assert_kind_of(Array, zone_names)
91
+ assert(zone_names.frozen?)
92
+ end
93
+
94
+ def test_zone_identifiers
95
+ assert_equal(Country.get('US').zone_names, Country.get('US').zone_identifiers)
96
+ end
97
+
98
+ def test_zones
99
+ zones = Country.get('US').zones
100
+ assert_equal(Country.get('US').zone_names, zones.collect {|z| z.identifier})
101
+
102
+ zones.each {|z|
103
+ assert_kind_of(TimezoneProxy, z)
104
+ }
105
+ end
106
+
107
+ def test_equals
108
+ require 'tzinfo/countries/GB'
109
+ assert(Countries::GB.new == Countries::GB.new)
110
+ assert(Countries::GB.new == Country.get('GB'))
111
+ assert(Country.get('GB') == Countries::GB.new)
112
+ end
113
+
114
+ def test_compare
115
+ require 'tzinfo/countries/GB'
116
+ require 'tzinfo/countries/FR'
117
+ require 'tzinfo/countries/US'
118
+ assert_equal(0, Countries::GB.new <=> Countries::GB.new)
119
+ assert_equal(0, Country.get('GB') <=> Countries::GB.new)
120
+ assert_equal(0, Countries::GB.new <=> Country.get('GB'))
121
+ assert_equal(0, Country.get('GB') <=> Country.get('GB'))
122
+ assert((Country.get('GB') <=> Country.get('US')) < 0)
123
+ assert((Country.get('US') <=> Country.get('GB')) > 0)
124
+ assert((Country.get('FR') <=> Country.get('US')) < 0)
125
+ assert((Country.get('US') <=> Country.get('FR')) > 0)
126
+ end
127
+ end
@@ -0,0 +1,262 @@
1
+ $:.unshift File.join(File.dirname(__FILE__), "..", "lib")
2
+ require 'test/unit'
3
+ require 'tzinfo/timezone'
4
+
5
+ include TZInfo
6
+
7
+ class TCTimezone < Test::Unit::TestCase
8
+ def test_get_valid_1
9
+ tz = Timezone.get('Europe/London')
10
+
11
+ assert_not_nil(tz)
12
+ require 'tzinfo/definitions/Europe/London'
13
+ assert_same(Definitions::Europe::London.instance, tz)
14
+ assert_equal('Europe/London', tz.identifier)
15
+ end
16
+
17
+ def test_get_valid_2
18
+ tz = Timezone.get('UTC')
19
+
20
+ assert_not_nil(tz)
21
+ require 'tzinfo/definitions/UTC'
22
+ assert_same(Definitions::UTC.instance, tz)
23
+ assert_equal('UTC', tz.identifier)
24
+ end
25
+
26
+ def test_get_valid_3
27
+ tz = Timezone.get('America/Argentina/Buenos_Aires')
28
+
29
+ assert_not_nil(tz)
30
+ assert_same(Definitions::America::Argentina::Buenos_Aires.instance, tz)
31
+ require 'tzinfo/definitions/America/Argentina/Buenos_Aires'
32
+ assert_equal('America/Argentina/Buenos_Aires', tz.identifier)
33
+ end
34
+
35
+ def test_get_not_exist
36
+ assert_raise(InvalidTimezoneIdentifier) {
37
+ Timezone.get('Nowhere/Special')
38
+ }
39
+ end
40
+
41
+ def test_get_invalid
42
+ assert_raise(InvalidTimezoneIdentifier) {
43
+ Timezone.get('../Definitions/UTC')
44
+ }
45
+ end
46
+
47
+ def test_get_nil
48
+ assert_raise(InvalidTimezoneIdentifier) {
49
+ Timezone.get(nil)
50
+ }
51
+ end
52
+
53
+ def test_get_plus
54
+ tz = Timezone.get('Etc/GMT+1')
55
+
56
+ assert_not_nil(tz)
57
+ assert_same(Definitions::Etc::GMT__p__1.instance, tz)
58
+ assert_equal('Etc/GMT+1', tz.identifier)
59
+ end
60
+
61
+ def test_get_minus
62
+ tz = Timezone.get('Etc/GMT-1')
63
+
64
+ assert_not_nil(tz)
65
+ assert_same(Definitions::Etc::GMT__m__1.instance, tz)
66
+ assert_equal('Etc/GMT-1', tz.identifier)
67
+ end
68
+
69
+ def test_get_case
70
+ assert_raise(InvalidTimezoneIdentifier) {
71
+ Timezone.get('Europe/london')
72
+ }
73
+ end
74
+
75
+ def test_new_no_args
76
+ tz = Timezone.new
77
+
78
+ assert_equal('Unknown', tz.identifier)
79
+ end
80
+
81
+ def test_new_nil
82
+ tz = Timezone.new(nil)
83
+
84
+ assert_equal('Unknown', tz.identifier)
85
+ end
86
+
87
+ def test_new_arg
88
+ tz = Timezone.new('Europe/London')
89
+ assert_same(Timezone.get('Europe/London'), tz)
90
+ end
91
+
92
+ def test_new_arg_not_exist
93
+ assert_raise(InvalidTimezoneIdentifier) {
94
+ Timezone.new('Nowhere/Special')
95
+ }
96
+ end
97
+
98
+ def test_all
99
+ assert_equal(Timezone.all_country_zones, Timezone.all)
100
+ end
101
+
102
+ def test_all_identifiers
103
+ assert_equal(Timezone.all_country_zone_identifiers, Timezone.all_identifiers)
104
+ end
105
+
106
+ def test_all_country_zones
107
+ # Probably should relax this test - just need all the zones, don't care
108
+ # about order.
109
+ expected = Country.all.inject([]) {|expected,country|
110
+ expected += country.zones
111
+ }
112
+ expected.uniq!
113
+
114
+ all_country_zones = Timezone.all_country_zones
115
+ assert_equal(expected, all_country_zones)
116
+
117
+ all_country_zone_identifiers = Timezone.all_country_zone_identifiers
118
+ assert_equal(all_country_zone_identifiers.length, all_country_zones.length)
119
+
120
+ all_country_zones.each {|zone|
121
+ assert_kind_of(TimezoneProxy, zone)
122
+ assert(all_country_zone_identifiers.include?(zone.identifier))
123
+ }
124
+ end
125
+
126
+ def test_all_country_zone_identifiers
127
+ # Probably should relax this test - just need all the zones, don't care
128
+ # about order.
129
+ expected = Country.all.inject([]) {|expected,country|
130
+ expected += country.zone_identifiers
131
+ }
132
+ expected.uniq!
133
+
134
+ assert_equal(expected, Timezone.all_country_zone_identifiers)
135
+ end
136
+
137
+ def test_us_zones
138
+ # Probably should relax this test - just need all the zones, don't care
139
+ # about order.
140
+ us_zones = Timezone.us_zones
141
+ assert_equal(Country.get('US').zones.uniq, us_zones)
142
+
143
+ us_zone_identifiers = Timezone.us_zone_identifiers
144
+ assert_equal(us_zone_identifiers.length, us_zones.length)
145
+
146
+ us_zones.each {|zone|
147
+ assert_kind_of(TimezoneProxy, zone)
148
+ assert(us_zone_identifiers.include?(zone.identifier))
149
+ }
150
+ end
151
+
152
+ def test_us_zone_identifiers
153
+ # Probably should relax this test - just need all the zones, don't care
154
+ # about order.
155
+ assert_equal(Country.get('US').zone_identifiers.uniq, Timezone.us_zone_identifiers)
156
+ end
157
+
158
+ def test_identifier
159
+ assert_equal('Unknown', Timezone.new.identifier)
160
+ assert_equal('Europe/Paris', Timezone.get('Europe/Paris').identifier)
161
+ end
162
+
163
+ def test_name
164
+ assert_equal('Unknown', Timezone.new.name)
165
+ assert_equal('Europe/Paris', Timezone.get('Europe/Paris').name)
166
+ end
167
+
168
+ def test_friendly_identifier
169
+ assert_equal('Paris', Timezone.get('Europe/Paris').friendly_identifier(true))
170
+ assert_equal('Europe - Paris', Timezone.get('Europe/Paris').friendly_identifier(false))
171
+ assert_equal('Europe - Paris', Timezone.get('Europe/Paris').friendly_identifier)
172
+ assert_equal('Knox, Indiana', Timezone.get('America/Indiana/Knox').friendly_identifier(true))
173
+ assert_equal('America - Knox, Indiana', Timezone.get('America/Indiana/Knox').friendly_identifier(false))
174
+ assert_equal('America - Knox, Indiana', Timezone.get('America/Indiana/Knox').friendly_identifier)
175
+ assert_equal('Dumont D\'Urville', Timezone.get('Antarctica/DumontDUrville').friendly_identifier(true))
176
+ assert_equal('Antarctica - Dumont D\'Urville', Timezone.get('Antarctica/DumontDUrville').friendly_identifier(false))
177
+ assert_equal('Antarctica - Dumont D\'Urville', Timezone.get('Antarctica/DumontDUrville').friendly_identifier)
178
+ assert_equal('McMurdo', Timezone.get('Antarctica/McMurdo').friendly_identifier(true))
179
+ assert_equal('Antarctica - McMurdo', Timezone.get('Antarctica/McMurdo').friendly_identifier(false))
180
+ assert_equal('Antarctica - McMurdo', Timezone.get('Antarctica/McMurdo').friendly_identifier)
181
+ assert_equal('GMT+1', Timezone.get('Etc/GMT+1').friendly_identifier(true))
182
+ assert_equal('Etc - GMT+1', Timezone.get('Etc/GMT+1').friendly_identifier(false))
183
+ assert_equal('Etc - GMT+1', Timezone.get('Etc/GMT+1').friendly_identifier)
184
+ assert_equal('UTC', Timezone.get('UTC').friendly_identifier(true))
185
+ assert_equal('UTC', Timezone.get('UTC').friendly_identifier(false))
186
+ assert_equal('UTC', Timezone.get('UTC').friendly_identifier)
187
+ end
188
+
189
+ def test_to_s
190
+ assert_equal('Europe - Paris', Timezone.get('Europe/Paris').to_s)
191
+ assert_equal('America - Knox, Indiana', Timezone.get('America/Indiana/Knox').to_s)
192
+ assert_equal('Antarctica - Dumont D\'Urville', Timezone.get('Antarctica/DumontDUrville').to_s)
193
+ assert_equal('Antarctica - McMurdo', Timezone.get('Antarctica/McMurdo').to_s)
194
+ assert_equal('Etc - GMT+1', Timezone.get('Etc/GMT+1').to_s)
195
+ assert_equal('UTC', Timezone.get('UTC').to_s)
196
+ end
197
+
198
+ def test_utc_to_local
199
+ dt = DateTime.new(2005,2,18,16,24,23)
200
+ t = Time.utc(2005,2,18,16,24,23)
201
+
202
+ assert_equal(dt, Timezone.get('Europe/London').utc_to_local(dt))
203
+ assert_not_equal(t, Timezone.get('Europe/London').utc_to_local(dt))
204
+ assert_equal(t, Timezone.get('Europe/London').utc_to_local(t))
205
+ assert_not_equal(dt, Timezone.get('Europe/London').utc_to_local(t))
206
+ end
207
+
208
+ def test_local_to_utc
209
+ dt = DateTime.new(2005,2,18,16,24,23)
210
+ t = Time.utc(2005,2,18,16,24,23)
211
+
212
+ assert_equal(dt, Timezone.get('Europe/London').local_to_utc(dt))
213
+ assert_not_equal(t, Timezone.get('Europe/London').local_to_utc(dt))
214
+ assert_equal(t, Timezone.get('Europe/London').local_to_utc(t))
215
+ assert_not_equal(dt, Timezone.get('Europe/London').local_to_utc(t))
216
+ end
217
+
218
+ def test_now
219
+ assert_kind_of(Time, Timezone.get('Europe/London').now)
220
+ end
221
+
222
+ def test_current_period
223
+ assert_kind_of(TimezonePeriod, Timezone.get('Europe/London').current_period)
224
+ end
225
+
226
+ def test_current_period_and_time
227
+ current = Timezone.get('Europe/London').current_period_and_time
228
+ assert_equal(2, current.length)
229
+ assert_kind_of(Time, current[0])
230
+ assert_kind_of(TimezonePeriod, current[1])
231
+ end
232
+
233
+ def test_current_time_and_period
234
+ current = Timezone.get('Europe/London').current_time_and_period
235
+ assert_equal(2, current.length)
236
+ assert_kind_of(Time, current[0])
237
+ assert_kind_of(TimezonePeriod, current[1])
238
+ end
239
+
240
+ def test_equals
241
+ require 'tzinfo/definitions/Europe/London'
242
+ assert(Definitions::Europe::London.new == Definitions::Europe::London.new)
243
+ assert(Definitions::Europe::London.new == Timezone.get('Europe/London'))
244
+ assert(Timezone.get('Europe/London') == Definitions::Europe::London.new)
245
+ assert(Timezone.get('Europe/London') == TimezoneProxy.new('Europe/London'))
246
+ assert(TimezoneProxy.new('Europe/London') == Timezone.get('Europe/London'))
247
+ end
248
+
249
+ def test_compare
250
+ require 'tzinfo/definitions/Europe/London'
251
+ require 'tzinfo/definitions/Europe/Paris'
252
+ require 'tzinfo/definitions/America/New_York'
253
+ assert_equal(0, Definitions::Europe::London.new <=> Definitions::Europe::London.new)
254
+ assert_equal(0, Timezone.get('Europe/London') <=> Definitions::Europe::London.new)
255
+ assert_equal(0, Definitions::Europe::London.new <=> Timezone.get('Europe/London'))
256
+ assert_equal(0, Timezone.get('Europe/London') <=> Timezone.get('Europe/London'))
257
+ assert((Timezone.get('Europe/London') <=> Timezone.get('Europe/Paris')) < 0)
258
+ assert((Timezone.get('Europe/Paris') <=> Timezone.get('Europe/London')) > 0)
259
+ assert((Timezone.get('America/New_York') <=> Timezone.get('Europe/Paris')) < 0)
260
+ assert((Timezone.get('Europe/Paris') <=> Timezone.get('America/New_York')) > 0)
261
+ end
262
+ end
@@ -0,0 +1,47 @@
1
+ $:.unshift File.join(File.dirname(__FILE__), "..", "lib")
2
+ require 'test/unit'
3
+ require 'tzinfo/timezone'
4
+
5
+ include TZInfo
6
+
7
+ class TCTimezoneLondon < Test::Unit::TestCase
8
+ def test_2004
9
+ #Europe/London Sun Mar 28 00:59:59 2004 UTC = Sun Mar 28 00:59:59 2004 GMT isdst=0 gmtoff=0
10
+ #Europe/London Sun Mar 28 01:00:00 2004 UTC = Sun Mar 28 02:00:00 2004 BST isdst=1 gmtoff=3600
11
+ #Europe/London Sun Oct 31 00:59:59 2004 UTC = Sun Oct 31 01:59:59 2004 BST isdst=1 gmtoff=3600
12
+ #Europe/London Sun Oct 31 01:00:00 2004 UTC = Sun Oct 31 01:00:00 2004 GMT isdst=0 gmtoff=0
13
+
14
+ tz = Timezone.get('Europe/London')
15
+ assert_equal(DateTime.new(2004,3,28,0,59,59), tz.utc_to_local(DateTime.new(2004,3,28,0,59,59)))
16
+ assert_equal(DateTime.new(2004,3,28,2,0,0), tz.utc_to_local(DateTime.new(2004,3,28,1,0,0)))
17
+ assert_equal(DateTime.new(2004,10,31,1,59,59), tz.utc_to_local(DateTime.new(2004,10,31,0,59,59)))
18
+ assert_equal(DateTime.new(2004,10,31,1,0,0), tz.utc_to_local(DateTime.new(2004,10,31,1,0,0)))
19
+
20
+ assert_equal(DateTime.new(2004,3,28,0,59,59), tz.local_to_utc(DateTime.new(2004,3,28,0,59,59)))
21
+ assert_equal(DateTime.new(2004,3,28,1,0,0), tz.local_to_utc(DateTime.new(2004,3,28,2,0,0)))
22
+ assert_equal(DateTime.new(2004,10,31,0,59,59), tz.local_to_utc(DateTime.new(2004,10,31,1,59,59)))
23
+ assert_equal(DateTime.new(2004,10,31,0,0,0), tz.local_to_utc(DateTime.new(2004,10,31,1,0,0))) # returns first possible utc time
24
+
25
+ assert_raise(PeriodNotFound) { tz.local_to_utc(DateTime.new(2004,3,28,1,0,0)) }
26
+
27
+ assert_equal(:GMT, tz.period_for_utc(DateTime.new(2004,3,28,0,59,59)).zone_identifier)
28
+ assert_equal(:BST, tz.period_for_utc(DateTime.new(2004,3,28,1,0,0)).zone_identifier)
29
+ assert_equal(:BST, tz.period_for_utc(DateTime.new(2004,10,31,0,59,59)).zone_identifier)
30
+ assert_equal(:GMT, tz.period_for_utc(DateTime.new(2004,10,31,1,0,0)).zone_identifier)
31
+
32
+ assert_equal(:GMT, tz.period_for_local(DateTime.new(2004,3,28,0,59,59)).zone_identifier)
33
+ assert_equal(:BST, tz.period_for_local(DateTime.new(2004,3,28,2,0,0)).zone_identifier)
34
+ assert_equal(:BST, tz.period_for_local(DateTime.new(2004,10,31,1,59,59)).zone_identifier)
35
+ assert_equal(:BST, tz.period_for_local(DateTime.new(2004,10,31,1,0,0)).zone_identifier)
36
+
37
+ assert_equal(0, tz.period_for_utc(DateTime.new(2004,3,28,0,59,59)).utc_total_offset)
38
+ assert_equal(3600, tz.period_for_utc(DateTime.new(2004,3,28,1,0,0)).utc_total_offset)
39
+ assert_equal(3600, tz.period_for_utc(DateTime.new(2004,10,31,0,59,59)).utc_total_offset)
40
+ assert_equal(0, tz.period_for_utc(DateTime.new(2004,10,31,1,0,0)).utc_total_offset)
41
+
42
+ assert_equal(0, tz.period_for_local(DateTime.new(2004,3,28,0,59,59)).utc_total_offset)
43
+ assert_equal(3600, tz.period_for_local(DateTime.new(2004,3,28,2,0,0)).utc_total_offset)
44
+ assert_equal(3600, tz.period_for_local(DateTime.new(2004,10,31,1,59,59)).utc_total_offset)
45
+ assert_equal(3600, tz.period_for_local(DateTime.new(2004,10,31,1,0,0)).utc_total_offset)
46
+ end
47
+ end
@@ -0,0 +1,47 @@
1
+ $:.unshift File.join(File.dirname(__FILE__), "..", "lib")
2
+ require 'test/unit'
3
+ require 'tzinfo/timezone'
4
+
5
+ include TZInfo
6
+
7
+ class TCTimezoneMelbourne < Test::Unit::TestCase
8
+ def test_2004
9
+ #Australia/Melbourne Sat Mar 27 15:59:59 2004 UTC = Sun Mar 28 02:59:59 2004 EST isdst=1 gmtoff=39600
10
+ #Australia/Melbourne Sat Mar 27 16:00:00 2004 UTC = Sun Mar 28 02:00:00 2004 EST isdst=0 gmtoff=36000
11
+ #Australia/Melbourne Sat Oct 30 15:59:59 2004 UTC = Sun Oct 31 01:59:59 2004 EST isdst=0 gmtoff=36000
12
+ #Australia/Melbourne Sat Oct 30 16:00:00 2004 UTC = Sun Oct 31 03:00:00 2004 EST isdst=1 gmtoff=39600
13
+
14
+ tz = Timezone.get('Australia/Melbourne')
15
+ assert_equal(DateTime.new(2004,3,28,2,59,59), tz.utc_to_local(DateTime.new(2004,3,27,15,59,59)))
16
+ assert_equal(DateTime.new(2004,3,28,2,0,0), tz.utc_to_local(DateTime.new(2004,3,27,16,0,0)))
17
+ assert_equal(DateTime.new(2004,10,31,1,59,59), tz.utc_to_local(DateTime.new(2004,10,30,15,59,59)))
18
+ assert_equal(DateTime.new(2004,10,31,3,0,0), tz.utc_to_local(DateTime.new(2004,10,30,16,0,0)))
19
+
20
+ assert_equal(DateTime.new(2004,3,27,15,59,59), tz.local_to_utc(DateTime.new(2004,3,28,2,59,59)))
21
+ assert_equal(DateTime.new(2004,3,27,15,0,0), tz.local_to_utc(DateTime.new(2004,3,28,2,0,0))) # returns first possible utc time
22
+ assert_equal(DateTime.new(2004,10,30,15,59,59), tz.local_to_utc(DateTime.new(2004,10,31,1,59,59)))
23
+ assert_equal(DateTime.new(2004,10,30,16,0,0), tz.local_to_utc(DateTime.new(2004,10,31,3,0,0)))
24
+
25
+ assert_raise(PeriodNotFound) { tz.local_to_utc(DateTime.new(2004,10,31,2,0,0)) }
26
+
27
+ assert_equal(:EST, tz.period_for_utc(DateTime.new(2004,3,27,15,59,59)).zone_identifier)
28
+ assert_equal(:EST, tz.period_for_utc(DateTime.new(2004,3,27,16,0,0)).zone_identifier)
29
+ assert_equal(:EST, tz.period_for_utc(DateTime.new(2004,10,30,15,59,59)).zone_identifier)
30
+ assert_equal(:EST, tz.period_for_utc(DateTime.new(2004,10,30,16,0,0)).zone_identifier)
31
+
32
+ assert_equal(:EST, tz.period_for_local(DateTime.new(2004,3,28,2,59,59)).zone_identifier)
33
+ assert_equal(:EST, tz.period_for_local(DateTime.new(2004,3,28,2,0,0)).zone_identifier)
34
+ assert_equal(:EST, tz.period_for_local(DateTime.new(2004,10,31,1,59,59)).zone_identifier)
35
+ assert_equal(:EST, tz.period_for_local(DateTime.new(2004,10,31,3,0,0)).zone_identifier)
36
+
37
+ assert_equal(39600, tz.period_for_utc(DateTime.new(2004,3,27,15,59,59)).utc_total_offset)
38
+ assert_equal(36000, tz.period_for_utc(DateTime.new(2004,3,27,16,0,0)).utc_total_offset)
39
+ assert_equal(36000, tz.period_for_utc(DateTime.new(2004,10,30,15,59,59)).utc_total_offset)
40
+ assert_equal(39600, tz.period_for_utc(DateTime.new(2004,10,30,16,0,0)).utc_total_offset)
41
+
42
+ assert_equal(39600, tz.period_for_local(DateTime.new(2004,3,28,2,59,59)).utc_total_offset)
43
+ assert_equal(39600, tz.period_for_local(DateTime.new(2004,3,28,2,0,0)).utc_total_offset)
44
+ assert_equal(36000, tz.period_for_local(DateTime.new(2004,10,31,1,59,59)).utc_total_offset)
45
+ assert_equal(39600, tz.period_for_local(DateTime.new(2004,10,31,3,0,0)).utc_total_offset)
46
+ end
47
+ end
@@ -0,0 +1,47 @@
1
+ $:.unshift File.join(File.dirname(__FILE__), "..", "lib")
2
+ require 'test/unit'
3
+ require 'tzinfo/timezone'
4
+
5
+ include TZInfo
6
+
7
+ class TCTimezoneNewYork < Test::Unit::TestCase
8
+ def test_2004
9
+ #America/New_York Sun Apr 4 06:59:59 2004 UTC = Sun Apr 4 01:59:59 2004 EST isdst=0 gmtoff=-18000
10
+ #America/New_York Sun Apr 4 07:00:00 2004 UTC = Sun Apr 4 03:00:00 2004 EDT isdst=1 gmtoff=-14400
11
+ #America/New_York Sun Oct 31 05:59:59 2004 UTC = Sun Oct 31 01:59:59 2004 EDT isdst=1 gmtoff=-14400
12
+ #America/New_York Sun Oct 31 06:00:00 2004 UTC = Sun Oct 31 01:00:00 2004 EST isdst=0 gmtoff=-18000
13
+
14
+ tz = Timezone.get('America/New_York')
15
+ assert_equal(DateTime.new(2004,4,4,1,59,59), tz.utc_to_local(DateTime.new(2004,4,4,6,59,59)))
16
+ assert_equal(DateTime.new(2004,4,4,3,0,0), tz.utc_to_local(DateTime.new(2004,4,4,7,0,0)))
17
+ assert_equal(DateTime.new(2004,10,31,1,59,59), tz.utc_to_local(DateTime.new(2004,10,31,5,59,59)))
18
+ assert_equal(DateTime.new(2004,10,31,1,0,0), tz.utc_to_local(DateTime.new(2004,10,31,6,0,0)))
19
+
20
+ assert_equal(DateTime.new(2004,4,4,6,59,59), tz.local_to_utc(DateTime.new(2004,4,4,1,59,59)))
21
+ assert_equal(DateTime.new(2004,4,4,7,0,0), tz.local_to_utc(DateTime.new(2004,4,4,3,0,0)))
22
+ assert_equal(DateTime.new(2004,10,31,5,59,59), tz.local_to_utc(DateTime.new(2004,10,31,1,59,59)))
23
+ assert_equal(DateTime.new(2004,10,31,5,0,0), tz.local_to_utc(DateTime.new(2004,10,31,1,0,0))) # returns first possible utc time
24
+
25
+ assert_raise(PeriodNotFound) { tz.local_to_utc(DateTime.new(2004,4,4,2,0,0)) }
26
+
27
+ assert_equal(:EST, tz.period_for_utc(DateTime.new(2004,4,4,6,59,59)).zone_identifier)
28
+ assert_equal(:EDT, tz.period_for_utc(DateTime.new(2004,4,4,7,0,0)).zone_identifier)
29
+ assert_equal(:EDT, tz.period_for_utc(DateTime.new(2004,10,31,5,59,59)).zone_identifier)
30
+ assert_equal(:EST, tz.period_for_utc(DateTime.new(2004,10,31,6,0,0)).zone_identifier)
31
+
32
+ assert_equal(:EST, tz.period_for_local(DateTime.new(2004,4,4,1,59,59)).zone_identifier)
33
+ assert_equal(:EDT, tz.period_for_local(DateTime.new(2004,4,4,3,0,0)).zone_identifier)
34
+ assert_equal(:EDT, tz.period_for_local(DateTime.new(2004,10,31,1,59,59)).zone_identifier)
35
+ assert_equal(:EDT, tz.period_for_local(DateTime.new(2004,10,31,1,0,0)).zone_identifier)
36
+
37
+ assert_equal(-18000, tz.period_for_utc(DateTime.new(2004,4,4,6,59,59)).utc_total_offset)
38
+ assert_equal(-14400, tz.period_for_utc(DateTime.new(2004,4,4,7,0,0)).utc_total_offset)
39
+ assert_equal(-14400, tz.period_for_utc(DateTime.new(2004,10,31,5,59,59)).utc_total_offset)
40
+ assert_equal(-18000, tz.period_for_utc(DateTime.new(2004,10,31,6,0,0)).utc_total_offset)
41
+
42
+ assert_equal(-18000, tz.period_for_local(DateTime.new(2004,4,4,1,59,59)).utc_total_offset)
43
+ assert_equal(-14400, tz.period_for_local(DateTime.new(2004,4,4,3,0,0)).utc_total_offset)
44
+ assert_equal(-14400, tz.period_for_local(DateTime.new(2004,10,31,1,59,59)).utc_total_offset)
45
+ assert_equal(-14400, tz.period_for_local(DateTime.new(2004,10,31,1,0,0)).utc_total_offset)
46
+ end
47
+ end
@@ -0,0 +1,200 @@
1
+ $:.unshift File.join(File.dirname(__FILE__), "..", "lib")
2
+ require 'test/unit'
3
+ require 'tzinfo/timezone'
4
+
5
+ include TZInfo
6
+
7
+ class TCTimezonePeriod < Test::Unit::TestCase
8
+ def test_initialize
9
+ p1 = TimezonePeriod.new(DateTime.new(2005,1,1,0,0,0), DateTime.new(2005,1,2,0,0,0), -7200, 3600, :TEST)
10
+ p2 = TimezonePeriod.new(nil, nil, -7200, 3600, :TEST)
11
+
12
+ assert_equal(DateTime.new(2005,1,1,0,0,0), p1.utc_start)
13
+ assert_equal(DateTime.new(2005,1,2,0,0,0), p1.utc_end)
14
+ assert_equal(-7200, p1.utc_offset)
15
+ assert_equal(3600, p1.std_offset)
16
+ assert_equal(:TEST, p1.zone_identifier)
17
+ assert_equal(DateTime.new(2004,12,31,23,0,0), p1.local_start)
18
+ assert_equal(DateTime.new(2005,1,1,23,0,0), p1.local_end)
19
+
20
+ assert_nil(p2.utc_start)
21
+ assert_nil(p2.utc_end)
22
+ assert_equal(-7200, p2.utc_offset)
23
+ assert_equal(3600, p2.std_offset)
24
+ assert_equal(:TEST, p2.zone_identifier)
25
+ assert_nil(p2.local_start)
26
+ assert_nil(p2.local_end)
27
+ end
28
+
29
+ def test_utc_total_offset
30
+ p1 = TimezonePeriod.new(DateTime.new(2005,1,1,0,0,0), DateTime.new(2005,1,2,0,0,0), -14400, 3600, :TEST)
31
+ p2 = TimezonePeriod.new(DateTime.new(2005,1,1,0,0,0), DateTime.new(2005,1,2,0,0,0), -14400, 0, :TEST)
32
+ p3 = TimezonePeriod.new(DateTime.new(2005,1,1,0,0,0), DateTime.new(2005,1,2,0,0,0), 7200, 3600, :TEST)
33
+
34
+ assert_equal(-10800, p1.utc_total_offset)
35
+ assert_equal(-14400, p2.utc_total_offset)
36
+ assert_equal(10800, p3.utc_total_offset)
37
+ end
38
+
39
+ def test_utc_total_offset_rational
40
+ p = TimezonePeriod.new(DateTime.new(2005,1,1,0,0,0), DateTime.new(2005,1,2,0,0,0), -14400, 3600, :TEST)
41
+
42
+ assert_equal(Rational(-10800, 24 * 60 * 60), p.utc_total_offset_rational)
43
+ end
44
+
45
+ def test_dst
46
+ p1 = TimezonePeriod.new(DateTime.new(2005,1,1,0,0,0), DateTime.new(2005,1,2,0,0,0), -14400, 3600, :TEST)
47
+ p2 = TimezonePeriod.new(DateTime.new(2005,1,1,0,0,0), DateTime.new(2005,1,2,0,0,0), -14400, 0, :TEST)
48
+ p3 = TimezonePeriod.new(DateTime.new(2005,1,1,0,0,0), DateTime.new(2005,1,2,0,0,0), -14400, -3600, :TEST)
49
+ p4 = TimezonePeriod.new(DateTime.new(2005,1,1,0,0,0), DateTime.new(2005,1,2,0,0,0), -14400, 7200, :TEST)
50
+ p5 = TimezonePeriod.new(DateTime.new(2005,1,1,0,0,0), DateTime.new(2005,1,2,0,0,0), -14400, -7200, :TEST)
51
+
52
+ assert_equal(true, p1.dst?)
53
+ assert_equal(false, p2.dst?)
54
+ assert_equal(true, p3.dst?)
55
+ assert_equal(true, p4.dst?)
56
+ assert_equal(true, p5.dst?)
57
+ end
58
+
59
+ def test_valid_for_utc
60
+ p1 = TimezonePeriod.new(DateTime.new(2005,1,1,1,1,1), DateTime.new(2005,2,2,2,2,2), -7200, 3600, :TEST)
61
+ p2 = TimezonePeriod.new(nil, DateTime.new(2005,2,2,2,2,2), -7200, 3600, :TEST)
62
+ p3 = TimezonePeriod.new(DateTime.new(2005,1,1,1,1,1), nil, -7200, 3600, :TEST)
63
+ p4 = TimezonePeriod.new(nil, nil, -7200, 3600, :TEST)
64
+
65
+ assert_equal(true, p1.valid_for_utc?(DateTime.new(2005,1,1,1,1,1)))
66
+ assert_equal(true, p1.valid_for_utc?(DateTime.new(2005,2,2,2,2,1)))
67
+ assert_equal(true, p1.valid_for_utc?(DateTime.new(2005,1,1,1,1,2)))
68
+ assert_equal(true, p1.valid_for_utc?(DateTime.new(2005,2,2,2,2,0)))
69
+ assert_equal(false, p1.valid_for_utc?(DateTime.new(2005,1,1,1,1,0)))
70
+ assert_equal(false, p1.valid_for_utc?(DateTime.new(2005,2,2,2,2,3)))
71
+
72
+ assert_equal(true, p2.valid_for_utc?(DateTime.new(2005,1,1,1,1,1)))
73
+ assert_equal(true, p2.valid_for_utc?(DateTime.new(2005,2,2,2,2,1)))
74
+ assert_equal(true, p2.valid_for_utc?(DateTime.new(2005,1,1,1,1,2)))
75
+ assert_equal(true, p2.valid_for_utc?(DateTime.new(2005,2,2,2,2,0)))
76
+ assert_equal(true, p2.valid_for_utc?(DateTime.new(2005,1,1,1,1,0)))
77
+ assert_equal(false, p2.valid_for_utc?(DateTime.new(2005,2,2,2,2,3)))
78
+
79
+ assert_equal(true, p3.valid_for_utc?(DateTime.new(2005,1,1,1,1,1)))
80
+ assert_equal(true, p3.valid_for_utc?(DateTime.new(2005,2,2,2,2,1)))
81
+ assert_equal(true, p3.valid_for_utc?(DateTime.new(2005,1,1,1,1,2)))
82
+ assert_equal(true, p3.valid_for_utc?(DateTime.new(2005,2,2,2,2,0)))
83
+ assert_equal(false, p3.valid_for_utc?(DateTime.new(2005,1,1,1,1,0)))
84
+ assert_equal(true, p3.valid_for_utc?(DateTime.new(2005,2,2,2,2,3)))
85
+
86
+ assert_equal(true, p4.valid_for_utc?(DateTime.new(2005,1,1,1,1,1)))
87
+ assert_equal(true, p4.valid_for_utc?(DateTime.new(2005,2,2,2,2,1)))
88
+ assert_equal(true, p4.valid_for_utc?(DateTime.new(2005,1,1,1,1,2)))
89
+ assert_equal(true, p4.valid_for_utc?(DateTime.new(2005,2,2,2,2,0)))
90
+ assert_equal(true, p4.valid_for_utc?(DateTime.new(2005,1,1,1,1,0)))
91
+ assert_equal(true, p4.valid_for_utc?(DateTime.new(2005,2,2,2,2,3)))
92
+ end
93
+
94
+ def test_utc_after_start
95
+ p1 = TimezonePeriod.new(DateTime.new(2005,1,1,1,1,1), DateTime.new(2005,2,2,2,2,2), -7200, 3600, :TEST)
96
+ p2 = TimezonePeriod.new(nil, DateTime.new(2005,2,2,2,2,2), -7200, 3600, :TEST)
97
+
98
+ assert_equal(true, p1.utc_after_start?(DateTime.new(2005,1,1,1,1,1)))
99
+ assert_equal(true, p1.utc_after_start?(DateTime.new(2005,1,1,1,1,2)))
100
+ assert_equal(false, p1.utc_after_start?(DateTime.new(2005,1,1,1,1,0)))
101
+
102
+ assert_equal(true, p2.utc_after_start?(DateTime.new(2005,1,1,1,1,1)))
103
+ assert_equal(true, p2.utc_after_start?(DateTime.new(2005,1,1,1,1,2)))
104
+ assert_equal(true, p2.utc_after_start?(DateTime.new(2005,1,1,1,1,0)))
105
+ end
106
+
107
+ def test_utc_before_end
108
+ p1 = TimezonePeriod.new(DateTime.new(2005,1,1,1,1,1), DateTime.new(2005,2,2,2,2,2), -7200, 3600, :TEST)
109
+ p2 = TimezonePeriod.new(DateTime.new(2005,1,1,1,1,1), nil, -7200, 3600, :TEST)
110
+
111
+ assert_equal(true, p1.utc_before_end?(DateTime.new(2005,2,2,2,2,1)))
112
+ assert_equal(true, p1.utc_before_end?(DateTime.new(2005,2,2,2,2,0)))
113
+ assert_equal(false, p1.utc_before_end?(DateTime.new(2005,2,2,2,2,3)))
114
+
115
+ assert_equal(true, p2.utc_before_end?(DateTime.new(2005,2,2,2,2,1)))
116
+ assert_equal(true, p2.utc_before_end?(DateTime.new(2005,2,2,2,2,0)))
117
+ assert_equal(true, p2.utc_before_end?(DateTime.new(2005,2,2,2,2,3)))
118
+ end
119
+
120
+ def test_valid_for_local
121
+ p1 = TimezonePeriod.new(DateTime.new(2005,1,1,2,1,1), DateTime.new(2005,2,2,3,2,2), -7200, 3600, :TEST)
122
+ p2 = TimezonePeriod.new(nil, DateTime.new(2005,2,2,3,2,2), -7200, 3600, :TEST)
123
+ p3 = TimezonePeriod.new(DateTime.new(2005,1,1,2,1,1), nil, -7200, 3600, :TEST)
124
+ p4 = TimezonePeriod.new(nil, nil, -7200, 3600, :TEST)
125
+
126
+ assert_equal(true, p1.valid_for_local?(DateTime.new(2005,1,1,1,1,1)))
127
+ assert_equal(true, p1.valid_for_local?(DateTime.new(2005,2,2,2,2,1)))
128
+ assert_equal(true, p1.valid_for_local?(DateTime.new(2005,1,1,1,1,2)))
129
+ assert_equal(true, p1.valid_for_local?(DateTime.new(2005,2,2,2,2,0)))
130
+ assert_equal(false, p1.valid_for_local?(DateTime.new(2005,1,1,1,1,0)))
131
+ assert_equal(false, p1.valid_for_local?(DateTime.new(2005,2,2,2,2,3)))
132
+
133
+ assert_equal(true, p2.valid_for_local?(DateTime.new(2005,1,1,1,1,1)))
134
+ assert_equal(true, p2.valid_for_local?(DateTime.new(2005,2,2,2,2,1)))
135
+ assert_equal(true, p2.valid_for_local?(DateTime.new(2005,1,1,1,1,2)))
136
+ assert_equal(true, p2.valid_for_local?(DateTime.new(2005,2,2,2,2,0)))
137
+ assert_equal(true, p2.valid_for_local?(DateTime.new(2005,1,1,1,1,0)))
138
+ assert_equal(false, p2.valid_for_local?(DateTime.new(2005,2,2,2,2,3)))
139
+
140
+ assert_equal(true, p3.valid_for_local?(DateTime.new(2005,1,1,1,1,1)))
141
+ assert_equal(true, p3.valid_for_local?(DateTime.new(2005,2,2,2,2,1)))
142
+ assert_equal(true, p3.valid_for_local?(DateTime.new(2005,1,1,1,1,2)))
143
+ assert_equal(true, p3.valid_for_local?(DateTime.new(2005,2,2,2,2,0)))
144
+ assert_equal(false, p3.valid_for_local?(DateTime.new(2005,1,1,1,1,0)))
145
+ assert_equal(true, p3.valid_for_local?(DateTime.new(2005,2,2,2,2,3)))
146
+
147
+ assert_equal(true, p4.valid_for_local?(DateTime.new(2005,1,1,1,1,1)))
148
+ assert_equal(true, p4.valid_for_local?(DateTime.new(2005,2,2,2,2,1)))
149
+ assert_equal(true, p4.valid_for_local?(DateTime.new(2005,1,1,1,1,2)))
150
+ assert_equal(true, p4.valid_for_local?(DateTime.new(2005,2,2,2,2,0)))
151
+ assert_equal(true, p4.valid_for_local?(DateTime.new(2005,1,1,1,1,0)))
152
+ assert_equal(true, p4.valid_for_local?(DateTime.new(2005,2,2,2,2,3)))
153
+ end
154
+
155
+ def test_local_after_start
156
+ p1 = TimezonePeriod.new(DateTime.new(2005,1,1,2,1,1), DateTime.new(2005,2,2,3,2,2), -7200, 3600, :TEST)
157
+ p2 = TimezonePeriod.new(nil, DateTime.new(2005,2,2,3,2,2), -7200, 3600, :TEST)
158
+
159
+ assert_equal(true, p1.local_after_start?(DateTime.new(2005,1,1,1,1,1)))
160
+ assert_equal(true, p1.local_after_start?(DateTime.new(2005,1,1,1,1,2)))
161
+ assert_equal(false, p1.local_after_start?(DateTime.new(2005,1,1,1,1,0)))
162
+
163
+ assert_equal(true, p2.local_after_start?(DateTime.new(2005,1,1,1,1,1)))
164
+ assert_equal(true, p2.local_after_start?(DateTime.new(2005,1,1,1,1,2)))
165
+ assert_equal(true, p2.local_after_start?(DateTime.new(2005,1,1,1,1,0)))
166
+ end
167
+
168
+ def test_local_before_end
169
+ p1 = TimezonePeriod.new(DateTime.new(2005,1,1,2,1,1), DateTime.new(2005,2,2,3,2,2), -7200, 3600, :TEST)
170
+ p2 = TimezonePeriod.new(DateTime.new(2005,1,1,2,1,1), nil, -7200, 3600, :TEST)
171
+
172
+ assert_equal(true, p1.local_before_end?(DateTime.new(2005,2,2,2,2,1)))
173
+ assert_equal(true, p1.local_before_end?(DateTime.new(2005,2,2,2,2,0)))
174
+ assert_equal(false, p1.local_before_end?(DateTime.new(2005,2,2,2,2,3)))
175
+
176
+ assert_equal(true, p2.local_before_end?(DateTime.new(2005,2,2,2,2,1)))
177
+ assert_equal(true, p2.local_before_end?(DateTime.new(2005,2,2,2,2,0)))
178
+ assert_equal(true, p2.local_before_end?(DateTime.new(2005,2,2,2,2,3)))
179
+ end
180
+
181
+ def test_to_local
182
+ p1 = TimezonePeriod.new(DateTime.new(2005,1,1,0,0,0), DateTime.new(2005,2,2,0,0,0), -14400, 3600, :TEST)
183
+ p2 = TimezonePeriod.new(DateTime.new(2005,1,1,0,0,0), DateTime.new(2005,2,2,0,0,0), -14400, 0, :TEST)
184
+ p3 = TimezonePeriod.new(DateTime.new(2005,1,1,0,0,0), DateTime.new(2005,2,2,0,0,0), 7200, 3600, :TEST)
185
+
186
+ assert_equal(DateTime.new(2005,1,19,22,0,0), p1.to_local(DateTime.new(2005,1,20,1,0,0)))
187
+ assert_equal(DateTime.new(2005,1,19,21,0,0), p2.to_local(DateTime.new(2005,1,20,1,0,0)))
188
+ assert_equal(DateTime.new(2005,1,20,4,0,0), p3.to_local(DateTime.new(2005,1,20,1,0,0)))
189
+ end
190
+
191
+ def test_to_utc
192
+ p1 = TimezonePeriod.new(DateTime.new(2005,1,1,0,0,0), DateTime.new(2005,2,2,0,0,0), -14400, 3600, :TEST)
193
+ p2 = TimezonePeriod.new(DateTime.new(2005,1,1,0,0,0), DateTime.new(2005,2,2,0,0,0), -14400, 0, :TEST)
194
+ p3 = TimezonePeriod.new(DateTime.new(2005,1,1,0,0,0), DateTime.new(2005,2,2,0,0,0), 7200, 3600, :TEST)
195
+
196
+ assert_equal(DateTime.new(2005,1,20,4,0,0), p1.to_utc(DateTime.new(2005,1,20,1,0,0)))
197
+ assert_equal(DateTime.new(2005,1,20,5,0,0), p2.to_utc(DateTime.new(2005,1,20,1,0,0)))
198
+ assert_equal(DateTime.new(2005,1,19,22,0,0), p3.to_utc(DateTime.new(2005,1,20,1,0,0)))
199
+ end
200
+ end
@@ -0,0 +1,76 @@
1
+ $:.unshift File.join(File.dirname(__FILE__), "..", "lib")
2
+ require 'test/unit'
3
+ require 'tzinfo/timezone'
4
+
5
+ include TZInfo
6
+
7
+ class TCTimezoneProxy < Test::Unit::TestCase
8
+ def test_not_exist
9
+ proxy = TimezoneProxy.new('Nothing/Special')
10
+ assert_equal('Nothing/Special', proxy.identifier)
11
+ assert_raise(InvalidTimezoneIdentifier) { proxy.now }
12
+ assert_raise(InvalidTimezoneIdentifier) { proxy.current_period }
13
+ assert_raise(InvalidTimezoneIdentifier) { proxy.current_period_and_time }
14
+ assert_raise(InvalidTimezoneIdentifier) { proxy.current_time_and_period }
15
+ assert_raise(InvalidTimezoneIdentifier) { proxy.utc_to_local(DateTime.new(2006,1,1,0,0,0)) }
16
+ assert_raise(InvalidTimezoneIdentifier) { proxy.local_to_utc(DateTime.new(2006,1,1,0,0,0)) }
17
+ assert_raise(InvalidTimezoneIdentifier) { proxy.period_for_utc(DateTime.new(2006,1,1,0,0,0)) }
18
+ assert_raise(InvalidTimezoneIdentifier) { proxy.period_for_local(DateTime.new(2006,1,1,0,0,0)) }
19
+ end
20
+
21
+ def test_valid
22
+ proxy = TimezoneProxy.new('Europe/London')
23
+ assert_equal('Europe/London', proxy.identifier)
24
+
25
+ assert_nothing_raised { proxy.now }
26
+ assert_nothing_raised { proxy.current_period }
27
+ assert_nothing_raised { proxy.current_period_and_time }
28
+ assert_nothing_raised { proxy.current_time_and_period }
29
+
30
+ real = Timezone.get('Europe/London')
31
+
32
+ assert_equal(real.utc_to_local(DateTime.new(2005,8,1,0,0,0)), proxy.utc_to_local(DateTime.new(2005,8,1,0,0,0)))
33
+ assert_equal(real.local_to_utc(DateTime.new(2005,8,1,0,0,0)), proxy.local_to_utc(DateTime.new(2005,8,1,0,0,0)))
34
+ assert_equal(real.period_for_utc(DateTime.new(2005,8,1,0,0,0)), proxy.period_for_utc(DateTime.new(2005,8,1,0,0,0)))
35
+ assert_equal(real.period_for_local(DateTime.new(2005,8,1,0,0,0)), proxy.period_for_local(DateTime.new(2005,8,1,0,0,0)))
36
+ assert_equal(real.identifier, proxy.identifier)
37
+ assert_equal(real.name, proxy.name)
38
+ assert_equal(real.to_s, proxy.to_s)
39
+ assert_equal(real.friendly_identifier(true), proxy.friendly_identifier(true))
40
+ assert_equal(real.friendly_identifier(false), proxy.friendly_identifier(false))
41
+ assert_equal(real.friendly_identifier, proxy.friendly_identifier)
42
+
43
+ assert_equal('Europe/London', proxy.identifier)
44
+
45
+ assert(real == proxy)
46
+ assert(proxy == real)
47
+ assert_equal(0, real <=> proxy)
48
+ assert_equal(0, proxy <=> real)
49
+ end
50
+
51
+ def test_equals
52
+ assert(TimezoneProxy.new('Europe/London') == TimezoneProxy.new('Europe/London'))
53
+ end
54
+
55
+ def test_compare
56
+ assert_equal(0, TimezoneProxy.new('Europe/London') <=> TimezoneProxy.new('Europe/London'))
57
+ assert_equal(0, Timezone.get('Europe/London') <=> TimezoneProxy.new('Europe/London'))
58
+ assert_equal(0, TimezoneProxy.new('Europe/London') <=> Timezone.get('Europe/London'))
59
+ assert((TimezoneProxy.new('Europe/London') <=> TimezoneProxy.new('Europe/Paris')) < 0)
60
+ assert((Timezone.get('Europe/London') <=> TimezoneProxy.new('Europe/Paris')) < 0)
61
+ assert((TimezoneProxy.new('Europe/London') <=> Timezone.get('Europe/Paris')) < 0)
62
+ assert((TimezoneProxy.new('Europe/Paris') <=> TimezoneProxy.new('Europe/London')) > 0)
63
+ assert((Timezone.get('Europe/Paris') <=> TimezoneProxy.new('Europe/London')) > 0)
64
+ assert((TimezoneProxy.new('Europe/Paris') <=> Timezone.get('Europe/London')) > 0)
65
+ assert((TimezoneProxy.new('America/New_York') <=> TimezoneProxy.new('Europe/Paris')) < 0)
66
+ assert((Timezone.get('America/New_York') <=> TimezoneProxy.new('Europe/Paris')) < 0)
67
+ assert((TimezoneProxy.new('America/New_York') <=> Timezone.get('Europe/Paris')) < 0)
68
+ assert((TimezoneProxy.new('Europe/Paris') <=> TimezoneProxy.new('America/New_York')) > 0)
69
+ assert((Timezone.get('Europe/Paris') <=> TimezoneProxy.new('America/New_York')) > 0)
70
+ assert((TimezoneProxy.new('Europe/Paris') <=> Timezone.get('America/New_York')) > 0)
71
+ end
72
+
73
+ def test_kind
74
+ assert_kind_of(Timezone, TimezoneProxy.new('America/New_York'))
75
+ end
76
+ end
@@ -0,0 +1,2 @@
1
+ require 'test/unit'
2
+ Dir['tc_*.rb'].each {|t| require t}
metadata CHANGED
@@ -3,8 +3,8 @@ rubygems_version: 0.8.10
3
3
  specification_version: 1
4
4
  name: tzinfo
5
5
  version: !ruby/object:Gem::Version
6
- version: 0.0.3
7
- date: 2005-09-17
6
+ version: 0.0.4
7
+ date: 2005-09-18
8
8
  summary: Daylight-savings aware timezone library
9
9
  require_paths:
10
10
  - lib
@@ -850,11 +850,21 @@ files:
850
850
  - lib/tzinfo/definitions/US/Pacific.rb
851
851
  - lib/tzinfo/definitions/US/Pacific__m__New.rb
852
852
  - lib/tzinfo/definitions/US/Samoa.rb
853
+ - test/tc_country.rb
854
+ - test/tc_timezone.rb
855
+ - test/tc_timezone_london.rb
856
+ - test/tc_timezone_melbourne.rb
857
+ - test/tc_timezone_new_york.rb
858
+ - test/tc_timezone_period.rb
859
+ - test/tc_timezone_proxy.rb
860
+ - test/ts_all.rb
853
861
  - README
862
+ - CHANGES
854
863
  test_files: []
855
864
  rdoc_options: []
856
865
  extra_rdoc_files:
857
866
  - README
867
+ - CHANGES
858
868
  executables: []
859
869
  extensions: []
860
870
  requirements: []