tzinfo 1.2.1 → 2.0.5

Sign up to get free protection for your applications and to get access to all the features.
Files changed (146) hide show
  1. checksums.yaml +5 -5
  2. checksums.yaml.gz.sig +0 -0
  3. data/.yardopts +3 -0
  4. data/CHANGES.md +693 -315
  5. data/LICENSE +13 -13
  6. data/README.md +368 -113
  7. data/lib/tzinfo/annual_rules.rb +71 -0
  8. data/lib/tzinfo/country.rb +148 -120
  9. data/lib/tzinfo/country_timezone.rb +71 -101
  10. data/lib/tzinfo/data_source.rb +400 -144
  11. data/lib/tzinfo/data_sources/constant_offset_data_timezone_info.rb +56 -0
  12. data/lib/tzinfo/data_sources/country_info.rb +42 -0
  13. data/lib/tzinfo/data_sources/data_timezone_info.rb +91 -0
  14. data/lib/tzinfo/data_sources/linked_timezone_info.rb +33 -0
  15. data/lib/tzinfo/data_sources/posix_time_zone_parser.rb +181 -0
  16. data/lib/tzinfo/data_sources/ruby_data_source.rb +145 -0
  17. data/lib/tzinfo/data_sources/timezone_info.rb +47 -0
  18. data/lib/tzinfo/data_sources/transitions_data_timezone_info.rb +214 -0
  19. data/lib/tzinfo/data_sources/zoneinfo_data_source.rb +596 -0
  20. data/lib/tzinfo/data_sources/zoneinfo_reader.rb +486 -0
  21. data/lib/tzinfo/data_sources.rb +8 -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/country_definer.rb +17 -0
  25. data/lib/tzinfo/format1/country_index_definition.rb +64 -0
  26. data/lib/tzinfo/format1/timezone_definer.rb +64 -0
  27. data/lib/tzinfo/format1/timezone_definition.rb +39 -0
  28. data/lib/tzinfo/format1/timezone_index_definition.rb +77 -0
  29. data/lib/tzinfo/format1.rb +10 -0
  30. data/lib/tzinfo/format2/country_definer.rb +68 -0
  31. data/lib/tzinfo/format2/country_index_definer.rb +68 -0
  32. data/lib/tzinfo/format2/country_index_definition.rb +46 -0
  33. data/lib/tzinfo/format2/timezone_definer.rb +94 -0
  34. data/lib/tzinfo/format2/timezone_definition.rb +73 -0
  35. data/lib/tzinfo/format2/timezone_index_definer.rb +45 -0
  36. data/lib/tzinfo/format2/timezone_index_definition.rb +55 -0
  37. data/lib/tzinfo/format2.rb +10 -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 +154 -0
  43. data/lib/tzinfo/timestamp.rb +552 -0
  44. data/lib/tzinfo/timestamp_with_offset.rb +85 -0
  45. data/lib/tzinfo/timezone.rb +997 -473
  46. data/lib/tzinfo/timezone_offset.rb +91 -54
  47. data/lib/tzinfo/timezone_period.rb +163 -188
  48. data/lib/tzinfo/timezone_proxy.rb +75 -49
  49. data/lib/tzinfo/timezone_transition.rb +77 -99
  50. data/lib/tzinfo/transition_rule.rb +455 -0
  51. data/lib/tzinfo/transitions_timezone_period.rb +63 -0
  52. data/lib/tzinfo/untaint_ext.rb +18 -0
  53. data/lib/tzinfo/version.rb +7 -0
  54. data/lib/tzinfo/with_offset.rb +61 -0
  55. data/lib/tzinfo.rb +74 -29
  56. data.tar.gz.sig +0 -0
  57. metadata +72 -120
  58. metadata.gz.sig +0 -0
  59. data/Rakefile +0 -104
  60. data/lib/tzinfo/country_index_definition.rb +0 -31
  61. data/lib/tzinfo/country_info.rb +0 -42
  62. data/lib/tzinfo/data_timezone_info.rb +0 -55
  63. data/lib/tzinfo/linked_timezone_info.rb +0 -26
  64. data/lib/tzinfo/offset_rationals.rb +0 -77
  65. data/lib/tzinfo/ruby_core_support.rb +0 -146
  66. data/lib/tzinfo/ruby_country_info.rb +0 -70
  67. data/lib/tzinfo/ruby_data_source.rb +0 -136
  68. data/lib/tzinfo/time_or_datetime.rb +0 -333
  69. data/lib/tzinfo/timezone_definition.rb +0 -36
  70. data/lib/tzinfo/timezone_index_definition.rb +0 -54
  71. data/lib/tzinfo/timezone_info.rb +0 -30
  72. data/lib/tzinfo/timezone_transition_definition.rb +0 -101
  73. data/lib/tzinfo/transition_data_timezone_info.rb +0 -274
  74. data/lib/tzinfo/zoneinfo_country_info.rb +0 -35
  75. data/lib/tzinfo/zoneinfo_data_source.rb +0 -460
  76. data/lib/tzinfo/zoneinfo_timezone_info.rb +0 -245
  77. data/test/tc_country.rb +0 -236
  78. data/test/tc_country_index_definition.rb +0 -69
  79. data/test/tc_country_info.rb +0 -16
  80. data/test/tc_country_timezone.rb +0 -161
  81. data/test/tc_data_source.rb +0 -192
  82. data/test/tc_data_timezone.rb +0 -99
  83. data/test/tc_data_timezone_info.rb +0 -18
  84. data/test/tc_info_timezone.rb +0 -34
  85. data/test/tc_linked_timezone.rb +0 -155
  86. data/test/tc_linked_timezone_info.rb +0 -23
  87. data/test/tc_offset_rationals.rb +0 -23
  88. data/test/tc_ruby_core_support.rb +0 -168
  89. data/test/tc_ruby_country_info.rb +0 -80
  90. data/test/tc_ruby_data_source.rb +0 -143
  91. data/test/tc_time_or_datetime.rb +0 -639
  92. data/test/tc_timezone.rb +0 -1334
  93. data/test/tc_timezone_definition.rb +0 -113
  94. data/test/tc_timezone_index_definition.rb +0 -73
  95. data/test/tc_timezone_info.rb +0 -11
  96. data/test/tc_timezone_london.rb +0 -143
  97. data/test/tc_timezone_melbourne.rb +0 -142
  98. data/test/tc_timezone_new_york.rb +0 -142
  99. data/test/tc_timezone_offset.rb +0 -126
  100. data/test/tc_timezone_period.rb +0 -548
  101. data/test/tc_timezone_proxy.rb +0 -113
  102. data/test/tc_timezone_transition.rb +0 -352
  103. data/test/tc_timezone_transition_definition.rb +0 -284
  104. data/test/tc_timezone_utc.rb +0 -27
  105. data/test/tc_transition_data_timezone_info.rb +0 -423
  106. data/test/tc_zoneinfo_country_info.rb +0 -64
  107. data/test/tc_zoneinfo_data_source.rb +0 -985
  108. data/test/tc_zoneinfo_timezone_info.rb +0 -814
  109. data/test/test_utils.rb +0 -132
  110. data/test/ts_all.rb +0 -7
  111. data/test/ts_all_ruby.rb +0 -5
  112. data/test/ts_all_zoneinfo.rb +0 -7
  113. data/test/tzinfo-data/tzinfo/data/definitions/America/Argentina/Buenos_Aires.rb +0 -89
  114. data/test/tzinfo-data/tzinfo/data/definitions/America/New_York.rb +0 -315
  115. data/test/tzinfo-data/tzinfo/data/definitions/Australia/Melbourne.rb +0 -218
  116. data/test/tzinfo-data/tzinfo/data/definitions/EST.rb +0 -19
  117. data/test/tzinfo-data/tzinfo/data/definitions/Etc/GMT__m__1.rb +0 -21
  118. data/test/tzinfo-data/tzinfo/data/definitions/Etc/GMT__p__1.rb +0 -21
  119. data/test/tzinfo-data/tzinfo/data/definitions/Etc/UTC.rb +0 -21
  120. data/test/tzinfo-data/tzinfo/data/definitions/Europe/Amsterdam.rb +0 -261
  121. data/test/tzinfo-data/tzinfo/data/definitions/Europe/Andorra.rb +0 -186
  122. data/test/tzinfo-data/tzinfo/data/definitions/Europe/London.rb +0 -321
  123. data/test/tzinfo-data/tzinfo/data/definitions/Europe/Paris.rb +0 -265
  124. data/test/tzinfo-data/tzinfo/data/definitions/Europe/Prague.rb +0 -220
  125. data/test/tzinfo-data/tzinfo/data/definitions/UTC.rb +0 -16
  126. data/test/tzinfo-data/tzinfo/data/indexes/countries.rb +0 -927
  127. data/test/tzinfo-data/tzinfo/data/indexes/timezones.rb +0 -594
  128. data/test/tzinfo-data/tzinfo/data/version.rb +0 -14
  129. data/test/tzinfo-data/tzinfo/data.rb +0 -8
  130. data/test/zoneinfo/America/Argentina/Buenos_Aires +0 -0
  131. data/test/zoneinfo/America/New_York +0 -0
  132. data/test/zoneinfo/Australia/Melbourne +0 -0
  133. data/test/zoneinfo/EST +0 -0
  134. data/test/zoneinfo/Etc/UTC +0 -0
  135. data/test/zoneinfo/Europe/Amsterdam +0 -0
  136. data/test/zoneinfo/Europe/Andorra +0 -0
  137. data/test/zoneinfo/Europe/London +0 -0
  138. data/test/zoneinfo/Europe/Paris +0 -0
  139. data/test/zoneinfo/Europe/Prague +0 -0
  140. data/test/zoneinfo/Factory +0 -0
  141. data/test/zoneinfo/iso3166.tab +0 -275
  142. data/test/zoneinfo/posix/Europe/London +0 -0
  143. data/test/zoneinfo/posixrules +0 -0
  144. data/test/zoneinfo/right/Europe/London +0 -0
  145. data/test/zoneinfo/zone.tab +0 -452
  146. data/tzinfo.gemspec +0 -21
