Python Glossary Русский

all Python 3timefunction

time.mktime()

time conversion

The inverse of localtime(): turns a struct_time or 9-tuple in local time into seconds since the epoch (a float).

Syntax

time.mktime(time_tuple) -> float

The tuple is always treated as local time: passing it the result of gmtime() is off by the zone offset; for UTC there is calendar.timegm(). All nine fields are required; tm_isdst=-1 asks it to work out DST itself. Out-of-range values are normalized: January 32 becomes February 1, which is handy but hides input errors.

Examples

import time
print(time.mktime(time.localtime(1_000_000_000)))  # → 1000000000.0
ts = time.mktime((2024, 1, 32, 12, 0, 0, 0, 0, -1))
print(time.strftime('%Y-%m-%d', time.localtime(ts)))  # → 2024-02-01

See also

Common errors

Open in the full glossarydocs.python.orgReport an inaccuracy