By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
464,491 Members | 862 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 464,491 IT Pros & Developers. It's quick & easy.

Intelligent Date & Time parsing

P: n/a
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?
Mar 7 '08 #1
Share this Question
Share on Google+
10 Replies

P: n/a
On Mar 7, 4:08 pm, shak...@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?
There's the dateutil module that's a fancy wrapper for the datetime
module. The author's site has lots of examples as well as the source:

http://labix.org/python-dateutil

I use it quite a bit.

HTH

Mike
Mar 7 '08 #2

P: n/a
On Mar 7, 4:08 pm, shak...@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?
I forgot to say, thanks ahead of time, and I'd appreciate any
direction that you can give me! (How rude of me!)

- Jacob Alheid
Mar 7 '08 #3

P: n/a
On Mar 7, 4:10 pm, Mike Driscoll <kyoso...@gmail.comwrote:
On Mar 7, 4:08 pm, shak...@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?

There's the dateutil module that's a fancy wrapper for the datetime
module. The author's site has lots of examples as well as the source:

http://labix.org/python-dateutil

I use it quite a bit.

HTH

Mike
Holy Crap that was fast. I'll check it out - thanks for the help!

Jacob
Mar 7 '08 #4

P: n/a
On Mar 7, 4:10 pm, Mike Driscoll <kyoso...@gmail.comwrote:
On Mar 7, 4:08 pm, shak...@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?

There's the dateutil module that's a fancy wrapper for the datetime
module. The author's site has lots of examples as well as the source:

http://labix.org/python-dateutil

I use it quite a bit.

HTH

Mike
So close - I tried it in the interpreter it works great for most
things (like "10pm tuesday") but I'm really hoping for something
that'll work with generics like "tomorrow", "yesterday", "last
tuesday", "next month", etc. Although I know that's pretty language
specific. I was just sort of wishing someone had written it before
me... maybe? Possibly?

Although if I end up writing my own I'll sure use
datetime.parser.parse to get everything left over once you remove
strings like "this saturday". So it helps a little!

Jacob

Mar 7 '08 #5

P: n/a
On Mar 7, 4:22 pm, shak...@gmail.com wrote:
[snip]
Although if I end up writing my own I'll sure use
datetime.parser.parse to get everything left over once you remove
I mean dateutil.parser.parse. Tomorrow I'll have to leave off the last
Lunchtime Guiness.
strings like "this saturday". So it helps a little!

Jacob
And on the same thought - does anyone know of a good website, resource
or community that's got intelligible descriptions of all the different
modules that are generally available? I know if I tab complete 'apt-
get install python-*' it tries to list 1000-some possibilities, so I'm
thinking there's quite a few modules out there that I might like to
know about and use...

Thanks!
Jacob
Mar 7 '08 #6

P: n/a
I figured I might as well share the code I ended up using, in case
anyone else wants an easy way to get strings to, for instance, SQL-
storable datetimes.

jake@house:~$ cat test.py
#!/usr/bin/python
from datetime import datetime
import subprocess

def parsedate( date ):
p = subprocess.Popen(['date','+%s.%N',"--date=%s" %
date],stdout=subprocess.PIPE)
out = float(p.stdout.read())
return "%s" % datetime.fromtimestamp(out)

jake@house:~$ python -i test.py
>>parsedate("today")
'2008-03-07 21:20:31.870489'
>>parsedate("tomorrow")
'2008-03-08 21:20:40.516243'
>>parsedate("next monday")
'2008-03-10 00:00:00'
>>parsedate("10pm last week")
'2008-02-29 22:00:00'
>>parsedate("last tuesday")
'2008-03-04 00:00:00'

Thanks to everyone who helped!

Jacob
Mar 8 '08 #7

P: n/a
On Mar 7, 9:23*pm, shak...@gmail.com wrote:
I figured I might as well share the code I ended up using, in case
anyone else wants an easy way to get strings to, for instance, SQL-
storable datetimes.

jake@house:~$ cat test.py
#!/usr/bin/python
from datetime import datetime
import subprocess

def parsedate( date ):
* * p = subprocess.Popen(['date','+%s.%N',"--date=%s" %
date],stdout=subprocess.PIPE)
* * out = float(p.stdout.read())
* * return "%s" % datetime.fromtimestamp(out)

jake@house:~$ python -i test.py>>parsedate("today")

'2008-03-07 21:20:31.870489'>>parsedate("tomorrow")

'2008-03-08 21:20:40.516243'>>parsedate("next monday")

'2008-03-10 00:00:00'>>parsedate("10pm last week")

'2008-02-29 22:00:00'>>parsedate("last tuesday")

'2008-03-04 00:00:00'
I am a GNU newbie. (I know C &o.) Can you point me to a place to
find the source for 'date'?
Mar 8 '08 #8

P: n/a
I am a GNU newbie. (I know C &o.) Can you point me to a
place to find the source for 'date'?
It's part of the GNU Coreutils:

http://ftp.gnu.org/gnu/coreutils/

Within the file, you're likely interested in lib/getdate.*

It helps if you have a working knowledge of Yacc.

-tkc
Mar 8 '08 #9

P: n/a
sh*****@gmail.com writes:
>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?
http://code-bear.com/code/parsedatetime/
Mar 10 '08 #10

P: n/a
On Mar 7, 5:26*pm, shak...@gmail.com wrote:
On Mar 7, 4:22 pm, shak...@gmail.com wrote:
[snip]Although if I end up writing my own I'll sure use
datetime.parser.parse to get everything left over once you remove

I mean dateutil.parser.parse. Tomorrow I'll have to leave off the last
Lunchtime Guiness.
strings like "this saturday". So it helps a little!
Jacob

And on the same thought - does anyone know of a good website, resource
or community that's got intelligible descriptions of all the different
modules that are generally available? I know if I tab complete 'apt-
get install python-*' it tries to list 1000-some possibilities, so I'm
thinking there's quite a few modules out there that I might like to
know about and use...

Thanks!
Jacob
Jacom, the http://www.python.org/ site offers most of the modules,
including an index: http://pypi.python.org/pypi

Then there's the ASPN site:

http://aspn.activestate.com/ASPN/Downloads/ActivePython

Mar 10 '08 #11

This discussion thread is closed

Replies have been disabled for this discussion.