Difference between revisions of "GenDates.py"
Jump to navigation
Jump to search
PeterHarding (talk | contribs) |
PeterHarding (talk | contribs) |
||
Line 4: | Line 4: | ||
#!/usr/bin/env python | #!/usr/bin/env python | ||
# | # | ||
# Purpose: Generate | # Purpose: Generate date range | ||
# | # | ||
# $Id:$ | # $Id:$ | ||
Line 11: | Line 11: | ||
""" | """ | ||
Usage: | Usage: gen_date_range.py [-v] [-d] [-D2] [-S <start_date> [-E <end_date>]] | ||
... | ... | ||
Line 20: | Line 20: | ||
import sys | import sys | ||
import getopt | import getopt | ||
import logging | |||
#------------------------------------------------------------------------------- | #------------------------------------------------------------------------------- | ||
Line 28: | Line 29: | ||
__version__ = "1.0.0" | __version__ = "1.0.0" | ||
__id__ = "@(#) | __id__ = "@(#) gen_date_range.py [%s] 2008-07-15" | ||
verbose_flg = False | verbose_flg = False | ||
Line 43: | Line 44: | ||
6 : 'Sun' | 6 : 'Sun' | ||
} | } | ||
START = datetime(day=1, month=1, year=2007) | |||
#=============================================================================== | #=============================================================================== | ||
Line 56: | Line 59: | ||
#------------------------------------------------------------------------------- | #------------------------------------------------------------------------------- | ||
def | def date_range(start=None, end=None, no_days=None): | ||
d_start = | |||
d_end = | if start: | ||
d_start = start | |||
else: | |||
d_start = START | |||
if end: | |||
d_end = end | |||
elif no_days: | |||
d_end = d_start + timedelta(days=no_days) | |||
else: | |||
d_end = datetime.now() | |||
d = d_start | d = d_start | ||
Line 65: | Line 77: | ||
while d < d_end: | while d < d_end: | ||
dow = d.weekday() | dow = d.weekday() | ||
if dow < 5: | if dow < 5: | ||
print '%s,%s' % (d.strftime("%d/%m/%Y"), DoW[dow]) | print '%s,%s' % (d.strftime("%d/%m/%Y"), DoW[dow]) | ||
d += timedelta(days=1) | d += timedelta(days=1) | ||
#------------------------------------------------------------------------------- | #------------------------------------------------------------------------------- | ||
def | def generate_for_date_range(): | ||
dates = [] | dates = [] | ||
Line 90: | Line 104: | ||
global debug_level | global debug_level | ||
start_date = None | |||
end_date = None | |||
no_days = None | |||
try: | try: | ||
opts, args = getopt.getopt(argv, "dD:hn:vV?", | opts, args = getopt.getopt(argv, "dD:E:hn:S:t:vV?", | ||
["debug", "debug_level=", "help", "no_days=", "verbose", "version"]) | ["debug", "debug_level=", "end_date=", "help", | ||
"no_days=", "start_date=", "verbose", "version"]) | |||
except getopt.GetoptError: | except getopt.GetoptError: | ||
usage() | usage() | ||
return 2 | |||
for opt, arg in opts: | for opt, arg in opts: | ||
if opt in ("-d", "--debug"): | if opt in ("-d", "--debug"): | ||
debug_level | debug_level += 1 | ||
debug_flg = True | debug_flg = True | ||
elif opt in ('-D', "--debug_level"): | elif opt in ('-D', "--debug_level"): | ||
debug_level | debug_level = int(arg) | ||
elif opt in ('-n', "--no_days"): | |||
no_days = int(arg) | |||
elif opt in ("-t", "--target_date"): | elif opt in ("-t", "--target_date"): | ||
target_date = arg | target_date = datetime.strptime(arg, "%Y%m%d") | ||
elif opt in ("-E", "--end_date"): | |||
end_date = datetime.strptime(arg, "%Y%m%d") | |||
elif opt in ("-S", "--start_date"): | |||
start_date = datetime.strptime(arg, "%Y%m%d") | |||
elif opt in ("-h", "-?", "--help"): | elif opt in ("-h", "-?", "--help"): | ||
# print __doc__ | # print __doc__ | ||
usage() | usage() | ||
return 1 | return 1 | ||
elif opt in ("-v", "--verbose"): | elif opt in ("-v", "--verbose"): | ||
verbose_flg = True | verbose_flg = True | ||
if (debug_level > 0): print "Debugging level set to %d" % debug_level | if (debug_level > 0): print "Debugging level set to %d" % debug_level | ||
date_range(start=start_date, end=end_date, no_days=no_days) | |||
return 0 | return 0 | ||
Line 155: | Line 158: | ||
Date Who Description | Date Who Description | ||
-------- --- ------------------------------------------------------------ | -------- --- ------------------------------------------------------------ | ||
20060811 plh Initial implementation | |||
Problems to fix: | Problems to fix: |
Revision as of 13:26, 15 July 2008
Script
#!/usr/bin/env python # # Purpose: Generate date range # # $Id:$ # #------------------------------------------------------------------------------- """ Usage: gen_date_range.py [-v] [-d] [-D2] [-S <start_date> [-E <end_date>]] ... """ #------------------------------------------------------------------------------- import os import sys import getopt import logging #------------------------------------------------------------------------------- from datetime import datetime, timedelta #------------------------------------------------------------------------------- __version__ = "1.0.0" __id__ = "@(#) gen_date_range.py [%s] 2008-07-15" verbose_flg = False debug_level = 0 DoW = { 0 : 'Mon', 1 : 'Tue', 2 : 'Wed', 3 : 'Thu', 4 : 'Fri', 5 : 'Sat', 6 : 'Sun' } START = datetime(day=1, month=1, year=2007) #=============================================================================== def is_weekday(d): dow = d.weekday() if dow < 5: return True else: return False #------------------------------------------------------------------------------- def date_range(start=None, end=None, no_days=None): if start: d_start = start else: d_start = START if end: d_end = end elif no_days: d_end = d_start + timedelta(days=no_days) else: d_end = datetime.now() d = d_start while d < d_end: dow = d.weekday() if dow < 5: print '%s,%s' % (d.strftime("%d/%m/%Y"), DoW[dow]) d += timedelta(days=1) #------------------------------------------------------------------------------- def generate_for_date_range(): dates = [] for n in range(no_days): d = date - datetime.timedelta(days=n) print d.strftime("%y%m%d") dates.append(d) # print date.strftime("%y%m%d") for d in dates: generate(d) #=============================================================================== def main(argv): global verbose_flg global debug_level start_date = None end_date = None no_days = None try: opts, args = getopt.getopt(argv, "dD:E:hn:S:t:vV?", ["debug", "debug_level=", "end_date=", "help", "no_days=", "start_date=", "verbose", "version"]) except getopt.GetoptError: usage() return 2 for opt, arg in opts: if opt in ("-d", "--debug"): debug_level += 1 debug_flg = True elif opt in ('-D', "--debug_level"): debug_level = int(arg) elif opt in ('-n', "--no_days"): no_days = int(arg) elif opt in ("-t", "--target_date"): target_date = datetime.strptime(arg, "%Y%m%d") elif opt in ("-E", "--end_date"): end_date = datetime.strptime(arg, "%Y%m%d") elif opt in ("-S", "--start_date"): start_date = datetime.strptime(arg, "%Y%m%d") elif opt in ("-h", "-?", "--help"): # print __doc__ usage() return 1 elif opt in ("-v", "--verbose"): verbose_flg = True if (debug_level > 0): print "Debugging level set to %d" % debug_level date_range(start=start_date, end=end_date, no_days=no_days) return 0 #------------------------------------------------------------------------------- if __name__ == '__main__' or __name__ == sys.argv[0]: try: sys.exit(main(sys.argv[1:])) except KeyboardInterrupt, e: print "[gen_dates] Interrupted!" #------------------------------------------------------------------------------- """ Revision History: Date Who Description -------- --- ------------------------------------------------------------ 20060811 plh Initial implementation Problems to fix: To Do: Issues: """