DateUtils.py 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. import datetime
  2. from dateutil.relativedelta import relativedelta
  3. from dateutil import rrule
  4. YYmmddHHMMSS = "%Y%m%d%H%M%S"
  5. YYmmdd = "%Y%m%d"
  6. YYmm = "%Y%m"
  7. # 根据开始月份结束月份获取所有月份
  8. def get_each_month(start_month, end_month):
  9. if str(start_month).count('-') != 1 or str(end_month).count('-') != 1:
  10. print("Parameter Error: Pls input a string such as '2019-01'")
  11. return []
  12. if int(str(start_month).split('-')[1]) > 12 or int(str(end_month).split('-')[1]) > 12:
  13. print('Parameter Error: Pls input correct month range such as between 1 to 12')
  14. return []
  15. if int(str(start_month).split('-')[1]) == 0 or int(str(end_month).split('-')[1]) == 13:
  16. print('Parameter Error: Pls input correct month range such as between 1 to 12')
  17. return []
  18. start = datetime.datetime.strptime(start_month, "%Y-%m")
  19. end = datetime.datetime.strptime(end_month, "%Y-%m")
  20. month_count = rrule.rrule(rrule.MONTHLY, dtstart=start, until=end).count() # 计算总月份数
  21. if end < start:
  22. print("Parameter Error: Pls input right date range,start_month can't latter than end_month")
  23. return []
  24. else:
  25. list_month = []
  26. year = int(str(start)[:7].split('-')[0]) # 截取起始年份
  27. for m in range(month_count): # 利用range函数填充结果列表
  28. month = int(str(start)[:7].split('-')[1]) # 截取起始月份,写在for循环里,作为每次迭代的累加基数
  29. month = month + m
  30. if month > 12:
  31. if month % 12 > 0:
  32. month = month % 12 # 计算结果大于12,取余数
  33. if month == 1:
  34. year += 1 # 只需在1月份的时候对年份加1,注意year的初始化在for循环外
  35. else:
  36. month = 12
  37. if len(str(month)) == 1:
  38. list_month.append(str(year) + '-0' + str(month))
  39. else:
  40. list_month.append(str(year) + '-' + str(month))
  41. return list_month
  42. # 转换格式,去掉"-",%Y%m
  43. def get_eachmonth(start_month, end_month):
  44. startmonth = start_month[0:4] + "-" + start_month[4:6]
  45. endmonth = end_month[0:4] + "-" + end_month[4:6]
  46. months = get_each_month(startmonth, endmonth)
  47. list_month = [i[0:4] + i[5:7] for i in months]
  48. return list_month
  49. def get_month(starttime, endtime):
  50. months = []
  51. starttime = datetime.datetime.strptime(starttime, YYmmddHHMMSS)
  52. endtime = datetime.datetime.strptime(endtime, YYmmddHHMMSS)
  53. while starttime <= endtime:
  54. start = starttime.strftime("%Y%m")
  55. if start not in months:
  56. months.append(start)
  57. starttime = starttime + datetime.timedelta(days=1)
  58. return months
  59. def get_month_range(starttime, endtime):
  60. months = []
  61. starttime = datetime.datetime.strptime(starttime[0:8], YYmmdd)
  62. endtime = datetime.datetime.strptime(endtime[0:8], YYmmdd)
  63. while starttime < endtime:
  64. start = starttime.strftime(YYmmddHHMMSS)
  65. endMonth = datetime.datetime.strptime(start[0:6], YYmm) + relativedelta(months=+1)
  66. end = endMonth.strftime(YYmmddHHMMSS)
  67. if endtime <= endMonth:
  68. end = endtime.strftime(YYmmddHHMMSS)
  69. starttime = endMonth
  70. months.append([start, end])
  71. return months
  72. def get_month1(starttime, endtime):
  73. months = []
  74. startyear = int(starttime[0:4])
  75. startmonth = int(starttime[4:6])
  76. endyear = int(endtime[0:4])
  77. endmonth = int(endtime[4:6])
  78. while startyear != endyear or startmonth != endmonth:
  79. startyearstr = str(startyear)
  80. startmonthstr = str(startmonth)
  81. if startmonth < 10:
  82. startmonthstr = "0" + str(startmonth)
  83. months.append(startyearstr + startmonthstr)
  84. startmonth += 1
  85. if startmonth == 13:
  86. startyear += 1
  87. startmonth = 1
  88. months.append(endtime[0:6])
  89. return months
  90. #根据开始结束时间获取最大区间为1天的时间区间
  91. # def get_day(starttime,endtime):
  92. # times = []
  93. # starttime = datetime.datetime.strptime(starttime, YYmmddHHMMSS)
  94. # endtime = datetime.datetime.strptime(endtime, YYmmddHHMMSS)
  95. # while starttime < endtime:
  96. # start = starttime.strftime(YYmmddHHMMSS)
  97. # starttime_delta = starttime + datetime.timedelta(days=1)
  98. # end = starttime_delta.strftime(YYmmddHHMMSS)
  99. # if str(starttime)[0:7]<str(starttime_delta)[0:7] :
  100. # end = starttime_delta.strftime("%Y%m") +"01000000"
  101. # starttime = datetime.datetime.strptime(end, YYmmddHHMMSS)
  102. # times.append([start, end])
  103. # continue
  104. # if starttime >= starttime_delta:
  105. # end = starttime_delta.strftime(YYmmddHHMMSS)
  106. # starttime = starttime + datetime.timedelta(days=1)
  107. # times.append([start,end])
  108. # return times
  109. #根据开始结束时间获取最大区间为1天的时间区间
  110. def get_day(starttime,endtime):
  111. times = []
  112. starttime = datetime.datetime.strptime(starttime, YYmmddHHMMSS)
  113. endtime = datetime.datetime.strptime(endtime, YYmmddHHMMSS)
  114. while starttime < endtime:
  115. start = starttime.strftime(YYmmddHHMMSS)
  116. stratDay = datetime.datetime.strptime(start[0:8],YYmmdd)
  117. starttime_delta = stratDay + datetime.timedelta(days=1)
  118. end = starttime_delta.strftime(YYmmddHHMMSS)
  119. if endtime <= starttime_delta:
  120. end = endtime.strftime(YYmmddHHMMSS)
  121. starttime = stratDay + datetime.timedelta(days=1)
  122. times.append([start,end])
  123. return times
  124. if __name__ == '__main__':
  125. print(get_day("20220101000000", "20220201000000"))