@@ -1,70 +1,96 @@
1
+ # encoding: UTF-8
2
+
1
3
  module TZInfo
2
4
 
3
- # A proxy class representing a timezone with a given identifier. TimezoneProxy
4
- # inherits from Timezone and can be treated like any Timezone loaded with
5
- # Timezone.get.
5
+ # A proxy class standing in for a {Timezone} with a given identifier.
6
+ # {TimezoneProxy} inherits from {Timezone} and can be treated identically to
7
+ # {Timezone} instances loaded with {Timezone.get}.
8
+ #
9
+ # {TimezoneProxy} instances are used to avoid the performance overhead of
10
+ # loading time zone data into memory, for example, by {Timezone.all}.
6
11
  #
7
- # The first time an attempt is made to access the data for the timezone, the
8
- # real Timezone is loaded. If the proxy's identifier was not valid, then an
9
- # exception will be raised at this point.
12
+ # The first time an attempt is made to access the data for the time zone, the
13
+ # real {Timezone} will be loaded is loaded. If the proxy's identifier was not
14
+ # valid, then an exception will be raised at this point.
10
15
  class TimezoneProxy < Timezone
11
- # Construct a new TimezoneProxy for the given identifier. The identifier
12
- # is not checked when constructing the proxy. It will be validated on the
13
- # when the real Timezone is loaded.
14
- def self.new(identifier)
15
- # Need to override new to undo the behaviour introduced in Timezone#new.
16
- tzp = super()
17
- tzp.send(:setup, identifier)
18
- tzp
16
+ # Initializes a new {TimezoneProxy}.
17
+ #
18
+ # The `identifier` parameter is not checked when initializing the proxy. It
19
+ # will be validated when the real {Timezone} instance is loaded.
20
+ #
21
+ # @param identifier [String] an IANA Time Zone Database time zone
22
+ # identifier.
23
+ def initialize(identifier)
24
+ super()
25
+ @identifier = identifier
26
+ @real_timezone = nil
19
27
  end
