Search code examples
pythonpandasdatetimetimezoneunix-timestamp

python .timestamp() method calcualtes wrong (or the same) unix time for different timezones


My goal is to convert UTM to local time and then to Unix time. This is the original time:

import pandas as pd
pd.to_datetime(nw_pd_file['datetime(utc)'][0],format='%Y-%m-%d %H:%M:%S')
Timestamp('2019-04-26 21:38:11')

Adding utc info

>>> pd.to_datetime(nw_pd_file['datetime(utc)'][0],format='%Y-%m-%d %H:%M:%S').tz_localize('UTC')
Timestamp('2019-04-26 21:38:11+0000', tz='UTC')

converting to local Alaska time:

>>> pd.to_datetime(nw_pd_file['datetime(utc)'][0],format='%Y-%m-%d %H:%M:%S').tz_localize('UTC').tz_convert(pytz.timezone("America/Anchorage"))
Timestamp('2019-04-26 13:38:11-0800', tz='America/Anchorage')

Up to here all is good. But when I convert the correct local time to unix time, the restul is in UTC and not in Alaska local time:

>>>pd.to_datetime(nw_pd_file['datetime(utc)'][0],format='%Y-%m-%d %H:%M:%S').tz_localize('UTC').tz_convert(local).timestamp()
1556314691.0

When it should be

1556314691.0-8*3600=1556285891.0 # 8 for eight hours Alaska daylight time.

In fact, I get the same Unix timestamp independent of the time-zone:

>>> pd.to_datetime(nw_pd_file['datetime(utc)'][0],format='%Y-%m-%d %H:%M:%S').tz_localize('UTC').timestamp()
1556314691.0

Is the timestamp() meothod cannot read the timezone correctly?

thanks


Solution

  • The UNIX timestamp is designed to be timezone independent. See this thread Do unix timestamps change across timezones?

    Your code convert the same time to two different time zones, but their UNIX timestamps remain the same.