[Python-Dev] redux: fractional seconds in strptime
Skip Montanaro
skip at pobox.com
Thu Jan 13 22:56:19 CET 2005
A couple months ago I proposed (maybe in a SF bug report) that
time.strptime() grow some way to parse time strings containing fractional
seconds based on my experience with the logging module. I've hit that
stumbling block again, this time in parsing files with timestamps that were
generated using datetime.time objects. I hacked around it again (in
miserable fashion), but I really think this shortcoming should be addressed.
A couple possibilities come to mind:
1. Extend the %S format token to accept simple decimals that match
the re pattern "[0-9]+(?:\.[0-9]+)".
2. Add a new token that accepts decimals as above to avoid overloading
the meaning of %S.
3. Add a token that matches integers corresponding to fractional parts.
The Perl DateTime module uses %N to match nanoseconds (wanna bet that
was added by a physicist?). Arbitrary other units can be specified
by sticking a number between the "%" and the "N". I didn't see an
example, but I presume "%6N" would match integers that are
interpreted as microseconds.
The advantage of the third choice is that you can use anything as the
"decimal" point. The logging module separates seconds from their fractional
part with a comma for some reason. (I live in the USofA where decimal
points are usually represented by a period. I would be in favor of
replacing the comma with a locale-specific decimal point in a future version
of the logging module.) I'm not sure I like the optional exponent thing in
Perl's DateTime module but it does make it easy to interpret integers
representing fractions of a second when they occur without a decimal point
to tell you where it is.
I'm open to suggestions and will be happy to implement whatever is agreed
to.
Skip
More information about the Python-Dev
mailing list