Python Glossary Русский

all Python 3deprecated in 3.12datetimefunction

datetime.datetime.utcfromtimestamp()

datetime methods

Creates a naive datetime in UTC from a Unix timestamp. A deprecated method — prefer fromtimestamp(ts, tz=timezone.utc).

Syntax

datetime.utcfromtimestamp(timestamp)

Since Python 3.12 this call is deprecated and emits a DeprecationWarning; removal is planned for a future release. The warning is not the real problem: the result is naive, so a later timestamp() or astimezone() will treat it as local time and the round trip breaks on any machine that is not on UTC. Use fromtimestamp(ts, tz=timezone.utc) instead, so the zone travels with the object.

Examples

from datetime import datetime
dt = datetime.utcfromtimestamp(1704067200)
print(dt)  # 2024-01-01 00:00:00  (naive, UTC)
from datetime import datetime
# современная альтернатива:
from datetime import timezone
dt2 = datetime.fromtimestamp(1704067200, tz=timezone.utc)

See also

Common errors

Open in the full glossarydocs.python.orgReport an inaccuracy