datetime command line argument in python 2.4

Posted by Ike Walker on Stack Overflow See other posts from Stack Overflow or by Ike Walker
Published on 2010-03-11T21:16:13Z Indexed on 2010/03/11 21:19 UTC
Read the original article Hit count: 299

Filed under:
|

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)

© Stack Overflow or respective owner

Related posts about python

Related posts about datetime