Overview
Python can convert timestamps with datetime.fromtimestamp. For reliable server code, pass timezone.utc or another explicit timezone so the datetime object is aware rather than naive.
This guide focuses on practical implementation choices: how values should be stored, how they should cross API boundaries, and where developers most often introduce timezone or precision bugs. The safest pattern is to keep the stored value unambiguous, document the unit, and format the date only when it reaches a human-facing interface.
When to use it
Use Python timestamp conversion for data pipelines, logs, analytics events, API clients, ETL jobs, and backend services.
For production systems, also consider how the value will be indexed, logged, serialized, and read by other teams. A timestamp field that is obvious in one programming language can become ambiguous when it is consumed by JavaScript, SQL, mobile clients, or third-party integrations.
Developer examples
Use examples like this as a starting point, then adapt the timezone and precision to your application contract.
from datetime import datetime, timezone
unix_seconds = 1717243200
dt = datetime.fromtimestamp(unix_seconds, tz=timezone.utc)
print(dt.isoformat())
Common pitfalls
Naive datetimes do not carry timezone information. They can silently behave differently across machines and environments.
Most timestamp bugs come from hidden assumptions: local time treated as UTC, seconds treated as milliseconds, formatted strings parsed without offsets, or narrow integer columns copied from old examples. Add tests for boundary dates and document the expected unit beside every external timestamp field.
FAQ
How do I get the current Unix timestamp in Python?
Use int(datetime.now(tz=timezone.utc).timestamp()) for seconds.
Should I use utcfromtimestamp?
Prefer timezone-aware datetime.fromtimestamp(..., tz=timezone.utc).