Skip to content

Commit 4895b49

Browse files
dkhalanskyjbhfhbd
andauthored
Bump Kotlin to 1.6.0 and remove ExperimentalTime (#161)
Fixes #156 Co-authored-by: hfhbd <[email protected]>
1 parent bbbbd9c commit 4895b49

13 files changed

+73
-83
lines changed

core/common/src/Clock.kt

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,7 @@
55

66
package kotlinx.datetime
77

8-
import kotlin.time.Duration
9-
import kotlin.time.ExperimentalTime
10-
import kotlin.time.TimeMark
11-
import kotlin.time.TimeSource
8+
import kotlin.time.*
129

1310
public interface Clock {
1411
public fun now(): Instant
@@ -25,7 +22,6 @@ public interface Clock {
2522
public fun Clock.todayAt(timeZone: TimeZone): LocalDate =
2623
now().toLocalDateTime(timeZone).date
2724

28-
2925
@ExperimentalTime
3026
public fun Clock.asTimeSource(): TimeSource = object : TimeSource {
3127
override fun markNow(): TimeMark = InstantTimeMark(now(), this@asTimeSource)

core/common/src/DateTimePeriod.kt

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import kotlinx.datetime.serializers.DatePeriodIso8601Serializer
99
import kotlinx.datetime.serializers.DateTimePeriodIso8601Serializer
1010
import kotlin.math.*
1111
import kotlin.time.Duration
12-
import kotlin.time.ExperimentalTime
1312
import kotlinx.serialization.Serializable
1413

1514
@Serializable(with = DateTimePeriodIso8601Serializer::class)
@@ -307,7 +306,6 @@ public fun DateTimePeriod(
307306
): DateTimePeriod = buildDateTimePeriod(totalMonths(years, months), days,
308307
totalNanoseconds(hours, minutes, seconds, nanoseconds))
309308

310-
@OptIn(ExperimentalTime::class)
311309
public fun Duration.toDateTimePeriod(): DateTimePeriod = buildDateTimePeriod(totalNanoseconds = inWholeNanoseconds)
312310

313311
public operator fun DateTimePeriod.plus(other: DateTimePeriod): DateTimePeriod = buildDateTimePeriod(
@@ -320,4 +318,3 @@ public operator fun DatePeriod.plus(other: DatePeriod): DatePeriod = DatePeriod(
320318
safeAdd(totalMonths, other.totalMonths),
321319
safeAdd(days, other.days),
322320
)
323-

core/common/src/DateTimeUnit.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ package kotlinx.datetime
88
import kotlinx.datetime.serializers.*
99
import kotlinx.serialization.Serializable
1010
import kotlin.time.*
11+
import kotlin.time.Duration.Companion.nanoseconds
1112

1213
@Serializable(with = DateTimeUnitSerializer::class)
1314
public sealed class DateTimeUnit {
@@ -52,9 +53,8 @@ public sealed class DateTimeUnit {
5253

5354
override fun times(scalar: Int): TimeBased = TimeBased(safeMultiply(nanoseconds, scalar.toLong()))
5455

55-
@ExperimentalTime
5656
public val duration: Duration
57-
get() = Duration.nanoseconds(nanoseconds)
57+
get() = nanoseconds.nanoseconds
5858

5959
override fun equals(other: Any?): Boolean =
6060
this === other || (other is TimeBased && this.nanoseconds == other.nanoseconds)

core/common/src/Instant.kt

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import kotlinx.datetime.serializers.InstantIso8601Serializer
99
import kotlinx.serialization.Serializable
1010
import kotlin.time.*
1111

12-
@OptIn(ExperimentalTime::class)
1312
@Serializable(with = InstantIso8601Serializer::class)
1413
public expect class Instant : Comparable<Instant> {
1514

@@ -51,7 +50,6 @@ public expect class Instant : Comparable<Instant> {
5150
*
5251
* The return value is clamped to the platform-specific boundaries for [Instant] if the result exceeds them.
5352
*/
54-
@ExperimentalTime
5553
public operator fun plus(duration: Duration): Instant
5654

5755
/**
@@ -62,7 +60,6 @@ public expect class Instant : Comparable<Instant> {
6260
*
6361
* The return value is clamped to the platform-specific boundaries for [Instant] if the result exceeds them.
6462
*/
65-
@ExperimentalTime
6663
public operator fun minus(duration: Duration): Instant
6764

6865
// questionable
@@ -75,7 +72,6 @@ public expect class Instant : Comparable<Instant> {
7572
* The result is never clamped, but note that for instants that are far apart,
7673
* the value returned may represent the duration between them inexactly due to the loss of precision.
7774
*/
78-
@ExperimentalTime
7975
public operator fun minus(other: Instant): Duration
8076

8177
/**
@@ -483,4 +479,4 @@ internal const val DISTANT_FUTURE_SECONDS = 3093527980800
483479
*
484480
* Be careful: this function may throw for some values of the [Instant].
485481
*/
486-
internal expect fun Instant.toStringWithOffset(offset: UtcOffset): String
482+
internal expect fun Instant.toStringWithOffset(offset: UtcOffset): String

core/common/src/serializers/DateTimeUnitSerializers.kt

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public object TimeBasedDateTimeUnitSerializer: KSerializer<DateTimeUnit.TimeBase
2626
}
2727
}
2828

29-
@ExperimentalSerializationApi
29+
@OptIn(ExperimentalSerializationApi::class)
3030
@Suppress("INVISIBLE_MEMBER") // to be able to throw `MissingFieldException`
3131
override fun deserialize(decoder: Decoder): DateTimeUnit.TimeBased {
3232
var seen = false
@@ -65,7 +65,7 @@ public object DayBasedDateTimeUnitSerializer: KSerializer<DateTimeUnit.DayBased>
6565
}
6666
}
6767

68-
@ExperimentalSerializationApi
68+
@OptIn(ExperimentalSerializationApi::class)
6969
@Suppress("INVISIBLE_MEMBER") // to be able to throw `MissingFieldException`
7070
override fun deserialize(decoder: Decoder): DateTimeUnit.DayBased {
7171
var seen = false
@@ -104,7 +104,7 @@ public object MonthBasedDateTimeUnitSerializer: KSerializer<DateTimeUnit.MonthBa
104104
}
105105
}
106106

107-
@ExperimentalSerializationApi
107+
@OptIn(ExperimentalSerializationApi::class)
108108
@Suppress("INVISIBLE_MEMBER") // to be able to throw `MissingFieldException`
109109
override fun deserialize(decoder: Decoder): DateTimeUnit.MonthBased {
110110
var seen = false
@@ -149,11 +149,13 @@ public object DateBasedDateTimeUnitSerializer: AbstractPolymorphicSerializer<Dat
149149
SerializationStrategy<DateTimeUnit.DateBased>? =
150150
impl.findPolymorphicSerializerOrNull(encoder, value)
151151

152-
@InternalSerializationApi
152+
153+
@OptIn(InternalSerializationApi::class)
153154
override val baseClass: KClass<DateTimeUnit.DateBased>
154155
get() = DateTimeUnit.DateBased::class
155156

156-
@InternalSerializationApi
157+
158+
@OptIn(InternalSerializationApi::class)
157159
override val descriptor: SerialDescriptor
158160
get() = impl.descriptor
159161

@@ -175,11 +177,13 @@ public object DateTimeUnitSerializer: AbstractPolymorphicSerializer<DateTimeUnit
175177
override fun findPolymorphicSerializerOrNull(encoder: Encoder, value: DateTimeUnit): SerializationStrategy<DateTimeUnit>? =
176178
impl.findPolymorphicSerializerOrNull(encoder, value)
177179

178-
@InternalSerializationApi
180+
181+
@OptIn(InternalSerializationApi::class)
179182
override val baseClass: KClass<DateTimeUnit>
180183
get() = DateTimeUnit::class
181184

182-
@InternalSerializationApi
185+
186+
@OptIn(InternalSerializationApi::class)
183187
override val descriptor: SerialDescriptor
184188
get() = impl.descriptor
185189

core/common/test/DateTimePeriodTest.kt

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,13 @@
55

66
package kotlinx.datetime.test
77

8-
import kotlin.test.*
98
import kotlinx.datetime.*
9+
import kotlin.test.*
1010
import kotlin.time.*
11-
11+
import kotlin.time.Duration.Companion.hours
12+
import kotlin.time.Duration.Companion.nanoseconds
13+
import kotlin.time.Duration.Companion.minutes
14+
import kotlin.time.Duration.Companion.seconds
1215

1316
class DateTimePeriodTest {
1417

@@ -151,7 +154,6 @@ class DateTimePeriodTest {
151154
assertTrue(dp2 is DatePeriod)
152155
}
153156

154-
@OptIn(ExperimentalTime::class)
155157
@Test
156158
fun durationConversion() {
157159
val periodZero = Duration.ZERO.toDateTimePeriod()
@@ -160,10 +162,10 @@ class DateTimePeriodTest {
160162
assertTrue(periodZero is DatePeriod)
161163

162164
for ((period, duration) in listOf(
163-
DateTimePeriod(hours = 1) to Duration.hours(1),
164-
DateTimePeriod(hours = 2) to Duration.minutes(120),
165-
DateTimePeriod(minutes = 2, seconds = 30) to Duration.seconds(150),
166-
DateTimePeriod(seconds = 2) to Duration.nanoseconds(2e9)
165+
DateTimePeriod(hours = 1) to 1.hours,
166+
DateTimePeriod(hours = 2) to 120.minutes,
167+
DateTimePeriod(minutes = 2, seconds = 30) to 150.seconds,
168+
DateTimePeriod(seconds = 2) to 2e9.nanoseconds
167169
)) {
168170
assertEquals(period, duration.toDateTimePeriod())
169171
}

core/common/test/InstantTest.kt

Lines changed: 28 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,16 @@
44
*/
55

66
package kotlinx.datetime.test
7+
78
import kotlinx.datetime.*
89
import kotlinx.datetime.Clock // currently, requires an explicit import due to a conflict with the deprecated Clock from kotlin.time
910
import kotlin.random.*
1011
import kotlin.test.*
1112
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
1217

1318
class 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)
430428
class 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

Comments
 (0)