There are number of posts in the "Internets" on how to convert timestamp to datetime. But all of them are either mistaken or consider converting timestamp to local timezoned datetime object.
The correct (and awfully akward) mean to convert timestamp to UTC datetime object:
from datetime import datetime
from time import mktime, timezone
def utcdatetime_to_ts(dt):
return mktime(dt.utctimetuple()) - timezone
Then you can always:
assert utcdatetime_to_ts(datetime.utcnow()) - time() <= 1
Check also a better and shorter version in the comments.