Intelligent Date & Time parsing

S

shakefu

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?
 
M

Mike Driscoll

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
 
S

shakefu

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
 
S

shakefu

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
 
S

shakefu

On Mar 7, 4:22 pm, (e-mail address removed) 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
 
C

Carl Banks

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
 
S

shakefu

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

Super sweet! I didn't even know you could do craziness like that in
python. Thanks for the advice!
 
S

shakefu

Django comes with some pretty handy filters for doing this sort of
formatting. Check out the "date", "now", "timesince" and "timeuntil"
filters here:

http://www.djangoproject.com/documentation/templates/#built-in-filter...

Jeffrey

Very cool - that's definitely handy to know for the output side of
things. I was mostly interested in writing a custom widget for
handling datetime input, 'cause I can't imagine anyone being studious
enough to use the 2008-03-07 12:00:00 format all the time... besides,
it's hard to type! I'd much rather allow for users to just be able to
type "12pm today".

So much to learn, so little time!

Jacob
 
C

castironpi

Very cool - that's definitely handy to know for the output side of
things. I was mostly interested in writing a custom widget for
handling datetime input, 'cause I can't imagine anyone being studious
enough to use the 2008-03-07 12:00:00 format all the time... besides,
it's hard to type! I'd much rather allow for users to just be able to
type "12pm today".

So much to learn, so little time!

Jacob

With some acquaintence with the user, a program can even honor,
"remind me 'later' to...".

Will you assign meanings to weird ambiguities like, 'last
February' (No, I mean laaaaaaast February.) if it's April and
"tomorrow" if it's 4 a.m.? Just raise an exception:
TimeOfDayException: "Yes, but it's 4 a.m." (or, Those probabilities
are close.)

The vocabulary for referring to time isn't that large. What did I
miss?

Yesterday, today, tomorrow, ago, from now, later, after, before,
[units], [absolutes], wikipedia.

But what's the elegant way to structure the expression?
 
S

shakefu

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'2008-03-04 00:00:00'

Thanks to everyone who helped!

Jacob
 
C

castironpi

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'?
 
C

castironpi

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

Ah excellent. I am looking at the part with:

static table const meridian_table[] =
{
{ "AM", tMERIDIAN, MERam },
...
{ "JANUARY", tMONTH, 1 },
{ "FEBRUARY", tMONTH, 2 },
...
{ "YEAR", tYEAR_UNIT, 1 },
{ "MONTH", tMONTH_UNIT, 1 },

(where is 'lunar month'? ;) )

How do you like it? Certainly Python cleans it up by a multiple, but
what about the rest?
 
E

Eddie Corns

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/
 
R

rockingred

On Mar 7, 4:22 pm, (e-mail address removed) 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!

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top