File: tc_time_with_offset.rb

package info (click to toggle)
ruby-tzinfo 2.0.6-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,824 kB
  • sloc: ruby: 21,667; makefile: 6
file content (371 lines) | stat: -rw-r--r-- 12,924 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
# encoding: UTF-8
# frozen_string_literal: true

require_relative 'test_utils'

class TCTimeWithOffset < Minitest::Test
  include TZInfo

  def time_with_offset(year, month, day, hour, minute, second, tz_offset)
    TimeWithOffset.new(year, month, day, hour, minute, second, tz_offset.observed_utc_offset).set_timezone_offset(tz_offset)
  end

  def assert_is_time_or_time_with_offset_with_nil_timezone_offset(value)
    if value.kind_of?(TimeWithOffset)
      assert_nil(value.timezone_offset)
    else
      assert_equal(Time, value.class)
    end
  end

  def test_set_timezone_offset_unchanged_offset
    [TimeWithOffset.new(2017,1,15,23,0,Rational(11,10),0), TimeWithOffset.new(2017,1,15,23,0,Rational(11,10),3600)].each do |two|
      o1 = TimezoneOffset.new(two.utc_offset, 0, 'TEST')
      o2 = TimezoneOffset.new(0, two.utc_offset, 'TEST')
      assert_nil(two.timezone_offset)
      assert_same(two, two.set_timezone_offset(o1))
      assert_same(o1, two.timezone_offset)
      assert_same(two, two.set_timezone_offset(o2))
      assert_same(o2, two.timezone_offset)
    end
  end

  def test_set_timezone_offset_set_offset
    o = TimezoneOffset.new(3600, 0, 'TEST')

    [TimeWithOffset.utc(2017,1,15,23,0,Rational(11,10)), TimeWithOffset.new(2017,1,15,23,0,Rational(11,10),0), TimeWithOffset.new(2017,1,15,22,0,Rational(11,10),-3600)].each do |two|
      assert_nil(two.timezone_offset)
      assert_same(two, two.set_timezone_offset(o))
      assert_same(o, two.timezone_offset)
      assert_equal_with_offset(Time.new(2017,1,16,0,0,Rational(11,10),3600), two)
    end
  end

  def test_set_timezone_offset_utc
    two = TimeWithOffset.utc(2017,1,15,23,0,Rational(11,10))
    o = TimezoneOffset.new(0, 0, 'TEST')

    assert_equal(true, two.utc?)
    assert_same(two, two.set_timezone_offset(o))
    assert_same(o, two.timezone_offset)
    assert_equal_with_offset(Time.new(2017,1,15,23,0,Rational(11,10),0), two)
  end

  def test_set_timezone_offset_nil_timezone_offset
    two = TimeWithOffset.new(2017,1,15,23,0,Rational(11,10),0)
    error = assert_raises(ArgumentError) { two.set_timezone_offset(nil) }
    assert_match(/\btimezone_offset\b/, error.message)
  end

  def test_strftime
    two = time_with_offset(2017,1,15,23,0,1,TimezoneOffset.new(0, 3600, 'TEST'))
    assert_equal('23:00:01 TEST', two.strftime('%H:%M:%S %Z'))
    assert_equal('TEST', two.strftime('%Z'))
    assert_equal('%ZTEST', two.strftime('%%Z%Z'))
    assert_equal('TEST TEST', two.strftime('%Z %Z'))
    assert_equal('TEST %Z %TEST %%Z %%TEST', two.strftime('%Z %%Z %%%Z %%%%Z %%%%%Z'))
  end

  def test_strftime_handles_percent_in_abbreviation
    two = time_with_offset(2017,1,15,23,0,1,TimezoneOffset.new(0, 0, '%H:%M:%S'))
    assert_equal('%H:%M:%S', two.strftime('%Z'))
  end

  def test_strftime_nil_timezone_offset
    # Will use Time#strftime's handling of the %Z directive.
    t = Time.new(2017,1,15,23,0,1,3600)
    two = TimeWithOffset.new(2017,1,15,23,0,1,3600)
    assert_nil(two.timezone_offset)

    # JRuby 1.7 returns '+01:00' instead of empty string.
    assert_equal(t.strftime('%Z'), two.strftime('%Z'))
  end

  def test_strftime_nil_format
    two = time_with_offset(2017,1,15,23,0,1,TimezoneOffset.new(0, 0, 'TEST'))
    error = assert_raises(ArgumentError) { two.strftime(nil) }
    assert_match(/\bformat\b/, error.message)
  end

  def test_add
    two = time_with_offset(2017,1,15,23,0,1,TimezoneOffset.new(0, 3600, 'TEST'))

    # MRI returns Time. JRuby returns TimeWithOffset.
    assert_is_time_or_time_with_offset_with_nil_timezone_offset(two + 1)
  end

  def test_subtract_seconds
    two = time_with_offset(2017,1,15,23,0,1,TimezoneOffset.new(0, 3600, 'TEST'))

    # MRI returns Time. JRuby returns TimeWithOffset.
    assert_is_time_or_time_with_offset_with_nil_timezone_offset(two - 1)
  end

  def test_subtract_time
    two = time_with_offset(2017,1,15,23,0,1,TimezoneOffset.new(0, 3600, 'TEST'))
    assert_equal(1, two - Time.utc(2017,1,15,22,0,0))
  end

  def test_subtract_time_with_offset
    two1 = time_with_offset(2017,1,15,23,0,1,TimezoneOffset.new(0, 3600, 'TEST'))
    two2 = time_with_offset(2017,1,15,23,0,0,TimezoneOffset.new(0, 3600, 'TEST'))
    assert_equal(1, two1 - two2)
  end

  def test_compare
    o = TimezoneOffset.new(0, 0, 'TEST')
    t1 = Time.new(2017,1,15,23,0,1,0)
    t2 = Time.new(2017,1,15,23,0,2,0)
    two1 = time_with_offset(2017,1,15,23,0,1,o)
    two2 = time_with_offset(2017,1,15,23,0,2,o)

    assert_equal(0, two1 <=> two1)
    assert_equal(0, two1 <=> t1)
    assert_equal(0, t1 <=> two1)

    assert_equal(-1, two1 <=> two2)
    assert_equal(-1, two1 <=> t2)
    assert_equal(-1, t1 <=> two2)

    assert_equal(1, two2 <=> two1)
    assert_equal(1, two2 <=> t1)
    assert_equal(1, t2 <=> two1)
  end

  def test_dst
    two1 = time_with_offset(2017,1,15,23,0,1,TimezoneOffset.new(   0,    0, 'TEST'))
    two2 = time_with_offset(2017,1,15,23,0,1,TimezoneOffset.new(   0, 3600, 'TEST'))
    two3 = time_with_offset(2017,1,15,23,0,1,TimezoneOffset.new(3600,    0, 'TEST'))
    two4 = time_with_offset(2017,1,15,23,0,1,TimezoneOffset.new(3600, 3600, 'TEST'))

    assert_equal(false, two1.dst?)
    assert_equal(true,  two2.dst?)
    assert_equal(false, two3.dst?)
    assert_equal(true,  two4.dst?)
  end

  def test_dst_nil_timezone_offset
    two = TimeWithOffset.new(2017,1,15,23,0,1,3600)
    assert_nil(two.timezone_offset)
    assert_equal(false, two.dst?)
  end

  def test_eql
    o = TimezoneOffset.new(0, 0, 'TEST')
    t1 = Time.new(2017,1,15,23,0,1,0)
    t2 = Time.new(2017,1,15,23,0,2,0)
    two1 = time_with_offset(2017,1,15,23,0,1,o)
    two2 = time_with_offset(2017,1,15,23,0,2,o)

    assert_equal(true, two1.eql?(two1))
    assert_equal(true, two1.eql?(t1))
    assert_equal(true, t1.eql?(two1))

    assert_equal(false, two1.eql?(two2))
    assert_equal(false, two1.eql?(t2))
    assert_equal(false, t1.eql?(two2))
  end

  def test_getgm
    two = time_with_offset(2017,1,15,23,0,1,TimezoneOffset.new(0, 0, 'TEST'))

    # MRI returns TimeWithOffset. JRuby returns Time.
    assert_is_time_or_time_with_offset_with_nil_timezone_offset(two.getgm)
  end

  def test_getlocal
    two = time_with_offset(2017,1,15,23,0,1,TimezoneOffset.new(0, 0, 'TEST'))

    # MRI returns TimeWithOffset. JRuby returns Time.
    assert_is_time_or_time_with_offset_with_nil_timezone_offset(two.getlocal)
  end

  def test_getlocal_0
    two = time_with_offset(2017,1,15,23,0,1,TimezoneOffset.new(0, 0, 'TEST'))

    # MRI and JRuby >= 9.3 returns TimeWithOffset. JRuby < 9.3 returns Time.
    local = two.getlocal(0)
    assert_equal(0, local.utc_offset)
    assert_is_time_or_time_with_offset_with_nil_timezone_offset(two.getlocal(0))
  end

  def test_getutc
    two = time_with_offset(2017,1,15,23,0,1,TimezoneOffset.new(0, 0, 'TEST'))

    # MRI returns TimeWithOffset. JRuby returns Time.
    assert_is_time_or_time_with_offset_with_nil_timezone_offset(two.getutc)
  end

  def test_gmtime
    two = time_with_offset(2017,1,15,23,0,1,TimezoneOffset.new(3600, 0, 'TEST'))
    assert_same(two, two.gmtime)
    assert_equal(true, two.utc?)
    assert_nil(two.timezone_offset)
  end

  def test_isdst
    two1 = time_with_offset(2017,1,15,23,0,1,TimezoneOffset.new(   0,    0, 'TEST'))
    two2 = time_with_offset(2017,1,15,23,0,1,TimezoneOffset.new(   0, 3600, 'TEST'))
    two3 = time_with_offset(2017,1,15,23,0,1,TimezoneOffset.new(3600,    0, 'TEST'))
    two4 = time_with_offset(2017,1,15,23,0,1,TimezoneOffset.new(3600, 3600, 'TEST'))

    assert_equal(false, two1.isdst)
    assert_equal(true,  two2.isdst)
    assert_equal(false, two3.isdst)
    assert_equal(true,  two4.isdst)
  end

  def test_isdst_nil_timezone_offset
    two = TimeWithOffset.new(2017,1,15,23,0,1,3600)
    assert_nil(two.timezone_offset)
    assert_equal(false, two.isdst)
  end

  def test_localtime_without_offset
    two = time_with_offset(2017,1,15,23,0,1,TimezoneOffset.new(0, 0, 'TEST'))
    assert_same(two, two.localtime)
    assert_nil(two.timezone_offset)
  end

  def test_localtime_with_offset
    two = time_with_offset(2017,1,15,23,0,1,TimezoneOffset.new(0, 0, 'TEST'))
    assert_same(two, two.localtime(3600))
    assert_equal(3600, two.utc_offset)
    assert_nil(two.timezone_offset)
  end

  def test_localtime_with_offset_unchanged
    two = time_with_offset(2017,1,15,23,0,1,TimezoneOffset.new(3600, 0, 'TEST'))
    assert_same(two, two.localtime(3600))
    assert_equal(3600, two.utc_offset)
    assert_nil(two.timezone_offset)
  end

  def test_round
    o = TimezoneOffset.new(3600, 0, 'TEST')
    two = time_with_offset(2017,1,15,23,0,Rational(111,100),o)

    r = two.round
    r0 = two.round(0)
    r1 = two.round(1)

    [r, r0, r1].each do |result|
      assert_kind_of(TimeWithOffset, result)
      assert_same(o, result.timezone_offset)
    end

    assert_equal_with_offset(Time.new(2017,1,15,23,0,1,3600), r)
    assert_equal_with_offset(Time.new(2017,1,15,23,0,1,3600), r0)
    assert_equal_with_offset(Time.new(2017,1,15,23,0,Rational(11,10),3600), r1)
  end

  def test_round_nil_timezone_offset
    two = TimeWithOffset.new(2017,1,15,23,0,Rational(111,100),3600)
    assert_nil(two.timezone_offset)

    r = two.round
    r0 = two.round(0)
    r1 = two.round(1)

    [r, r0, r1].each { |result| assert_equal(Time, result.class) }

    assert_equal_with_offset(Time.new(2017,1,15,23,0,1,3600), r)
    assert_equal_with_offset(Time.new(2017,1,15,23,0,1,3600), r0)
    assert_equal_with_offset(Time.new(2017,1,15,23,0,Rational(11,10),3600), r1)
  end

  def test_succ
    skip('Time#succ is not supported') unless Time.new.respond_to?(:succ)

    two = time_with_offset(2017,1,15,23,0,1,TimezoneOffset.new(0, 3600, 'BST'))

    # succ is obsolete and outputs a warning.
    without_warnings do
      assert_is_time_or_time_with_offset_with_nil_timezone_offset(two.succ)
    end
  end

  def test_to_a
    two1 = time_with_offset(2017,1,15,23,0,1,TimezoneOffset.new(   0,    0, 'TEST'))
    two2 = time_with_offset(2017,1,15,23,0,1,TimezoneOffset.new(   0, 3600, 'TEST'))
    two3 = time_with_offset(2017,1,15,23,0,1,TimezoneOffset.new(3600,    0, 'TEST'))
    two4 = time_with_offset(2017,1,15,23,0,1,TimezoneOffset.new(3600, 3600, 'TEST'))

    assert_equal([1,0,23,15,1,2017,0,15,false,'TEST'], two1.to_a)
    assert_equal([1,0,23,15,1,2017,0,15,true, 'TEST'], two2.to_a)
    assert_equal([1,0,23,15,1,2017,0,15,false,'TEST'], two3.to_a)
    assert_equal([1,0,23,15,1,2017,0,15,true, 'TEST'], two4.to_a)
  end

  def test_to_a_nil_timezone_offset
    t = Time.new(2017,1,15,23,0,1,3600)
    two = TimeWithOffset.new(2017,1,15,23,0,1,3600)
    assert_nil(two.timezone_offset)

    # For local times, the last element is nil on MRI and empty string on JRuby.
    # Compare with the actual Time#to_a output.
    assert_equal(t.to_a, two.to_a)
  end

  def test_utc
    two = time_with_offset(2017,1,15,23,0,1,TimezoneOffset.new(3600, 0, 'TEST'))
    assert_same(two, two.utc)
    assert_equal(true, two.utc?)
    assert_nil(two.timezone_offset)
  end

  def test_zone
    two1 = time_with_offset(2017,1,15,23,0,1,TimezoneOffset.new(3600, 0, 'TEST'))
    two2 = TimeWithOffset.utc(2017,1,15,23,0,1).set_timezone_offset(TimezoneOffset.new(0, 0, 'GMT'))
    assert_equal('TEST', two1.zone)
    assert_equal('GMT', two2.zone)
  end

  def test_zone_nil_timezone_offset
    t = Time.new(2017,1,15,23,0,1,3600)
    two = TimeWithOffset.new(2017,1,15,23,0,1,3600)
    assert_nil(two.timezone_offset)

    # For local times, zone returns nil on MRI and empty string on JRuby.
    # Compare with the actual Time#zone output.
    assert_nil_or_equal(t.zone, two.zone)
  end

  def test_to_datetime
    o = TimezoneOffset.new(3600, 0, 'TEST')
    two = time_with_offset(2017,1,15,23,0,Rational(11,10),o)
    d = two.to_datetime
    assert_kind_of(DateTimeWithOffset, d)
    assert_same(o, d.timezone_offset)
    assert_equal_with_offset(DateTime.new(2017,1,15,23,0,Rational(11,10),Rational(1,24)), d)
  end

  def test_to_datetime_nil_timezone_offset
    two = TimeWithOffset.new(2017,1,15,23,0,Rational(11,10),3600)
    assert_nil(two.timezone_offset)
    d = two.to_datetime
    assert_equal(DateTime, d.class)
    assert_equal_with_offset(DateTime.new(2017,1,15,23,0,Rational(11,10),Rational(1,24)), d)
  end

  def test_class_at_returns_local_time
    two = TimeWithOffset.at(1484521201)
    assert_kind_of(TimeWithOffset, two)
    assert_nil(two.timezone_offset)
    assert_equal(1484521201, two.to_i)
  end

  def test_class_new_returns_local_time
    two = TimeWithOffset.new(2017,1,15,23,0,1,0)
    assert_kind_of(TimeWithOffset, two)
    assert_nil(two.timezone_offset)
    assert_equal(1484521201, two.to_i)
  end

  def test_class_utc_returns_local_time
    two = TimeWithOffset.utc(2017,1,15,23,0,1)
    assert_kind_of(TimeWithOffset, two)
    assert_nil(two.timezone_offset)
    assert_equal(1484521201, two.to_i)
  end
end