Home > Backend Development > Python Tutorial > How to Accurately Convert Python `datetime.date` and `datetime.datetime` Objects to UTC Timestamps?

How to Accurately Convert Python `datetime.date` and `datetime.datetime` Objects to UTC Timestamps?

Patricia Arquette
Release: 2024-12-19 22:53:10
Original
523 people have browsed it

How to Accurately Convert Python `datetime.date` and `datetime.datetime` Objects to UTC Timestamps?

Converting datetime.date to UTC Timestamp in Python

When dealing with dates in Python, the need arises to convert them to UTC timestamps for various applications. However, using conventional methods like datetime.datetime.utcfromtimestamp(time.mktime(d)) may lead to incorrect results.

Converting a UTC Date to Seconds Since Epoch

For a date d represented in UTC, the correct formula to convert it to seconds since epoch is:

timestamp1 = calendar.timegm(d.timetuple())
Copy after login

This method ensures the resulting timestamp represents the date in UTC.

Converting a Local Time Date to Seconds Since Epoch

If the date d is in the local timezone, using mktime() may yield incorrect results. To remedy this, we can use:

timestamp2 = time.mktime(d.timetuple())
Copy after login

However, note that timestamp1 and timestamp2 may differ if the local midnight differs from UTC midnight.

Converting datetime.date in UTC without calendar.timegm()

To convert a datetime.date object directly to a UTC timestamp, you can use the following formulae:

timestamp = (utc_date.toordinal() - date(1970, 1, 1).toordinal()) * DAY
timestamp = (utc_date - date(1970, 1, 1)).days * DAY
Copy after login

Converting datetime.datetime in UTC to POSIX Timestamp

For a datetime.datetime object already in UTC, the following methods are available:

  • Python 3.3 offers datetime.timestamp():
timestamp = dt.replace(tzinfo=timezone.utc).timestamp()
Copy after login
  • Python 3 (< 3.3):
timestamp = (dt - datetime(1970, 1, 1)) / timedelta(seconds=1)
Copy after login
  • Python 2:
timestamp = (dt - datetime(1970, 1, 1)).total_seconds()
Copy after login

Converting an Aware datetime Object to POSIX Timestamp

For an aware datetime object (datetime with timezone information), the following approach is recommended:

timestamp = dt.timestamp() # Python 3.3+
timestamp = (dt - epoch) / timedelta(seconds=1) # Python 3
timestamp = (dt - epoch) // timedelta(seconds=1) # Python 3 integer timestamp
Copy after login

The above is the detailed content of How to Accurately Convert Python `datetime.date` and `datetime.datetime` Objects to UTC Timestamps?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template