Intelligent Date & Time parsing
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://mail.python.org/mailman/listinfo/python-list
Re: Intelligent Date & Time parsing
On Mar 7, 4:08 pm, [EMAIL PROTECTED] 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 -- http://mail.python.org/mailman/listinfo/python-list
Re: Intelligent Date & Time parsing
On Mar 7, 4:10 pm, Mike Driscoll <[EMAIL PROTECTED]> wrote: > On Mar 7, 4:08 pm, [EMAIL PROTECTED] 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 -- http://mail.python.org/mailman/listinfo/python-list
Re: Intelligent Date & Time parsing
On Mar 7, 4:10 pm, Mike Driscoll <[EMAIL PROTECTED]> wrote: > On Mar 7, 4:08 pm, [EMAIL PROTECTED] 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 -- http://mail.python.org/mailman/listinfo/python-list
Re: Intelligent Date & Time parsing
On Mar 7, 4:22 pm, [EMAIL PROTECTED] 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 -- http://mail.python.org/mailman/listinfo/python-list
Re: Intelligent Date & Time parsing
On Mar 7, 4:33 pm, Carl Banks <[EMAIL PROTECTED]> wrote: > On Mar 7, 5:08 pm, [EMAIL PROTECTED] 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 Super sweet! I didn't even know you could do craziness like that in python. Thanks for the advice! -- http://mail.python.org/mailman/listinfo/python-list
Re: Intelligent Date & Time parsing
On Mar 7, 4:35 pm, Jeffrey Froman <[EMAIL PROTECTED]> wrote: > [EMAIL PROTECTED] wrote: > > 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. > > 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 -- http://mail.python.org/mailman/listinfo/python-list
Re: Intelligent Date & Time parsing
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.
[EMAIL PROTECTED]:~$ 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)
[EMAIL PROTECTED]:~$ 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
--
http://mail.python.org/mailman/listinfo/python-list
