Pure Python implementation of strptime - strptime.py (0/1)

Drifty drifty at bigfoot.com
Wed May 23 04:20:55 EDT 2001


After discovering that time.strptime() did not run under Windows, I
decided to put together a quck Python version of the function for use
in another project of mine.  Well, one thing led to another and I
ended up with a pure Python module that checks data integrity and
fills in missing info when enough information is available.  It is
being released under the BSD license to the general public as version
1.0 today.

The module is available through
http://www.ocf.berkeley.edu/~bac/Askewed_Thoughts/HTML/code/index.php3#strptime
or directly from
http://www.ocf.berkeley.edu/~bac/Askewed_Thoughts/HTML/code/Python/toolkit/strptime/strptime.py
.  I have also attached it to this post.

A testing module is also available through the above page or directly
from
http://www.ocf.berkeley.edu/~bac/Askewed_Thoughts/HTML/code/Python/toolkit/strptime/strptime_test.py
.  It is my first attempt at using PyUnit for regression testing so
don't expect any miracles from it.  Simple tests for things that were
added after I started the module.  It is not as thorough as it could
be, but I am fairly confident that the module works as it should.

To use it, just run strptime.strptime().  The formal parameters go
(string, format, options, locale_setting).  String is the data string
that has the time info, format is the directive string that the data
string is formatted in, options takes in strptime.AS_IS,
strptime.CHECK, or strptime.FILL_IN.  AS_IS just gets what info it can
from the string and in no way touches that info.  CHECK checks to make
sure that the extrapolated info falls within an acceptable range; it
raises a DataError exception if it doesn't.  FILL_IN attempts to fill
in missing info when the proper information is available for the
calculation; it also forces the checking of the data integrity of the
info.  This currently includes figuring out the julian date (from
year, month, day), the month and day (from year and julian date), and
the day of the week from the year, month, and day.  locale_setting
takes in a specified language (currently English and Swedish) and
adjusts the locale-dependent "stuff" accordingly.  I have tried to
write good doc strings and hopefully those can answer any questions
you have that are not answered here.

Please let me know what you think.  I am open to suggestions on
improvements and new features and such.  Bug fixes are obviously
welcome as well as the info needed to add more locale settings for
other languages.

If the community-at-large thinks that this module is worthy, I am more
than willing to put the time and effort into trying to get this module
included in the language or at least out to who ever needs it.  If
that requires writing up a PEP proposal, so be it (although PEP 42
already requests it).  If that means submitting it to the Python
Cookbook, so be it.  I want to give back to the Python community any
way that I can; I hope this does in some way.

Just a quick thanks to Andrew Markebo for writing his pure Python
strptime hack.  It inspired me to make the locale setting option (and
provided me with the info needed to include Swedish in this initial
release).  And of course thanks go out to Guido van Rossum and every
other person who has contributed to Python in some way.  You all have
made programming fun for me.  Thanks.

-Brett Cannon



More information about the Python-list mailing list