time.time() returns UTC-based Unix epoch seconds—specifically the number of seconds (as a float) elapsed since 1970-01-01T00:00:00Z (UTC), completely independent of your machine’s local timezone or Daylight Saving Time (DST) settings.
Basic Example
import time
from datetime import datetime, timezone
# 1. `time.time()` returns the exact same Unix timestamp regardless of OS timezone:
ts = time.time()
print(ts)
# Example output: 1795955940.123456
# 2. Convert `time.time()` to an explicit UTC-aware datetime (Recommended):
utc_dt = datetime.fromtimestamp(ts, tz=timezone.utc)
print(utc_dt.isoformat())
# Output: 2026-11-29T12:39:00.123456+00:00
# 3. Convert the SAME timestamp to local wall-clock time (e.g., PKT = UTC+5):
local_dt = datetime.fromtimestamp(ts).astimezone()
print(local_dt.isoformat())
# Output: 2026-11-29T17:39:00.123456+05:00
Key Details & Edge Cases
- Unix epoch timestamps have NO timezone—they represent a single universal instant in UTC:
- Because
1970-01-01 00:00:00 UTCis a fixed point in time, callingtime.time()simultaneously on servers in New York (UTC-5), London (UTC+0), and Lahore/Mustafabad (UTC+5) returns the exact same float value. - Confusion usually arises when formatting that timestamp into human-readable text: calling
datetime.fromtimestamp(ts)withouttz=timezone.utc(ortime.localtime(ts)) applies your OS timezone offset to produce local wall-clock time, whereasdatetime.fromtimestamp(ts, tz=timezone.utc)(ortime.gmtime(ts)) formats it in UTC.
- Because
- Avoid deprecated
datetime.utcnow()anddatetime.utcfromtimestamp(ts)(deprecated in Python 3.12+):datetime.utcfromtimestamp(ts)returns a naivedatetimeobject (tzinfo=None), which Python treats as local time if later passed to.timestamp()—causing silent 5-hour (or local offset) bugs! Always passtz=timezone.utctodatetime.fromtimestamp(ts, tz=timezone.utc)(or usedatetime.now(timezone.utc)).
- Do NOT use
time.time()to measure elapsed durations or benchmark code:time.time()reads the OS wall-clock, which can jump backward or forward if NTP (Network Time Protocol) adjusts the system clock. Usetime.perf_counter()(for high-resolution benchmarking) ortime.monotonic()(for timeouts and intervals) instead.
- Leap seconds on POSIX systems:
- Per the POSIX standard,
time.time()counts exactly86,400seconds per day and does not count leap seconds (the OS clock steps or smears across occasional UTC leap seconds).
- Per the POSIX standard,
- Nanosecond integer precision (
time.time_ns()):- In Python 3.7+, use
time.time_ns()when you need an exact integer nanosecond UTC epoch timestamp without IEEE 754 floating-point precision loss (which degrades below ~microsecond precision for modern 10-digit epoch timestamps).
- In Python 3.7+, use
Comparison of Python Time & Timestamp Functions
| Function | Time Reference | Return Type | Affected by Local OS Timezone? | Best Used When |
|---|---|---|---|---|
time.time() (Recommended for epoch) |
UTC (1970-01-01T00:00:00Z) |
float (seconds) |
No (always UTC epoch) | Logging Unix timestamps, database epoch columns, JWT exp/iat claims, and cross-timezone sync. |
time.time_ns() |
UTC (1970-01-01T00:00:00Z) |
int (nanoseconds) |
No (always UTC epoch) | High-precision event ordering, trace IDs, and avoiding float rounding loss. |
datetime.now(timezone.utc) |
UTC (timezone-aware) | datetime (tzinfo=UTC) |
No (explicitly UTC) | Modern timezone-aware UTC datetimes, ISO 8601 serialization (.isoformat()), and date math. |
time.perf_counter() / time.monotonic() |
Arbitrary monotonic start point | float (seconds) |
No (immune to NTP clock jumps) | Measuring elapsed duration, execution benchmarks, rate limiters, and timeouts. |
You must log in or register to comment.
