Does Python's time.time() return the local or UTC timestamp?
The time.time() function in Python returns the current timestamp in seconds since the epoch (January 1, 1970) in the local timezone.
The time.time() function in Python returns the current timestamp as a Unix timestamp (seconds since the epoch, January 1, 1970 UTC). Here is an example of using the time.time() function to get the current timestamp:
Get the current timestamp in Python
<div class="alert alert-info flex not-prose">![]()
<span class="hidden md:block">Watch a video course</span>Python - The Practical Guide</div>
If you want a UTC timestamp, you can use datetime.now(timezone.utc) from Python's datetime library. Here is an example of using datetime.now(timezone.utc) to get the current UTC time:
Get the current UTC timestamp in Python
Note that the above example returns a timezone-aware datetime object, not a numeric timestamp. If you need a numeric UTC timestamp (seconds since epoch), you can call .timestamp() on it:
current_utc_timestamp = datetime.now(timezone.utc).timestamp()
print(current_utc_timestamp)