4
4
*/
5
5
6
6
package kotlinx.datetime.test
7
+
7
8
import kotlinx.datetime.*
8
9
import kotlinx.datetime.Clock // currently, requires an explicit import due to a conflict with the deprecated Clock from kotlin.time
9
10
import kotlin.random.*
10
11
import kotlin.test.*
11
12
import kotlin.time.*
13
+ import kotlin.time.Duration.Companion.hours
14
+ import kotlin.time.Duration.Companion.milliseconds
15
+ import kotlin.time.Duration.Companion.nanoseconds
16
+ import kotlin.time.Duration.Companion.seconds
12
17
13
18
class InstantTest {
14
19
@@ -30,12 +35,11 @@ class InstantTest {
30
35
assertNotEquals(notEqualInstant, instant)
31
36
}
32
37
33
- @OptIn(ExperimentalTime ::class )
34
38
@Test
35
39
fun instantArithmetic () {
36
40
val instant = Clock .System .now().toEpochMilliseconds().let { Instant .fromEpochMilliseconds(it) } // round to millis
37
41
val diffMillis = Random .nextLong(1000 , 1_000_000_000 )
38
- val diff = Duration .milliseconds(diffMillis)
42
+ val diff = diffMillis .milliseconds
39
43
40
44
val nextInstant = (instant.toEpochMilliseconds() + diffMillis).let { Instant .fromEpochMilliseconds(it) }
41
45
@@ -129,7 +133,6 @@ class InstantTest {
129
133
}
130
134
}
131
135
132
- @OptIn(ExperimentalTime ::class )
133
136
@Test
134
137
fun instantCalendarArithmetic () {
135
138
val zone = TimeZone .of(" Europe/Berlin" )
@@ -180,7 +183,7 @@ class InstantTest {
180
183
checkComponents(instant5.toLocalDateTime(zone), 2019 , 10 , 28 , 3 , 59 )
181
184
assertEquals(period, instant1.periodUntil(instant5, zone))
182
185
assertEquals(period, instant5.minus(instant1, zone))
183
- assertEquals(Duration .hours( 26 ) , instant5.minus(instant1))
186
+ assertEquals(26 .hours, instant5.minus(instant1))
184
187
assertEquals(instant1.plus(DateTimeUnit .HOUR ), instant5.minus(period, zone))
185
188
186
189
val instant6 = instant1.plus(23 , DateTimeUnit .HOUR , zone)
@@ -202,7 +205,6 @@ class InstantTest {
202
205
assertEquals(pow2_32, instant3.epochSeconds)
203
206
}
204
207
205
- @OptIn(ExperimentalTime ::class )
206
208
@Test
207
209
fun unitMultiplesUntil () {
208
210
val unit1000days = DateTimeUnit .DAY * 1000
@@ -227,7 +229,6 @@ class InstantTest {
227
229
assertEquals(start, end.plus(- diffUs, DateTimeUnit .MICROSECOND , zone))
228
230
}
229
231
230
- @OptIn(ExperimentalTime ::class )
231
232
@Test
232
233
fun instantOffset () {
233
234
val zone = TimeZone .of(" Europe/Berlin" )
@@ -237,15 +238,15 @@ class InstantTest {
237
238
checkComponents(ldt1, 2019 , 10 , 27 , 2 , 59 )
238
239
assertEquals(instant1, ldt1.toInstant(offset1))
239
240
240
- val instant2 = instant1 + Duration .hours( 1 )
241
+ val instant2 = instant1 + 1 .hours
241
242
val ldt2 = instant2.toLocalDateTime(zone)
242
243
val offset2 = instant2.offsetIn(zone)
243
244
assertEquals(ldt1, ldt2)
244
245
assertEquals(instant2, ldt2.toInstant(offset2))
245
246
assertNotEquals(offset1, offset2)
246
- assertEquals(Duration .seconds( offset1.totalSeconds), Duration .seconds( offset2.totalSeconds) + Duration .hours( 1 ) )
247
+ assertEquals(offset1.totalSeconds.seconds, offset2.totalSeconds.seconds + 1 .hours)
247
248
248
- val instant3 = instant2 - Duration .hours( 2 )
249
+ val instant3 = instant2 - 2 .hours
249
250
val offset3 = instant3.offsetIn(zone)
250
251
assertEquals(offset1, offset3)
251
252
}
@@ -348,7 +349,6 @@ class InstantTest {
348
349
/* Based on the ThreeTenBp project.
349
350
* Copyright (c) 2007-present, Stephen Colebourne & Michael Nascimento Santos
350
351
*/
351
- // @ExperimentalTime
352
352
@Test
353
353
fun strings () {
354
354
assertEquals(" 0000-01-02T00:00:00Z" , LocalDateTime (0 , 1 , 2 , 0 , 0 , 0 , 0 ).toInstant(TimeZone .UTC ).toString())
@@ -401,7 +401,6 @@ class InstantTest {
401
401
assertEquals(" +19999-12-31T23:59:59.000000009Z" , LocalDateTime (19999 , 12 , 31 , 23 , 59 , 59 , 9 ).toInstant(TimeZone .UTC ).toString())
402
402
}
403
403
404
- @ExperimentalTime
405
404
@Test
406
405
fun distantPastAndFuture () {
407
406
val distantFutureString = " +100000-01-01T00:00:00Z"
@@ -414,10 +413,10 @@ class InstantTest {
414
413
assertTrue(Instant .DISTANT_FUTURE .isDistantFuture)
415
414
assertFalse(Instant .DISTANT_PAST .isDistantFuture)
416
415
assertFalse(Instant .DISTANT_FUTURE .isDistantPast)
417
- assertFalse((Instant .DISTANT_PAST + Duration .nanoseconds( 1 ) ).isDistantPast)
418
- assertFalse((Instant .DISTANT_FUTURE - Duration .nanoseconds( 1 ) ).isDistantFuture)
419
- assertTrue((Instant .DISTANT_PAST - Duration .nanoseconds( 1 ) ).isDistantPast)
420
- assertTrue((Instant .DISTANT_FUTURE + Duration .nanoseconds( 1 ) ).isDistantFuture)
416
+ assertFalse((Instant .DISTANT_PAST + 1 .nanoseconds).isDistantPast)
417
+ assertFalse((Instant .DISTANT_FUTURE - 1 .nanoseconds).isDistantFuture)
418
+ assertTrue((Instant .DISTANT_PAST - 1 .nanoseconds).isDistantPast)
419
+ assertTrue((Instant .DISTANT_FUTURE + 1 .nanoseconds).isDistantFuture)
421
420
assertTrue(Instant .MAX .isDistantFuture)
422
421
assertFalse(Instant .MAX .isDistantPast)
423
422
assertTrue(Instant .MIN .isDistantPast)
@@ -426,7 +425,6 @@ class InstantTest {
426
425
427
426
}
428
427
429
- @OptIn(ExperimentalTime ::class )
430
428
class InstantRangeTest {
431
429
private val UTC = TimeZone .UTC
432
430
private val maxValidInstant = LocalDateTime .MAX .toInstant(UTC )
@@ -435,8 +433,8 @@ class InstantRangeTest {
435
433
private val largePositiveLongs = listOf (Long .MAX_VALUE , Long .MAX_VALUE - 1 , Long .MAX_VALUE - 50 )
436
434
private val largeNegativeLongs = listOf (Long .MIN_VALUE , Long .MIN_VALUE + 1 , Long .MIN_VALUE + 50 )
437
435
438
- private val largePositiveInstants = listOf (Instant .MAX , Instant .MAX - Duration .seconds( 1 ) , Instant .MAX - Duration .seconds( 50 ) )
439
- private val largeNegativeInstants = listOf (Instant .MIN , Instant .MIN + Duration .seconds( 1 ) , Instant .MIN + Duration .seconds( 50 ) )
436
+ private val largePositiveInstants = listOf (Instant .MAX , Instant .MAX - 1 .seconds, Instant .MAX - 50 .seconds)
437
+ private val largeNegativeInstants = listOf (Instant .MIN , Instant .MIN + 1 .seconds, Instant .MIN + 50 .seconds)
440
438
441
439
private val smallInstants = listOf (
442
440
Instant .fromEpochMilliseconds(0 ),
@@ -504,8 +502,8 @@ class InstantRangeTest {
504
502
assertEquals(Instant .MIN , instant - duration)
505
503
}
506
504
}
507
- assertEquals(Instant .MAX , (Instant .MAX - Duration .seconds( 4 )) + Duration .seconds( 5 ) )
508
- assertEquals(Instant .MIN , (Instant .MIN + Duration .seconds( 10 )) - Duration .seconds( 12 ) )
505
+ assertEquals(Instant .MAX , (Instant .MAX - 4 .seconds) + 5 .seconds)
506
+ assertEquals(Instant .MIN , (Instant .MIN + 10 .seconds) - 12 .seconds)
509
507
}
510
508
511
509
@Test
@@ -534,8 +532,8 @@ class InstantRangeTest {
534
532
// Overflowing a LocalDateTime in input
535
533
maxValidInstant.plus(DateTimePeriod (nanoseconds = - 1 ), UTC )
536
534
minValidInstant.plus(DateTimePeriod (nanoseconds = 1 ), UTC )
537
- assertArithmeticFails { (maxValidInstant + Duration .nanoseconds( 1 ) ).plus(DateTimePeriod (nanoseconds = - 2 ), UTC ) }
538
- assertArithmeticFails { (minValidInstant - Duration .nanoseconds( 1 ) ).plus(DateTimePeriod (nanoseconds = 2 ), UTC ) }
535
+ assertArithmeticFails { (maxValidInstant + 1 .nanoseconds).plus(DateTimePeriod (nanoseconds = - 2 ), UTC ) }
536
+ assertArithmeticFails { (minValidInstant - 1 .nanoseconds).plus(DateTimePeriod (nanoseconds = 2 ), UTC ) }
539
537
// Overflowing a LocalDateTime in result
540
538
assertArithmeticFails { maxValidInstant.plus(DateTimePeriod (nanoseconds = 1 ), UTC ) }
541
539
assertArithmeticFails { minValidInstant.plus(DateTimePeriod (nanoseconds = - 1 ), UTC ) }
@@ -557,8 +555,8 @@ class InstantRangeTest {
557
555
// Overflowing a LocalDateTime in input
558
556
maxValidInstant.plus(- 1 , DateTimeUnit .NANOSECOND , UTC )
559
557
minValidInstant.plus(1 , DateTimeUnit .NANOSECOND , UTC )
560
- assertArithmeticFails { (maxValidInstant + Duration .nanoseconds( 1 ) ).plus(- 2 , DateTimeUnit .NANOSECOND , UTC ) }
561
- assertArithmeticFails { (minValidInstant - Duration .nanoseconds( 1 ) ).plus(2 , DateTimeUnit .NANOSECOND , UTC ) }
558
+ assertArithmeticFails { (maxValidInstant + 1 .nanoseconds).plus(- 2 , DateTimeUnit .NANOSECOND , UTC ) }
559
+ assertArithmeticFails { (minValidInstant - 1 .nanoseconds).plus(2 , DateTimeUnit .NANOSECOND , UTC ) }
562
560
// Overflowing a LocalDateTime in result
563
561
assertArithmeticFails { maxValidInstant.plus(1 , DateTimeUnit .NANOSECOND , UTC ) }
564
562
assertArithmeticFails { maxValidInstant.plus(1 , DateTimeUnit .YEAR , UTC ) }
@@ -586,8 +584,8 @@ class InstantRangeTest {
586
584
fun periodUntilOutOfRange () {
587
585
// Instant.periodUntil
588
586
maxValidInstant.periodUntil(maxValidInstant, UTC )
589
- assertArithmeticFails { (maxValidInstant + Duration .nanoseconds( 1 ) ).periodUntil(maxValidInstant, UTC ) }
590
- assertArithmeticFails { minValidInstant.periodUntil(minValidInstant - Duration .nanoseconds( 1 ) , UTC ) }
587
+ assertArithmeticFails { (maxValidInstant + 1 .nanoseconds).periodUntil(maxValidInstant, UTC ) }
588
+ assertArithmeticFails { minValidInstant.periodUntil(minValidInstant - 1 .nanoseconds, UTC ) }
591
589
}
592
590
593
591
@Test
@@ -603,11 +601,10 @@ class InstantRangeTest {
603
601
fun unitsUntilOutOfRange () {
604
602
// Instant.until
605
603
// Overflowing a LocalDateTime in input
606
- assertArithmeticFails { (maxValidInstant + Duration .nanoseconds( 1 ) ).until(maxValidInstant, DateTimeUnit .NANOSECOND , UTC ) }
607
- assertArithmeticFails { maxValidInstant.until(maxValidInstant + Duration .nanoseconds( 1 ) , DateTimeUnit .NANOSECOND , UTC ) }
604
+ assertArithmeticFails { (maxValidInstant + 1 .nanoseconds).until(maxValidInstant, DateTimeUnit .NANOSECOND , UTC ) }
605
+ assertArithmeticFails { maxValidInstant.until(maxValidInstant + 1 .nanoseconds, DateTimeUnit .NANOSECOND , UTC ) }
608
606
// Overloads without a TimeZone should not fail on overflowing a LocalDateTime
609
- (maxValidInstant + Duration .nanoseconds( 1 ) ).until(maxValidInstant, DateTimeUnit .NANOSECOND )
610
- maxValidInstant.until(maxValidInstant + Duration .nanoseconds( 1 ) , DateTimeUnit .NANOSECOND )
607
+ (maxValidInstant + 1 .nanoseconds).until(maxValidInstant, DateTimeUnit .NANOSECOND )
608
+ maxValidInstant.until(maxValidInstant + 1 .nanoseconds, DateTimeUnit .NANOSECOND )
611
609
}
612
610
}
613
-
0 commit comments