tzinfo 1.2.9 → 2.0.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (152) hide show
  1. checksums.yaml +4 -4
  2. checksums.yaml.gz.sig +1 -3
  3. data.tar.gz.sig +0 -0
  4. data/.yardopts +3 -0
  5. data/CHANGES.md +535 -386
  6. data/LICENSE +12 -12
  7. data/README.md +368 -114
  8. data/lib/tzinfo.rb +70 -40
  9. data/lib/tzinfo/annual_rules.rb +32 -12
  10. data/lib/tzinfo/country.rb +141 -129
  11. data/lib/tzinfo/country_timezone.rb +70 -112
  12. data/lib/tzinfo/data_source.rb +389 -144
  13. data/lib/tzinfo/data_sources.rb +8 -0
  14. data/lib/tzinfo/data_sources/constant_offset_data_timezone_info.rb +56 -0
  15. data/lib/tzinfo/data_sources/country_info.rb +42 -0
  16. data/lib/tzinfo/data_sources/data_timezone_info.rb +91 -0
  17. data/lib/tzinfo/data_sources/linked_timezone_info.rb +33 -0
  18. data/lib/tzinfo/data_sources/posix_time_zone_parser.rb +181 -0
  19. data/lib/tzinfo/data_sources/ruby_data_source.rb +145 -0
  20. data/lib/tzinfo/data_sources/timezone_info.rb +47 -0
  21. data/lib/tzinfo/data_sources/transitions_data_timezone_info.rb +214 -0
  22. data/lib/tzinfo/data_sources/zoneinfo_data_source.rb +580 -0
  23. data/lib/tzinfo/data_sources/zoneinfo_reader.rb +486 -0
  24. data/lib/tzinfo/data_timezone.rb +33 -47
  25. data/lib/tzinfo/datetime_with_offset.rb +153 -0
  26. data/lib/tzinfo/format1.rb +10 -0
  27. data/lib/tzinfo/format1/country_definer.rb +17 -0
  28. data/lib/tzinfo/format1/country_index_definition.rb +64 -0
  29. data/lib/tzinfo/format1/timezone_definer.rb +64 -0
  30. data/lib/tzinfo/format1/timezone_definition.rb +39 -0
  31. data/lib/tzinfo/format1/timezone_index_definition.rb +77 -0
  32. data/lib/tzinfo/format2.rb +10 -0
  33. data/lib/tzinfo/format2/country_definer.rb +68 -0
  34. data/lib/tzinfo/format2/country_index_definer.rb +68 -0
  35. data/lib/tzinfo/format2/country_index_definition.rb +46 -0
  36. data/lib/tzinfo/format2/timezone_definer.rb +94 -0
  37. data/lib/tzinfo/format2/timezone_definition.rb +73 -0
  38. data/lib/tzinfo/format2/timezone_index_definer.rb +45 -0
  39. data/lib/tzinfo/format2/timezone_index_definition.rb +55 -0
  40. data/lib/tzinfo/info_timezone.rb +26 -21
  41. data/lib/tzinfo/linked_timezone.rb +33 -52
  42. data/lib/tzinfo/offset_timezone_period.rb +42 -0
  43. data/lib/tzinfo/string_deduper.rb +118 -0
  44. data/lib/tzinfo/time_with_offset.rb +154 -0
  45. data/lib/tzinfo/timestamp.rb +548 -0
  46. data/lib/tzinfo/timestamp_with_offset.rb +85 -0
  47. data/lib/tzinfo/timezone.rb +989 -502
  48. data/lib/tzinfo/timezone_offset.rb +84 -74
  49. data/lib/tzinfo/timezone_period.rb +151 -217
  50. data/lib/tzinfo/timezone_proxy.rb +70 -79
  51. data/lib/tzinfo/timezone_transition.rb +77 -109
  52. data/lib/tzinfo/transition_rule.rb +207 -77
  53. data/lib/tzinfo/transitions_timezone_period.rb +63 -0
  54. data/lib/tzinfo/untaint_ext.rb +18 -0
  55. data/lib/tzinfo/version.rb +7 -0
  56. data/lib/tzinfo/with_offset.rb +61 -0
  57. metadata +49 -103
  58. metadata.gz.sig +0 -0
  59. data/Rakefile +0 -107
  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/posix_time_zone_parser.rb +0 -136
  66. data/lib/tzinfo/ruby_core_support.rb +0 -169
  67. data/lib/tzinfo/ruby_country_info.rb +0 -74
  68. data/lib/tzinfo/ruby_data_source.rb +0 -140
  69. data/lib/tzinfo/time_or_datetime.rb +0 -351
  70. data/lib/tzinfo/timezone_definition.rb +0 -36
  71. data/lib/tzinfo/timezone_index_definition.rb +0 -54
  72. data/lib/tzinfo/timezone_info.rb +0 -30
  73. data/lib/tzinfo/timezone_transition_definition.rb +0 -104
  74. data/lib/tzinfo/transition_data_timezone_info.rb +0 -274
  75. data/lib/tzinfo/zoneinfo_country_info.rb +0 -37
  76. data/lib/tzinfo/zoneinfo_data_source.rb +0 -497
  77. data/lib/tzinfo/zoneinfo_timezone_info.rb +0 -520
  78. data/test/tc_annual_rules.rb +0 -95
  79. data/test/tc_country.rb +0 -238
  80. data/test/tc_country_index_definition.rb +0 -69
  81. data/test/tc_country_info.rb +0 -16
  82. data/test/tc_country_timezone.rb +0 -173
  83. data/test/tc_data_source.rb +0 -218
  84. data/test/tc_data_timezone.rb +0 -99
  85. data/test/tc_data_timezone_info.rb +0 -18
  86. data/test/tc_info_timezone.rb +0 -34
  87. data/test/tc_linked_timezone.rb +0 -155
  88. data/test/tc_linked_timezone_info.rb +0 -23
  89. data/test/tc_offset_rationals.rb +0 -23
  90. data/test/tc_posix_time_zone_parser.rb +0 -261
  91. data/test/tc_ruby_core_support.rb +0 -168
  92. data/test/tc_ruby_country_info.rb +0 -110
  93. data/test/tc_ruby_data_source.rb +0 -167
  94. data/test/tc_time_or_datetime.rb +0 -674
  95. data/test/tc_timezone.rb +0 -1361
  96. data/test/tc_timezone_definition.rb +0 -113
  97. data/test/tc_timezone_index_definition.rb +0 -73
  98. data/test/tc_timezone_info.rb +0 -11
  99. data/test/tc_timezone_london.rb +0 -143
  100. data/test/tc_timezone_melbourne.rb +0 -142
  101. data/test/tc_timezone_new_york.rb +0 -142
  102. data/test/tc_timezone_offset.rb +0 -126
  103. data/test/tc_timezone_period.rb +0 -555
  104. data/test/tc_timezone_proxy.rb +0 -136
  105. data/test/tc_timezone_transition.rb +0 -366
  106. data/test/tc_timezone_transition_definition.rb +0 -295
  107. data/test/tc_timezone_utc.rb +0 -27
  108. data/test/tc_transition_data_timezone_info.rb +0 -433
  109. data/test/tc_transition_rule.rb +0 -663
  110. data/test/tc_zoneinfo_country_info.rb +0 -78
  111. data/test/tc_zoneinfo_data_source.rb +0 -1204
  112. data/test/tc_zoneinfo_timezone_info.rb +0 -2153
  113. data/test/test_utils.rb +0 -192
  114. data/test/ts_all.rb +0 -7
  115. data/test/ts_all_ruby.rb +0 -5
  116. data/test/ts_all_zoneinfo.rb +0 -9
  117. data/test/tzinfo-data/tzinfo/data.rb +0 -8
  118. data/test/tzinfo-data/tzinfo/data/definitions/America/Argentina/Buenos_Aires.rb +0 -89
  119. data/test/tzinfo-data/tzinfo/data/definitions/America/New_York.rb +0 -327
  120. data/test/tzinfo-data/tzinfo/data/definitions/Australia/Melbourne.rb +0 -230
  121. data/test/tzinfo-data/tzinfo/data/definitions/EST.rb +0 -19
  122. data/test/tzinfo-data/tzinfo/data/definitions/Etc/GMT__m__1.rb +0 -21
  123. data/test/tzinfo-data/tzinfo/data/definitions/Etc/GMT__p__1.rb +0 -21
  124. data/test/tzinfo-data/tzinfo/data/definitions/Etc/UTC.rb +0 -21
  125. data/test/tzinfo-data/tzinfo/data/definitions/Europe/Amsterdam.rb +0 -273
  126. data/test/tzinfo-data/tzinfo/data/definitions/Europe/Andorra.rb +0 -198
  127. data/test/tzinfo-data/tzinfo/data/definitions/Europe/London.rb +0 -333
  128. data/test/tzinfo-data/tzinfo/data/definitions/Europe/Paris.rb +0 -277
  129. data/test/tzinfo-data/tzinfo/data/definitions/Europe/Prague.rb +0 -235
  130. data/test/tzinfo-data/tzinfo/data/definitions/UTC.rb +0 -16
  131. data/test/tzinfo-data/tzinfo/data/indexes/countries.rb +0 -940
  132. data/test/tzinfo-data/tzinfo/data/indexes/timezones.rb +0 -609
  133. data/test/tzinfo-data/tzinfo/data/version.rb +0 -20
  134. data/test/zoneinfo/America/Argentina/Buenos_Aires +0 -0
  135. data/test/zoneinfo/America/New_York +0 -0
  136. data/test/zoneinfo/Australia/Melbourne +0 -0
  137. data/test/zoneinfo/EST +0 -0
  138. data/test/zoneinfo/Etc/UTC +0 -0
  139. data/test/zoneinfo/Europe/Amsterdam +0 -0
  140. data/test/zoneinfo/Europe/Andorra +0 -0
  141. data/test/zoneinfo/Europe/London +0 -0
  142. data/test/zoneinfo/Europe/Paris +0 -0
  143. data/test/zoneinfo/Europe/Prague +0 -0
  144. data/test/zoneinfo/Factory +0 -0
  145. data/test/zoneinfo/iso3166.tab +0 -274
  146. data/test/zoneinfo/leapseconds +0 -78
  147. data/test/zoneinfo/posix/Europe/London +0 -0
  148. data/test/zoneinfo/posixrules +0 -0
  149. data/test/zoneinfo/right/Europe/London +0 -0
  150. data/test/zoneinfo/zone.tab +0 -452
  151. data/test/zoneinfo/zone1970.tab +0 -384
  152. data/tzinfo.gemspec +0 -21
