Chapter 13. Time Operations
A Python program can handle time in several ways. Time intervals are floating-point numbers in units of seconds (a fraction of a second is the fractional part of the interval): all standard library functions accepting an argument that expresses a time interval in seconds accept a float as the value of that argument. Instants in time are expressed in seconds since a reference instant, known as the epoch. (Although epochs vary per language and per platform, on all platforms, Python’s epoch is midnight, UTC, January 1, 1970.) Time instants often also need to be expressed as a mixture of units of measurement (e.g., years, months, days, hours, minutes, and seconds), particularly for I/O purposes. I/O, of course, also requires the ability to format times and dates into human-readable strings, and parse them back from string formats.
The time Module
The time module is somewhat dependent on the underlying system’s C library, which sets the range of dates that the time module can handle. On older Unix systems, the years 1970 and 2038 were typical cutoff points1 (a limitation avoided by using datetime, discussed in the following section). Time instants are normally specified in UTC (Coordinated Universal Time, once known as GMT, or Greenwich Mean Time). The time module also supports local time zones and daylight savings time (DST), but only to the extent the underlying C system library does.2
As an alternative to seconds since the epoch, a time instant can be ...
Get Python in a Nutshell, 4th Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.