News for hackers. Stuff that matters.
Re:Ten Python datetime pitfalls, and what libraries are (not... by amluto on Sunday February 18, 2024 @09:51AM (#39419499)
> # The local system timezone > LocalDateTime,

Here’s my feedback: one should essentially never use “local” time. I would even argue that the existence of systemwide local time is a historical mistake.

Why would you ever want to use the “local” timezone of the system on which you are running? If the user is literally sitting in front of a monitor plugged in to the machine, it might be appropriate (but it should be a per-user setting). But if the user is using X forwarding or ssh or a remote desktop, you have no idea what timezone the user is in. (And this was common in the mainframe era!) If you’re servicing a request over the network, you don’t want local time.

The only sort of legitimate use for “local” time I can think of is compatibility with libc and it’s horrible time API.

You can’t even tell what local time means without getenv, which is a footgun.

So maybe LocalDateTime should be called LibcCompatLocalDateTime.

Re:Ten Python datetime pitfalls, and what libraries are (not... by MereInterest on Sunday February 18, 2024 @10:32AM (#39419838)
Because the local time is the thing that you have immediate access to, with no additional conversions applied. Every conversion requires an external source of information. Converting from local time to time zone requires measuring the system clock drift. Converting from time zone to UTC requires knowing which time zone you are in. Converting from UNIX timestamp to UTC requires measurements of the earth’s rotation to know how many leap seconds to add.

I’d propose going in the opposite direction: All timestamps should indicate which clock they were produced from. There is no such thing as a global clock, only conversions between your local clock and the UTC clock.

Re:Ten Python datetime pitfalls, and what libraries are (not... by amluto on Sunday February 18, 2024 @12:38PM (#39421111)
> Because the local time is the thing that you have immediate access to, with no additional conversions applied.

Only in the sense that BIOS systems historically programmed non-DST-aware local time into the RTC.

Other than that, you actually have no-conversion-needed access to UTC or something UTC-like. Linux mostly tracks the conversion from the hardware clock (TSC, for example) to UTC. NTP gives UTC, and GPS gives something that is a lot closer to UTC than to local time.

Re:Ten Python datetime pitfalls, and what libraries are (not... by MereInterest on Sunday February 18, 2024 @02:37PM (#39422319)
> Only in the sense that BIOS systems historically programmed non-DST-aware local time into the RTC

I would love to hear proposals on how, at a hardware level, you would track Daylight Saving Time. Not just how a Daylight Saving Time correction would be applied, but also how the hardware would receive updates to dates at which DST is observed, within which geographic borders, and how to handle conflicting updates for locations where the geographic borders are contested.

> you actually have no-conversion-needed access to UTC or something UTC-like

No, you don't. You may have access to an API that has already applied conversions when by the time a result it returned to you, but that is significantly different than having direct access to a universal clock. (And assumes that such a universal clock even exists.)

Your clock is a hardware-based oscillator, with a counter for the integer number of oscillations that has occurred. You do not have a UTC clock. You have an experimentally-determined and infrequently-updated conversion between your clock and the UTC clock.

> NTP gives UTC

No, it doesn't. NTP gives an approximation to UTC, and is only available when you have a network connection available. The accuracy of that approximation depends on quality of that network connection, and whether it has asymmetric delays.

> GPS gives something that is a lot closer to UTC than to local time

While true, this is only available for devices that have GPS capabilities, in a location without sharp elevation changes that would delay GPS signals.

Everything has a conversion factor. Everything. If you record the original measurement and the conversion factor used, you can recover from errors in the conversion. If you only record the measurement after conversion factors have been applied, you cannot.

Re:Ten Python datetime pitfalls, and what libraries are (not... by o11c on Sunday February 18, 2024 @03:49PM (#39423147)
>I would love to hear proposals on how, at a hardware level, you would track Daylight Saving Time.

Traditionally the BIOS ignores it, and Windows on reboot detects if the clock needs to be adjusted according to the latest rules that it knows about. This failed quite often in practice.

Linux systems using tzdata are much more reliable (Windows is still very buggy with historical data), but UTC time support on non-Linux systems was flaky for a long time, leading to bad API design in languages historically.

`timegm` despite being a critical and irreplaceable function is still not in POSIX, for example (though it is widely supported in practice)

Re:Ten Python datetime pitfalls, and what libraries are (not... by cm2187 on Sunday February 18, 2024 @10:12AM (#39419669)
Then how do you express that something must happen at a date in the future at 4pm local time? If you use UTC based on today's expectation of daylight savings, they may have changed by the time this date happens.
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.

Re:Ten Python datetime pitfalls, and what libraries are (not... by bongodongobob on Sunday February 18, 2024 @11:19AM (#39420269)
Settle down. Most people aren't running or accessing things globally.

Local time is perfectly acceptable for a list of obvious reasons.

Re:Ten Python datetime pitfalls, and what libraries are (not... by make3 on Sunday February 18, 2024 @11:34AM (#39420392)
... a huge fraction of Python code runs on servers everywhere
Re:Ten Python datetime pitfalls, and what libraries are (not... by azinman2 on Sunday February 18, 2024 @01:37PM (#39421697)
And a bunch isn’t.

Time is hard no matter what. Having a variety of types and options is a good thing, as long as everything is explicit and convertible.

The wonder if obj-c/swift do well here with NSDate/Date representing a moment in time separate from any calendar/time zone… although the name is confusing for what it represents.

Re:Ten Python datetime pitfalls, and what libraries are (not... by bongodongobob on Sunday February 18, 2024 @01:42PM (#39421753)
Which is exactly what you use time zones for.
Re:Ten Python datetime pitfalls, and what libraries are (not... by CJefferson on Sunday February 18, 2024 @10:06AM (#39419623)
What is your alternative? I suspect 99.999% of computer and phone users want the time of their local device. Do you want them all to have to explicitly set their time zone?
Re:Ten Python datetime pitfalls, and what libraries are (not... by adrian_b on Sunday February 18, 2024 @10:50AM (#39420013)
Since the appearance of computer networks, all operating systems require during installation an explicit setting of the time zone.

This includes Windows and any operating system that chooses to keep the time as local time, because even those need to be able to convert the local time to other time zones.

The users who buy a computer with a preinstalled operating system may not see this, but someone has explicitly set their time zone.

Because the time zone must be set anyway, it is always better to use inside programs a time that is either UTC or TAI and convert it only for presentation purpose.

For purposes like storing the time at which future events are scheduled, if they are established in local time, so the future correct conversion is yet unknown, a distinct type must be used and that must be a structure with 3 members, date, time of the day and time zone. It must not be a time expressed in seconds or other time units, as it may be used for UTC or TAI.

Re:Ten Python datetime pitfalls, and what libraries are (not... by CJefferson on Sunday February 18, 2024 @03:39PM (#39423019)
Windows 11 does not require setting timezone. I just did it in a VM to check I wasn't confused. Neither do android or iPhones.
Re:Ten Python datetime pitfalls, and what libraries are (not... by jraph on Sunday February 18, 2024 @04:34PM (#39423613)
Any Android install I did myself in VMs or on a phone asked me for the timezone. (lineage and derivative). Maybe it could guess from the GSM network though.

My dumb phone also does, though in terms of UTC offset + DST offset.

Re:Ten Python datetime pitfalls, and what libraries are (not... by jncfhnb on Sunday February 18, 2024 @09:59AM (#39419564)
Imagine having the audacity to run software on the machine in front of you
Re:Ten Python datetime pitfalls, and what libraries are (not... by ahartmetz on Sunday February 18, 2024 @03:37PM (#39422989)
You shouldn't do that, it's bad for some business models.
Re:Ten Python datetime pitfalls, and what libraries are (not... by coldtea on Sunday February 18, 2024 @03:31PM (#39422883)
>But if the user is using X forwarding or ssh or a remote desktop, you have no idea what timezone the user is in.

Perhaps I don't care, and I want to present things from the point of view of the server's location?