SHARE
TWEET

Untitled

a guest Jul 16th, 2019 52 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/usr/bin/env python
  2. # coding:utf-8
  3. # 日期生成
  4. # 带上两个日期做为开始和结束日期,会生成从开始到结束的所有日期
  5.  
  6. import re
  7. import sys
  8. import time
  9. import datetime
  10.  
  11. date_1_str = sys.argv[1]
  12. date_2_str = sys.argv[2]
  13.  
  14. date_format = '%Y%m%d' if \
  15.     (re.match(r'^\d{8}$', date_1_str) and re.match(r'^\d{8}$', date_2_str)) else \
  16.         '%Y-%m-%d' if \
  17.             (re.match(r'^\d{4}-\d{1,2}-\d{1,2}$', date_1_str) and re.match(r'^\d{4}-\d{1,2}-\d{1,2}$', date_2_str)) else \
  18.                 ''
  19. if date_format:
  20.     date_1 = datetime.datetime.strptime(date_1_str, date_format)
  21.     date_2 = datetime.datetime.strptime(date_2_str, date_format)
  22.     date_1_timestamp, date_2_timestamp = map(int, map(time.mktime, [date_1.timetuple(), date_2.timetuple()]))
  23.     start_date, end_date = [i[1] for i in sorted([(date_1_timestamp, date_1), (date_2_timestamp, date_2)], key=lambda x:x[0])]
  24.     for day in xrange((end_date - start_date).days + 1):
  25.         print((start_date + datetime.timedelta(day)).strftime(date_format))
  26. else:
  27.     print('Date Format: 2019-07-04 or 20190704')
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top