Kotlin / kotlinx-datetime

KotlinX multiplatform date/time library
Apache License 2.0
2.42k stars 101 forks source link

Easier ways to analyze a `DateTimePeriod` instance #428

Open nesk opened 2 months ago

nesk commented 2 months ago

The Duration class in the standard library provides everything you need to analyze its state:

Those things are missing in the DateTimePeriod class.

Would you consider adding those features?

dkhalanskyjb commented 2 months ago

ZERO is okay is there are some use cases for it, but isNegative and isPositive don't make sense for DateTimePeriod because of periods like 1 month - 30 days. If you add this period to 2024-01-01 you'll get a later date, and if you add it to 2024-02-01, you'll get an earlier date:

val date1 = LocalDate(2024, 1, 1)
val date2 = LocalDate(2024, 2, 1)
val period = DatePeriod(months = 1, days = -30)
println(date1 < date1 + period) // true
println(date2 < date2 + period) // false

(https://pl.kotl.in/oTONoY3XP)

Do you know of any use cases for ZERO?

nesk commented 2 months ago

I can see your point for negative/positive, I understand.

ZERO would be mainly used for verifications. I've just done a subtraction two dates, is it equal to zero or no?

dkhalanskyjb commented 2 months ago

Can't one write date1 == date2 instead of date1 - date2 == ZERO?