I'm debugging some datetime functionality, specifically converting between local time and UTC. The program I'm working on will only be running on Linux and Mac but I would like to see a cross-platform solution. I'm using datetime.timestamp() to generate a utc timestamp in seconds to be stored in a database but am noticing some very erratic behavior when converting to and fro.
According to the python docs, datetime.timestamp() is supposed to return a float representing POSIX(UTC) time as seconds since the epoch. As I understand... For a tz naive object (or one already localized in UTC), this would just be total seconds. For a tz aware object, this time should be automatically be converted from the given tz to utc.
To test this behavior, I create a tz-naive datetime object at Jan, 1 2000. This time is then localized as US/Pacific and also converted to UTC with .astimezone(). Here's some setup code:
import datetime, time
from pytz import timezone
tz_pac = timezone("US/Pacific")
tz_utc = timezone("UTC")
start_tuple = (2000, 1, 1, 0, 0, 0)
naive_time_obj = datetime.datetime(*start_tuple, tzinfo = None)
pac_time_obj = tz_pac.localize(naive_time_obj)
utc_time_obj = pac_time_obj.astimezone(tz_utc)
naive_seconds = int(naive_time_obj.strftime("%s"))
pac_seconds = int(pac_time_obj.strftime("%s"))
utc_seconds = int(utc_time_obj.strftime("%s"))
print("Naive\tseconds:", naive_seconds, "\ttimestamp:", naive_time_obj.timestamp(), "\trepr:", naive_time_obj)
print("PAC\tseconds:", pac_seconds, "\ttimestamp:", pac_time_obj.timestamp(), "\trepr:", pac_time_obj)
print("UTC\tseconds:", utc_seconds, "\ttimestamp:", utc_time_obj.timestamp(), "\trepr:", utc_time_obj)
And here's what I'm getting:
Naive seconds: 946713600 timestamp: 946713600.0 repr: 2000-01-01 00:00:00
PAC seconds: 946713600 timestamp: 946713600.0 repr: 2000-01-01 00:00:00-08:00
UTC seconds: 946742400 timestamp: 946713600.0 repr: 2000-01-01 08:00:00+00:00
Output for the naive object makes sense as there is no timezone info to convert with. For the PAC localized object, I expect timestamp to be tz converted from seconds, but instead they are equal. For the UTC localized object, I expect seconds and timestamp to be equal, but a conversion has taken place. What am I missing?