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 UTC is a fixed point in time, calling time.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) without tz=timezone.utc (or time.localtime(ts)) applies your OS timezone offset to produce local wall-clock time, whereas datetime.fromtimestamp(ts, tz=timezone.utc) (or time.gmtime(ts)) formats it in UTC.
  • Avoid deprecated datetime.utcnow() and datetime.utcfromtimestamp(ts) (deprecated in Python 3.12+):
    • datetime.utcfromtimestamp(ts) returns a naive datetime object (tzinfo=None), which Python treats as local time if later passed to .timestamp()—causing silent 5-hour (or local offset) bugs! Always pass tz=timezone.utc to datetime.fromtimestamp(ts, tz=timezone.utc) (or use datetime.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. Use time.perf_counter() (for high-resolution benchmarking) or time.monotonic() (for timeouts and intervals) instead.
  • Leap seconds on POSIX systems:
    • Per the POSIX standard, time.time() counts exactly 86,400 seconds per day and does not count leap seconds (the OS clock steps or smears across occasional UTC leap seconds).
  • 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).

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.