20
-
21
- # The identifier of the timezone, e.g. "Europe/Paris".
28
+
29
+ # (see Timezone#identifier)
22
30
  def identifier
23
31
  @real_timezone ? @real_timezone.identifier : @identifier
24
32
  end
25
-
26
- # Returns the TimezonePeriod for the given UTC time. utc can either be
27
- # a DateTime, Time or integer timestamp (Time.to_i). Any timezone
28
- # information in utc is ignored (it is treated as a UTC time).
29
- def period_for_utc(utc)
30
- real_timezone.period_for_utc(utc)
33
+
34
+ # (see Timezone#period_for)
35
+ def period_for(time)
36
+ real_timezone.period_for_utc(time)
31
37
  end
32
-
33
- # Returns the set of TimezonePeriod instances that are valid for the given
34
- # local time as an array. If you just want a single period, use
35
- # period_for_local instead and specify how abiguities should be resolved.
36
- # Returns an empty array if no periods are found for the given time.
37
- def periods_for_local(local)
38
- real_timezone.periods_for_local(local)
38
+
39
+ # (see Timezone#periods_for_local)
40
+ def periods_for_local(local_time)
41
+ real_timezone.periods_for_local(local_time)
42
+ end
43
+
44
+ # (see Timezone#transitions_up_to)
45
+ def transitions_up_to(to, from = nil)
46
+ real_timezone.transitions_up_to(to, from)
39
47
  end
