Skip to content

Make DateTimeUnit constructors non public #95

@PaulWoitaschek

Description

@PaulWoitaschek

I just got hit by an exception when adding days to a LocalDate

I took a look at the function signature:

public actual fun LocalDate.plus(unit: DateTimeUnit.DateBased): LocalDate = plusNumber(1, unit)

So I was using DateTimeUnit.DateBased to dynamically add days:

loopDate.plus(DateTimeUnit.DateBased.DayBased(fastingDays.size))

This was throwing an exception:

java.lang.IllegalArgumentException: Unit duration must be positive, but was 0 days.
	at kotlinx.datetime.DateTimeUnit$DateBased$DayBased.<init>(DateTimeUnit.kt:68)

On the slack channel I discovered that I should not use the DayBased constructor at all and instead use a period:

.plus(DatePeriod(days = 0))

I would suggest to mention that in the documentation and make the constructors internal so such usage errors don't happen.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions