tzinfo 0.0.2 → 0.0.3

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.

Files changed (4) hide show
  1. data/README +1 -1
  2. data/lib/tzinfo/country.rb +43 -21
  3. data/lib/tzinfo/timezone.rb +119 -22
  4. metadata +2 -2
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.2.gem
67
+ % gem install tzinfo-0.0.3.gem
68
68
 
69
69
  or to automatically download and install:
70
70
 
@@ -8,6 +8,8 @@ module TZInfo
8
8
  # puts us.zone_identifiers
9
9
  # puts us.zones
10
10
  class Country
11
+ include Comparable
12
+
11
13
  # Gets a Country by its ISO 3166 code. Raising an exception if it couldn't
12
14
  # be found.
13
15
  def self.get(identifier)
@@ -43,7 +45,7 @@ module TZInfo
43
45
 
44
46
  end
45
47
 
46
- # The country code.
48
+ # The ISO 3166 country code.
47
49
  def code
48
50
  'Unknown'
49
51
  end
@@ -53,6 +55,11 @@ module TZInfo
53
55
  'Unknown'
54
56
  end
55
57
 
58
+ # Alias for name.
59
+ def to_s
60
+ name
61
+ end
62
+
56
63
  # Array of Timezone identifiers for the country.
57
64
  def zone_names
58
65
  []
@@ -63,33 +70,32 @@ module TZInfo
63
70
  zone_names
64
71
  end
65
72
 
66
- # An array of all the Timezones for this country.
73
+ # An array of all the Timezones for this country. Returns TimezoneProxy
74
+ # objects to avoid the overhead of loading Timezone definitions until
75
+ # a conversion is actually required.
67
76
  def zones
68
77
  zone_names.collect {|zone_name|
69
- Timezone.get(zone_name)
78
+ TimezoneProxy.new(zone_name)
70
79
  }
80
+ end
81
+
82
+ # Two Countries are considered to be equal if their codes are equal.
83
+ def ==(c)
84
+ code == c.code
85
+ end
86
+
87
+ # Compare two Countries based on their code. Returns -1 if tz is less
88
+ # than self, 0 if tz is equal to self and +1 if tz is greater than self.
89
+ def <=>(tz)
90
+ identifier <=> tz.identifier
71
91
  end
72
92
 
73
93
  protected
74
94
  def self.setup
75
95
  class_eval <<CODE
76
96
  @@zone_names = []
77
- def self.add_zone(zone_name)
78
- @@zone_names << zone_name
79
- end
80
-
81
- def self.zones_added
82
- @@zone_names.freeze
83
- end
84
-
85
- def self.set_code(code)
86
- @@code = code
87
- end
97
+ @@instance = new
88
98
 
89
- def self.set_name(name)
90
- @@name = name
91
- end
92
-
93
99
  def zone_names
94
100
  @@zone_names
95
101
  end
@@ -101,11 +107,27 @@ module TZInfo
101
107
  def name
102
108
  @@name
103
109
  end
104
-
105
- @@instance = new
110
+
106
111
  def self.instance
107
112
  @@instance
108
- end
113
+ end
114
+
115
+ protected
116
+ def self.add_zone(zone_name)
117
+ @@zone_names << zone_name
118
+ end
119
+
120
+ def self.zones_added
121
+ @@zone_names.freeze
122
+ end
123
+
124
+ def self.set_code(code)
125
+ @@code = code
126
+ end
127
+
128
+ def self.set_name(name)
129
+ @@name = name
130
+ end
109
131
  CODE
110
132
  end
111
133
  end
@@ -16,6 +16,8 @@ module TZInfo
16
16
  # The timezone information all comes from the tz database
17
17
  # (see http://www.twinsun.com/tz/tz-link.htm)
18
18
  class Timezone
19
+ include Comparable
20
+
19
21
  # Returns a timezone by its identifier (e.g. "Europe/London",
20
22
  # "America/Chicago" or "UTC").
21
23
  #
@@ -35,6 +37,7 @@ module TZInfo
35
37
  # If identifier is nil calls super(), else calls get(identifier).
36
38
  def self.new(identifier = nil)
37
39
  if identifier
40
+ puts 'getting'
38
41
  get(identifier)
39
42
  else
40
43
  super()
@@ -58,11 +61,9 @@ module TZInfo
58
61
  # Returns all the Timezones defined for all Countries. This is not the
59
62
  # complete set of Timezones as some are not country specific (e.g.
60
63
  # 'Etc/GMT').
61
- #
62
- # This method will take a substantial time to return the first time it is
63
- # called as all the Timezone classes will have to be loaded by Ruby. If
64
- # you just want the zone identifiers use all_country_zone_identifiers
65
- # instead.
64
+ #
65
+ # Returns TimezoneProxy objects to avoid the overhead of loading Timezone
66
+ # definitions until a conversion is actually required.
66
67
  def self.all_country_zones
