(One of my summaries of a talk at the 2018 European djangocon.)
Handling time and timezones is complex and painful.
It starts with leap years. Every four years, there is a leap year. Except every 100 years. Except except every 400 years. The latter two rules are from the gregorian calendar, which replaced the julian calander in 1582 (at least, in parts of the world…)
When did the “october revolution” happen? Well, either 25 oct or 7 november 1917 (update: sorry, I had 1918 here originally :-) …) . Russia still had the Julian calender at that time :-)
Year? Well, some countries use years based on the lunar cycle… Or they count from a different starting point.
In IT we had the Y2k problem. In 20 years time there’ll be the 32 bit epoch overflow. It already crashed the AOL mail servers (!) years ago.
In python, there’s the time
module. It represents how your computer thinks
about time. It isn’t terribly useful if you actually want to do something with
it that you’re going to show to the user.
The datetime
module is the one you’ll probably want to use. But do you use
a date or a datetime? A date doesn’t have a timezone. The day Hawkings died,
and you asked it on google, you’d get “he died tomorrow” if you asked it from
the USA….
You’ll need to use datetimes. With timezone info, not “naïve datetimes”.
You absolutely have to use the pytz
module. That is absolutely
necessary. It is regularly updated. This year, it already had 5
updates. Countries change their timezone. Sometimes retrospectively.
He showed a couple of weird timezones examples. And he didn’t even have to leave Australia.
Timezones are date sensitive. Daylight savings time, for instance. And as timezones can change….
Reading in dates: hard to get right. People write dates differently. 1/6/18 can be 1 jan 2018, 18 june 2001 etc….
And specifying the right timezone is hard. ISO8601 has a way of specifying the
timezone as +08:00
, for instance. But then you only know which of the 20
possible timezones it is because you only have the +8 hours, not the
timezone…
Oh, there are also leap seconds. 23:59:61
sometimes is a valid time!
Oh, and don’t communicate like “it will be released in fall 2018”: on the southern hemisphere, fall is in the first half of the year.
A request:
Always include a year.
Always use the text version of the month (localized).
Always include a timezone.
Always use ISO8601 in logs.
Remember:
A date means nothing without a time
A time means nothing without a date.
Both are nothing without an accurate timezone
If you thought everything was hard now, what when we start colonizing planets? A day on Venus is longer than a year on Venus :-)
Photo explanation: station signs on the way from Utrecht (NL) to Heidelberg (DE).
My name is Reinout van Rees and I program in Python, I live in the Netherlands, I cycle recumbent bikes and I have a model railway.
Most of my website content is in my weblog. You can keep up to date by subscribing to the automatic feeds (for instance with Google reader):