Hacker News new | ask | show | jobs
by rcoveson 1588 days ago
> It’s like asking if 2022-01-01 is an orange.

This is way overstated. It's more like asking if the integer `1L` is between the float `1.0D` and `2.0D`. It requires an implicit cast. Decide whether operations involving LocalDate[0] and LocalDateTime should use the former or the latter as the working type (the latter, IMO), do the cast (LocalDate becomes a LocalDateTime with time component 00:00:00, or LocalDateTime becomes a LocalDate by dropping its time component), then do the operation.

If the types you're dealing with are LocalDate and Instant, then you'd need a contextual timezone to perform the conversion in either direction.

I don't think this is a hard problem, it's just one that requires more specificity than one might think at first glance. Users should be encouraged to read the spec of any language that does these sorts of implicit casts, in the same way that C programmers should be aware of the implicit typecasting rules in that language.

0. I'm using the java.time/org.joda.time type names here. Their equivalents should exist in any good time library. However, I think in reality many libraries fall short of the types you really need to express date & time concepts, which is what leads to so much confusion around them. Once you're familiar with LocalDate, LocalTime, LocalDateTime, OffsetDateTime, ZonedDateTime, Instant, Duration, and Period, (whew), it's possible to be clear about what you're doing.