Intelligent strptime?

Jeff jam at quark.emich.edu
Wed Nov 10 18:49:26 EST 1999


On Wed, Nov 10, 1999 at 08:40:23PM +0100, Gerrit Holl wrote:
[..snipped discussion about how best to handle the 'various date format'
problem..]
> 
> It could return a tuple or something then. But if I want people to choose:
> either input "Nov, 23" or "12/6/99" or "1 December 2000", I don't want to
> check too much. There are much date processing programs (date, I think) that
> do this intelligent. You could also let the above think depend on the timezone,
> if it's an american timezone, take the american way, else, take the
> european way. But returning a tuple on 132 days seems better to me.
> 

I ported a small piece of code from the CVS sources into a python module
that returns the number of seconds since the Epoch instead of a tuple. I
called it 'getdate', and released it a while ago to my startship.python.net
account (ftp://starship.python.net/pub/crew/jam/). 

the module is comprised of one function that is able to act intelligently
when given various date formats like the ones you are describing and seems
to make the 'right' choice 'most' of the time. please try it out-- it might
suit your purposes nicely, especially if you are writing an interactive
application that accepts date input from a human user.

here is an example of this module in action:

[~] [6:36pm] [jam at toast-pts/3] % python
Python 1.5.2 (#1, Apr 18 1999, 16:03:16)  [GCC pgcc-2.91.60 19981201
(egcs-1.1.1  on linux2
Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam
>>> import getdate
>>> dir(getdate)
['_RCSID', '__doc__', '__file__', '__name__', 'error', 'getdate', 'parse']
>>> getdate.getdate("1 december 2000")
975646800L
>>> getdate.getdate("1999-12-01")
944024400L
>>> getdate.getdate("12/1/1999")
944024400L
>>> getdate.getdate("last friday 15:00 PST")
941842800L
>>> getdate.getdate("now")
942277099L
>>> import time
>>> time.strftime("%Y-%m-%d %H:%M %Z", time.localtime(getdate.getdate("now")))
'1999-11-10 18:39 EST'
>>> time.strftime("%Y-%m-%d %H:%M %Z", time.localtime(getdate.getdate("yesterday")))
'1999-11-09 18:39 EST'
>>> time.strftime("%Y-%m-%d %H:%M %Z", time.localtime(getdate.getdate("last friday 15:00 PST")))
'1999-11-05 18:00 EST'
>>> time.strftime("%Y-%m-%d %H:%M %Z", time.localtime(getdate.getdate("8pm")))
'1999-11-10 20:00 EST'

please let me know if there are any questions or concerns.

regards,
J
-- 
|| visit gfd <http://quark.emich.edu/>
|| psa member #293 <http://www.python.org/> 
|| New Image Systems & Services, Inc. <http://www.newimage.com/>




More information about the Python-list mailing list