Chapter 12. 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). Particular instants in time are expressed in seconds since a reference instant, known as the epoch. (Midnight, UTC, of January 1, 1970, is a popular epoch used on both Unix and Windows platforms.) 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.
This chapter covers the time
module, which supplies Python’s core time-handling functionality. The time
module is somewhat dependent on the underlying system’s C library. The chapter also presents the datetime
, sched
, and calendar
modules from the standard Python library, the third-party modules dateutil
and pytz
, and some essentials of the popular extension mx.DateTime
. mx.DateTime
has been around for many years, with behavior across platforms more uniform than time
’s, which helps account for its popularity, particularly for date-time representation in database interfaces.
Get Python in a Nutshell, 2nd 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.