[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.python

Intelligent Date & Time parsing

shakefu

3/7/2008 10:08:00 PM

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?
19 Answers

Mike Driscoll

3/7/2008 10:10:00 PM

0

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/pytho...

I use it quite a bit.

HTH

Mike

shakefu

3/7/2008 10:11:00 PM

0

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

shakefu

3/7/2008 10:12:00 PM

0

On Mar 7, 4:10 pm, Mike Driscoll <kyoso...@gmail.com> wrote:
> 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/pytho...
>
> I use it quite a bit.
>
> HTH
>
> Mike

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

Jacob

shakefu

3/7/2008 10:22:00 PM

0

On Mar 7, 4:10 pm, Mike Driscoll <kyoso...@gmail.com> wrote:
> 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/pytho...
>
> 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

shakefu

3/7/2008 10:27:00 PM

0

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

Carl Banks

3/7/2008 10:33:00 PM

0

On Mar 7, 5: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?

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

Jeffrey Froman

3/7/2008 10:36:00 PM

0

shakefu@gmail.com 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

shakefu

3/7/2008 10:41:00 PM

0

On Mar 7, 4:33 pm, Carl Banks <pavlovevide...@gmail.com> wrote:
> On Mar 7, 5: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?
>
> 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!

shakefu

3/7/2008 11:01:00 PM

0

On Mar 7, 4:35 pm, Jeffrey Froman <jeff...@fro.man> wrote:
> shak...@gmail.com 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......
>
> 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

Aaron Brady

3/8/2008 1:05:00 AM

0

On Mar 7, 5:00 pm, shak...@gmail.com wrote:
> On Mar 7, 4:35 pm, Jeffrey Froman <jeff...@fro.man> wrote:
>
> > shak...@gmail.com 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......
>
> > 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

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?