@@ -1,74 +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
- result = zones.collect {|zone| zone.identifier}.freeze
26
- return result if frozen?
27
- @zone_identifiers = result
28
- end
29
-
30
- @zone_identifiers
31
- end
32
-
33
- # Returns a frozen array of all the timezones for the for the country as
34
- # CountryTimezone instances. These are in the order they were added using
35
- # the timezone method.
36
- def zones
37
- # Thread-safety: It is possible that the value of @zones may be
38
- # calculated multiple times in concurrently executing threads. It is not
39
- # worth the overhead of locking to ensure that @zones is only
40
- # calculated once.
41
-
42
- unless @zones
43
- zones = Zones.new
44
- @block.call(zones) if @block
45
- result = zones.list.freeze
46
- return result if frozen?
47
- @block = nil
48
- @zones = result
49
- end
50
-
51
- @zones
52
- end
53
-
54
- # An instance of the Zones class is passed to the block used to define
55
- # timezones.
56
- #
57
- # @private
58
- class Zones #:nodoc:
59
- attr_reader :list
60
-
61
- def initialize
62
- @list = []
63
- end
64
-
65
- # Called by the index data to define a timezone for the country.
66
- def timezone(identifier, latitude_numerator, latitude_denominator,
67
- longitude_numerator, longitude_denominator, description = nil)
68
- @list << CountryTimezone.new!(identifier, latitude_numerator,
69
- latitude_denominator, longitude_numerator, longitude_denominator,
70
- description)
71
- end
72
- end
73
- end
74
- end
@@ -1,140 +0,0 @@
1
- module TZInfo
2
- # Use send as a workaround for erroneous 'wrong number of arguments' errors
3
- # with JRuby 9.0.5.0 when calling methods with Java implementations. See #114.
4
- send(:using, RubyCoreSupport::UntaintExt) if RubyCoreSupport.const_defined?(:UntaintExt)
5
-
6
- # A DataSource that loads data from the set of Ruby modules included in the
7
- # TZInfo::Data library (tzinfo-data gem).
8
- #
9
- # To have TZInfo use this DataSource, call TZInfo::DataSource.set as follows:
10
- #
11
- # TZInfo::DataSource.set(:ruby)
12
- class RubyDataSource < DataSource
13
- # Whether the timezone index has been loaded yet.
14
- @@timezone_index_loaded = false
15
-
16
- # Whether the country index has been loaded yet.
17
- @@country_index_loaded = false
18
-
19
- # Initializes a new RubyDataSource instance.
20
- def initialize
21
- tzinfo_data = File.join('tzinfo', 'data')
22
- begin
23
- require(tzinfo_data)
24
-
25
- data_file = File.join('', 'tzinfo', 'data.rb')
26
- path = $".reverse_each.detect {|p| p.end_with?(data_file) }
27
- if path
28
- @base_path = File.join(File.dirname(path), 'data').untaint
29
- else
30
- @base_path = tzinfo_data
31
- end
32
- rescue LoadError
33
- @base_path = tzinfo_data
34
- end
35
- end
36
-
37
- # Returns a TimezoneInfo instance for a given identifier.
38
- # Raises InvalidTimezoneIdentifier if the timezone is not found or the
39
- # identifier is invalid.
40
- def load_timezone_info(identifier)
41
- raise InvalidTimezoneIdentifier, 'Invalid identifier' if identifier !~ /^[A-Za-z0-9\+\-_]+(\/[A-Za-z0-9\+\-_]+)*$/
42
-
43
- identifier = identifier.gsub(/-/, '__m__').gsub(/\+/, '__p__')
44
-
45
- # Untaint identifier after it has been reassigned to a new string. We
46
- # don't want to modify the original identifier. identifier may also be
47
- # frozen and therefore cannot be untainted.
48
- identifier.untaint
49
-
50
- identifier = identifier.split('/')
51
- begin
52
- require_definition(identifier)
53
-
54
- m = Data::Definitions
55
- identifier.each {|part|
56
- m = m.const_get(part)
57
- }
58
-
59
- m.get
60
- rescue LoadError, NameError => e
61
- raise InvalidTimezoneIdentifier, e.message
62
- end
63
- end
64
-
65
- # Returns an array of all the available timezone identifiers.
66
- def timezone_identifiers
67
- load_timezone_index
68
- Data::Indexes::Timezones.timezones
69
- end
70
-
71
- # Returns an array of all the available timezone identifiers for
72
- # data timezones (i.e. those that actually contain definitions).
73
- def data_timezone_identifiers
74
- load_timezone_index
75
- Data::Indexes::Timezones.data_timezones
76
- end
77
-
78
- # Returns an array of all the available timezone identifiers that
79
- # are links to other timezones.
80
- def linked_timezone_identifiers
81
- load_timezone_index
82
- Data::Indexes::Timezones.linked_timezones
83
- end
84
-
85
- # Returns a CountryInfo instance for the given ISO 3166-1 alpha-2
86
- # country code. Raises InvalidCountryCode if the country could not be found
87
- # or the code is invalid.
88
- def load_country_info(code)
89
- load_country_index
90
- info = Data::Indexes::Countries.countries[code]
91
- raise InvalidCountryCode, 'Invalid country code' unless info
92
- info
93
- end
94
-
95
- # Returns an array of all the available ISO 3166-1 alpha-2
96
- # country codes.
97
- def country_codes
98
- load_country_index
99
- Data::Indexes::Countries.countries.keys.freeze
100
- end
101
-
102
- # Returns the name of this DataSource.
103
- def to_s
104
- "Ruby DataSource"
105
- end
106
-
107
- private
108
-
109
- # Requires a zone definition by its identifier (split on /).
110
- def require_definition(identifier)
111
- require_data(*(['definitions'] + identifier))
112
- end
113
-
114
- # Requires an index by its name.
115
- def require_index(name)
116
- require_data(*['indexes', name])
117
- end
118
-
119
- # Requires a file from tzinfo/data.
120
- def require_data(*file)
121
- require(File.join(@base_path, *file))
122
- end
123
-
124
- # Loads in the index of timezones if it hasn't already been loaded.
125
- def load_timezone_index
126
- unless @@timezone_index_loaded
127
- require_index('timezones')
128
- @@timezone_index_loaded = true
129
- end
130
- end
131
-
132
- # Loads in the index of countries if it hasn't already been loaded.
133
- def load_country_index
134
- unless @@country_index_loaded
135
- require_index('countries')
136
- @@country_index_loaded = true
137
- end
138
- end
139
- end
140
- end
@@ -1,351 +0,0 @@
1
- require 'date'
2
- require 'rational' unless defined?(Rational)
3
- require 'time'
4
-
5
- module TZInfo
6
- # Used by TZInfo internally to represent either a Time, DateTime or
7
- # an Integer timestamp (seconds since 1970-01-01 00:00:00).
8
- class TimeOrDateTime
9
- include Comparable
10
-
11
- # Constructs a new TimeOrDateTime. timeOrDateTime can be a Time, DateTime
12
- # or Integer. If using a Time or DateTime, any time zone information
13
- # is ignored.
14
- #
15
- # Integer timestamps must be within the range supported by Time on the
16
- # platform being used.
17
- def initialize(timeOrDateTime)
18
- @time = nil
19
- @datetime = nil
20
- @timestamp = nil
21
-
22
- if timeOrDateTime.is_a?(Time)
23
- @time = timeOrDateTime
24
-
25
- # Avoid using the slower Rational class unless necessary.
26
- nsec = RubyCoreSupport.time_nsec(@time)
27
- usec = nsec % 1000 == 0 ? nsec / 1000 : Rational(nsec, 1000)
28
-
29
- @time = Time.utc(@time.year, @time.mon, @time.mday, @time.hour, @time.min, @time.sec, usec) unless @time.utc?
30
- @orig = @time
31
- elsif timeOrDateTime.is_a?(DateTime)
32
- @datetime = timeOrDateTime
33
- @datetime = @datetime.new_offset(0) unless @datetime.offset == 0
34
- @orig = @datetime
35
- else
36
- @timestamp = timeOrDateTime.to_i
37
-
38
- if !RubyCoreSupport.time_supports_64bit && (@timestamp > 2147483647 || @timestamp < -2147483648 || (@timestamp < 0 && !RubyCoreSupport.time_supports_negative))
39
- raise RangeError, 'Timestamp is outside the supported range of Time on this platform'
40
- end
41
-
42
- @orig = @timestamp
43
- end
44
- end
45
-
46
- # Returns the time as a Time.
47
- #
48
- # When converting from a DateTime, the result is truncated to microsecond
49
- # precision.
50
- def to_time
51
- # Thread-safety: It is possible that the value of @time may be
52
- # calculated multiple times in concurrently executing threads. It is not
53
- # worth the overhead of locking to ensure that @time is only
54
- # calculated once.
55
-
56
- unless @time
57
- result = if @timestamp
58
- Time.at(@timestamp).utc
59
- else
60
- Time.utc(year, mon, mday, hour, min, sec, usec)
61
- end
62
-
63
- return result if frozen?
64
- @time = result
65
- end
66
-
67
- @time
68
- end
69
-
70
- # Returns the time as a DateTime.
71
- #
72
- # When converting from a Time, the result is truncated to microsecond
73
- # precision.
74
- def to_datetime
75
- # Thread-safety: It is possible that the value of @datetime may be
76
- # calculated multiple times in concurrently executing threads. It is not
77
- # worth the overhead of locking to ensure that @datetime is only
78
- # calculated once.
79
-
80
- unless @datetime
81
- # Avoid using Rational unless necessary.
82
- u = usec
83
- s = u == 0 ? sec : Rational(sec * 1000000 + u, 1000000)
84
- result = RubyCoreSupport.datetime_new(year, mon, mday, hour, min, s)
85
- return result if frozen?
86
- @datetime = result
87
- end
88
-
89
- @datetime
90
- end
91
-
92
- # Returns the time as an integer timestamp.
93
- def to_i
94
- # Thread-safety: It is possible that the value of @timestamp may be
95
- # calculated multiple times in concurrently executing threads. It is not
96
- # worth the overhead of locking to ensure that @timestamp is only
97
- # calculated once.
98
-
99
- unless @timestamp
100
- result = to_time.to_i
101
- return result if frozen?
102
- @timestamp = result
103
- end
104
-
105
- @timestamp
106
- end
107
-
108
- # Returns the time as the original time passed to new.
109
- def to_orig
110
- @orig
111
- end
112
-
113
- # Returns a string representation of the TimeOrDateTime.
114
- def to_s
115
- if @orig.is_a?(Time)
116
- "Time: #{@orig.to_s}"
117
- elsif @orig.is_a?(DateTime)
118
- "DateTime: #{@orig.to_s}"
119
- else
120
- "Timestamp: #{@orig.to_s}"
121
- end
122
- end
123
-
124
- # Returns internal object state as a programmer-readable string.
125
- def inspect
126
- "#<#{self.class}: #{@orig.inspect}>"
127
- end
128
-
129
- # Returns the year.
130
- def year
131
- if @time
132
- @time.year
133
- elsif @datetime
134
- @datetime.year
135
- else
136
- to_time.year
137
- end
138
- end
139
-
140
- # Returns the month of the year (1..12).
141
- def mon
142
- if @time
143
- @time.mon
144
- elsif @datetime
145
- @datetime.mon
146
- else
147
- to_time.mon
148
- end
149
- end
150
- alias :month :mon
151
-
152
- # Returns the day of the month (1..n).
153
- def mday
154
- if @time
155
- @time.mday
156
- elsif @datetime
157
- @datetime.mday
158
- else
159
- to_time.mday
160
- end
161
- end
162
- alias :day :mday
163
-
164
- # Returns the day of the week (0..6 for Sunday to Saturday).
165
- def wday
166
- if @time
167
- @time.wday
168
- elsif @datetime
169
- @datetime.wday
170
- else
171
- to_time.wday
172
- end
173
- end
174
-
175
- # Returns the hour of the day (0..23).
176
- def hour
177
- if @time
178
- @time.hour
179
- elsif @datetime
180
- @datetime.hour
181
- else
182
- to_time.hour
183
- end
184
- end
185
-
186
- # Returns the minute of the hour (0..59).
187
- def min
188
- if @time
189
- @time.min
190
- elsif @datetime
191
- @datetime.min
192
- else
193
- to_time.min
194
- end
195
- end
196
-
197
- # Returns the second of the minute (0..60). (60 for a leap second).
198
- def sec
199
- if @time
200
- @time.sec
201
- elsif @datetime
202
- @datetime.sec
203
- else
204
- to_time.sec
205
- end
206
- end
207
-
208
- # Returns the number of microseconds for the time.
209
- def usec
210
- if @time
211
- @time.usec
212
- elsif @datetime
213
- # Ruby 1.8 has sec_fraction (of which the documentation says
214
- # 'I do NOT recommend you to use this method'). sec_fraction no longer
215
- # exists in Ruby 1.9.
216
-
217
- # Calculate the sec_fraction from the day_fraction.
218
- ((@datetime.day_fraction - OffsetRationals.rational_for_offset(@datetime.hour * 3600 + @datetime.min * 60 + @datetime.sec)) * 86400000000).to_i
219
- else
220
- 0
221
- end
222
- end
223
-
224
- # Compares this TimeOrDateTime with another Time, DateTime, timestamp
225
- # (Integer) or TimeOrDateTime. Returns -1, 0 or +1 depending
226
- # whether the receiver is less than, equal to, or greater than
227
- # timeOrDateTime.
228
- #
229
- # Returns nil if the passed in timeOrDateTime is not comparable with
230
- # TimeOrDateTime instances.
231
- #
232
- # Comparisons involving a DateTime will be performed using DateTime#<=>.
233
- # Comparisons that don't involve a DateTime, but include a Time will be
234
- # performed with Time#<=>. Otherwise comparisons will be performed with
235
- # Integer#<=>.
236
- def <=>(timeOrDateTime)
237
- return nil unless timeOrDateTime.is_a?(TimeOrDateTime) ||
238
- timeOrDateTime.is_a?(Time) ||
239
- timeOrDateTime.is_a?(DateTime) ||
240
- timeOrDateTime.respond_to?(:to_i)
241
-
242
- unless timeOrDateTime.is_a?(TimeOrDateTime)
243
- timeOrDateTime = TimeOrDateTime.wrap(timeOrDateTime)
244
- end
245
-
246
- orig = timeOrDateTime.to_orig
247
-
248
- if @orig.is_a?(DateTime) || orig.is_a?(DateTime)
249
- # If either is a DateTime, assume it is there for a reason
250
- # (i.e. for its larger range of acceptable values on 32-bit systems).
251
- to_datetime <=> timeOrDateTime.to_datetime
252
- elsif @orig.is_a?(Time) || orig.is_a?(Time)
253
- to_time <=> timeOrDateTime.to_time
254
- else
255
- to_i <=> timeOrDateTime.to_i
256
- end
257
- end
258
-
259
- # Adds a number of seconds to the TimeOrDateTime. Returns a new
260
- # TimeOrDateTime, preserving what the original constructed type was.
261
- # If the original type is a Time and the resulting calculation goes out of
262
- # range for Times, then an exception will be raised by the Time class.
263
- def +(seconds)
264
- if seconds == 0
265
- self
266
- else
267
- if @orig.is_a?(DateTime)
268
- TimeOrDateTime.new(@orig + OffsetRationals.rational_for_offset(seconds))
269
- else
270
- # + defined for Time and Integer
271
- TimeOrDateTime.new(@orig + seconds)
272
- end
273
- end
274
- end
275
-
276
- # Subtracts a number of seconds from the TimeOrDateTime. Returns a new
277
- # TimeOrDateTime, preserving what the original constructed type was.
278
- # If the original type is a Time and the resulting calculation goes out of
279
- # range for Times, then an exception will be raised by the Time class.
280
- def -(seconds)
281
- self + (-seconds)
282
- end
283
-
284
- # Similar to the + operator, but converts to a DateTime based TimeOrDateTime
285
- # where the Time or Integer timestamp to go out of the allowed range for a
286
- # Time, converts to a DateTime based TimeOrDateTime.
287
- #
288
- # Note that the range of Time varies based on the platform.
289
- def add_with_convert(seconds)
290
- if seconds == 0
291
- self
292
- else
293
- if @orig.is_a?(DateTime)
294
- TimeOrDateTime.new(@orig + OffsetRationals.rational_for_offset(seconds))
295
- else
296
- # A Time or timestamp.
297
- result = to_i + seconds
298
-
299
- if ((result > 2147483647 || result < -2147483648) && !RubyCoreSupport.time_supports_64bit) || (result < 0 && !RubyCoreSupport.time_supports_negative)
300
- result = TimeOrDateTime.new(to_datetime + OffsetRationals.rational_for_offset(seconds))
301
- else
302
- result = TimeOrDateTime.new(@orig + seconds)
303
- end
304
- end
305
- end
306
- end
307
-
308
- # Returns true if todt represents the same time and was originally
309
- # constructed with the same type (DateTime, Time or timestamp) as this
310
- # TimeOrDateTime.
311
- def eql?(todt)
312
- todt.kind_of?(TimeOrDateTime) && to_orig.eql?(todt.to_orig)
313
- end
314
-
315
- # Returns a hash of this TimeOrDateTime.
316
- def hash
317
- @orig.hash
318
- end
319
-
320
- # If no block is given, returns a TimeOrDateTime wrapping the given
321
- # timeOrDateTime. If a block is specified, a TimeOrDateTime is constructed
322
- # and passed to the block. The result of the block must be a TimeOrDateTime.
323
- #
324
- # The result of the block will be converted to the type of the originally
325
- # passed in timeOrDateTime and then returned as the result of wrap.
326
- #
327
- # timeOrDateTime can be a Time, DateTime, timestamp (Integer) or
328
- # TimeOrDateTime. If a TimeOrDateTime is passed in, no new TimeOrDateTime
329
- # will be constructed and the value passed to wrap will be used when
330
- # calling the block.
331
- def self.wrap(timeOrDateTime)
332
- t = timeOrDateTime.is_a?(TimeOrDateTime) ? timeOrDateTime : TimeOrDateTime.new(timeOrDateTime)
333
-
334
- if block_given?
335
- t = yield t
336
-
337
- if timeOrDateTime.is_a?(TimeOrDateTime)
338
- t
339
- elsif timeOrDateTime.is_a?(Time)
340
- t.to_time
341
- elsif timeOrDateTime.is_a?(DateTime)
342
- t.to_datetime
343
- else
344
- t.to_i
345
- end
346
- else
347
- t
348
- end
349
- end
350
- end
351
- end