计算两个日期之间的夏日

时间:2017-10-10 12:53:24

标签: python datetime

我想计算两个日期之间的夏日。夏天是5月1日至8月。

这将计算所有日子:

import datetime

startdate=datetime.datetime(2015,1,1)
enddate=datetime.datetime(2016,6,1)

delta=enddate-startdate

print delta.days
>>517

但是,怎么只计算过去的夏日?

3 个答案:

答案 0 :(得分:4)

您可以定义一个生成器,以迭代startdateenddate之间的每个日期,定义一个函数来检查日期是否代表夏日,并使用sum计算夏令时:

import datetime

startdate = datetime.datetime(2015,1,1)
enddate = datetime.datetime(2016,6,1)

all_dates = (startdate + datetime.timedelta(days=x) for x in range(0, (enddate-startdate).days))

def is_summer_day(date):
    return 5 <= date.month <= 8

print(sum(1 for date in all_dates if is_summer_day(date)))
# 154

感谢生成器,您不需要在startdateenddate之间每天在内存中创建一个巨大的列表。

这个迭代仍然会考虑每一天,即使它不需要。对于非常大的差距,您可以根据您的定义使用每个完整年份有123个夏日的事实。

答案 1 :(得分:0)

您可以创建一些功能来计算两天之间的夏令时数:

from datetime import date

def get_summer_start(year):
    return date(year, 5, 1)

def get_summer_end(year):
    return date(year, 8, 31)

def get_start_date(date, year):
    return max(date, get_summer_start(year))

def get_end_date(date, year):
    return min(date, get_summer_end(year))

def count_summer_days(date1, date2):
    date1_year = date1.year
    date2_year = date2.year

    if date1_year == date2_year:
        s = get_start_date(date1, date1_year)
        e = get_end_date(date2, date1_year)
        return (e - s).days

    else:
        s1 = max(date1, get_summer_start(date1_year))
        e1 = get_summer_end(date1_year)

        first_year = max(0,(e1 -s1).days)

        s1 = get_summer_start(date2_year)
        e1 = min(date2, get_summer_end(date2_year))

        last_year = max(0,(e2 -s2).days)

        other_years = date2_year - date1_year - 1
        summer_days_per_year = (get_summer_end(date1_year) - get_summer_start(date1_year)).days

        return first_year + last_year + (other_years * summer_days_per_year)

date1 = date(2015,1,1)
date2 = date(2016,6,1)

print count_summer_days(date1, date2)

答案 2 :(得分:0)

这是一个更好的解决方案:

first_summer_day = (5,1)
last_summer_day = (8,31)

from datetime import date

startdate = date(2015,1,1)
enddate = date(2016,6,1)

# make sure that startdate > endate
if startdate > enddate:
    startdate, endate = endate, startdate


def iter_yearly_summer_days(startdate, enddate):
    for year in range(startdate.year, enddate.year+1):
        start_period = startdate if year == startdate.year else date(year, 1, 1)
        end_period = enddate if year == enddate.year else date(year, 12, 31)

        year_first_summer_day = date(year, *first_summer_day)
        year_last_summer_day = date(year, *last_summer_day)

        summer_days_that_year = (min(year_last_summer_day, end_period) - max(year_first_summer_day, start_period)).days
        print('year {} had {} days of summer'.format(year, summer_days_that_year))
        yield summer_days_that_year


print(sum(iter_yearly_summer_days(startdate, enddate)))