Intelligent Date & Time parsing

Carl Banks pavlovevidence at gmail.com
Fri Mar 7 23:33:20 CET 2008


On Mar 7, 5:08 pm, shak... at gmail.com wrote:
> I'm new to python and I was wondering if there are any intelligent
> date/time parsing modules out there. I've looked at strptime (or
> whichever it is) and mxDateTime from the eGenix package. I need
> something to parse user input for a django app, and it's awesome to be
> able to write "last monday", "a year ago", or "10pm tuesday" like
> PHP's strtotime.
>
> So are there any modules that allow for that kind of parsing?

GNU date can do a lot of these things--if your Django server is
running Linux it probably has GNU date installed.  (Might not be
practical to start a new process in the middle of a query, especially
a lot of them.)

>From a shell command line, get the current time (in seconds since
epoch, which you can pass to Python time functions) this way:

date +"%s.%N"

And you can enter all sort of relative and absolute date strings:

date +"%s.%N" --date='last monday'
date +"%s.%N" --date='a year ago'
date +"%s.%N" --date='10pm tuesday'

These all apparently work.  Now all you have to do is call it from
Python using subprocess module and you're set.  (DO NOT use os.system
for this since it starts a shell process, which is unnecessarily slow,
and dangerous when passed user input.)


Carl Banks



More information about the Python-list mailing list