|
@@ -1,10 +1,10 @@
|
|
|
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:
|
|
@@ -53,8 +53,8 @@ def get_eachmonth(start_month, end_month):
|
|
|
|
|
|
def get_month(starttime, endtime):
|
|
|
months = []
|
|
|
- starttime = datetime.datetime.strptime(starttime[0:6], "%Y%m")
|
|
|
- endtime = datetime.datetime.strptime(endtime[0:6], "%Y%m")
|
|
|
+ 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:
|
|
@@ -62,6 +62,22 @@ def get_month(starttime, endtime):
|
|
|
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 = []
|
|
@@ -118,4 +134,4 @@ def get_day(starttime,endtime):
|
|
|
return times
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
- print(get_each_month("2020-01", "2020-12"))
|
|
|
+ print(get_day("20220101000000", "20220201000000"))
|