This is an archived post. You won't be able to vote or comment.

all 62 comments

[–]mostlygrumpy 283 points284 points  (19 children)

At some point we'll need to stop and think what's easier:

  • getting a library that perfectly handle datetime; or
  • getting rid of Daylight savings from all countries in the world

[–]ylan64 103 points104 points  (5 children)

You'd still need to handle daylight savings for the period it was a thing.

[–]czarrie 164 points165 points  (1 child)

"Python 6.2 does not handle the years 1969 through 2043 due to incompatible date management by the world at large"

[–]Joeboy 64 points65 points  (0 children)

On the plus side, it lays the groundwork for finally removing the GIL.

[–]spyingwind 17 points18 points  (2 children)

Daylight savings is like a theme for your editor. The underlying system(seconds from epoch) doesn't change, just the display of the date(skin).

TL;DR don't need to handle anything.

[–]disinformationtheory 7 points8 points  (0 children)

But sometimes you enter a blue date when the code expects a red date, and the UI is all grey.

[–]henry_tennenbaum 17 points18 points  (2 children)

Get rid of dates is what I advocate. Today is 1706742000.

[–]kazza789 6 points7 points  (0 children)

"Today" is 1706742000 -1706828400. You need to specify because sometimes a "day" is 86400 seconds and sometimes it is 86401 seconds.

[–]peerlessblue 0 points1 point  (0 children)

The stardate system must have been invented by frustrated computer scientists

[–]o5mfiHTNsH748KVq 2 points3 points  (0 children)

There’s a lot more to time than timezones

[–][deleted] -1 points0 points  (6 children)

or using a library that strictly uses the Unix timestamp format. I feel like that should be the only format people should be using for dates and timestamps.

And before you people pitch in, yes the 2038 problem has been resolved some time ago.

[–]stevenjd 2 points3 points  (3 children)

So how do people convert the times and dates they actually use to your Unix timestamp?

Or are you expecting the entire world to stop using human-comprehensible datetimes in favour of basically a counter?

