在 1901-12-13 20:45:52 之前,utcoffset 为 4 小时 56 分钟。
您可以使用使用Olson database 的pytz 来确认这一点。这与 Pandas 用于执行时区计算的模块相同:
import pytz
eastern = pytz.timezone('US/Eastern')
for utcdate, info in zip(
eastern._utc_transition_times, eastern._transition_info):
utcoffset, dstoffset, tzabbrev = info
print('{} | {} '.format(utcdate, utcoffset.total_seconds()))
这会打印美国/东部时区的所有 utc 转换边界和 utcoffets(以秒为单位)。前几行是这样的
0001-01-01 00:00:00 | -17760.0
1901-12-13 20:45:52 | -18000.0
1918-03-31 07:00:00 | -14400.0
1918-10-27 06:00:00 | -18000.0
1919-03-30 07:00:00 | -14400.0
...
因此,在 1901-12-13 20:45:52 之前,utcoffset 为 -17760 秒(或者,相当于 4 小时 56 分钟)。
standard way 使用 pytz 从本地时间创建时区感知日期是调用 localize 方法:
import datetime as DT
import pytz
eastern = pytz.timezone('US/Eastern')
date = DT.datetime(1900,1,1)
local_date = eastern.localize(date)
print(local_date)
打印
1900-01-01 00:00:00-04:56
这证实了 Pandas 返回的时间戳是正确的:
import pandas as pd
string = "1900-01-01 00:00:00"
ts = pd.Timestamp(string, tz='US/Eastern')
print(ts)
# 1900-01-01 00:00:00-04:56