Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

time_1.replace(tzinfo=pytz.timezone("America/New_York")).timestamp()

time_1.replace(tzinfo=pytz.timezone("EST")).timestamp()

得到的unix时间戳不同?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
479 views
Welcome To Ask or Share your Answers For Others

1 Answer

>>> a=pytz.timezone("EST")
>>> a
<StaticTzInfo 'EST'>
>>> b=pytz.timezone("America/New_York")
>>> b
<DstTzInfo 'America/New_York' LMT-1 day, 19:04:00 STD>

注意这里的 LMT:这是Local Mean Time的缩写,意思是本地平均时。而且时间是19:04:00 STD,与标准时相差并不是整小时。

不同城市的时间

在19世纪,许多国家将本地平均时间正式用作民用时间。每个城市的经度定义了不同的本地时间,每度经度的差异为4分钟。这相当于纽约纬度上50英里或81公里的距离。

随着运输和通讯方式的发展,这种安排变得越来越不切实际,因此最终被标准时间和时区所取代。

处理办法

>>> d = datetime.datetime(2009,2,21,23,18,5)  
>>> tz.localize(d)  
datetime.datetime(2009, 2, 21, 23, 18, 5, tzinfo=<DstTzInfo 'Asia/Shanghai' CST+8:00:00 STD>)  

建议是生成带时区的时间时,一定要使用timezone.localize()来生成。不要在时间对象的构造函数中传入tzinfo的方式来实现。

参见: https://blog.csdn.net/starrai...


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share

548k questions

547k answers

4 comments

86.3k users

...