40
-
41
- # Returns the canonical zone for this Timezone.
48
+
49
+ # (see Timezone#canonical_zone)
42
50
  def canonical_zone
43
51
  real_timezone.canonical_zone
44
52
  end
45
-
46
- # Dumps this TimezoneProxy for marshalling.
53
+
54
+ # Returns a serialized representation of this {TimezoneProxy}. This method
55
+ # is called when using `Marshal.dump` with an instance of {TimezoneProxy}.
56
+ #
57
+ # @param limit [Integer] the maximum depth to dump - ignored. @return
58
+ # [String] a serialized representation of this {TimezoneProxy}.
59
+ # @return [String] a serialized representation of this {TimezoneProxy}.
47
60
  def _dump(limit)
48
61
  identifier
49
62
  end
50
-
51
- # Loads a marshalled TimezoneProxy.
63
+
64
+ # Loads a {TimezoneProxy} from the serialized representation returned by
65
+ # {_dump}. This is method is called when using `Marshal.load` or
66
+ # `Marshal.restore` to restore a serialized {Timezone}.
67
+ #
68
+ # @param data [String] a serialized representation of a {TimezoneProxy}.
69
+ # @return [TimezoneProxy] the result of converting `data` back into a
70
+ # {TimezoneProxy}.
52
71
  def self._load(data)
53
72
  TimezoneProxy.new(data)
54
73
  end
55
-
74
+
56
75
  private
57
- def setup(identifier)
58
- @identifier = identifier
59
- @real_timezone = nil
60
- end
61
-
62
- def real_timezone
63
- # Thread-safety: It is possible that the value of @real_timezone may be
64
- # calculated multiple times in concurrently executing threads. It is not
65
- # worth the overhead of locking to ensure that @real_timezone is only
66
- # calculated once.
67
- @real_timezone ||= Timezone.get(@identifier)
68
- end
69
- end
76
+
77
+ # Returns the real {Timezone} instance being proxied.
78
+ #
79
+ # The real {Timezone} is loaded using {Timezone.get} on the first access.
80
+ #
81
+ # @return [Timezone] the real {Timezone} instance being proxied.
82
+ def real_timezone
83
+ # Thread-safety: It is possible that the value of @real_timezone may be
84
+ # calculated multiple times in concurrently executing threads. It is not
85
+ # worth the overhead of locking to ensure that @real_timezone is only
86
+ # calculated once.
87
+ unless @real_timezone
88
+ result = Timezone.get(@identifier)
89
+ return result if frozen?
90
+ @real_timezone = result
91
+ end
92
+
93
+ @real_timezone
94
+ end
95
+ end
70
96
  end
@@ -1,120 +1,98 @@
1
+ # encoding: UTF-8
2
+ # frozen_string_literal: true
3
+
1
4
  module TZInfo
2
- # Represents a transition from one timezone offset to another at a particular
3
- # date and time.
5
+ # Represents a transition from one observed UTC offset ({TimezoneOffset} to
6
+ # another for a time zone.
4
7
  class TimezoneTransition
