Live data from Hacker News

Python datetime pitfalls, and what libraries are (not) doing about it

dev.arie.bovenberg.net

41–50 of 150 posts

Re: Python datetime pitfalls, and what libraries are (not) doing about it

#41

Across all languages, what are the best libraries, with least amount if pitfalls? (java.time seems good, anything even better elsewhere?)

I really like Go's "time" package. The only pitfall I can think of is that it doesn't really handle overflow at all.

https://pkg.go.dev/time

Re: Python datetime pitfalls, and what libraries are (not) doing about it

#42
post #3

I don't think any of the DST complaints are legitimate. If doing work involved with timestamps, not keeping everything in GMT is questionable. GMT is the UTF-8 of timestamps: the low common-denominator rules. The datetime library is mechanism. DST is policy. Policy is wildly variable and your system library would be unmaintainable if it had to chase policy. Just say no.

I tend to prefer: - Things happen/happened at UTC times - Things will happen at zoneless time + IANA DB label. Though there are some edges. Obviously it's possible to pre-apply and convert to UTC, which is okay, but whether that makes sense to do in practice varies some. Future UTC time of often worthless for understanding human centric dates and times.

For past events, knowing the local time might be useful context. I'd store local time with UTC offset, or with IANA time zone name (which might change the UTC meaning if there's some change affecting past dates in the IANA database).

Re: Python datetime pitfalls, and what libraries are (not) doing about it

#43

    from whenever import (
        # For the "UTC everywhere" case
        UTCDateTime,
        # Simple localization sans DST
        OffsetDateTime,
        # Full-featured IANA timezones
        ZonedDateTime,
        # The local system timezone
        LocalDateTime,
        # Detached from any timezones
        NaiveDateTime,
    )
Why so many different types? Doesn't ZonedDateTime cover all of the other use cases?

Re: Python datetime pitfalls, and what libraries are (not) doing about it

#44
post #34

> # 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…

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: Python datetime pitfalls, and what libraries are (not) doing about it

#45

Best code strategy here is twofold. 1. Maximize the code context where date/time is represented as a simple count of Unix epoch seconds. 2. Never pass calendar/clock representations through API methods. 3. Always pass a timezone to methods that require it. 4. Counting, like time, is hard. The only contexts I see where calendar/clock representation are needed are: - Accept calendar/clock info from the user or external…

I wouldn't use an int. Some things use milliseconds since 1970 instead of seconds to get sub-second precision without involving double, and you need to read the docs to understand what is expected. Or you can just pass a DateTime object standard for the language if running in a single binary, or something like ISO 8601 when there's a network in between. Databases can often store DateTime objects sanely.

Re: Python datetime pitfalls, and what libraries are (not) doing about it

#46
post #44
post #34

> # 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…

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.

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: Python datetime pitfalls, and what libraries are (not) doing about it

#47

Across all languages, what are the best libraries, with least amount if pitfalls? (java.time seems good, anything even better elsewhere?)

I really like Go's "time" package. The only pitfall I can think of is that it doesn't really handle overflow at all. https://pkg.go.dev/time

And date time parsing sucks with its predefined mask elements.

Re: Python datetime pitfalls, and what libraries are (not) doing about it

#48

Best code strategy here is twofold. 1. Maximize the code context where date/time is represented as a simple count of Unix epoch seconds. 2. Never pass calendar/clock representations through API methods. 3. Always pass a timezone to methods that require it. 4. Counting, like time, is hard. The only contexts I see where calendar/clock representation are needed are: - Accept calendar/clock info from the user or external…

The problem with Unix timestamps is that it doesn't take leap seconds into account, which makes it ambiguous during a leap second and makes it messy to compute the duration between two timestamps.

I think you're on the right track but a timestamp should only count the actual number of seconds that have passed, not some half-measure that tries to align with the rotation of the earth.

Re: Python datetime pitfalls, and what libraries are (not) doing about it

#49

The author provides this example to illustrate inconsistent handling of non-existent datetimes: # This time doesn't exist on this date d = datetime(2023, 3, 26, 2, 30, tzinfo=paris) # No timestamp exists, so it just makes one up t = d.timestamp() datetime.fromtimestamp(t) == d # False Criticisms: 1) The example would fail just as well for any datetime with given tzinfo. Because fromtimestamp returns native datetimes.…

> how do we represent the 2am in "clock changes forwards at 2am".

If we are willing to adjust a tiny bit, there are options:

Option1: “01:59 is followed by 03:00”.

Option2: “clock changes forwards at 2am GMT+2” (GMT+2 is offset before the hour jump)

Re: Python datetime pitfalls, and what libraries are (not) doing about it

#50
post #46
post #44

Earlier quoted context omitted.

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.

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.

Schedule an event to occur at 4pm every day, say in crontab.
Post reply on HN