In my program, I need to use a datetime object that is in utc time not my local time, and I also need the timestamp (seconds since epoch) of that datetime object.
import datetime
my_time = datetime.datetime.utcnow()
timestamp = my_time.timestamp()
However, the timestamp was wrong. It was the timestamp when my local time is my_time
, not the correct timestamp when the utc time is my_time
. How can I get the correct timestamp? I don't want to create another datetime object because there might be microseconds of difference.
Edit
Example:
import datetime
utc_time = datetime.datetime.utcnow()
local_time = datetime.datetime.now()
utc_timestamp = utc_time.timestamp()
local_timestamp = local_time.timestamp()
print(utc_timestamp)
print(local_timestamp)
My result:
1633133103.945903
1633161903.945903
The two are different by 8 hours, because I live in UTC+08:00 time zone, and the first one is not "seconds since epoch". I want to get the correct timestamp from utc_time
because technically those two object were not created at the same time.