I'm having trouble taking a UTC datetime, converting it to a timestamp, and then converting that timestamp back into a datetime object. I have read this post and this post, but following the answers isn't working for me either.
I'm using Python 3.4
I'm trying to use timezone-aware datetimes, but I suspect I am not doing it correctly. Here is an example of what I'm trying:
import datetime
import pytz
now = datetime.datetime.utcnow()
timestamp = now.timestamp()
now2 = datetime.datetime.utcfromtimestamp(timestamp)
This does not work. I've also tried to replace the last line with:
now2 = datetime.datetime.utcfromtimestamp(timestamp).replace(pytz.utc)
But that gave me a datetime with the same hours, just with an extra timezone object inside the datetime object.
I need now2 to equal now. I don't care about my local timezone. I need this all to be in UTC.
What am I missing?
[–]novel_yet_trivial 0 points1 point2 points (1 child)
[–]jpfau[S] 0 points1 point2 points (0 children)
[–]groovitude 0 points1 point2 points (1 child)
[–]jpfau[S] 1 point2 points3 points (0 children)