python 本周,本季度,本月,时间范围获取

2019-12-30  本文已影响0人  一只老梨花
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Date    : 2019-12-13 17:35:19
# @Author  : zhuhua 
import arrow

class DateUtils(object):
    """docstring for ClassName"""
    def __init__(self):
        self.now = arrow.utcnow().to("local")
        self.name2time = dict(
            昨天=lambda: self.get_yesterday_start_end(),
            今天=lambda: self.get_today_start_end(),
            本周=lambda: self.get_week_start_end(),
            上周=lambda: self.get_last_week_start_end(),
            本月=lambda: self.get_month_start_end(),
            上月=lambda: self.get_last_month_start_end(),
            本季度=lambda: self.get_quarter_start_end(),
            今年=lambda: self.get_year_start_end(),
        )


    def get_today_start_end(self):
        return self.now.floor("day").datetime, self.now.ceil("day").datetime

    def get_yesterday_start_end(self):
        yesterday = self.now.shift(days=-1)
        return yesterday.floor("day").datetime, yesterday.ceil("day").datetime

    def get_week_start_end(self):
        return self.now.floor("week").datetime, self.now.ceil("week").datetime

    def get_last_week_start_end(self):
        last_week = self.now.shift(weeks=-1)
        return last_week.floor("week").datetime, last_week.ceil("week").datetime

    def get_month_start_end(self):
        return self.now.floor("month").datetime, self.now.ceil("month").datetime

    def get_last_month_start_end(self):
        last_month = self.now.shift(months=-1)
        return last_month.floor("month").datetime, last_month.ceil("month").datetime

    def get_quarter_start_end(self):
        return self.now.floor("quarter").datetime, self.now.ceil("quarter").datetime

    def get_year_start_end(self):
        return self.now.floor("year").datetime, self.now.ceil("year").datetime


if __name__ == '__main__':
    bb=DateUtils()
    print(bb.name2time.get("昨天")())
上一篇下一篇

猜你喜欢

热点阅读