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

tm_year

1970–2038

Wider on some platforms.

1

Month

tm_mon

1–12

1 is January; 12 is December.

2

Day

tm_mday

1–31

 

3

Hour

tm_hour

0–23

0 is midnight; 12 is noon.

4

Minute

tm_min

0–59

 

5

Second

tm_sec

0–61

60 and 61 for leap seconds.

6

Weekday

tm_wday

0–6

0 is Monday; 6 is Sunday.

7

Year day

tm_yday

1–366

Day number within the year.

8

DST flag

tm_isdst ...

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.