67
68
  Country.all_codes.inject([]) {|zones,country|
68
69
  zones += Country.get(country).zones
@@ -80,8 +81,10 @@ module TZInfo
80
81
  end
81
82
 
82
83
  # Returns all US Timezone instances. A shortcut for
83
- # TZInfo::Country.get('US').zones. If you only need the zone identifiers,
84
- # use us_zone_identifiers instead.
84
+ # TZInfo::Country.get('US').zones.
85
+ #
86
+ # Returns TimezoneProxy objects to avoid the overhead of loading Timezone
87
+ # definitions until a conversion is actually required.
85
88
  def self.us_zones
86
89
  Country.get('US').zones
87
90
  end
@@ -97,6 +100,54 @@ module TZInfo
97
100
  'Unknown'
98
101
  end
99
102
 
103
+ # An alias for identifier.
104
+ def name
105
+ # Don't use alias, as identifier gets overridden.
106
+ identifier
107
+ end
108
+
109
+ # Returns a friendlier version of the idenfitifer.
110
+ def to_s
111
+ friendly_identifier
112
+ end
113
+
114
+ # Returns a friendlier version of the idenfitifer. Set skip_first_part to
115
+ # omit the first part of the identifier (typically a region name) where
116
+ # there is more than one part.
117
+ def friendly_identifier(skip_first_part = false)
118
+ parts = identifier.split('/')
119
+ if parts.empty?
120
+ # shouldn't happen
121
+ identifier
122
+ elsif parts.length == 1
123
+ parts[0]
124
+ else
125
+ if skip_first_part
126
+ result = ''
127
+ else
128
+ result = parts[0] + ' - '
129
+ end
130
+
131
+ parts[1, parts.length - 1].reverse_each {|part|
132
+ part.gsub!(/_/, ' ')
133
+
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')
141
+
142
+ result << part
143
+ result << ', '
144
+ }
145
+
146
+ result.slice!(result.length - 2, 2)
147
+ result
148
+ end
149
+ end
150
+
100
151
  # Returns the TimezonePeriod for the given UTC time. utc can either be
101
152
  # a DateTime or a Time. Any timezone information in utc is ignored (it is
102
153
  # treated as a UTC time).
@@ -207,31 +258,43 @@ module TZInfo
207
258
  [utc_to_local(utc), period_for_utc(utc)]
208
259
  end
209
260
 
261
+ # Two Timezones are considered to be equal if their identifiers are the same.
262
+ def ==(tz)
263
+ identifier == tz.identifier
264
+ end
265
+
266
+ # Compare two Timezones based on their identifier. Returns -1 if tz is less
267
+ # than self, 0 if tz is equal to self and +1 if tz is greater than self.
268
+ def <=>(tz)
269
+ identifier <=> tz.identifier
270
+ end
271
+
210
272
  protected
211
273
  def self.setup
212
274
  class_eval <<CODE
213
275
  @@periods = []
214
- def self.add_period(period)
215
- @@periods << period
216
- end
217
-
218
- def self.set_identifier(identifier)
219
- @@identifier = identifier
220
- end
221
-
222
- def periods
223
- @@periods
224
- end
225
- protected :periods
276
+ @@instance = new
226
277
 
227
278
  def identifier
228
279
  @@identifier
229
- end
280
+ end
230
281
 
231
- @@instance = new
232
282
  def self.instance
233
283
  @@instance
234
- end
284
+ end
285
+
286
+ def periods
287
+ @@periods.freeze
288
+ end
289
+
290
+ protected
291
+ def self.add_period(period)
292
+ @@periods << period
293
+ end
294
+
295
+ def self.set_identifier(identifier)
296
+ @@identifier = identifier
297
+ end
235
298
  CODE
236
299
  end
237
300
 
@@ -252,6 +315,40 @@ CODE
252
315
  end
253
316
  end
254
317
 
318
+ # A proxy class representing a timezone with a given identifier. It can be
319
+ # constructed with an identifier and behaves almost identically to a Timezone
320
+ # loaded through Timezone.get. The first time an attempt is made to perform
321
+ # a conversion on the proxy, the real Timezone class is loaded. If the
322
+ # proxy's identifier was not valid, then an exception will be thrown at this
323
+ # point.
324
+ class TimezoneProxy < Timezone
325
+ # Construct a new TimezoneProxy for the given identifier. The identifier
326
+ # is not checked when constructing the proxy. It will be validated on the
327
+ # first conversion.
328
+ def self.new(identifier)
329
+ # Need to override new to undo the behaviour introduced in Timezone#new.
330
+ tzp = super()
331
+ tzp.instance_eval <<CODE
332
+ @identifier = identifier
333
+ @real_tz = nil
334
+ CODE
335
+ tzp
336
+ end
337
+
338
+ # The identifier of the timezone, e.g. "Europe/Paris".
339
+ def identifier
340
+ @real_tz.nil? ? @identifier : @real_tz.identifier
341
+ end
342
+
343
+ def periods #:nodoc:
344
+ if @real_tz.nil?
345
+ # We now need the actual data. Load in the real timezone.
346
+ @real_tz = Timezone.get(@identifier)
347
+ end
348
+ @real_tz.periods
349
+ end
350
+ end
351
+
255
352
  # A period of time in a timezone where the same offset from UTC applies.
256
353
  class TimezonePeriod
257
354
  # Start time of the period (UTC). May be nil if unbounded.
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.2
7
- date: 2005-09-13
6
+ version: 0.0.3
7
+ date: 2005-09-17
8
8
  summary: Daylight-savings aware timezone library
9
9
  require_paths:
10
10
  - lib