Time and Date Done Right
Published at 22:15 on 17 June 2025
To avoid being simply negative and cutting others’ efforts down, I will contrast the shambolic time and date situation in Java with the superior one in Python.
The latter has two time and date packages. This is largely as a result of history. One, time, came early on, and basically is a way to call the Posix time and date functions from Python. The other, datetime, came later, and is geared to more advanced use cases.
I almost always just use time, because as I said earlier, by far the most common uses for a time datatype are to create an instance of one and to print it. For that, the traditional time library is more than sufficient.
But even datetime is so much better designed than its Java counterpart. Instead of nine absolute time types, there are three (a calendar date, a time of day, and a date with a time). Instead of two relative types, there is just one (which, really, is all one needs). Instead of an elaborate set of classes devoted to formatting and outputting a time, there is just a strftime method for each time object (and all the strftime‘s use exactly the same format-specifying mini-language, which is mostly compatible with the one used in the Posix/C world. The cognitive load of understanding it all is so much more reasonable.
As for the legacy class, its base time type is a Python floating point number. This is a value type, and as such avoids all the headaches associated with a mutable reference type. Its alternate time type is a Python tuple. Since tuples are immutable in Python, this also neatly avoids mutability headaches. And since standard, pre-existing data types are used, there is once more less cognitive load imposed on the programmer.
Since there is nothing fundamentally wrong with the older time class, it is considered just fine to use it in new programs. Because of course it is: it is the simpler of the two, and simplicity has long been a core value in the Python world.
It’s one of the reasons I can be so harsh on Java. They knew better, or rather should have known better. Python predates Java, and Python did not come up with this simplicity-as-a-virtue business. UNIX embodied it over two decades before Python popped onto the scene. By the time the 1990’s rolled around, UNIX had a longstanding reputation for being an exceptionally productive programming environment.
The principles were known. The Java world chose to ignore them, and has suffered as a result ever since.