The time Module
The underlying C library determines the range of dates that the time
module can handle. On Unix systems, years 1970 and 2038 are typical cut-off points, a limitation that datetime
and mx.DateTime
let you avoid. Time instants are normally specified in UTC (Coordinated Universal Time, once known as GMT, or Greenwich Mean Time). Module time
also supports local time zones and daylight saving time (DST), but only to the extent that support is supplied by the underlying C system library.
As an alternative to seconds since the epoch, a time instant can be represented by a tuple of nine integers, called a time-tuple. (Time-tuples are covered in Table 12-1.) All items are integers: time-tuples don’t keep track of fractions of a second. A time-tuple is an instance of struct_time
. You can use it as a tuple, and access the items as read-only attributes x
.tm_year
, x
.tm_mon
, and so on, with the attribute names listed in Table 12-1. Wherever a function requires a time-tuple argument, you can pass an instance of struct_time
or any other sequence whose items are nine integers in the right ranges.
Table 12-1. Tuple form of time representation
Item | Meaning | Field name | Range | Notes |
---|---|---|---|---|
0 | Year |
| 1970–2038 | Wider on some platforms. |
1 | Month |
| 1–12 |
|
2 | Day |
| 1–31 | |
3 | Hour |
| 0–23 |
|
4 | Minute |
| 0–59 | |
5 | Second |
| 0–61 |
|
6 | Weekday |
| 0–6 |
|
7 | Year day |
| 1–366 | Day number within the year. |
8 | DST flag |
|
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.