I need to get the current UNIX epoch at UTC in Python and I've tried the following:
from datetime import datetime
from datetime import timezone
import time
import calendar
def get_nonce():
unix_epoch = datetime.utcfromtimestamp(0).replace(tzinfo=timezone.utc)
now = datetime.now(tz=timezone.utc)
seconds = (now - unix_epoch).total_seconds()
return int(seconds)
def get_time():
current_time_seconds = time.time()
return int(current_time_seconds)
def get_time_utc():
current_time_seconds = datetime.now(timezone.utc).timestamp()
return int(current_time_seconds)
def get_time_again():
current_time = int(calendar.timegm(time.gmtime()))
return int(current_time)
print(get_nonce())
print(get_time())
print(get_time_utc())
print(get_time_again())
All are 1 hour behind from the current epoch which I check with this website: https://www.unixtimestamp.com/
What can be the reason why? time.tzname
says I'm at UTC and using date +%s
on the command line also returns a value that's about an hour behind as well.
I started getting the wrong epoch value yesterday and it's been hindering my progress since the API I'm integrating with requires the current epoch value in the body