def prev(date):
"""Subtract one month from the given date.
Args:
date (datetime.date): The starting date.
Returns:
datetime.date: One month before the starting date unless the starting
date falls on a day that is not in the previous month; in that case, it
returns the last day of the previous month.
Example:
>>> import datetime
>>> prev(datetime.date(2016, 3, 31))
datetime.date(2016, 2, 29)
"""
year, month = (date.year, date.month - 1) if date.month > 1 else (date.year - 1, 12)
_, length = calendar.monthrange(year, month)
day = min(length, date.day)
return datetime.date(year, month, day)
评论列表
文章目录