@@ -143,19 +143,20 @@ using Test, Dates
143
143
144
144
@testset " Show method" begin
145
145
t1 = ITime (10 )
146
- @test sprint (show, t1) == " 10 seconds [counter = 10, period = 1 second]"
146
+ @test sprint (show, t1) ==
147
+ " 10.0 seconds [counter = 10, period = 1 second]"
147
148
148
149
t2 = ITime (10 , epoch = Dates. DateTime (2024 , 1 , 1 ))
149
150
@test sprint (show, t2) ==
150
- " 10 seconds (2024-01-01T00:00:10) [counter = 10, period = 1 second, epoch = 2024-01-01T00:00:00]"
151
+ " 10.0 seconds (2024-01-01T00:00:10) [counter = 10, period = 1 second, epoch = 2024-01-01T00:00:00]"
151
152
152
153
t3 = ITime (
153
154
10 ,
154
155
period = Dates. Hour (1 ),
155
156
epoch = Dates. DateTime (2024 , 1 , 1 ),
156
157
)
157
158
@test sprint (show, t3) ==
158
- " 10 hours (2024-01-01T10:00:00) [counter = 10, period = 1 hour, epoch = 2024-01-01T00:00:00]"
159
+ " 10.0 hours (2024-01-01T10:00:00) [counter = 10, period = 1 hour, epoch = 2024-01-01T00:00:00]"
159
160
end
160
161
161
162
@testset " Find common epoch" begin
@@ -290,4 +291,38 @@ using Test, Dates
290
291
@test typeof (zero (t1). counter) == Int32
291
292
@test typeof (mod (t3, t2). counter) == Int32
292
293
end
294
+
295
+ @testset " Overflow test" begin
296
+ t1 = ITime (typemax (Int64), period = Second (2 ))
297
+ # Dates.seconds(t.period) returns an integer if t.period is in seconds
298
+ # Multiplying this by t.counter, an integer, results in an integer that
299
+ # could overflow
300
+ @test isapprox (float (t1), float (typemax (Int64)) * 2.0 )
301
+
302
+ # The computation epoch(t) + counter(t) * period(t) could overflow since
303
+ # counter(t) * period(t) could overflow. In this case, the maximum value
304
+ # of period(t) is 2^63 - 1 nanoseconds. Multiplying 2 nanoseconds by the
305
+ # counter overflow and give -2 nanoseconds. Here, we decide to throw an
306
+ # error instead of circumventing the overflow. One way to circumvent the
307
+ # overflow is to add counter(t) * oneunit(period(t)) n times, where n is
308
+ # the value of period.
309
+ t2 = ITime (
310
+ typemax (Int64),
311
+ period = Nanosecond (2 ),
312
+ epoch = Dates. DateTime (2010 ),
313
+ )
314
+ @test_throws ErrorException date (t2)
315
+
316
+ # Same tests as above, but with Int32
317
+ t3 = ITime (typemax (Int32), period = Second (2 ))
318
+ @test isapprox (float (t3), float (typemax (Int32)) * 2.0 )
319
+
320
+ t4 = ITime (
321
+ typemax (Int32),
322
+ period = Nanosecond (2 ),
323
+ epoch = Dates. DateTime (2010 ),
324
+ )
325
+ # We do not overflow because Period uses Int64 internally
326
+ @test date (t4) == Dates. DateTime (2010 ) + typemax (Int32) * Nanosecond (2 )
327
+ end
293
328
end
0 commit comments