5
- # The offset this transition changes to (a TimezoneOffset instance).
8
+ # @return [TimezoneOffset] the offset this transition changes to.
6
9
  attr_reader :offset
7
-
8
- # The offset this transition changes from (a TimezoneOffset instance).
10
+
11
+ # @return [TimezoneOffset] the offset this transition changes from.
9
12
  attr_reader :previous_offset
10
-
11
- # Initializes a new TimezoneTransition.
13
+
14
+ # When this transition occurs as an `Integer` number of seconds since
15
+ # 1970-01-01 00:00:00 UTC ignoring leap seconds (i.e. each day is treated as
16
+ # if it were 86,400 seconds long). Equivalent to the result of calling the
17
+ # {Timestamp#value value} method on the {Timestamp} returned by {at}.
18
+ #
19
+ # @return [Integer] when this transition occurs as a number of seconds since
20
+ # 1970-01-01 00:00:00 UTC ignoring leap seconds.
21
+ attr_reader :timestamp_value
22
+
23
+ # Initializes a new {TimezoneTransition}.
24
+ #
25
+ # {TimezoneTransition} instances should not normally be constructed
26
+ # manually.
12
27
  #
13
- # TimezoneTransition instances should not normally be constructed manually.
14
- def initialize(offset, previous_offset)
28
+ # @param offset [TimezoneOffset] the offset the transition changes to.
29
+ # @param previous_offset [TimezoneOffset] the offset the transition changes
30
+ # from.
31
+ # @param timestamp_value [Integer] when the transition occurs as a
32
+ # number of seconds since 1970-01-01 00:00:00 UTC ignoring leap seconds
33
+ # (i.e. each day is treated as if it were 86,400 seconds long).
34
+ def initialize(offset, previous_offset, timestamp_value)
15
35
  @offset = offset
16
36
  @previous_offset = previous_offset
17
- @local_end_at = nil
18
- @local_start_at = nil
37
+ @timestamp_value = timestamp_value
19
38
  end
20
-
21
- # A TimeOrDateTime instance representing the UTC time when this transition
22
- # occurs.
39
+
40
+ # Returns a {Timestamp} instance representing the UTC time when this
41
+ # transition occurs.
42
+ #
43
+ # To obtain the result as a `Time` or `DateTime`, call either
44
+ # {Timestamp#to_time to_time} or {Timestamp#to_datetime to_datetime} on the
45
+ # {Timestamp} instance that is returned.
46
+ #
47
+ # @return [Timestamp] the UTC time when this transition occurs.
23
48
  def at
24
- raise_not_implemented('at')
25
- end
26
-
27
- # The UTC time when this transition occurs, returned as a DateTime instance.
28
- def datetime
29
- at.to_datetime
49
+ Timestamp.utc(@timestamp_value)
30
50
  end
31
-
32
- # The UTC time when this transition occurs, returned as a Time instance.
33
- def time
34
- at.to_time
35
- end
36
-
37
- # A TimeOrDateTime instance representing the local time when this transition
38
- # causes the previous observance to end (calculated from at using
39
- # previous_offset).
51
+
52
+ # Returns a {TimestampWithOffset} instance representing the local time when
53
+ # this transition causes the previous observance to end (calculated from
54
+ # {at} using {previous_offset}).
55
+ #
56
+ # To obtain the result as a `Time` or `DateTime`, call either
57
+ # {TimestampWithOffset#to_time to_time} or {TimestampWithOffset#to_datetime
58
+ # to_datetime} on the {TimestampWithOffset} instance that is returned.
59
+ #
60
+ # @return [TimestampWithOffset] the local time when this transition causes
61
+ # the previous observance to end.
40
62
  def local_end_at
41
- # Thread-safety: It is possible that the value of @local_end_at may be
42
- # calculated multiple times in concurrently executing threads. It is not
43
- # worth the overhead of locking to ensure that @local_end_at is only
44
- # calculated once.
45
-
46
- @local_end_at = at.add_with_convert(@previous_offset.utc_total_offset) unless @local_end_at
47
- @local_end_at
48
- end
49
-
50
- # The local time when this transition causes the previous observance to end,
51
- # returned as a DateTime instance.
52
- def local_end
53
- local_end_at.to_datetime
63
+ TimestampWithOffset.new(@timestamp_value, 0, @previous_offset.observed_utc_offset).set_timezone_offset(@previous_offset)
54
64
  end
