Skip to content

Considering the possibility to represent 0 of some DateTimeUnit? #246

@kevincianfarini

Description

@kevincianfarini

Right now all of the instances of DateTimeUnit guard against instantiating with zero of the underlying asset. For example:

@Serializable(with = DayBasedDateTimeUnitSerializer::class)
public class DayBased(
    /**
     * The length of this unit in days.
     */
    public val days: Int
) : DateBased() {
    init {
        require(days > 0) { "Unit duration must be positive, but was $days days." }
    }

This feels really unintuitive. If someone is dynamically performing math operations of Instant, LocalDate, etc. it should be permissible to have an identity operation. Currently this requires null coalescing for identity-like operations. Eg:

val dateTimeUnit: DateTimeUnit? = /* TODO */
val date = clock.now().toLocalDate(/* omitted */).let { date ->
  if (dateTimeUnit == null) date else date.minus(dateTimeUnit)
} 

I'm hoping to understand the rationale for why zero of some DateTimeUnit is generally prohibited. Thank you!

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