Add millisecond formatting support to datetime.strftime

I’ve done some more thinking. It looks a bit contradictory to have

  • %Nf means “take the integer %f (microsecond number) and truncate to the highest N of 6 digits”
  • %Nm means “take the integer %m (month number) and pad to N digits”

but also %.Nf instead of %Nf is weird, arguably worse, because

  1. . is not used in any strftime(3) specifiers (I was confused with *printf(3)*, my bad :sweat_smile: ).
  2. Even if taking inspiration from printf(3) for this thread, microseconds is not a float (which printf(3)'s and Python’s precision . truncates to N digits) but rather it is an integer, which printf(3)'s (and Python’s upcoming PEP 786’s) . pads to a minimum number of digits, whereas we want to format to a fixed number of digits (6 for microseconds) and then truncated to the highest N.

Either syntax %Nf or %.Nf is going to be exceptional because of the niche task of formatting an integer to 6 digits, then truncating to the highest N.

However, %f is not part of strftime(3). It’s explicitly a Python extension. So either syntax we choose we don’t have to worry about it ‘breaking’ strftime(3). I think it reduces to just a question of “which syntax looks nicer / is most intuitive?”.

I think I prefer the %Nf syntax based off the understandable questions of

etc, firstly from a visual analysis to stop confusion

> “there’s a . in the specifier, where is the . in the output?”

and secondly as @jsbueno points out Rust’s strftime has different behavior for %.3f and %3f, and we want the behavior of the latter.


TLDR: I retract my %.Nf inquiry and I’m +1 on %Nf.

2 Likes