55
-
56
- # The local time when this transition causes the previous observance to end,
57
- # returned as a Time instance.
58
- def local_end_time
59
- local_end_at.to_time
60
- end
61
-
62
- # A TimeOrDateTime instance representing the local time when this transition
63
- # causes the next observance to start (calculated from at using offset).
65
+
66
+ # Returns a {TimestampWithOffset} instance representing the local time when
67
+ # this transition causes the next observance to start (calculated from {at}
68
+ # using {offset}).
69
+ #
70
+ # To obtain the result as a `Time` or `DateTime`, call either
71
+ # {TimestampWithOffset#to_time to_time} or {TimestampWithOffset#to_datetime
72
+ # to_datetime} on the {TimestampWithOffset} instance that is returned.
73
+ #
74
+ # @return [TimestampWithOffset] the local time when this transition causes
75
+ # the next observance to start.
64
76
  def local_start_at
65
- # Thread-safety: It is possible that the value of @local_start_at may be
66
- # calculated multiple times in concurrently executing threads. It is not
67
- # worth the overhead of locking to ensure that @local_start_at is only
68
- # calculated once.
69
-
70
- @local_start_at = at.add_with_convert(@offset.utc_total_offset) unless @local_start_at
71
- @local_start_at
77
+ TimestampWithOffset.new(@timestamp_value, 0, @offset.observed_utc_offset).set_timezone_offset(@offset)
72
78
  end
73
-
74
- # The local time when this transition causes the next observance to start,
75
- # returned as a DateTime instance.
76
- def local_start
77
- local_start_at.to_datetime
78
- end
79
-
80
- # The local time when this transition causes the next observance to start,
81
- # returned as a Time instance.
82
- def local_start_time
83
- local_start_at.to_time
84
- end
85
-
86
- # Returns true if this TimezoneTransition is equal to the given
87
- # TimezoneTransition. Two TimezoneTransition instances are
88
- # considered to be equal by == if offset, previous_offset and at are all
89
- # equal.
79
+
80
+ # Determines if this {TimezoneTransition} is equal to another instance.
81
+ #
82
+ # @param tti [Object] the instance to test for equality.
83
+ # @return [Boolean] `true` if `tti` is a {TimezoneTransition} with the same
84
+ # {offset}, {previous_offset} and {timestamp_value} as this
85
+ # {TimezoneTransition}, otherwise `false`.
90
86
  def ==(tti)
91
87
  tti.kind_of?(TimezoneTransition) &&
92
- offset == tti.offset && previous_offset == tti.previous_offset && at == tti.at
88
+ offset == tti.offset && previous_offset == tti.previous_offset && timestamp_value == tti.timestamp_value
93
89
  end
94
-
95
- # Returns true if this TimezoneTransition is equal to the given
96
- # TimezoneTransition. Two TimezoneTransition instances are
97
- # considered to be equal by eql? if offset, previous_offset and at are all
98
- # equal and the type used to define at in both instances is the same.
99
- def eql?(tti)
100
- tti.kind_of?(TimezoneTransition) &&
101
- offset == tti.offset && previous_offset == tti.previous_offset && at.eql?(tti.at)
102
- end
103
-
104
- # Returns a hash of this TimezoneTransition instance.
105
- def hash
106
- @offset.hash ^ @previous_offset.hash ^ at.hash
107
- end
108
-
109
- # Returns internal object state as a programmer-readable string.
110
- def inspect
111
- "#<#{self.class}: #{at.inspect},#{@offset.inspect}>"
112
- end
113
-
114
- private
90
+ alias eql? ==
115
91
 
116
- def raise_not_implemented(method_name)
117
- raise NotImplementedError, "Subclasses must override #{method_name}"
92
+ # @return [Integer] a hash based on {offset}, {previous_offset} and
93
+ # {timestamp_value}.
94
+ def hash
95
+ [@offset, @previous_offset, @timestamp_value].hash
118
96
  end
119
97
  end
120
98
  end