GenDates.py

From PeformIQ Upgrade
Revision as of 17:07, 19 July 2009 by PeterHarding (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

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:

"""