Open ptomato opened 3 years ago
@ptomato PHP's DateInterval
is not what this issue describes. It is what Temporal calls Duration
. You should strike it from the "Prior art" section. Also, the Luxon link leads to a 404.
@MidnightDesign You're right, DatePeriod
is PHP's equivalent of this. Thanks for the warning. I'll hide these comments since I've updated the OP.
It's possible there would have to be different Interval types for the different Temporal types: at least Instant and ZonedDateTime, probably PlainDateTime and PlainDate, and possibly PlainYearMonth and PlainTime.
This would make the most sense, as it would avoid type ambiguity.
I would also add to this 2 new potential API methods. Both could be instance methods and static methods as well.
const intersection = Temporal.Interval.getIntersection(interval1, interval2) { ... }
Its return value can be any one of the following:
null
if the intervals do not overlap.Possible return types: null
, Interval
.
const difference = Temporal.Interval.getDifference(interval1, interval2) { ... }
Given how differences work, this method could return up to 2 intervals, depending on the inputs:
null
, as there is no difference.Possible return types: null
, array<Interval>
of length 1 or 2.
I strongly support adding these Interval
classes to Temporal
, as they would greatly improve the DX when working with timelines, timetables, schedules, etc. These 2 newly proposed methods would improve upon what was already discussed here and in the related issues until now. There should be no need to rely on any 3rd party package for such trivialities.
Summary of discussions from https://github.com/tc39/proposal-temporal/issues/205 and https://github.com/tc39/proposal-temporal/issues/682.
From user feedback, it seems that the main use cases are iterating through a range between two points on the time line (whether exact time or calendar/wall-clock time), and having an interval object which boxes up two orderable Temporal objects with methods for checking whether another Temporal object or interval lies within the range.
Possible APIs proposed:
Advantages:
Intervals are not difficult to implement in userland, but it could be a benefit for Temporal to have a type with a consistent set of operations, and to avoid off-by-one errors.
Concerns:
An Interval object doesn't quite fit with the strict typing philosophy of Temporal. It's possible there would have to be different Interval types for the different Temporal types: at least Instant and ZonedDateTime, probably PlainDateTime and PlainDate, and possibly PlainYearMonth and PlainTime. Probably business logic would use at most one of these types at a time.
Prior art:
Interval
DatePeriod
Constraints / corner cases: