I want to pass a datetime value into my python script on the command line. My first idea was to use optparse and pass the value in as a string, then use datetime.strptime to convert it to a datetime. This works fine on my machine (python 2.6), but I also need to run this script on machines that are running python 2.4, which doesn't have datetime.strptime.
How can I pass the datetime value to the script in python 2.4?
Here's the code I'm using in 2.6:
parser = optparse.OptionParser()
parser.add_option("-m", "--max_timestamp", dest="max_timestamp",
help="only aggregate items older than MAX_TIMESTAMP",
metavar="MAX_TIMESTAMP(YYYY-MM-DD HH24:MM)")
options,args = parser.parse_args()
if options.max_timestamp:
# Try parsing the date argument
try:
max_timestamp = datetime.datetime.strptime(options.max_timestamp, "%Y-%m-%d %H:%M")
except:
print "Error parsing date input:",sys.exc_info()
sys.exit(1)