def monthrange(year, month): """How many days are in the specified month? @rtype: int""" if month==2: return calendar.monthrange(year, month)[1] return _monthranges[month]
def monthrange(year, month): """How many days are in the specified month? @rtype: int""" if month == 2: return calendar.monthrange(year, month)[1] return _monthranges[month]
def normalizedate(year, month, day): """Return a valid date (and an excellent case for metric time) And example is the 32nd of January is first of Feb, or Jan -2 is December 29th of previous year. You should call this after doing arithmetic on dates (for example you can just subtract 14 from the current day and then call this to get the correct date for two weeks ago. @rtype: tuple @return: (year, month, day) """ while day < 1 or month < 1 or month > 12 or ( day > 28 and day > monthrange(year, month)): if day < 1: month -= 1 if month < 1: month = 12 year -= 1 num = monthrange(year, month) day = num + day continue if day > 28 and day > monthrange(year, month): num = calendar.monthrange(year, month)[1] month += 1 if month > 12: month = 1 year += 1 day = day - num continue if month < 1: year -= 1 month = month + 12 continue if month > 12: year += 1 month -= 12 continue assert False, "can't get here" return year, month, day
def normalizedate(year, month, day): """Return a valid date (and an excellent case for metric time) And example is the 32nd of January is first of Feb, or Jan -2 is December 29th of previous year. You should call this after doing arithmetic on dates (for example you can just subtract 14 from the current day and then call this to get the correct date for two weeks ago. @rtype: tuple @return: (year, month, day) """ while day<1 or month<1 or month>12 or (day>28 and day>monthrange(year, month)): if day<1: month-=1 if month<1: month=12 year-=1 num=monthrange(year, month) day=num+day continue if day>28 and day>monthrange(year, month): num=calendar.monthrange(year, month)[1] month+=1 if month>12: month=1 year+=1 day=day-num continue if month<1: year-=1 month=month+12 continue if month>12: year+=1 month-=12 continue assert False, "can't get here" return year, month, day