import datetime
import calendar




class dateUtils:

    def get_current_start_end(self):
        """
        获取当日的开始时间和结束时间
        :return:
        """
        start, end = datetime.datetime.now().replace(hour=0, minute=0, second=0).strftime(
            "%Y-%m-%d %H:%M:%S"), datetime.datetime.now().replace(hour=23, minute=59, second=59).strftime(
            "%Y-%m-%d %H:%M:%S")
        return start, end


    def get_week_start_end(self):
        """
        获取本周的第一天和最后一天
        :return:
        """
        monday, sunday = datetime.date.today(), datetime.date.today()
        one_day = datetime.timedelta(days=1)
        while monday.weekday() != 0:
            monday -= one_day
        while sunday.weekday() != 6:
            sunday += one_day
        # 返回当前的星期一和星期天的日期
        monday, sunday = monday.strftime("%Y-%m-%d %H:%M:%S"), sunday.strftime("%Y-%m-%d %H:%M:%S").replace("00:00:00", "23:59:59")
        return monday, sunday

    def get_current_time(self):
        """
        获取当前时间，返回格式是yyyy-mm-dd hh:mm:ss
        :return:
        """
        curr_time = datetime.datetime.now()
        time = datetime.datetime.strftime(curr_time, '%Y-%m-%d %H:%M:%S')
        return time

    def get_current_date(self):
        """
        获取当前日期，返回格式是yyyy-mm-dd
        :return:
        """
        curr_time = datetime.datetime.now()
        date = datetime.datetime.strftime(curr_time, '%Y-%m-%d')
        return date

    def get_week_of_date(self, date):
        """
        获取传入的日期是星期几
        :param date:
        :return:
        """
        week_day_dict = {
            0: '星期一',
            1: '星期二',
            2: '星期三',
            3: '星期四',
            4: '星期五',
            5: '星期六',
            6: '星期日',
        }
        day = datetime.datetime.strptime(date, "%Y-%m-%d").weekday()
        return week_day_dict[day]

    def add_days(self, date, days):
        """
        获取新的日期
        :param date: 日期
        :param days: 天数（可正数，可负数）
        :return:
        """
        dt = datetime.datetime.strptime(date, "%Y-%m-%d")
        out_date = (dt + datetime.timedelta(days=days)).strftime("%Y-%m-%d")
        return out_date

    def add_hours(self, date, hours):
        """
        获取新的时间
        :param date:
        :param hours:
        :return:
        """
        dt = datetime.datetime.strptime(date, "%Y-%m-%d %H:%M:%S")
        out_date = (dt + datetime.timedelta(hours=hours)).strftime("%Y-%m-%d %H:%M:%S")
        return out_date

    def get_month_start_end(self, year, month):
        """
        获取本月的第一天和最后一天
        :return:
        """
        weekDay, monthCountDay = calendar.monthrange(year, month)
        firstDay = datetime.date(year, month, day=1)
        lastDay = datetime.date(year, month, day=monthCountDay)
        return firstDay, lastDay

    def get_every_month_start_end(self, year):
        """
        获取某年的每个月的第一天和最后一天
        :param year:
        :return:
        """
        start = []
        end = []
        for x in range(1, 13):
            dt_start = (datetime.datetime(int(year), x, 1)).strftime("%Y%m%d")
            if 12 == x:
                dt_end = (datetime.datetime(int(year), 12, 31)).strftime("%Y%m%d")
            else:
                dt_end = (datetime.datetime(int(year), x + 1, 1) - datetime.timedelta(days=1)).strftime("%Y%m%d")
            start.append(dt_start)
            end.append(dt_end)
        return start, end

    def get_year_month(self, time):
        """
        获取某个时间的年、月份
        :param time:
        :return:
        """
        dt = datetime.datetime.strptime(time, "%Y-%m-%d %H:%M:%S")
        return dt.year, dt.month





if __name__ == '__main__':
    print(dateUtils().get_current_date())
    print("   \n ")
    print(dateUtils().get_current_start_end())
    print("   \n ")
    print(dateUtils().add_days("2020-10-18", 10))
    print("   \n ")
    print(dateUtils().add_hours("2020-10-18 00:00:00", 3))
    print("   \n ")
    print(dateUtils().get_current_time())
    print("   \n ")
    print(dateUtils().get_every_month_start_end("2020"))
    print("   \n ")
    print(dateUtils().get_week_of_date("2020-10-18"))



