44 */
55
66package kotlinx.datetime.test
7+
78import kotlinx.datetime.*
89import kotlinx.datetime.Clock // currently, requires an explicit import due to a conflict with the deprecated Clock from kotlin.time
910import kotlin.random.*
1011import kotlin.test.*
1112import 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
1217
1318class InstantTest {
1419
@@ -30,12 +35,11 @@ class InstantTest {
3035 assertNotEquals(notEqualInstant, instant)
3136 }
3237
33- @OptIn(ExperimentalTime ::class )
3438 @Test
3539 fun instantArithmetic () {
3640 val instant = Clock .System .now().toEpochMilliseconds().let { Instant .fromEpochMilliseconds(it) } // round to millis
3741 val diffMillis = Random .nextLong(1000 , 1_000_000_000 )
38- val diff = Duration .milliseconds(diffMillis)
42+ val diff = diffMillis .milliseconds
3943
4044 val nextInstant = (instant.toEpochMilliseconds() + diffMillis).let { Instant .fromEpochMilliseconds(it) }
4145
@@ -129,7 +133,6 @@ class InstantTest {
129133 }
130134 }
131135
132- @OptIn(ExperimentalTime ::class )
133136 @Test
134137 fun instantCalendarArithmetic () {
135138 val zone = TimeZone .of(" Europe/Berlin" )
@@ -180,7 +183,7 @@ class InstantTest {
180183 checkComponents(instant5.toLocalDateTime(zone), 2019 , 10 , 28 , 3 , 59 )
181184 assertEquals(period, instant1.periodUntil(instant5, zone))
182185 assertEquals(period, instant5.minus(instant1, zone))
183- assertEquals(Duration .hours( 26 ) , instant5.minus(instant1))
186+ assertEquals(26 .hours, instant5.minus(instant1))
184187 assertEquals(instant1.plus(DateTimeUnit .HOUR ), instant5.minus(period, zone))
185188
186189 val instant6 = instant1.plus(23 , DateTimeUnit .HOUR , zone)
@@ -202,7 +205,6 @@ class InstantTest {
202205 assertEquals(pow2_32, instant3.epochSeconds)
203206 }
204207
205- @OptIn(ExperimentalTime ::class )
206208 @Test
207209 fun unitMultiplesUntil () {
208210 val unit1000days = DateTimeUnit .DAY * 1000
@@ -227,7 +229,6 @@ class InstantTest {
227229 assertEquals(start, end.plus(- diffUs, DateTimeUnit .MICROSECOND , zone))
228230 }
229231
230- @OptIn(ExperimentalTime ::class )
231232 @Test
232233 fun instantOffset () {
233234 val zone = TimeZone .of(" Europe/Berlin" )
@@ -237,15 +238,15 @@ class InstantTest {
237238 checkComponents(ldt1, 2019 , 10 , 27 , 2 , 59 )
238239 assertEquals(instant1, ldt1.toInstant(offset1))
239240
240- val instant2 = instant1 + Duration .hours( 1 )
241+ val instant2 = instant1 + 1 .hours
241242 val ldt2 = instant2.toLocalDateTime(zone)
242243 val offset2 = instant2.offsetIn(zone)
243244 assertEquals(ldt1, ldt2)
244245 assertEquals(instant2, ldt2.toInstant(offset2))
245246 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)
247248
248- val instant3 = instant2 - Duration .hours( 2 )
249+ val instant3 = instant2 - 2 .hours
249250 val offset3 = instant3.offsetIn(zone)
250251 assertEquals(offset1, offset3)
251252 }
@@ -348,7 +349,6 @@ class InstantTest {
348349 /* Based on the ThreeTenBp project.
349350 * Copyright (c) 2007-present, Stephen Colebourne & Michael Nascimento Santos
350351 */
351- // @ExperimentalTime
352352 @Test
353353 fun strings () {
354354 assertEquals(" 0000-01-02T00:00:00Z" , LocalDateTime (0 , 1 , 2 , 0 , 0 , 0 , 0 ).toInstant(TimeZone .UTC ).toString())
@@ -401,7 +401,6 @@ class InstantTest {
401401 assertEquals(" +19999-12-31T23:59:59.000000009Z" , LocalDateTime (19999 , 12 , 31 , 23 , 59 , 59 , 9 ).toInstant(TimeZone .UTC ).toString())
402402 }
403403
404- @ExperimentalTime
405404 @Test
406405 fun distantPastAndFuture () {
407406 val distantFutureString = " +100000-01-01T00:00:00Z"
@@ -414,10 +413,10 @@ class InstantTest {
414413 assertTrue(Instant .DISTANT_FUTURE .isDistantFuture)
415414 assertFalse(Instant .DISTANT_PAST .isDistantFuture)
416415 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)
421420 assertTrue(Instant .MAX .isDistantFuture)
422421 assertFalse(Instant .MAX .isDistantPast)
423422 assertTrue(Instant .MIN .isDistantPast)
@@ -426,7 +425,6 @@ class InstantTest {
426425
427426}
428427
429- @OptIn(ExperimentalTime ::class )
430428class InstantRangeTest {
431429 private val UTC = TimeZone .UTC
432430 private val maxValidInstant = LocalDateTime .MAX .toInstant(UTC )
@@ -435,8 +433,8 @@ class InstantRangeTest {
435433 private val largePositiveLongs = listOf (Long .MAX_VALUE , Long .MAX_VALUE - 1 , Long .MAX_VALUE - 50 )
436434 private val largeNegativeLongs = listOf (Long .MIN_VALUE , Long .MIN_VALUE + 1 , Long .MIN_VALUE + 50 )
437435
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)
440438
441439 private val smallInstants = listOf (
442440 Instant .fromEpochMilliseconds(0 ),
@@ -504,8 +502,8 @@ class InstantRangeTest {
504502 assertEquals(Instant .MIN , instant - duration)
505503 }
506504 }
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)
509507 }
510508
511509 @Test
@@ -534,8 +532,8 @@ class InstantRangeTest {
534532 // Overflowing a LocalDateTime in input
535533 maxValidInstant.plus(DateTimePeriod (nanoseconds = - 1 ), UTC )
536534 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 ) }
539537 // Overflowing a LocalDateTime in result
540538 assertArithmeticFails { maxValidInstant.plus(DateTimePeriod (nanoseconds = 1 ), UTC ) }
541539 assertArithmeticFails { minValidInstant.plus(DateTimePeriod (nanoseconds = - 1 ), UTC ) }
@@ -557,8 +555,8 @@ class InstantRangeTest {
557555 // Overflowing a LocalDateTime in input
558556 maxValidInstant.plus(- 1 , DateTimeUnit .NANOSECOND , UTC )
559557 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 ) }
562560 // Overflowing a LocalDateTime in result
563561 assertArithmeticFails { maxValidInstant.plus(1 , DateTimeUnit .NANOSECOND , UTC ) }
564562 assertArithmeticFails { maxValidInstant.plus(1 , DateTimeUnit .YEAR , UTC ) }
@@ -586,8 +584,8 @@ class InstantRangeTest {
586584 fun periodUntilOutOfRange () {
587585 // Instant.periodUntil
588586 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 ) }
591589 }
592590
593591 @Test
@@ -603,11 +601,10 @@ class InstantRangeTest {
603601 fun unitsUntilOutOfRange () {
604602 // Instant.until
605603 // 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 ) }
608606 // 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 )
611609 }
612610}
613-
0 commit comments