From 2db494c4ef93953a668f1d4d208552d5e1786484 Mon Sep 17 00:00:00 2001 From: Dmitry Khalanskiy Date: Fri, 18 Oct 2024 11:57:53 +0200 Subject: [PATCH 1/3] Deprecate DatePeriod.plus and DateTimePeriod.plus Fixes #381 --- core/common/src/DateTimePeriod.kt | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/core/common/src/DateTimePeriod.kt b/core/common/src/DateTimePeriod.kt index f9b2d3f3..418e364e 100644 --- a/core/common/src/DateTimePeriod.kt +++ b/core/common/src/DateTimePeriod.kt @@ -577,6 +577,10 @@ public fun Duration.toDateTimePeriod(): DateTimePeriod = buildDateTimePeriod(tot * * @throws DateTimeArithmeticException if arithmetic overflow happens. */ +@Deprecated( + "Adding periods is not a well-defined operation. See https://github.com/Kotlin/kotlinx-datetime/issues/381", + level = DeprecationLevel.ERROR +) public operator fun DateTimePeriod.plus(other: DateTimePeriod): DateTimePeriod = buildDateTimePeriod( safeAdd(totalMonths, other.totalMonths), safeAdd(days, other.days), @@ -591,6 +595,10 @@ public operator fun DateTimePeriod.plus(other: DateTimePeriod): DateTimePeriod = * * @throws DateTimeArithmeticException if arithmetic overflow happens. */ +@Deprecated( + "Adding periods is not a well-defined operation. See https://github.com/Kotlin/kotlinx-datetime/issues/381", + level = DeprecationLevel.ERROR +) public operator fun DatePeriod.plus(other: DatePeriod): DatePeriod = DatePeriod( safeAdd(totalMonths, other.totalMonths), safeAdd(days, other.days), From 388f02a37f54300440c33bd3e42343b83f0d95eb Mon Sep 17 00:00:00 2001 From: Dmitry Khalanskiy Date: Fri, 18 Oct 2024 12:00:35 +0200 Subject: [PATCH 2/3] Suppress deprecations in the tests --- core/common/test/DateTimePeriodTest.kt | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/core/common/test/DateTimePeriodTest.kt b/core/common/test/DateTimePeriodTest.kt index ccf516c0..a86cf5a7 100644 --- a/core/common/test/DateTimePeriodTest.kt +++ b/core/common/test/DateTimePeriodTest.kt @@ -144,14 +144,17 @@ class DateTimePeriodTest { val dp1 = DatePeriod(years = 1, months = 6) - assertEquals(DateTimePeriod(years = 10, days = 3, hours = 2), p1 + p2 + p3) - assertEquals(DatePeriod(years = 11, months = 6), dp1 + p1) - assertEquals(DatePeriod(years = 2, months = 12), dp1 + dp1) - assertEquals(DateTimePeriod(years = 1, months = 6, days = 3), p2 + dp1) - - val dp2 = dp1 + p3 + p4 - assertEquals(dp1, dp2) - assertTrue(dp2 is DatePeriod) + @Suppress("DEPRECATION_ERROR") + run { + assertEquals(DateTimePeriod(years = 10, days = 3, hours = 2), p1 + p2 + p3) + assertEquals(DatePeriod(years = 11, months = 6), dp1 + p1) + assertEquals(DatePeriod(years = 2, months = 12), dp1 + dp1) + assertEquals(DateTimePeriod(years = 1, months = 6, days = 3), p2 + dp1) + + val dp2 = dp1 + p3 + p4 + assertEquals(dp1, dp2) + assertTrue(dp2 is DatePeriod) + } } @Test From 32fb1b19b74f06167e3ffc0fb684ab6467fbba09 Mon Sep 17 00:00:00 2001 From: Dmitry Khalanskiy Date: Wed, 30 Oct 2024 11:51:25 +0100 Subject: [PATCH 3/3] Make the deprecation a warning --- core/common/src/DateTimePeriod.kt | 4 ++-- core/common/test/DateTimePeriodTest.kt | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/core/common/src/DateTimePeriod.kt b/core/common/src/DateTimePeriod.kt index 418e364e..b54f183f 100644 --- a/core/common/src/DateTimePeriod.kt +++ b/core/common/src/DateTimePeriod.kt @@ -579,7 +579,7 @@ public fun Duration.toDateTimePeriod(): DateTimePeriod = buildDateTimePeriod(tot */ @Deprecated( "Adding periods is not a well-defined operation. See https://github.com/Kotlin/kotlinx-datetime/issues/381", - level = DeprecationLevel.ERROR + level = DeprecationLevel.WARNING ) public operator fun DateTimePeriod.plus(other: DateTimePeriod): DateTimePeriod = buildDateTimePeriod( safeAdd(totalMonths, other.totalMonths), @@ -597,7 +597,7 @@ public operator fun DateTimePeriod.plus(other: DateTimePeriod): DateTimePeriod = */ @Deprecated( "Adding periods is not a well-defined operation. See https://github.com/Kotlin/kotlinx-datetime/issues/381", - level = DeprecationLevel.ERROR + level = DeprecationLevel.WARNING ) public operator fun DatePeriod.plus(other: DatePeriod): DatePeriod = DatePeriod( safeAdd(totalMonths, other.totalMonths), diff --git a/core/common/test/DateTimePeriodTest.kt b/core/common/test/DateTimePeriodTest.kt index a86cf5a7..25581396 100644 --- a/core/common/test/DateTimePeriodTest.kt +++ b/core/common/test/DateTimePeriodTest.kt @@ -144,7 +144,7 @@ class DateTimePeriodTest { val dp1 = DatePeriod(years = 1, months = 6) - @Suppress("DEPRECATION_ERROR") + @Suppress("DEPRECATION") run { assertEquals(DateTimePeriod(years = 10, days = 3, hours = 2), p1 + p2 + p3) assertEquals(DatePeriod(years = 11, months = 6), dp1 + p1)