Skip to content

Bump Kotlin to 1.6.0 and remove ExperimentalTime #161

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Dec 13, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 1 addition & 5 deletions core/common/src/Clock.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,7 @@

package kotlinx.datetime

import kotlin.time.Duration
import kotlin.time.ExperimentalTime
import kotlin.time.TimeMark
import kotlin.time.TimeSource
import kotlin.time.*

public interface Clock {
public fun now(): Instant
Expand All @@ -25,7 +22,6 @@ public interface Clock {
public fun Clock.todayAt(timeZone: TimeZone): LocalDate =
now().toLocalDateTime(timeZone).date


@ExperimentalTime
public fun Clock.asTimeSource(): TimeSource = object : TimeSource {
override fun markNow(): TimeMark = InstantTimeMark(now(), this@asTimeSource)
Expand Down
3 changes: 0 additions & 3 deletions core/common/src/DateTimePeriod.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import kotlinx.datetime.serializers.DatePeriodIso8601Serializer
import kotlinx.datetime.serializers.DateTimePeriodIso8601Serializer
import kotlin.math.*
import kotlin.time.Duration
import kotlin.time.ExperimentalTime
import kotlinx.serialization.Serializable

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

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

public operator fun DateTimePeriod.plus(other: DateTimePeriod): DateTimePeriod = buildDateTimePeriod(
Expand All @@ -320,4 +318,3 @@ public operator fun DatePeriod.plus(other: DatePeriod): DatePeriod = DatePeriod(
safeAdd(totalMonths, other.totalMonths),
safeAdd(days, other.days),
)

4 changes: 2 additions & 2 deletions core/common/src/DateTimeUnit.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ package kotlinx.datetime
import kotlinx.datetime.serializers.*
import kotlinx.serialization.Serializable
import kotlin.time.*
import kotlin.time.Duration.Companion.nanoseconds

@Serializable(with = DateTimeUnitSerializer::class)
public sealed class DateTimeUnit {
Expand Down Expand Up @@ -52,9 +53,8 @@ public sealed class DateTimeUnit {

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

@ExperimentalTime
public val duration: Duration
get() = Duration.nanoseconds(nanoseconds)
get() = nanoseconds.nanoseconds

override fun equals(other: Any?): Boolean =
this === other || (other is TimeBased && this.nanoseconds == other.nanoseconds)
Expand Down
6 changes: 1 addition & 5 deletions core/common/src/Instant.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import kotlinx.datetime.serializers.InstantIso8601Serializer
import kotlinx.serialization.Serializable
import kotlin.time.*

@OptIn(ExperimentalTime::class)
@Serializable(with = InstantIso8601Serializer::class)
public expect class Instant : Comparable<Instant> {

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

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

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

/**
Expand Down Expand Up @@ -483,4 +479,4 @@ internal const val DISTANT_FUTURE_SECONDS = 3093527980800
*
* Be careful: this function may throw for some values of the [Instant].
*/
internal expect fun Instant.toStringWithOffset(offset: UtcOffset): String
internal expect fun Instant.toStringWithOffset(offset: UtcOffset): String
18 changes: 11 additions & 7 deletions core/common/src/serializers/DateTimeUnitSerializers.kt
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public object TimeBasedDateTimeUnitSerializer: KSerializer<DateTimeUnit.TimeBase
}
}

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

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

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

@InternalSerializationApi

@OptIn(InternalSerializationApi::class)
override val baseClass: KClass<DateTimeUnit.DateBased>
get() = DateTimeUnit.DateBased::class

@InternalSerializationApi

@OptIn(InternalSerializationApi::class)
override val descriptor: SerialDescriptor
get() = impl.descriptor

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

@InternalSerializationApi

@OptIn(InternalSerializationApi::class)
override val baseClass: KClass<DateTimeUnit>
get() = DateTimeUnit::class

@InternalSerializationApi

@OptIn(InternalSerializationApi::class)
override val descriptor: SerialDescriptor
get() = impl.descriptor

Expand Down
16 changes: 9 additions & 7 deletions core/common/test/DateTimePeriodTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,13 @@

package kotlinx.datetime.test

import kotlin.test.*
import kotlinx.datetime.*
import kotlin.test.*
import kotlin.time.*

import kotlin.time.Duration.Companion.hours
import kotlin.time.Duration.Companion.nanoseconds
import kotlin.time.Duration.Companion.minutes
import kotlin.time.Duration.Companion.seconds

class DateTimePeriodTest {

Expand Down Expand Up @@ -151,7 +154,6 @@ class DateTimePeriodTest {
assertTrue(dp2 is DatePeriod)
}

@OptIn(ExperimentalTime::class)
@Test
fun durationConversion() {
val periodZero = Duration.ZERO.toDateTimePeriod()
Expand All @@ -160,10 +162,10 @@ class DateTimePeriodTest {
assertTrue(periodZero is DatePeriod)

for ((period, duration) in listOf(
DateTimePeriod(hours = 1) to Duration.hours(1),
DateTimePeriod(hours = 2) to Duration.minutes(120),
DateTimePeriod(minutes = 2, seconds = 30) to Duration.seconds(150),
DateTimePeriod(seconds = 2) to Duration.nanoseconds(2e9)
DateTimePeriod(hours = 1) to 1.hours,
DateTimePeriod(hours = 2) to 120.minutes,
DateTimePeriod(minutes = 2, seconds = 30) to 150.seconds,
DateTimePeriod(seconds = 2) to 2e9.nanoseconds
)) {
assertEquals(period, duration.toDateTimePeriod())
}
Expand Down
59 changes: 28 additions & 31 deletions core/common/test/InstantTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,16 @@
*/

package kotlinx.datetime.test

import kotlinx.datetime.*
import kotlinx.datetime.Clock // currently, requires an explicit import due to a conflict with the deprecated Clock from kotlin.time
import kotlin.random.*
import kotlin.test.*
import kotlin.time.*
import kotlin.time.Duration.Companion.hours
import kotlin.time.Duration.Companion.milliseconds
import kotlin.time.Duration.Companion.nanoseconds
import kotlin.time.Duration.Companion.seconds

class InstantTest {

Expand All @@ -30,12 +35,11 @@ class InstantTest {
assertNotEquals(notEqualInstant, instant)
}

@OptIn(ExperimentalTime::class)
@Test
fun instantArithmetic() {
val instant = Clock.System.now().toEpochMilliseconds().let { Instant.fromEpochMilliseconds(it) } // round to millis
val diffMillis = Random.nextLong(1000, 1_000_000_000)
val diff = Duration.milliseconds(diffMillis)
val diff = diffMillis.milliseconds

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

Expand Down Expand Up @@ -129,7 +133,6 @@ class InstantTest {
}
}

@OptIn(ExperimentalTime::class)
@Test
fun instantCalendarArithmetic() {
val zone = TimeZone.of("Europe/Berlin")
Expand Down Expand Up @@ -180,7 +183,7 @@ class InstantTest {
checkComponents(instant5.toLocalDateTime(zone), 2019, 10, 28, 3, 59)
assertEquals(period, instant1.periodUntil(instant5, zone))
assertEquals(period, instant5.minus(instant1, zone))
assertEquals(Duration.hours(26), instant5.minus(instant1))
assertEquals(26.hours, instant5.minus(instant1))
assertEquals(instant1.plus(DateTimeUnit.HOUR), instant5.minus(period, zone))

val instant6 = instant1.plus(23, DateTimeUnit.HOUR, zone)
Expand All @@ -202,7 +205,6 @@ class InstantTest {
assertEquals(pow2_32, instant3.epochSeconds)
}

@OptIn(ExperimentalTime::class)
@Test
fun unitMultiplesUntil() {
val unit1000days = DateTimeUnit.DAY * 1000
Expand All @@ -227,7 +229,6 @@ class InstantTest {
assertEquals(start, end.plus(-diffUs, DateTimeUnit.MICROSECOND, zone))
}

@OptIn(ExperimentalTime::class)
@Test
fun instantOffset() {
val zone = TimeZone.of("Europe/Berlin")
Expand All @@ -237,15 +238,15 @@ class InstantTest {
checkComponents(ldt1, 2019, 10, 27, 2, 59)
assertEquals(instant1, ldt1.toInstant(offset1))

val instant2 = instant1 + Duration.hours(1)
val instant2 = instant1 + 1.hours
val ldt2 = instant2.toLocalDateTime(zone)
val offset2 = instant2.offsetIn(zone)
assertEquals(ldt1, ldt2)
assertEquals(instant2, ldt2.toInstant(offset2))
assertNotEquals(offset1, offset2)
assertEquals(Duration.seconds(offset1.totalSeconds), Duration.seconds(offset2.totalSeconds) + Duration.hours(1))
assertEquals(offset1.totalSeconds.seconds, offset2.totalSeconds.seconds + 1.hours)

val instant3 = instant2 - Duration.hours(2)
val instant3 = instant2 - 2.hours
val offset3 = instant3.offsetIn(zone)
assertEquals(offset1, offset3)
}
Expand Down Expand Up @@ -348,7 +349,6 @@ class InstantTest {
/* Based on the ThreeTenBp project.
* Copyright (c) 2007-present, Stephen Colebourne & Michael Nascimento Santos
*/
// @ExperimentalTime
@Test
fun strings() {
assertEquals("0000-01-02T00:00:00Z", LocalDateTime(0, 1, 2, 0, 0, 0, 0).toInstant(TimeZone.UTC).toString())
Expand Down Expand Up @@ -401,7 +401,6 @@ class InstantTest {
assertEquals("+19999-12-31T23:59:59.000000009Z", LocalDateTime(19999, 12, 31, 23, 59, 59, 9).toInstant(TimeZone.UTC).toString())
}

@ExperimentalTime
@Test
fun distantPastAndFuture() {
val distantFutureString = "+100000-01-01T00:00:00Z"
Expand All @@ -414,10 +413,10 @@ class InstantTest {
assertTrue(Instant.DISTANT_FUTURE.isDistantFuture)
assertFalse(Instant.DISTANT_PAST.isDistantFuture)
assertFalse(Instant.DISTANT_FUTURE.isDistantPast)
assertFalse((Instant.DISTANT_PAST + Duration.nanoseconds(1)).isDistantPast)
assertFalse((Instant.DISTANT_FUTURE - Duration.nanoseconds(1)).isDistantFuture)
assertTrue((Instant.DISTANT_PAST - Duration.nanoseconds(1)).isDistantPast)
assertTrue((Instant.DISTANT_FUTURE + Duration.nanoseconds(1)).isDistantFuture)
assertFalse((Instant.DISTANT_PAST + 1.nanoseconds).isDistantPast)
assertFalse((Instant.DISTANT_FUTURE - 1.nanoseconds).isDistantFuture)
assertTrue((Instant.DISTANT_PAST - 1.nanoseconds).isDistantPast)
assertTrue((Instant.DISTANT_FUTURE + 1.nanoseconds).isDistantFuture)
assertTrue(Instant.MAX.isDistantFuture)
assertFalse(Instant.MAX.isDistantPast)
assertTrue(Instant.MIN.isDistantPast)
Expand All @@ -426,7 +425,6 @@ class InstantTest {

}

@OptIn(ExperimentalTime::class)
class InstantRangeTest {
private val UTC = TimeZone.UTC
private val maxValidInstant = LocalDateTime.MAX.toInstant(UTC)
Expand All @@ -435,8 +433,8 @@ class InstantRangeTest {
private val largePositiveLongs = listOf(Long.MAX_VALUE, Long.MAX_VALUE - 1, Long.MAX_VALUE - 50)
private val largeNegativeLongs = listOf(Long.MIN_VALUE, Long.MIN_VALUE + 1, Long.MIN_VALUE + 50)

private val largePositiveInstants = listOf(Instant.MAX, Instant.MAX - Duration.seconds(1), Instant.MAX - Duration.seconds(50))
private val largeNegativeInstants = listOf(Instant.MIN, Instant.MIN + Duration.seconds(1), Instant.MIN + Duration.seconds(50))
private val largePositiveInstants = listOf(Instant.MAX, Instant.MAX - 1.seconds, Instant.MAX - 50.seconds)
private val largeNegativeInstants = listOf(Instant.MIN, Instant.MIN + 1.seconds, Instant.MIN + 50.seconds)

private val smallInstants = listOf(
Instant.fromEpochMilliseconds(0),
Expand Down Expand Up @@ -504,8 +502,8 @@ class InstantRangeTest {
assertEquals(Instant.MIN, instant - duration)
}
}
assertEquals(Instant.MAX, (Instant.MAX - Duration.seconds(4)) + Duration.seconds(5))
assertEquals(Instant.MIN, (Instant.MIN + Duration.seconds(10)) - Duration.seconds(12))
assertEquals(Instant.MAX, (Instant.MAX - 4.seconds) + 5.seconds)
assertEquals(Instant.MIN, (Instant.MIN + 10.seconds) - 12.seconds)
}

@Test
Expand Down Expand Up @@ -534,8 +532,8 @@ class InstantRangeTest {
// Overflowing a LocalDateTime in input
maxValidInstant.plus(DateTimePeriod(nanoseconds = -1), UTC)
minValidInstant.plus(DateTimePeriod(nanoseconds = 1), UTC)
assertArithmeticFails { (maxValidInstant + Duration.nanoseconds(1)).plus(DateTimePeriod(nanoseconds = -2), UTC) }
assertArithmeticFails { (minValidInstant - Duration.nanoseconds(1)).plus(DateTimePeriod(nanoseconds = 2), UTC) }
assertArithmeticFails { (maxValidInstant + 1.nanoseconds).plus(DateTimePeriod(nanoseconds = -2), UTC) }
assertArithmeticFails { (minValidInstant - 1.nanoseconds).plus(DateTimePeriod(nanoseconds = 2), UTC) }
// Overflowing a LocalDateTime in result
assertArithmeticFails { maxValidInstant.plus(DateTimePeriod(nanoseconds = 1), UTC) }
assertArithmeticFails { minValidInstant.plus(DateTimePeriod(nanoseconds = -1), UTC) }
Expand All @@ -557,8 +555,8 @@ class InstantRangeTest {
// Overflowing a LocalDateTime in input
maxValidInstant.plus(-1, DateTimeUnit.NANOSECOND, UTC)
minValidInstant.plus(1, DateTimeUnit.NANOSECOND, UTC)
assertArithmeticFails { (maxValidInstant + Duration.nanoseconds(1)).plus(-2, DateTimeUnit.NANOSECOND, UTC) }
assertArithmeticFails { (minValidInstant - Duration.nanoseconds(1)).plus(2, DateTimeUnit.NANOSECOND, UTC) }
assertArithmeticFails { (maxValidInstant + 1.nanoseconds).plus(-2, DateTimeUnit.NANOSECOND, UTC) }
assertArithmeticFails { (minValidInstant - 1.nanoseconds).plus(2, DateTimeUnit.NANOSECOND, UTC) }
// Overflowing a LocalDateTime in result
assertArithmeticFails { maxValidInstant.plus(1, DateTimeUnit.NANOSECOND, UTC) }
assertArithmeticFails { maxValidInstant.plus(1, DateTimeUnit.YEAR, UTC) }
Expand Down Expand Up @@ -586,8 +584,8 @@ class InstantRangeTest {
fun periodUntilOutOfRange() {
// Instant.periodUntil
maxValidInstant.periodUntil(maxValidInstant, UTC)
assertArithmeticFails { (maxValidInstant + Duration.nanoseconds(1)).periodUntil(maxValidInstant, UTC) }
assertArithmeticFails { minValidInstant.periodUntil(minValidInstant - Duration.nanoseconds(1), UTC) }
assertArithmeticFails { (maxValidInstant + 1.nanoseconds).periodUntil(maxValidInstant, UTC) }
assertArithmeticFails { minValidInstant.periodUntil(minValidInstant - 1.nanoseconds, UTC) }
}

@Test
Expand All @@ -603,11 +601,10 @@ class InstantRangeTest {
fun unitsUntilOutOfRange() {
// Instant.until
// Overflowing a LocalDateTime in input
assertArithmeticFails { (maxValidInstant + Duration.nanoseconds(1)).until(maxValidInstant, DateTimeUnit.NANOSECOND, UTC) }
assertArithmeticFails { maxValidInstant.until(maxValidInstant + Duration.nanoseconds(1), DateTimeUnit.NANOSECOND, UTC) }
assertArithmeticFails { (maxValidInstant + 1.nanoseconds).until(maxValidInstant, DateTimeUnit.NANOSECOND, UTC) }
assertArithmeticFails { maxValidInstant.until(maxValidInstant + 1.nanoseconds, DateTimeUnit.NANOSECOND, UTC) }
// Overloads without a TimeZone should not fail on overflowing a LocalDateTime
(maxValidInstant + Duration.nanoseconds(1)).until(maxValidInstant, DateTimeUnit.NANOSECOND)
maxValidInstant.until(maxValidInstant + Duration.nanoseconds(1), DateTimeUnit.NANOSECOND)
(maxValidInstant + 1.nanoseconds).until(maxValidInstant, DateTimeUnit.NANOSECOND)
maxValidInstant.until(maxValidInstant + 1.nanoseconds, DateTimeUnit.NANOSECOND)
}
}

Loading