Skip to content
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

Deprecate DatePeriod.plus and DateTimePeriod.plus #449

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
8 changes: 8 additions & 0 deletions core/common/src/DateTimePeriod.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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.WARNING
)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would also recommend to start with a warning if we're not in rush.

public operator fun DateTimePeriod.plus(other: DateTimePeriod): DateTimePeriod = buildDateTimePeriod(
safeAdd(totalMonths, other.totalMonths),
safeAdd(days, other.days),
Expand All @@ -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.WARNING
)
public operator fun DatePeriod.plus(other: DatePeriod): DatePeriod = DatePeriod(
safeAdd(totalMonths, other.totalMonths),
safeAdd(days, other.days),
Expand Down
19 changes: 11 additions & 8 deletions core/common/test/DateTimePeriodTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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")
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
Expand Down