Skip to content

Commit a940847

Browse files
committed
Fix deprecations and other warnings after updating to Kotlin 1.5
1 parent 90b085f commit a940847

9 files changed

+49
-55
lines changed

core/common/src/DateTimePeriod.kt

+2-2
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ public sealed class DateTimePeriod {
162162
parseException("Value $this does not fit into an Int, which is required for component '$component'", iStart)
163163
return toInt()
164164
}
165-
when (text[i].toUpperCase()) {
165+
when (text[i].uppercaseChar()) {
166166
'Y' -> {
167167
if (state >= AFTER_YEAR)
168168
parseException(wrongOrder, i)
@@ -308,7 +308,7 @@ public fun DateTimePeriod(
308308
totalNanoseconds(hours, minutes, seconds, nanoseconds))
309309

310310
@OptIn(ExperimentalTime::class)
311-
public fun Duration.toDateTimePeriod(): DateTimePeriod = buildDateTimePeriod(totalNanoseconds = toLongNanoseconds())
311+
public fun Duration.toDateTimePeriod(): DateTimePeriod = buildDateTimePeriod(totalNanoseconds = inWholeNanoseconds)
312312

313313
public operator fun DateTimePeriod.plus(other: DateTimePeriod): DateTimePeriod = buildDateTimePeriod(
314314
safeAdd(totalMonths, other.totalMonths),

core/common/src/DateTimeUnit.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public sealed class DateTimeUnit {
5454

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

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

core/common/test/DateTimePeriodTest.kt

+4-4
Original file line numberDiff line numberDiff line change
@@ -160,10 +160,10 @@ class DateTimePeriodTest {
160160
assertTrue(periodZero is DatePeriod)
161161

162162
for ((period, duration) in listOf(
163-
DateTimePeriod(hours = 1) to 1.hours,
164-
DateTimePeriod(hours = 2) to 120.minutes,
165-
DateTimePeriod(minutes = 2, seconds = 30) to 150.seconds,
166-
DateTimePeriod(seconds = 2) to 2e9.nanoseconds
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)
167167
)) {
168168
assertEquals(period, duration.toDateTimePeriod())
169169
}

core/common/test/InstantTest.kt

+24-24
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ class InstantTest {
3535
fun instantArithmetic() {
3636
val instant = Clock.System.now().toEpochMilliseconds().let { Instant.fromEpochMilliseconds(it) } // round to millis
3737
val diffMillis = Random.nextLong(1000, 1_000_000_000)
38-
val diff = diffMillis.milliseconds
38+
val diff = Duration.milliseconds(diffMillis)
3939

4040
val nextInstant = (instant.toEpochMilliseconds() + diffMillis).let { Instant.fromEpochMilliseconds(it) }
4141

@@ -180,7 +180,7 @@ class InstantTest {
180180
checkComponents(instant5.toLocalDateTime(zone), 2019, 10, 28, 3, 59)
181181
assertEquals(period, instant1.periodUntil(instant5, zone))
182182
assertEquals(period, instant5.minus(instant1, zone))
183-
assertEquals(26.hours, instant5.minus(instant1))
183+
assertEquals(Duration.hours(26), instant5.minus(instant1))
184184
assertEquals(instant1.plus(DateTimeUnit.HOUR), instant5.minus(period, zone))
185185

186186
val instant6 = instant1.plus(23, DateTimeUnit.HOUR, zone)
@@ -237,15 +237,15 @@ class InstantTest {
237237
checkComponents(ldt1, 2019, 10, 27, 2, 59)
238238
assertEquals(instant1, ldt1.toInstant(offset1))
239239

240-
val instant2 = instant1 + 1.hours
240+
val instant2 = instant1 + Duration.hours(1)
241241
val ldt2 = instant2.toLocalDateTime(zone)
242242
val offset2 = instant2.offsetIn(zone)
243243
assertEquals(ldt1, ldt2)
244244
assertEquals(instant2, ldt2.toInstant(offset2))
245245
assertNotEquals(offset1, offset2)
246-
assertEquals(offset1.totalSeconds.seconds, offset2.totalSeconds.seconds + 1.hours)
246+
assertEquals(Duration.seconds(offset1.totalSeconds), Duration.seconds(offset2.totalSeconds) + Duration.hours(1))
247247

248-
val instant3 = instant2 - 2.hours
248+
val instant3 = instant2 - Duration.hours(2)
249249
val offset3 = instant3.offsetIn(zone)
250250
assertEquals(offset1, offset3)
251251
}
@@ -414,10 +414,10 @@ class InstantTest {
414414
assertTrue(Instant.DISTANT_FUTURE.isDistantFuture)
415415
assertFalse(Instant.DISTANT_PAST.isDistantFuture)
416416
assertFalse(Instant.DISTANT_FUTURE.isDistantPast)
417-
assertFalse((Instant.DISTANT_PAST + 1.nanoseconds).isDistantPast)
418-
assertFalse((Instant.DISTANT_FUTURE - 1.nanoseconds).isDistantFuture)
419-
assertTrue((Instant.DISTANT_PAST - 1.nanoseconds).isDistantPast)
420-
assertTrue((Instant.DISTANT_FUTURE + 1.nanoseconds).isDistantFuture)
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)
421421
assertTrue(Instant.MAX.isDistantFuture)
422422
assertFalse(Instant.MAX.isDistantPast)
423423
assertTrue(Instant.MIN.isDistantPast)
@@ -435,8 +435,8 @@ class InstantRangeTest {
435435
private val largePositiveLongs = listOf(Long.MAX_VALUE, Long.MAX_VALUE - 1, Long.MAX_VALUE - 50)
436436
private val largeNegativeLongs = listOf(Long.MIN_VALUE, Long.MIN_VALUE + 1, Long.MIN_VALUE + 50)
437437

438-
private val largePositiveInstants = listOf(Instant.MAX, Instant.MAX - 1.seconds, Instant.MAX - 50.seconds)
439-
private val largeNegativeInstants = listOf(Instant.MIN, Instant.MIN + 1.seconds, Instant.MIN + 50.seconds)
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))
440440

441441
private val smallInstants = listOf(
442442
Instant.fromEpochMilliseconds(0),
@@ -494,7 +494,7 @@ class InstantRangeTest {
494494

495495
@Test
496496
fun durationArithmeticClamping() {
497-
val longDurations = listOf(Duration.INFINITE, Double.MAX_VALUE.nanoseconds, Long.MAX_VALUE.seconds)
497+
val longDurations = listOf(Duration.INFINITE)
498498

499499
for (duration in longDurations) {
500500
for (instant in smallInstants + largeNegativeInstants + largePositiveInstants) {
@@ -504,8 +504,8 @@ class InstantRangeTest {
504504
assertEquals(Instant.MIN, instant - duration)
505505
}
506506
}
507-
assertEquals(Instant.MAX, (Instant.MAX - 4.seconds) + 5.seconds)
508-
assertEquals(Instant.MIN, (Instant.MIN + 10.seconds) - 12.seconds)
507+
assertEquals(Instant.MAX, (Instant.MAX - Duration.seconds(4)) + Duration.seconds(5))
508+
assertEquals(Instant.MIN, (Instant.MIN + Duration.seconds(10)) - Duration.seconds(12))
509509
}
510510

511511
@Test
@@ -534,8 +534,8 @@ class InstantRangeTest {
534534
// Overflowing a LocalDateTime in input
535535
maxValidInstant.plus(DateTimePeriod(nanoseconds = -1), UTC)
536536
minValidInstant.plus(DateTimePeriod(nanoseconds = 1), UTC)
537-
assertArithmeticFails { (maxValidInstant + 1.nanoseconds).plus(DateTimePeriod(nanoseconds = -2), UTC) }
538-
assertArithmeticFails { (minValidInstant - 1.nanoseconds).plus(DateTimePeriod(nanoseconds = 2), UTC) }
537+
assertArithmeticFails { (maxValidInstant + Duration.nanoseconds(1)).plus(DateTimePeriod(nanoseconds = -2), UTC) }
538+
assertArithmeticFails { (minValidInstant - Duration.nanoseconds(1)).plus(DateTimePeriod(nanoseconds = 2), UTC) }
539539
// Overflowing a LocalDateTime in result
540540
assertArithmeticFails { maxValidInstant.plus(DateTimePeriod(nanoseconds = 1), UTC) }
541541
assertArithmeticFails { minValidInstant.plus(DateTimePeriod(nanoseconds = -1), UTC) }
@@ -557,8 +557,8 @@ class InstantRangeTest {
557557
// Overflowing a LocalDateTime in input
558558
maxValidInstant.plus(-1, DateTimeUnit.NANOSECOND, UTC)
559559
minValidInstant.plus(1, DateTimeUnit.NANOSECOND, UTC)
560-
assertArithmeticFails { (maxValidInstant + 1.nanoseconds).plus(-2, DateTimeUnit.NANOSECOND, UTC) }
561-
assertArithmeticFails { (minValidInstant - 1.nanoseconds).plus(2, 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) }
562562
// Overflowing a LocalDateTime in result
563563
assertArithmeticFails { maxValidInstant.plus(1, DateTimeUnit.NANOSECOND, UTC) }
564564
assertArithmeticFails { maxValidInstant.plus(1, DateTimeUnit.YEAR, UTC) }
@@ -586,8 +586,8 @@ class InstantRangeTest {
586586
fun periodUntilOutOfRange() {
587587
// Instant.periodUntil
588588
maxValidInstant.periodUntil(maxValidInstant, UTC)
589-
assertArithmeticFails { (maxValidInstant + 1.nanoseconds).periodUntil(maxValidInstant, UTC) }
590-
assertArithmeticFails { minValidInstant.periodUntil(minValidInstant - 1.nanoseconds, UTC) }
589+
assertArithmeticFails { (maxValidInstant + Duration.nanoseconds(1)).periodUntil(maxValidInstant, UTC) }
590+
assertArithmeticFails { minValidInstant.periodUntil(minValidInstant - Duration.nanoseconds(1), UTC) }
591591
}
592592

593593
@Test
@@ -603,11 +603,11 @@ class InstantRangeTest {
603603
fun unitsUntilOutOfRange() {
604604
// Instant.until
605605
// Overflowing a LocalDateTime in input
606-
assertArithmeticFails { (maxValidInstant + 1.nanoseconds).until(maxValidInstant, DateTimeUnit.NANOSECOND, UTC) }
607-
assertArithmeticFails { maxValidInstant.until(maxValidInstant + 1.nanoseconds, DateTimeUnit.NANOSECOND, UTC) }
606+
assertArithmeticFails { (maxValidInstant + Duration.nanoseconds(1)).until(maxValidInstant, DateTimeUnit.NANOSECOND, UTC) }
607+
assertArithmeticFails { maxValidInstant.until(maxValidInstant + Duration.nanoseconds(1), DateTimeUnit.NANOSECOND, UTC) }
608608
// Overloads without a TimeZone should not fail on overflowing a LocalDateTime
609-
(maxValidInstant + 1.nanoseconds).until(maxValidInstant, DateTimeUnit.NANOSECOND)
610-
maxValidInstant.until(maxValidInstant + 1.nanoseconds, DateTimeUnit.NANOSECOND)
609+
(maxValidInstant + Duration.nanoseconds(1)).until(maxValidInstant, DateTimeUnit.NANOSECOND)
610+
maxValidInstant.until(maxValidInstant + Duration.nanoseconds(1), DateTimeUnit.NANOSECOND)
611611
}
612612
}
613613

core/common/test/LocalDateTimeTest.kt

+4-4
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,9 @@ class LocalDateTimeTest {
4343
val ldt2 = "2019-10-01T19:50:00.500600".toLocalDateTime()
4444

4545
val diff = with(TimeZone.UTC) { ldt2.toInstant() - ldt1.toInstant() }
46-
assertEquals(1.hours + 7.minutes - 15.seconds + 400100.microseconds, diff)
47-
assertFailsWith<DateTimeArithmeticException> { (Instant.MAX - 3.days).toLocalDateTime(TimeZone.UTC) }
48-
assertFailsWith<DateTimeArithmeticException> { (Instant.MIN + 6.hours).toLocalDateTime(TimeZone.UTC) }
46+
assertEquals(with(Duration) { hours(1) + minutes(7) - seconds(15) + microseconds(400100) }, diff)
47+
assertFailsWith<DateTimeArithmeticException> { (Instant.MAX - Duration.days(3)).toLocalDateTime(TimeZone.UTC) }
48+
assertFailsWith<DateTimeArithmeticException> { (Instant.MIN + Duration.hours(6)).toLocalDateTime(TimeZone.UTC) }
4949
}
5050

5151
@OptIn(ExperimentalTime::class)
@@ -54,7 +54,7 @@ class LocalDateTimeTest {
5454
val ldt1 = "2011-03-26T04:00:00".toLocalDateTime()
5555
val ldt2 = "2011-03-27T04:00:00".toLocalDateTime()
5656
val diff = with(TimeZone.of("Europe/Moscow")) { ldt2.toInstant() - ldt1.toInstant() }
57-
assertEquals(23.hours, diff)
57+
assertEquals(Duration.hours(23), diff)
5858
}
5959

6060
@Test

core/js/src/Instant.kt

+9-15
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,14 @@
66
package kotlinx.datetime
77

88
import kotlinx.datetime.internal.JSJoda.ZonedDateTime
9-
import kotlin.time.Duration
10-
import kotlin.time.ExperimentalTime
11-
import kotlin.time.nanoseconds
12-
import kotlin.time.seconds
139
import kotlinx.datetime.internal.JSJoda.Instant as jtInstant
1410
import kotlinx.datetime.internal.JSJoda.OffsetDateTime as jtOffsetDateTime
1511
import kotlinx.datetime.internal.JSJoda.Duration as jtDuration
1612
import kotlinx.datetime.internal.JSJoda.Clock as jtClock
1713
import kotlinx.datetime.internal.JSJoda.ChronoUnit
1814
import kotlinx.datetime.serializers.InstantIso8601Serializer
1915
import kotlinx.serialization.Serializable
20-
import kotlin.math.truncate
16+
import kotlin.time.*
2117

2218
@Serializable(with = InstantIso8601Serializer::class)
2319
@OptIn(ExperimentalTime::class)
@@ -31,14 +27,12 @@ public actual class Instant internal constructor(internal val value: jtInstant)
3127
public actual fun toEpochMilliseconds(): Long =
3228
epochSeconds * MILLIS_PER_ONE + nanosecondsOfSecond / NANOS_PER_MILLI
3329

34-
public actual operator fun plus(duration: Duration): Instant {
35-
val addSeconds = truncate(duration.inSeconds)
36-
val addNanos = (duration.inNanoseconds % NANOS_PER_ONE).toInt()
30+
public actual operator fun plus(duration: Duration): Instant = duration.toComponents { seconds, nanoseconds ->
3731
return try {
38-
Instant(plusFix(addSeconds, addNanos))
32+
Instant(plusFix(seconds.toDouble(), nanoseconds))
3933
} catch (e: Throwable) {
4034
if (!e.isJodaDateTimeException()) throw e
41-
if (addSeconds > 0) MAX else MIN
35+
if (seconds > 0) MAX else MIN
4236
}
4337
}
4438

@@ -52,7 +46,7 @@ public actual class Instant internal constructor(internal val value: jtInstant)
5246

5347
public actual operator fun minus(other: Instant): Duration {
5448
val diff = jtDuration.between(other.value, this.value)
55-
return diff.seconds().toDouble().seconds + diff.nano().toDouble().nanoseconds
49+
return Duration.seconds(diff.seconds().toDouble()) + Duration.nanoseconds(diff.nano().toDouble())
5650
}
5751

5852
public actual override operator fun compareTo(other: Instant): Int = this.value.compareTo(other.value).toInt()
@@ -127,7 +121,7 @@ public actual fun Instant.plus(period: DateTimePeriod, timeZone: TimeZone): Inst
127121
with(period) {
128122
thisZdt
129123
.run { if (totalMonths != 0) plusMonths(totalMonths) else this }
130-
.run { if (days != 0) plusDays(days) as ZonedDateTime else this }
124+
.run { if (days != 0) plusDays(days) else this }
131125
.run { if (hours != 0) plusHours(hours) else this }
132126
.run { if (minutes != 0) plusMinutes(minutes) else this }
133127
.run { if (seconds != 0) plusSeconds(seconds) else this }
@@ -152,7 +146,7 @@ public actual fun Instant.plus(value: Long, unit: DateTimeUnit, timeZone: TimeZo
152146
plus(value, unit).value.checkZone(timeZone)
153147
}
154148
is DateTimeUnit.DateBased.DayBased ->
155-
(thisZdt.plusDays(value.toDouble() * unit.days) as ZonedDateTime).toInstant()
149+
thisZdt.plusDays(value.toDouble() * unit.days).toInstant()
156150
is DateTimeUnit.DateBased.MonthBased ->
157151
thisZdt.plusMonths(value.toDouble() * unit.months).toInstant()
158152
}.let(::Instant)
@@ -168,7 +162,7 @@ public actual fun Instant.plus(value: Int, unit: DateTimeUnit, timeZone: TimeZon
168162
is DateTimeUnit.TimeBased ->
169163
plus(value.toLong(), unit).value.checkZone(timeZone)
170164
is DateTimeUnit.DateBased.DayBased ->
171-
(thisZdt.plusDays(value.toDouble() * unit.days) as ZonedDateTime).toInstant()
165+
thisZdt.plusDays(value.toDouble() * unit.days).toInstant()
172166
is DateTimeUnit.DateBased.MonthBased ->
173167
thisZdt.plusMonths(value.toDouble() * unit.months).toInstant()
174168
}.let(::Instant)
@@ -201,7 +195,7 @@ public actual fun Instant.periodUntil(other: Instant, timeZone: TimeZone): DateT
201195
val otherZdt = other.value.atZone(timeZone.zoneId)
202196

203197
val months = thisZdt.until(otherZdt, ChronoUnit.MONTHS).toDouble(); thisZdt = thisZdt.plusMonths(months)
204-
val days = thisZdt.until(otherZdt, ChronoUnit.DAYS).toDouble(); thisZdt = thisZdt.plusDays(days) as ZonedDateTime
198+
val days = thisZdt.until(otherZdt, ChronoUnit.DAYS).toDouble(); thisZdt = thisZdt.plusDays(days)
205199
val nanoseconds = thisZdt.until(otherZdt, ChronoUnit.NANOS).toDouble()
206200

207201
buildDateTimePeriod(months.toInt(), days.toInt(), nanoseconds.toLong())

core/jvm/src/Instant.kt

+2-2
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@ public actual class Instant internal constructor(internal val value: jtInstant)
4343
public actual operator fun minus(duration: Duration): Instant = plus(-duration)
4444

4545
public actual operator fun minus(other: Instant): Duration =
46-
(this.value.epochSecond - other.value.epochSecond).seconds + // won't overflow given the instant bounds
47-
(this.value.nano - other.value.nano).nanoseconds
46+
Duration.seconds(this.value.epochSecond - other.value.epochSecond) + // won't overflow given the instant bounds
47+
Duration.nanoseconds(this.value.nano - other.value.nano)
4848

4949
public actual override operator fun compareTo(other: Instant): Int = this.value.compareTo(other.value)
5050

core/native/src/Instant.kt

+2-2
Original file line numberDiff line numberDiff line change
@@ -166,8 +166,8 @@ public actual class Instant internal constructor(public actual val epochSeconds:
166166
public actual operator fun minus(duration: Duration): Instant = plus(-duration)
167167

168168
public actual operator fun minus(other: Instant): Duration =
169-
(this.epochSeconds - other.epochSeconds).seconds + // won't overflow given the instant bounds
170-
(this.nanosecondsOfSecond - other.nanosecondsOfSecond).nanoseconds
169+
Duration.seconds(this.epochSeconds - other.epochSeconds) + // won't overflow given the instant bounds
170+
Duration.nanoseconds(this.nanosecondsOfSecond - other.nanosecondsOfSecond)
171171

172172
actual override fun compareTo(other: Instant): Int {
173173
val s = epochSeconds.compareTo(other.epochSeconds)

core/native/src/TimeZone.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ public actual class ZoneOffset internal constructor(internal val offset: ZoneOff
198198
if (ch1 < '0' || ch1 > '9' || ch2 < '0' || ch2 > '9') {
199199
throw IllegalTimeZoneException("Invalid ID for ZoneOffset, non numeric characters found: $offsetId")
200200
}
201-
return (ch1.toInt() - 48) * 10 + (ch2.toInt() - 48)
201+
return (ch1 - '0') * 10 + (ch2 - '0')
202202
}
203203
}
204204
}

0 commit comments

Comments
 (0)