News for hackers. Stuff that matters.
Re:Ten Python datetime pitfalls, and what libraries are (not... by amluto on Sunday February 18, 2024 @10:18AM (#39419725)
You start by figuring out what you’re even trying to do.

For example, I read your comment three times, and I don’t know what you mean by “4 pm local time”. I don’t even have what I would consider to be a credible guess.

Re:Ten Python datetime pitfalls, and what libraries are (not... by MereInterest on Sunday February 18, 2024 @10:48AM (#39420001)
You and I would like to meet at a coffee shop, on a specific date about six months from now, when the clock on the wall of the coffee shop reads "4:00 PM".

* The coffee shop may not be located within UTC+00:00. If you show up at UTC time 16:00+00:00, I won't be there.

* The coffee shop may not be located in your current time zone. If you show up when it is "4:00 PM" in your local time zone, I won't be there.

* The coffee shop may be located somewhere that follows Daylight Saving Time. If you show up when it is "4:00 PM" in the current UTC offset of the coffee shop, I won't be there.

* The coffee shop may currently be located somewhere that follows Daylight Saving Time, but the legislature stops following Daylight Saving Time between now and our meeting. If you show up when it is "4:00 PM" according to the predicted UTC offset of the coffee shop for the day of the meeting, rather than the actual UTC offset of the coffee shop for the day of the meeting, I won't be there.

Saying "local time" implies all of the above. It means that something is being specified according to the local convention of timekeeping.

Re:Ten Python datetime pitfalls, and what libraries are (not... by amluto on Sunday February 18, 2024 @10:52AM (#39420030)
> Saying "local time" implies all of the above. It means that something is being specified according to the local convention of timekeeping.

No, it really doesn’t. Saying “4pm at the coffee shop” means that, and this is a big distinction.

If I’m in a different time zone two days before the meeting, looking at my calendar, I sure hope my calendar understands the difference. If I move my entire home outside the timezone, the appointment doesn’t magically shift an hour as a result.

And any decent time library can handle this just fine as long as you don’t use “local” time. Just specify the actual timezone!

Re:Ten Python datetime pitfalls, and what libraries are (not... by lijok on Sunday February 18, 2024 @12:51PM (#39421240)
Unfortunately timezones have the nasty ability to change.

See: https://codeblog.jonskeet.uk/2019/03/27/storing-utc-is-not-a...

Re:Ten Python datetime pitfalls, and what libraries are (not... by Hackbraten on Sunday February 18, 2024 @03:46PM (#39423115)
That's why they wrote "state the actual timezone" and not "state the actual UTC offset."

Actual timezone would mean "2025-01-01 16:00:00 Europe/Berlin," not just "2025-01-01 16:00:00 +01:00."

Re:Ten Python datetime pitfalls, and what libraries are (not... by lijok on Sunday February 18, 2024 @03:55PM (#39423228)
Even those change
Re:Ten Python datetime pitfalls, and what libraries are (not... by Hackbraten on Sunday February 18, 2024 @04:34PM (#39423619)
I agree. The city where the cafe is located might be annexed or usurped by another country, and have another timezone imposed on it.

Handling that kind of change would be beyond impractical though?

Re:Ten Python datetime pitfalls, and what libraries are (not... by lijok on Sunday February 18, 2024 @06:04PM (#39424431)
Good question. I wonder what impact timezone wise the annexation of Crimea had on hypergiants like FAANG. I can imagine some weirdness when rerunning say historical domain events to hydrate a new service.

Wonder if they have libraries to handle such edgecases

Re:Ten Python datetime pitfalls, and what libraries are (not... by MereInterest on Sunday February 18, 2024 @02:55PM (#39422486)
> No, it really doesn’t. Saying “4pm at the coffee shop” means that, and this is a big distinction.

Some words and phrases, such as "4 pm local time", require some of their semantic meaning to be inferred from context. This doesn't mean that they are meaningless. It means that the relevant context must be provided in order to interpret the phrase. If you and I have been scheduling the coffee shop meetup, then the phrases "4pm", "4pm local time", and "4pm as measured by the applicable standards in the area of the coffee shop on the day of the meeting" are all equivalent.

> Just specify the actual timezone!

This is the problem that I pointed out. The appropriate context for "4pm local time" is "at the coffee shop". However, most calendar programs will only let you provide context such as "Eastern Standard Time". This is the wrong context altogether, and choosing it assumes that the area will have extended the duration of DST between time of scheduling and time of meeting.

Re:Ten Python datetime pitfalls, and what libraries are (not... by amluto on Sunday February 18, 2024 @04:34PM (#39423620)
> Some words and phrases, such as "4 pm local time", require some of their semantic meaning to be inferred from context. This doesn't mean that they are meaningless. It means that the relevant context must be provided in order to interpret the phrase.

This is not what the proposed LocalDateTime would do. This is a “naive” time, and those are only really useful for calendar math or when you want to apply a concrete timezone.

Re:Ten Python datetime pitfalls, and what libraries are (not... by lelandbatey on Sunday February 18, 2024 @04:48PM (#39423758)
Nah, you need a location; it's right there in the name "local" which implies time at a place. Saying "local time" alone doesn't always give sufficient detail in your example, because you want the device to be able to change it's location (its timekeeping context) while maintaining awareness of the difference between the device's local time and the local time of the thing you've recorded.

"Local time" is basically never "good enough", actually. At a minimum, a time as you've described should include:

- a location where you're tracking the local time

- the destination local time at that location, from which you can derive most of the timekeeping context (time zone), usually

- the current time of the thing doing the computation, in order to accurately compare device to destination time

You then use those to get a UTC time that you can use to do things like a countdown. Or you can just make assumptions about those things and accept they'll be wrong; that's allowed but it's not usually what users want given we KNOW how to give them what they want.

Re:Ten Python datetime pitfalls, and what libraries are (not... by joshribakoff on Sunday February 18, 2024 @12:26PM (#39420978)
> don’t know what you mean by “4 pm local time”

It means “whenever it is 4pm wherever you are”.

As in, i want my alarm to go off at 4pm everyday regardless of whether i am traveling.

Python calls it a “naive” time, it has no timezone or offset. Its just a data structure that says “4pm” rather than “4pm eastern time”

An example ive used is when a user had to specify schedules for a digital menu board system. When the user says breakfast ends at 10am, that means wherever the digital menu board is located, not “10am where i was when i set the schedule”. It also doesn’t mean “end breakfast an hour early/late when daylight savings occurs”

It makes sense in any context where you need to do something at a certain time each day, and you specifically don’t mean “do this thing exactly every 24hrs”

Re:Ten Python datetime pitfalls, and what libraries are (not... by amluto on Sunday February 18, 2024 @01:08PM (#39421405)
> As in, i want my alarm to go off at 4pm everyday regardless of whether i am traveling.

I think this may be the exception that proves the rule. Sure, this is a valid, if specialized, semantic use case. But you only barely need a LocalDateTime type for this. If you stick that 4pm into a local database, you want to make sure that everything, including the database, knows you’re storing hours, minutes, and seconds relative to an unspecified “local time”.

So IMO this is more of an UnspecifiedZoneTime than a LocalDateTime. (Also, it has no date portion.) It makes no sense to compare it to a UTC or other date+time.

Re:Ten Python datetime pitfalls, and what libraries are (not... by coldtea on Sunday February 18, 2024 @03:36PM (#39422969)
>So IMO this is more of an UnspecifiedZoneTime than a LocalDateTime. (Also, it has no date portion.

It very well could also have a date portion.

E.g. "I want this alarm/cleanup/whatever to happen on 9am on the 1st Monday of each month" based on the timezone the user happens to be at that time - i.e. not tied to any specific timezone.

Re:Ten Python datetime pitfalls, and what libraries are (not... by amluto on Sunday February 18, 2024 @04:24PM (#39423526)
That’s still a different thing: it’s a recurring event, which is an almost entirely separate can of worms from a datetime.

And a recurrence library probably wants to avoid libc-style “localtime” here — plumbing the concept of users in places to it seems awkward. A genuinely naive time may be better — the interface could be “calculate the next recurrence of this recurring event in this timezone,” not “here’s a recurring event with associated ‘local’ time — calculate the next occurrence given my /etc/localtime and TZ environment variable”.

Re:Ten Python datetime pitfalls, and what libraries are (not... by zodiac on Sunday February 18, 2024 @10:33AM (#39419845)
Something like "schedule a reminder to stop using the computer at 10pm so I can sleep by 11pm"? Since I want to sleep at 11pm local time every day
Re:Ten Python datetime pitfalls, and what libraries are (not... by amluto on Sunday February 18, 2024 @10:56AM (#39420074)
This is about the only use case for local time that makes sense to me. But this is a UI issue, and it seems to me that that should be something that comes from a UI library or framework, not from libc and or /etc or environment variables.

(I’m using “UI” broadly. A user session on a UNIXy system could have an associated timezone, although such a design should allow changing the timezone without logging on and off. And this “10pm local” thing is quite different from an ordinary zoned time in that “10pm local in 10 days” does not correspond to an actual known UTC time because the local time zone may change.)

Re:Ten Python datetime pitfalls, and what libraries are (not... by falserum on Sunday February 18, 2024 @10:46AM (#39419986)
1. Contract says that goods must be delivered at 4pm.

2. Government suddenly changes the law and moves the offset of whole country.

3. Contract is still valid, goods have to be delivered still at 4pm, but with new offset (so at different utc time).

(I guess timezoned time would still be better than local time, but UTC would not work here)

Re:Ten Python datetime pitfalls, and what libraries are (not... by amluto on Sunday February 18, 2024 @10:52AM (#39420038)
> (I guess timezoned time would still be better than local time, but UTC would not work here)

Exactly.

Re:Ten Python datetime pitfalls, and what libraries are (not... by mharig on Tuesday February 20, 2024 @08:55AM (#39441278)
1. Contract says that goods must be delivered at 4pm UTC

Problem solved, unless the government changes the law too little before 4pm UTC. But handling this case in software is impractical.

Re:Ten Python datetime pitfalls, and what libraries are (not... by jcranmer on Sunday February 18, 2024 @10:27AM (#39419801)
Schedule an event to occur at 4pm every day, say in crontab.
Re:Ten Python datetime pitfalls, and what libraries are (not... by amluto on Sunday February 18, 2024 @10:59AM (#39420095)
Works great until you have an office in a different time zone.

The whole concept of a computer belonging to a timezone works to some extent when that computer has a single human tenant, but it falls apart pretty quickly when the scope broadens.

Re:Ten Python datetime pitfalls, and what libraries are (not... by jcranmer on Sunday February 18, 2024 @11:08AM (#39420182)
If I'm scheduling a meeting, then yes, I am going to want to schedule the meeting using a specific time zone, which may or may not be my local time zone at that moment.

But if I'm scheduling when I want my computer to reboot, I want that to be a fixed in time zone in my local time zone. And if I subsequently change my local time zone, I want it to remain at that time in the new local time zone, not the old one.

Yes, local time zones don't always make sense. But that is a far cry from saying they never make sense!

Re:Ten Python datetime pitfalls, and what libraries are (not... by amluto on Sunday February 18, 2024 @01:11PM (#39421435)
I don’t know about you, but I have computers that aren’t in the same time zone I’m it. I probably want them to reboot at night where I am, not where they are.

But yes, “reboot a laptop at 2am” is in the general category of alarm clock use cases, where a time (but not a date!) with a separately determined time zone makes sense.

The OP mentioned LocalDateTime, which doesn’t make sense for this use case.

Re:Ten Python datetime pitfalls, and what libraries are (not... by falserum on Sunday February 18, 2024 @01:59PM (#39421950)
Instant in history -> GMT+0/UTC

Future shedule for humans to coordinate (contracts, meetings) -> ZonedDateTime

Personal alarm that should be kept in sync with the sun (e.g. go to sleep) -> LocalDateTime

What’s good example/purpose for OffsetDateTime?

Re:Ten Python datetime pitfalls, and what libraries are (not... by jcranmer on Sunday February 18, 2024 @03:08PM (#39422614)
OffsetDateTime would be useful for a lot of historical records. For example, if I'm chronicling a WW2 battle in the Pacific, I need to know what the local times of the events were, but I don't necessarily care about knowing the DST rules.

Additionally, most date/time serialization formats (e.g., RFC 822, ISO 3601) only allow you to specify up to OffsetDateTime and don't provide a full timezone reference like US/Eastern.