如何在Python中查找同一小时(包括DST)的第二天的Unix时间戳? [英] How to find next day's Unix timestamp for same hour, including DST, in Python?
问题描述
在Python中,我可以像这样(使用pytz)知道时区,找到本地时间的Unix时间戳:
In Python, I can find the Unix time stamp of a local time, knowing the time zone, like this (using pytz):
>>> import datetime as DT
>>> import pytz
>>> mtl = pytz.timezone('America/Montreal')
>>> naive_time3 = DT.datetime.strptime('2013/11/03', '%Y/%m/%d')
>>> naive_time3
datetime.datetime(2013, 11, 3, 0, 0)
>>> localized_time3 = mtl.localize(naive_time3)
>>> localized_time3
datetime.datetime(2013, 11, 3, 0, 0, tzinfo=<DstTzInfo 'America/Montreal' EDT-1 day, 20:00:00 DST>)
>>> localized_time3.timestamp()
1383451200.0
到目前为止,很好。 naive_time
未知时区,而 localized_time
知道2013年11月3日午夜蒙特利尔,因此(UTC)Unix时间戳很好。此时区也是我的本地时区,并且此时间戳似乎正确:
So far, so good. naive_time
is not aware of the time zone, whereas localized_time
knows its midnight on 2013/11/03 in Montréal, so the (UTC) Unix time stamp is good. This time zone is also my local time zone and this time stamp seems right:
$ date -d @1383451200
Sun Nov 3 00:00:00 EDT 2013
现在,时钟在11月3日向后调整了一个小时2:00在蒙特利尔,所以那天我们多了一个小时。这表示在2013/11/03至2013/11/04之间有 25小时。这表明:
Now, clocks were adjusted one hour backward November 3rd at 2:00 here in Montréal, so we gained an extra hour that day. This means that there were, here, 25 hours between 2013/11/03 and 2013/11/04. This shows it:
>>> naive_time4 = DT.datetime.strptime('2013/11/04', '%Y/%m/%d')
>>> localized_time4 = mtl.localize(naive_time4)
>>> localized_time4
datetime.datetime(2013, 11, 4, 0, 0, tzinfo=<DstTzInfo 'America/Montreal' EST-1 day, 19:00:00 STD>)
>>> (localized_time4.timestamp() - localized_time3.timestamp()) / 3600
25.0
现在,我正在寻找一种简单的方法来从 localized_time3
获取 localized_time4
对象,因为我想获取下一个同一小时(此处为午夜)的本地化日期。我尝试了 timedelta
,但我认为它不知道时区或DST:
Now, I'm looking for an easy way to get the localized_time4
object from localized_time3
, knowing I want to get the next localized day at the same hour (here, midnight). I tried timedelta
, but I believe it's not aware of time zones or DST:
>>> localized_time4td = localized_time3 + DT.timedelta(1)
>>> localized_time4td
datetime.datetime(2013, 11, 4, 0, 0, tzinfo=<DstTzInfo 'America/Montreal' EDT-1 day, 20:00:00 DST>)
>>> (localized_time4td.timestamp() - localized_time3.timestamp()) / 3600
24.0
我的目的是获取有关每个本地日以Unix时间戳存储的日志条目的信息。当然,如果我使用 localized_time3.timestamp()
并在此处添加 24 * 3600
(这与 localized_time4td.timestamp()
),我会错过在 localized_time4td.timestamp()
和<$之间发生的所有日志条目c $ c> localized_time4td.timestamp()+ 3600 。
My purpose is to get informations about log entries that are stored with their Unix timestamp for each local day. Of course, if I use localized_time3.timestamp()
and add 24 * 3600
here (which will be the same as localized_time4td.timestamp()
), I will miss all log entries that happened between localized_time4td.timestamp()
and localized_time4td.timestamp() + 3600
.
换句话说,我正在寻找的函数或方法应该知道何时
In other words, the function or method I'm looking for should know when to add 25 hours, 24 hours or 23 hours sometimes to a Unix time stamp, depending on when DST shifts happen.
推荐答案
无,根据Unix时间戳的变化,有时会在Unix时间戳上添加25小时,24小时或23小时。使用新软件包:
Without using a new package:
def add_day(x):
d = x.date()+DT.timedelta(1)
return mtl.localize(x.replace(year=d.year, month=d.month, day=d.day, tzinfo=None))
完整脚本:
import datetime as DT
import pytz
import calendar
mtl = pytz.timezone('America/Montreal')
naive_time3 = DT.datetime.strptime('2013/11/03', '%Y/%m/%d')
print repr(naive_time3)
#datetime.datetime(2013, 11, 3, 0, 0)
localized_time3 = mtl.localize(naive_time3)
print repr(localized_time3)
#datetime.datetime(2013, 11, 3, 0, 0, tzinfo=<DstTzInfo 'America/Montreal' EDT-1 day, 20:00:00 DST>)
print calendar.timegm(localized_time3.utctimetuple())
#1383451200.0
def add_day(x):
d = x.date()+DT.timedelta(1)
return mtl.localize(x.replace(year=d.year, month=d.month, day=d.day, tzinfo=None))
print repr(add_day(localized_time3))
#datetime.datetime(2013, 11, 4, 0, 0, tzinfo=<DstTzInfo 'America/Montreal' EST-1 day, 19:00:00 STD>)
(日历
适用于Python2。)
(calendar
is for Python2.)
这篇关于如何在Python中查找同一小时(包括DST)的第二天的Unix时间戳?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!