https://kotlinlang.org logo
Title
p

Paul Woitaschek

01/25/2021, 5:45 PM
What we found annoying so far with the DateTimeUnit things is that they throw an exception on duration=0. We have code where we have a dynamic amount of days added:
loopDate.plus(DateTimeUnit.DateBased.DayBased(fastingDays.size))
Now this throws:
java.lang.IllegalArgumentException: Unit duration must be positive, but was 0 days.
	at kotlinx.datetime.DateTimeUnit$DateBased$DayBased.<init>(DateTimeUnit.kt:68)
Instead I would prefer to have the function just return the initial date instead of throwing when adding 0 dates. Now we always need to write != 0 checks everywhere and use the initial instance
m

Marc Knaup

01/25/2021, 6:38 PM
I don’t think that
DateTimeUnit.DateBased.DayBased
is supposed to be used 🤔 Use one of those:
.plus(0, DateTimeUnit.DAY)
.plus(DatePeriod(days = 0))
p

Paul Woitaschek

01/25/2021, 7:05 PM
Then thats a bad api
I was looking at
public actual fun LocalDate.plus(unit: DateTimeUnit.DateBased): LocalDate = plusNumber(1, unit)
So to me it was intuitive to use
DateTimeUnit.DayBased(x)