Difference between revisions of "GenDates.py"
Jump to navigation
Jump to search
PeterHarding (talk | contribs) (New page: =Script= <pre> #!/usr/bin/env python # # Purpose: # # $Id:$ # #------------------------------------------------------------------------------- """ Skeleton Python script ... """ ...) |
PeterHarding (talk | contribs) |
||
Line 4: | Line 4: | ||
#!/usr/bin/env python | #!/usr/bin/env python | ||
# | # | ||
# Purpose: | # Purpose: Generate a date range | ||
# | # | ||
# $Id:$ | # $Id:$ | ||
Line 11: | Line 11: | ||
""" | """ | ||
Usage: GenDates.py [-v] [-d] [-D2] [-S <start_date> [-E <end_date>]] | |||
... | ... | ||
Line 20: | Line 20: | ||
import sys | import sys | ||
import getopt | import getopt | ||
#------------------------------------------------------------------------------- | #------------------------------------------------------------------------------- | ||
Line 65: | Line 64: | ||
while d < d_end: | while d < d_end: | ||
dow = d.weekday() | |||
print '%s,%s' % (d.strftime("%d/%m/%Y"), DoW[ | if dow < 5: | ||
print '%s,%s' % (d.strftime("%d/%m/%Y"), DoW[dow]) | |||
d += timedelta(days=1) | d += timedelta(days=1) | ||
Revision as of 12:47, 15 July 2008
Script
#!/usr/bin/env python # # Purpose: Generate a date range # # $Id:$ # #------------------------------------------------------------------------------- """ Usage: GenDates.py [-v] [-d] [-D2] [-S <start_date> [-E <end_date>]] ... """ #------------------------------------------------------------------------------- import os import sys import getopt #------------------------------------------------------------------------------- from datetime import datetime, timedelta #------------------------------------------------------------------------------- __version__ = "1.0.0" __id__ = "@(#) gen_dates.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' } #=============================================================================== def is_weekday(d): dow = d.weekday() if dow < 5: return True else: return False #------------------------------------------------------------------------------- def gen_dates(): d_start = datetime(day=1, month=1, year=2008) d_end = datetime(day=5, month=5, year=2008) d_now = 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 gen_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 # try: # terminal_type = os.environ["TERM"] # except KeyError, e: # print "Set TERM environment variable and rerun!" # return 1 # wrk_path = os.getcwd() # wrk_dir = os.path.basename(wrk_path) # print "[gen_dates] Working directory is %s" % wrk_path # data_dir = wrk_dir + '/DATA/' # pid_path = data_dir + PIDFILE # os.chdir(data_dir) try: opts, args = getopt.getopt(argv, "dD:hn:vV?", ["debug", "debug_level=", "help", "no_days=", "verbose", "version"]) except getopt.GetoptError: usage() sys.exit(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(a) elif opt in ("-t", "--target_date"): target_date = arg date = datetime.datetime.strptime(target_date, "%Y%m%d") print date elif opt in ("-h", "-?", "--help"): # print __doc__ usage() return 1 elif opt in ("-n", "--no_days"): no_days = int(arg) elif opt in ("-v", "--verbose"): verbose_flg = True if (debug_level > 0): print "Debugging level set to %d" % debug_level #init() gen_dates() 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 -------- --- ------------------------------------------------------------ 20031014 plh Initial implementation Problems to fix: To Do: Issues: """