DateUtils.py 4.9 KB

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