LocalDateTime + TimeZone #237
Replies: 10 comments 48 replies
-
What do you think would be a good name for this data structure, instead of |
Beta Was this translation helpful? Give feedback.
-
Which operations would you expect this data structure to have and how would you use them? Examples of possible operations:
|
Beta Was this translation helpful? Give feedback.
-
Considering the serializability and persistent storage of the proposed data structure, it might be not a good idea to store a time zone reference for future events for a long time. The reason is that if a future event is scheduled to a local time in some geographic location, it's not guaranteed that the time zone identifier (not the rules of the time zone) of that location won't change later due to further administrative decisions. Usually no one cares about this aspect, but providing such a shortcut for representing and storing future events could make one care even less. |
Beta Was this translation helpful? Give feedback.
-
Consider another possible useful resolver (naming TBD of course) -- maybe even the default one: LocalDateTimeResolver.UNAMBIGUOUS The purpose of this resolver would be to not allow a time specification that falls into an overlap. Rather than shifting into the first or second hour in this case, it would throw or otherwise error. For the purposes of scheduling human activity this is pretty useful, as no one sane schedules anything in overlap hours -- there is no easy way for a person to know which hour is referred to. |
Beta Was this translation helpful? Give feedback.
-
I think the use cases need some work. Lets take the two given in turn:
This is an interesting case because the specific instant at which the pill was taken is relevant, but so is the local time at which it was taken. Just storing Just storing So Does the zone in and of itself add anything here to the data model over storing One case I can think of for the zone as a shortcut for location is that my calendar will generally know my zone, but not my location. Therefore, zone may be the only information available to the system. But even in this case, I think we still need to explain why we would need
What is the advantage here to using Lets take another case -- the conference spans over a DST switch. In this case, Lets take another case -- the location of the conference switches from New York to Los Angeles. Would it be correct for the keynote speech to now be given at 12:00 instead of 09:00? No, its likely the schedule would stay exactly the same and There may be a valid case here, but I don't think the example given is it. Please clarify the use case for |
Beta Was this translation helpful? Give feedback.
-
There's an interesting recent proposal for changes to the Highlights:
|
Beta Was this translation helpful? Give feedback.
-
Any reason there is no consideration for
|
Beta Was this translation helpful? Give feedback.
-
Is this really a use-case? This is not the behavior I would expect, and this is not how Google Calendar nor KOrganizer (the two calendar apps I use) operate. These calendars make a difference between:
In this situation, I changed locations, so I would see the past event in my calendar to have a different wallclock time. So, I would expect to see the time I took that pill to be in the middle of the night (probably, I didn't do the math). However, if I took the pill at 8:13 in Moscow, and the next day Moscow changes time offset, then the next day I would expect to see the event at 8:13 in my calendar: although the timezone has changed, it was not due to me being in a different place, and thus it displays continuously. This is the behavior I expect to see in a hypothetical
Now, I'm a bit confused that no one mentioned this in the thread… I believe this is how most calendars work, and I would see value in having this behavior. |
Beta Was this translation helpful? Give feedback.
-
Another common case, is finding midnight at that same time zone, this is basically impossible using kdatetime:
|
Beta Was this translation helpful? Give feedback.
-
I'm surprised to see no references in the comments to the I have used this class for many years, back to when it was introduced with joda, and it performs a useful function admirably. I'm sure I am missing something but without an explicit explanation on why kotlinx-datetime doesn't have this concept, it looks like you are attempting to re-invent the wheel. I'm just getting started with kotlinx-datetime but I guess Is this idea already covered on another issue? |
Beta Was this translation helpful? Give feedback.
-
We received several requests for a combination of
LocalDateTime
andTimeZone
. This is a proposal that is up for discussion and may end up being implemented.Use cases
Instant
, after the time zone database does update, the reading may become incorrect.Instant
is a wrong representation here andLocalDateTime
is what we should be storing. The same goes for scheduling plane flights, school lessons, or anything else that's user-visible.Lots of thanks to @wkornewald for raising them!
ZonedDateTime
A data structure that is called
ZonedDateTime
in Java is typically used to represent these. However, it's actually unsuitable for that. Previous discussions: #90, #163.Internal model
ZonedDateTime
a combination ofInstant
+TimeZone
, despite it being possible to create it fromLocalDateTime
andTimeZone
. In most cases, this is the same asLocalDateTime
+TimeZone
, because there's typically exactly one local date-time corresponding to an instant, except in two cases: when clocks are set forward and the times in-between don't exist (a gap occurs), and when clocks are set backward and the times repeat (an overlap occurs). For further information, see https://stackoverflow.com/tags/dst/info.Creation
There are several ways to create a
ZonedDateTime
LocalDateTime
andTimeZone
is in a gap,LocalDateTime
will be adjusted (moved forward) to be correct, and if there's an overlap, it's possible to configure which of the two instants corresponding to this combination is correct, but by default, the earlier one is used.UtcOffset
along withLocalDateTime
andTimeZone
and validates that the combination is correct and unambiguous, throwing otherwise.Instant
andTimeZone
, or parsing:Evidently,
ZonedDateTime
attempts to construct anInstant
in any case, sometimes guessing it from the passedLocalDateTime
, and does not treatLocalDateTime
as the source of truth.Updates
It's possible to add date-time units (
plusYears
,plusHours
) or try to set specific fields (withHour
). Adding time-based units works on the underlyingInstant
directly, whereas adding units or setting fields modifiesLocalDateTime
and creates a newZonedDateTime
based on that.This leads to addition being non-associative:
Because an intermediate computation (
plusDays(1)
) fell into the gap, all future results get shifted by an hour in the LocalDateTime.Actual prior art
Didn't manage to find any.
Proposal
A new data structure
An outline of the class, conveying the general idea, without any of the niceties and with subpar naming:
Properties:
LocalDateTime
arithmetic unless the final result happens to be in a time gap as well.plus
operation associative, which corresponds to the everyday understanding: waiting for a day and then waiting for a day is the same as waiting for two days.DateTimeInZone(localDateTime, timeZone).plus(n, unit).localDateTime == ZonedDateTime(localDateTime, timeZone).plus(n, unit).toLocalDateTime()
for all validlocalDateTime
+timeZone
combinations.Modifications to existing data structures
Internally,
Instant.plus
on date-based units works by converting to aZonedDateTime
. Instead, we can move this functionality toDateTimeInZone
. This would makeDateTimeInZone
the way to do date-based arithmetic operations, andInstant.plus
acceptingDateTimeUnit.DateBased
would just be a convenience. Potentially, we can even deprecateInstant.plus(DateTimeUnit.DateBased)
.Unclear details
Instant
value is ambiguous.ZonedDateTime
stores anInstant
, so the offset is always known. However,DateTimeInZone
data structure considers the local date-time and the time zone to be ground truth, and the offset may be out of sync with them, and so, can only be treated as a recommendation, not as part of the knowledge about the date-time. However, it would still be nice to be able to representInstant
values asDateTimeInZone
unambiguously when we know them for sure.Instant
, add the unit, and then convert back toDateTimeInZone
.Instant
andDateTimeInZone
would allow adding arbitraryDateTimeUnit
s, withInstant
being time-centric andDateTimeInZone
being date-centric, with only subtle differences between them. Maybe it would be a good idea to deprecateInstant.plus(DateTimeUnit.DateBased)
altogether and only allow arbitraryDateTimeUnits
to be added toDateTimeInZone
?Beta Was this translation helpful? Give feedback.
All reactions