我如何知道今天是否是由于更改民用当地时间的日子,例如标准python和pandas时间戳中的夏令时



根据英国夏令时/夏令时规则(https://www.gov.uk/when-do-the-clocks-change)时钟:

  • 三月最后一个星期日凌晨1点前进1小时
  • 10月最后一个星期日凌晨2点回去1个小时

2019年,这一民用当地时间变化发生在3月31日和10月27日,但每年的天数略有变化。有没有一种干净的方法来了解每个输入年份的这些日期?

我需要以自动的方式检查这些"更改时间"日期,有没有办法避免for循环来检查每个日期的详细信息,看看它是否是"更改时间"日期?

目前,我正在探索2019年的这些日期,只是想找出一个可重复/自动的程序,我发现了这个:

# using datetime from the standard library
march_utc_30 = datetime.datetime(2019, 3, 30, 0, 0, 0, 0, tzinfo=datetime.timezone.utc)
march_utc_31 = datetime.datetime(2019, 3, 31, 0, 0, 0, 0, tzinfo=datetime.timezone.utc)
april_utc_1 = datetime.datetime(2019, 4, 1, 0, 0, 0, 0, tzinfo=datetime.timezone.utc)
# using pandas timestamps
pd_march_utc_30 = pd.Timestamp(march_utc_30) #, tz='UTC')
pd_march_utc_31 = pd.Timestamp(march_utc_31) #, tz='UTC')
pd_april_utc_1 = pd.Timestamp(april_utc_1) #, tz='UTC')
# using pandas wrappers
pd_local_march_utc_30 = pd_march_utc_30.tz_convert('Europe/London')
pd_local_march_utc_31 = pd_march_utc_31.tz_convert('Europe/London')
pd_local_april_utc_1 = pd_april_utc_1.tz_convert('Europe/London')
# then printing all these dates
print("march_utc_30 {} pd_march_utc_30 {} pd_local_march_utc_30 {}".format(march_utc_30, pd_march_utc_30, pd_local_march_utc_30))
print("march_utc_31 {} pd_march_utc_31 {} pd_local_march_utc_31 {}".format(march_utc_31, pd_march_utc_31, pd_local_march_utc_31))
print("april_utc_1  {} pd_april_utc_1  {} pd_local_april_utc_1  {}".format(april_utc_1, pd_april_utc_1, pd_local_april_utc_1))

这些print语句的输出是:

march_utc_30 2019-03-30 00:00:00+00:00 pd_march_utc_30 2019-03-30 00:00:00+00:00 pd_local_march_utc_30 2019-03-30 00:00:00+00:00
march_utc_31 2019-03-31 00:00:00+00:00 pd_march_utc_31 2019-03-31 00:00:00+00:00 pd_local_march_utc_31 2019-03-31 00:00:00+00:00
april_utc_1  2019-04-01 00:00:00+00:00 pd_april_utc_1  2019-04-01 00:00:00+00:00 pd_local_april_utc_1  2019-04-01 01:00:00+01:00

我可以使用for循环来确定当前日期是否是该月的最后一个星期日,或者比较当前日期和次日日期之间的"小时增量",看看是否有+1,但我想知道是否有更干净的方法可以做到这一点?

年份是否有附加内容,例如,如果知道输入年份是2019,那么我们就可以确定3月的"更改日期"将是第31天?

使用dateutil.rrule可以有所帮助(使用pip install python-dateutil安装)。

因为我们可以按周提取日期,所以我们不需要任何循环,

from dateutil.rrule import rrule, WEEKLY
from dateutil.rrule import SU as Sunday
from datetime import date
import datetime
def get_last_sunday(year, month):
date = datetime.datetime(year=year, month=month, day=1)
# we can find max 5 sundays in a months
days = rrule(freq=WEEKLY, dtstart=date, byweekday=Sunday, count=5)
# Check if last date is same month,
# If not this couple year/month only have 4 Sundays
if days[-1].month == month:
return days[-1]
else:
return days[-2]
def get_march_switch(year):
# Get 5 next Sundays from first March
day = get_last_sunday(year, 3)
return day.replace(hour=1, minute=0, second=0, microsecond=0)
def get_october_switch(year):
day = get_last_sunday(year, 10)
return day.replace(hour=2, minute=0, second=0, microsecond=0)
print('2019:')
print('  {}'.format(get_march_switch(2019)))
print('  {}'.format(get_october_switch(2019)))
print('2021:')
print('  {}'.format(get_march_switch(2021)))
print('  {}'.format(get_october_switch(2021)))

get_sundays()返回给定month的第一天起的5个下周日,因为一个月最多可以有5个周日。

然后我只是检查(在get_(march|october)_switch()内)最后一个给定的星期天是否来自预期的月份,如果这个月不太好,只有4个星期天,我选择了这个。

最后我修正了小时、秒和微秒。

输出:

2019:
2019-03-31 01:00:00
2019-10-27 02:00:00
2021:
2021-03-28 01:00:00
2021-10-24 02:00:00

我知道这个话题现在已经很老了。然而,我今天也有同样的问题,最后我找到了一个对我来说似乎很简单的解决方案,只使用标准日期时间:

我想检查我的日期refdate是否是10月夏令时-我用以下方式进行了检查:

refdate是我的标准日期时间对象。如果您有熊猫时间戳,您可以使用.to_pydatetime()将其转换为本地日期时间

if refdate.month == 10 and refdate.weekday() == 6 and (refdate + dt.timedelta(weeks = 1)).month == 11:
oct_dst = 1

最新更新