[Python-Dev] redux: fractional seconds in strptime

Brett C. bac at OCF.Berkeley.EDU
Fri Jan 14 05:16:16 CET 2005


Skip Montanaro wrote:
> A couple months ago I proposed (maybe in a SF bug report)

http://www.python.org/sf/1006786

  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 problem I have always had with this proposal is that the value is 
worthless, time tuples do not have a slot for fractional seconds.  Yes, it 
could possibly be changed to return a float for seconds, but that could 
possibly break things.

My vote is that if something is added it be like %N but without the optional 
optional digit count.  This allows any separator to be used while still 
consuming the digits.  It also doesn't suddenly add optional args which are not 
supported for any other directive.

-Brett


More information about the Python-Dev mailing list