import datetime
from dateutil.relativedelta import relativedelta
from dateutil import rrule

YYmmddHHMMSS = "%Y%m%d%H%M%S"
YYmmdd = "%Y%m%d"
YYmm = "%Y%m"
# 根据开始月份结束月份获取所有月份
def get_each_month(start_month, end_month):
    if str(start_month).count('-') != 1 or str(end_month).count('-') != 1:
        print("Parameter Error: Pls input a string such as '2019-01'")
        return []
    if int(str(start_month).split('-')[1]) > 12 or int(str(end_month).split('-')[1]) > 12:
        print('Parameter Error: Pls input correct month range such as between 1 to 12')
        return []
    if int(str(start_month).split('-')[1]) == 0 or int(str(end_month).split('-')[1]) == 13:
        print('Parameter Error: Pls input correct month range such as between 1 to 12')
        return []
    start = datetime.datetime.strptime(start_month, "%Y-%m")
    end = datetime.datetime.strptime(end_month, "%Y-%m")
    month_count = rrule.rrule(rrule.MONTHLY, dtstart=start, until=end).count()  # 计算总月份数
    if end < start:
        print("Parameter Error: Pls input right date range,start_month can't latter than end_month")
        return []
    else:
        list_month = []
        year = int(str(start)[:7].split('-')[0])  # 截取起始年份
        for m in range(month_count):  # 利用range函数填充结果列表
            month = int(str(start)[:7].split('-')[1])  # 截取起始月份，写在for循环里，作为每次迭代的累加基数
            month = month + m
            if month > 12:
                if month % 12 > 0:
                    month = month % 12  # 计算结果大于12，取余数
                    if month == 1:
                        year += 1  # 只需在1月份的时候对年份加1，注意year的初始化在for循环外
                else:
                    month = 12
            if len(str(month)) == 1:
                list_month.append(str(year) + '-0' + str(month))
            else:
                list_month.append(str(year) + '-' + str(month))
        return list_month


# 转换格式,去掉"-"，%Y%m
def get_eachmonth(start_month, end_month):
    startmonth = start_month[0:4] + "-" + start_month[4:6]
    endmonth = end_month[0:4] + "-" + end_month[4:6]
    months = get_each_month(startmonth, endmonth)
    list_month = [i[0:4] + i[5:7] for i in months]
    return list_month


def get_month(starttime, endtime):
    months = []
    starttime = datetime.datetime.strptime(starttime, YYmmddHHMMSS)
    endtime = datetime.datetime.strptime(endtime, YYmmddHHMMSS)
    while starttime <= endtime:
        start = starttime.strftime("%Y%m")
        if start not in months:
            months.append(start)
        starttime = starttime + datetime.timedelta(days=1)
    return months

def get_month_range(starttime, endtime):
    months = []
    starttime = datetime.datetime.strptime(starttime[0:8], YYmmdd)
    endtime = datetime.datetime.strptime(endtime[0:8], YYmmdd)
    while starttime < endtime:
        start = starttime.strftime(YYmmddHHMMSS)
        endMonth = datetime.datetime.strptime(start[0:6], YYmm) + relativedelta(months=+1)

        end = endMonth.strftime(YYmmddHHMMSS)
        if endtime <= endMonth:
            end = endtime.strftime(YYmmddHHMMSS)
        starttime = endMonth
        months.append([start, end])
    return months



def get_month1(starttime, endtime):
    months = []
    startyear = int(starttime[0:4])
    startmonth = int(starttime[4:6])
    endyear = int(endtime[0:4])
    endmonth = int(endtime[4:6])
    while startyear != endyear or startmonth != endmonth:
        startyearstr = str(startyear)
        startmonthstr = str(startmonth)
        if startmonth < 10:
            startmonthstr = "0" + str(startmonth)
        months.append(startyearstr + startmonthstr)
        startmonth += 1
        if startmonth == 13:
            startyear += 1
            startmonth = 1
    months.append(endtime[0:6])
    return months

#根据开始结束时间获取最大区间为1天的时间区间
# def get_day(starttime,endtime):
#     times = []
#     starttime = datetime.datetime.strptime(starttime, YYmmddHHMMSS)
#     endtime = datetime.datetime.strptime(endtime, YYmmddHHMMSS)
#     while starttime < endtime:
#         start = starttime.strftime(YYmmddHHMMSS)
#         starttime_delta = starttime + datetime.timedelta(days=1)
#         end = starttime_delta.strftime(YYmmddHHMMSS)
#         if str(starttime)[0:7]<str(starttime_delta)[0:7] :
#             end = starttime_delta.strftime("%Y%m") +"01000000"
#             starttime = datetime.datetime.strptime(end, YYmmddHHMMSS)
#             times.append([start, end])
#             continue
#         if starttime >= starttime_delta:
#             end = starttime_delta.strftime(YYmmddHHMMSS)
#         starttime = starttime + datetime.timedelta(days=1)
#         times.append([start,end])
#     return times
#根据开始结束时间获取最大区间为1天的时间区间
def get_day(starttime,endtime):
    times = []
    starttime = datetime.datetime.strptime(starttime, YYmmddHHMMSS)
    endtime = datetime.datetime.strptime(endtime, YYmmddHHMMSS)
    while starttime < endtime:
        start = starttime.strftime(YYmmddHHMMSS)
        stratDay = datetime.datetime.strptime(start[0:8],YYmmdd)
        starttime_delta = stratDay + datetime.timedelta(days=1)
        end = starttime_delta.strftime(YYmmddHHMMSS)
        if endtime <= starttime_delta:
            end = endtime.strftime(YYmmddHHMMSS)
        starttime = stratDay + datetime.timedelta(days=1)
        times.append([start,end])
    return times

if __name__ == '__main__':
    print(get_day("20220101000000", "20220201000000"))
