Get UTC timestamp in Python
UTC timestamp represents a specific point in time as measured from the "Unix epoch" — January 1, 1970, 00:00:00 UTC. This timestamp is often used in computing because it is not affected by time zones or daylight saving time, providing a consistent reference time across the globe. When working with time, UTC is often the standard for consistency across different time zones. In Python, there are several ways to get the current UTC timestamp, and the most commonly used module is datetime.
Example: Using datetime module
from datetime import datetime
# Get the current UTC time and convert to timestamp
a = datetime.utcnow().timestamp()
print(a)
Output
1743769015.000206
Explanation:
- datetime.utcnow() returns the current UTC date and time.
- .timestamp() converts the datetime object to a timestamp (seconds since epoch).
Examples to get UTC timestamp
Example 1: Using datetime.datetime.now()
Use the datetime.datetime.now() to get the current date and time. Then use tzinfo class to convert our datetime to UTC. Lastly, use the timestamp() to convert the datetime object, in UTC, to get the UTC timestamp.
from datetime import timezone
import datetime
# Getting the current date and time
dt = datetime.datetime.now(timezone.utc)
a = dt.replace(tzinfo=timezone.utc)
b = a.timestamp()
print(b)
Output:
1743769180.382352
Explanation:
- datetime.datetime.now(timezone.utc) gets the current date and time in UTC by using timezone.utc as the time zone.
- dt.replace(tzinfo=timezone.utc) ensures that the datetime object dt is explicitly set to the UTC time zone. (This step is redundant in this case because dt is already in UTC.)
- utc_time.timestamp() converts the datetime object utc_time to a Unix timestamp (the number of seconds since January 1, 1970, 00:00:00 UTC).
- print(utc_timestamp) prints the UTC timestamp, which is the current time in seconds since the Unix epoch.
Example 2: Using time module
The time module provides a way to get the current UTC time in seconds since the Unix epoch.
import time
# Get current UTC timestamp
a = time.time()
print(a)
Output
1743769381.9550037
Explanation: time.time() returns the number of seconds since the Unix epoch (January 1, 1970), in UTC.
Example 3: Using calendar module
The calendar module can also be used to get UTC timestamps.
import calendar
from datetime import datetime
# Get the UTC timestamp
a = calendar.timegm(datetime.utcnow().utctimetuple())
print(a)
Output
1743769492
Explanation:
- datetime.utcnow().utctimetuple() returns the UTC time as a struct_time.
- calendar.timegm() converts the struct_time to a timestamp.
Example 4: Using pytz module (third-party library)
If we want to handle timezones explicitly and work with more robust timezone handling, you can use the pytz library.
from datetime import datetime
import pytz
# Set UTC timezone
a = pytz.utc
# Get the current UTC time and convert to timestamp
b = datetime.now(a).timestamp()
print(b)
Output
1743769635.195786
Explanation:
- pytz.utc ensures that the datetime is in UTC.
- .timestamp() converts the datetime object to a timestamp.