periods.py 文件源码

python
阅读 27 收藏 0 点赞 0 评论 0

项目:beansoup 作者: fxtlabs 项目源码 文件源码
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)
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号