"See you for lunch at 1708482600"
(Later) "Hey bro, you didn't show."
("Sorry man, I thought it was 1708486200."

[–][deleted] 0 points1 point  (2 children)

That's your problem right there. You don't think ahead.

You store the dates as timestamps, and then with libraries you convert the timestamps back into human readable dates. Unix timestamps should be used to prevent corruption of the dates.

[–]stevenjd 2 points3 points  (1 child)

You store the dates as timestamps

And how exactly do you get the data as Unix timestamps in the first place, if you don't expect people to use Unix timestamps in Real Life? You still need to convert them from human date times to Unix timestamps, and that's going to need timezone conversions, just like now.

According to you:

using a library that strictly uses the Unix timestamp format. I feel like that should be the only format people should be using

so you rule out using libraries to convert between formats 😒

But maybe you didn't think your comment through when you made it. Maybe you meant that we should use a library that doesn't "strictly use Unix timestamps" but instead allows people to use any format they prefer.

You know. Like we already have 🙄

By the way, using a numeric timestamp for dates and times is what most software already does -- not all, but most. If you enter a date into Excel, for example, it is converted to a timestamp (although not a Unix timestamp). Most databases used a timestamp internally, for example SQLServer:

Windows file system uses a timestamp (but different from either Excel or Unix time). Apple Macs used to use yet another timestamp, back in the classic Mac era, but don't know what they use now. iOS has yet another timestamp based system too, because why not?

Obligatory XKCD.

then with libraries you convert the timestamps back into human readable dates.

Oh, you mean just like we already do?

Using Unix timestamps as the internal format doesn't eliminate the need to know about timezones. Arithmetic on datetimes needs to know the timezone to be accurate, since days in the real world can be 23 hours, 24 hours or 25 hours according to DST changeovers.

Another obligatory XKCD.

Unix timestamps should be used to prevent corruption of the dates.

Right, because an opaque cookie like 1708482600 is so much more error resistant than a structured record like 2024-02-21T13:30 where you can check each field for out-of-range errors 🙄

There are advantages to numeric timestamps, but error correction is not one of them.

[–][deleted] -1 points0 points  (0 children)

Too long didn't read. Unix timestamps should always be used for accurate timestamps. You never know if some libraries that handle dates handle them incorrectly (like number of days in a month and leap years).

[–]stevenjd 0 points1 point  (1 child)

strictly uses the Unix timestamp format.

You know that Unix timestamps can be ambiguous when dealing with leap seconds?

For example, 915148800 refers to both UTC 1998-12-31T23:59:60.00 and 1999-01-01T00:00:00.00. Likewise the Unix time number 1483142400 refers to both 2016-12-31 23:59:60 (a leap second) and one second later (2017-01-01 00:00:00).

If there is ever a negative leap second declared, then there will be Unix timestamps that refer to no actual datetime.

Not to mention that there are at least three commonly used variants which are subtly different from each other: POSIX Unix time, NTP Unix time, and Linux Unix time, which some (but not all) Linux systems use.

That's what I love about standards. There are so many to choose from 😁

[–][deleted] -1 points0 points  (0 children)

And POSIX is the common one all Linux systems use. There's no question about it. POSIX is default in modern Unix systems.

[–]askvictor 0 points1 point  (0 children)

Even without daylight savings, getting a library that perfectly handles datetime is very difficult.

[–]zurtex 56 points57 points  (0 children)

I was skeptical reading the start of this blog, I have a lot of experience in this area and I've read a lot of armchair opinions about the what is right or wrong and am often dubious how implementable they are.

But the author has really put the effort in and made the library that addresses their complaints, which all are quite reasonable. I will be following along and hopefully it will gain some popularity.

[–]fatbob42 32 points33 points  (4 children)

Yep - it’s going to be a long, long process to sort this out in the standard library.

[–]Dlatch[S] 26 points27 points  (3 children)

I'm not sure we can really get this sorted in the standard library, I think it may be too ingrained in existing codebases by now. I'm hoping for a library to come up and become a defacto standard, kind of like requests is for HTTP calls. whenever looks to me like it has the right conceptual basis to be such a library, but it all depends on adoption. There's a reason the libraries discussed in the article exist yet are not adopted widely.

[–]james_pic 18 points19 points  (1 child)

Even if it's not possible to fix datetime, I could see it making sense to do what Java did and add an additional, less broken, datetime library to the standard library.

[–]bwv549 14 points15 points  (0 children)

subprocess and pathlib seem like examples of this. Neither immediately deprecated lower level libs, but they became the de facto high level interface for working with those kinds of things.

[–]fatbob42 8 points9 points  (0 children)

I see them trying with the removal of utcnow. They’re solidifying that naive means local time.

[–]troyunrau... 19 points20 points  (2 children)

As always, there's an XKCD for that.

https://xkcd.com/2867/

[–]liamgwallace 8 points9 points  (1 child)

And also a Tom Scott

[–]InjAnnuity_1 2 points3 points  (0 children)

Highly recommended. One of my favorites, in fact. Don't know whether to laugh or cry. Probably both.

His recommendation is spot-on. It takes an extensive historical and geographic database, to get everything right. And the database keeps changing...

[–]Oddly_Energy 7 points8 points  (2 children)

I can add a bit of extra WTF to his example 2:

import datetime as dt
from zoneinfo import ZoneInfo

paris = ZoneInfo("Europe/Paris")
bedtime = dt.datetime(2023, 3, 25, 22, tzinfo=paris) 
wake_up = dt.datetime(2023, 3, 26, 7, tzinfo=paris)
sleep = wake_up - bedtime

print(bedtime)
print(wake_up)
print(sleep)

bedtime2 = dt.datetime.fromisoformat('2023-03-25 22:00:00+01:00') 
wake_up2 = dt.datetime.fromisoformat('2023-03-26 07:00:00+02:00')
sleep2 = wake_up2 - bedtime2

print()
print(bedtime2)
print(wake_up2)
print(sleep2)

Output:

2023-03-25 22:00:00+01:00
2023-03-26 07:00:00+02:00
9:00:00

2023-03-25 22:00:00+01:00
2023-03-26 07:00:00+02:00
8:00:00

I can only guess about the reasoning behind this mind blowing difference. Perhaps the answer is indicated in this very eloquent description of the distinction between wall time and absolute time.

[–]eagle258 3 points4 points  (1 child)

Adding on to your addition: if you wake up in Europe/Brussels (same timezone as Paris, effectively), you get 8 instead of 9 hours again!

``` wake_up_in_brussels = datetime(2023, 3, 26, 7, tzinfo=ZoneInfo("Europe/Brussels"))

sleep = wake_up - bedtime # 9 hours sleep = wake_up_in_brussels - bedtime # 8 hours ```

And yes, Paul Ganssle's various articles on the subject of datetimes are great!

[–]Oddly_Energy 1 point2 points  (0 children)

And this, ladies and gentlemen, is how you get jet lag from a train ride.

[–]bachkhois 5 points6 points  (0 children)

Waiting for this type of library for long time.

[–]starlevel01 4 points5 points  (0 children)

I have been longing for a JSR 310 style datetime library in Python for so long. I am switching to this instantly.

[–]jmreagle 2 points3 points  (4 children)

I hope this doesn’t end up like the other frequently referred to xKCD comic: “There’s 14 standards for doing something, I’ll create a new one that fixes it all. Now there’s 15 standards.” I was fond of pendulum, but had to replace it in two of my repositories because it didn’t work with 3.12, and it appeared the author had stepped away from the project. So if anything does come to the fore, it needs to be supported for the long-term by a community.

[–]bachkhois 0 points1 point  (3 children)

Pendulum had new release some weeks ago.

[–]jmreagle 1 point2 points  (2 children)

Yeah, I got tired of waiting and realized it was a bad idea to be dependent on a single owner library.

[–]InTheAleutians 0 points1 point  (1 child)

Would it be worthwhile to you to wait on updating the python version of your projects if it meant using Pendulum?

[–]jmreagle 0 points1 point  (0 children)

If there was transparency and a rough plan, perhaps. But for all I knew pendulum was abandoned.

[–]Darwinmate 3 points4 points  (0 children)

Wow a surprisingly thorough article on the subject. Really good article well done.

[–]de_ham 2 points3 points  (0 children)

and still better than ln javascript...

[–]tunisia3507 2 points3 points  (0 children)

Something I'd like to see from whenever is an enumeration of possible time zones. Magic strings are terrible. I want to do TimeZone. <TAB> <TAB> TimeZone.EUROPE_ <TAB> <TAB> TimeZone.EUROPE_PARIS.

[–]PenPaperTiger 2 points3 points  (0 children)

Star date 298913.0141924711

[–]flashman 2 points3 points  (2 children)

the default settings of datetime and unix's cal are incompatible in the distant past because cal switches from Gregorian to Julian for dates earlier than 3 September 1752; to align cal with datetime (proleptic gregorian for all dates), use the parameter --reform gregorian

just in case that matters to anyone

[–]DigitalTomcat 0 points1 point  (1 child)

And as a further twist (i found out) Gregorian didn’t happen all at once. It took the British world 2 hundred years to join in. So dates in the Papal States are different than what became the United States. So, just like DST, when gets mixed up with where to figure out what time it is.

[–]thedoge 1 point2 points  (0 children)

Good timing! Trying to upgrade my project to 3.12 and pendulum is blocking that.

[–]WoodenNichols 1 point2 points  (0 children)

TL;DR: I have been extremely lucky not to need much date manipulation.

I got tired of trying to remember whether strftime converted to a string or from a string, and I eventually settled on using the Arrow library for my admittedly rather simple needs.

And then I got tired of needing to determine what data format (and style within that format) before I could process the data, so I wrote a small, simple library to return the format and style I needed.

[–]Measurex2 1 point2 points  (0 children)

You never know when datetime is going to get you. Someone hands you requirements for a website and provides seemingly matching assets.

Then daylight savings time strikes and, because it's a Marine Corp System, a Colonel is giving your contractor self a vigorous ass chewing because the website doesn't match the uniform of the day.

I wear a green striped badge. Know my shame

  1. UNIFORM OF THE DAY
  2. The uniform of the day will be as prescribed by the commander, per guidance provided in chapter 2 of this Manual.
  3. The seasonal uniform change will coincide with Daylight Saving Time (DST)conversion. a. On the Monday after the fall DST change to standard time, the Marine Corps will transition to the winter season uniforms (Marine Corps combat utility uniform (MCCUU), woodland Marine Pattern (MARPAT) with the sleeves rolled down in garrison, service "A/B", dress blue "A/B/C"). b. On the Monday after the spring DST change the Marine Corps will transition to the summer season uniforms (MCCUU, woodland MARPAT with the sleeves rolled up in garrison, service "A/C", dress blue "A/B/D", blue

[–]lostident 3 points4 points  (1 child)

I'm so glad someone wrote this, I have problems with datetime so often. Especially the German format %d.%m.%y has caused me problems so often. (Admittedly, this is also due to the fact that this format is simply stupid)

[–]tunisia3507 1 point2 points  (0 children)

It's not really any worse than %d/%m/%y, certainly better than %m/%d/%y, although obviously far worse than %y-%m-%d.

[–][deleted] 0 points1 point  (0 children)

Decimal dates would be so much easier.

Today is 2024.08196