0

指定された日付範囲の (StartDateOfMonth, EndDateOfMonth) 値のリストを生成したいと考えています。ex 時間範囲: 2011 年 9 月 11 日、2013 年 4 月 24 日、リストは次のようになります。

[('2011-9-11', '2011-9-30'), ('2011-10-01', '2011-10-31'), ('2011-11-01', '2011-11-30'), ('2011-12-01', '2011-12-31'), ('2012-1-01', '2012-1-31'), ('2012-2-01', '2012-2-29'), ('2012-3-01', '2012-3-31'), ('2012-4-01', '2012-4-30'), ('2012-5-01', '2012-5-31'), ('2012-6-01', '2012-6-30'), ('2012-7-01', '2012-7-31'), ('2012-8-01', '2012-8-31'), ('2012-9-01', '2012-9-30'), ('2012-10-01', '2012-10-31'), ('2012-11-01', '2012-11-30'), ('2012-12-01', '2012-12-31'), ('2013-1-01', '2013-1-31'), ('2013-2-01', '2013-2-28'), ('2013-3-01', '2013-3-31'), ('2013-4-01', '2013-4-24')]

やや醜いコードを思いつきました。これは、リストの理解力やその他の Python の機能が不足していることが原因の 1 つです。コードは次のとおりです。

def getMonthRanges(startDate, endDate):
    dateRange = []

    allYears= [eachYear for eachYear in range(startDate.year, endDate.year+1)]
    allMonths= [eachMonth for eachMonth in range(1, 13)]

    for eachYear in allYears:
        for eachMonth in allMonths:
            if eachYear == startDate.year:
                if eachMonth == startDate.month:
                   startOfMonth = str(eachYear)+'-'+str(eachMonth) + '-'+str(startDate.day)
                   endOfMonth =   str(eachYear)+ '-'+str(eachMonth) + '-'+str(calendar.monthrange(eachYear, eachMonth)[1])
                   dateRange.append((startOfMonth, endOfMonth))
                elif eachMonth > startDate.month:
                   startOfMonth = str(eachYear)+ '-'+str(eachMonth) + '-01'
                   endOfMonth = str(eachYear)+'-'+str(eachMonth)+ '-'+ str(calendar.monthrange(eachYear, eachMonth)[1])
                   dateRange.append((startOfMonth, endOfMonth))
                else:
                    continue
            if eachYear == endDate.year:
                if eachMonth == endDate.month:
                    startOfMonth = str(eachYear)+'-'+str(eachMonth) + '-01'
                    endOfMonth =   str(eachYear)+ '-'+str(eachMonth) + '-'+str(endDate.day)
                    dateRange.append((startOfMonth, endOfMonth))
                    break
                elif eachMonth < endDate.month:
                    startOfMonth = str(eachYear)+ '-'+str(eachMonth) + '-01'
                    endOfMonth = str(eachYear)+'-'+str(eachMonth)+ '-'+ str(calendar.monthrange(eachYear, eachMonth)[1])
                    dateRange.append((startOfMonth, endOfMonth))
            elif eachYear > startDate.year and eachYear < endDate.year:
                startOfMonth = str(eachYear)+ '-'+str(eachMonth) + '-01'
                endOfMonth = str(eachYear)+'-'+str(eachMonth)+ '-'+ str(calendar.monthrange(eachYear, eachMonth)[1])
                dateRange.append((startOfMonth, endOfMonth))

    return dateRange

このコードを要約/改善できるかどうか、他の開発者からのフィードバックを求めていますか?

4

2 に答える 2

2

datetimeモジュールのみを使用してそれを行う方法は次のとおりです。

>>> from datetime import date, timedelta
>>> from pprint import pprint

>>> def next_month(x):
        'Advance the first of the month, wrapping the year if necessary'
        if x.month < 12:
            return x.replace(month=x.month+1, day=1)
        return x.replace(year=x.year+1, month=1)

>>> def getMonthRanges(startDate, endDate):
        result = []
        first = startDate
        while first < endDate:
            nm = next_month(first)
            last = min(endDate, nm - timedelta(days=1))
            result.append([str(first), str(last)])
            first = nm
        return result

>>> pprint(getMonthRanges(date(2011, 9, 11), date(2013, 4, 24)))
[['2011-09-11', '2011-09-30'],
 ['2011-10-01', '2011-10-31'],
 ['2011-11-01', '2011-11-30'],
 ['2011-12-01', '2011-12-31'],
 ['2012-01-01', '2012-01-31'],
 ['2012-02-01', '2012-02-29'],
 ['2012-03-01', '2012-03-31'],
 ['2012-04-01', '2012-04-30'],
 ['2012-05-01', '2012-05-31'],
 ['2012-06-01', '2012-06-30'],
 ['2012-07-01', '2012-07-31'],
 ['2012-08-01', '2012-08-31'],
 ['2012-09-01', '2012-09-30'],
 ['2012-10-01', '2012-10-31'],
 ['2012-11-01', '2012-11-30'],
 ['2012-12-01', '2012-12-31'],
 ['2013-01-01', '2013-01-31'],
 ['2013-02-01', '2013-02-28'],
 ['2013-03-01', '2013-03-31'],
 ['2013-04-01', '2013-04-24']]
于 2013-05-11T18:12:38.740 に答える
1
import datetime as DT
import calendar
def getMonthRanges(startDate, endDate):
    while startDate <= endDate:
        year, month = startDate.year, startDate.month
        weekday, day = calendar.monthrange(year, month)
        end = min(DT.date(year, month, day), endDate)
        yield (startDate, end)
        startDate = end+DT.timedelta(days=1)

ranges = [map(str, interval) 
          for interval in getMonthRanges(DT.date(2011,9,11), DT.date(2013,4,24))]
print(ranges)

収量

[['2011-09-11', '2011-09-30'], ['2011-10-01', '2011-10-31'], ['2011-11-01', '2011-11-30'], ['2011-12-01', '2011-12-31'], ['2012-01-01', '2012-01-31'], ['2012-02-01', '2012-02-29'], ['2012-03-01', '2012-03-31'], ['2012-04-01', '2012-04-30'], ['2012-05-01', '2012-05-31'], ['2012-06-01', '2012-06-30'], ['2012-07-01', '2012-07-31'], ['2012-08-01', '2012-08-31'], ['2012-09-01', '2012-09-30'], ['2012-10-01', '2012-10-31'], ['2012-11-01', '2012-11-30'], ['2012-12-01', '2012-12-31'], ['2013-01-01', '2013-01-31'], ['2013-02-01', '2013-02-28'], ['2013-03-01', '2013-03-31'], ['2013-04-01', '2013-04-24']]
于 2013-05-11T17:49:23.270 に答える