Skip to content

time.strftime does't work properly with format specifier %f on Windows #123788

@igor-sadchenko

Description

@igor-sadchenko

When using the time.strftime() function with the format string "%Y-%m-%dT%H:%M:%S.%f000Z" on Windows, the %f specifier for microseconds does not work as expected. The same format works correctly on Linux and macOS.

Example code:

import time
time.strftime("%Y-%m-%dT%H:%M:%S.%fZ", time.gmtime())

Expected behavior:

The %f specifier should return the microsecond part of the time when used in the time.strftime() format string, similar to how it works on Linux and macOS.

Example output on Linux/macOS:

2024-09-06T14:53:10.123000Z

Actual behavior on Windows:

On Windows, the %f specifier is not recognized by time.strftime(), and no microseconds are included in the output.

Example output on Windows:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: Invalid format string

Platform Information:

  • OS: Windows 10 (also tested on other Windows versions)
  • Python version: Affected on Python 3.x (tested on Python 3.8, 3.9, 3.10, 3.11, 3.12)

Additional information:

  • The %f specifier seems to work correctly on Unix-based systems (Linux/macOS).
  • The issue might be related to how the Windows implementation of time.strftime() interacts with the underlying system libraries.
  • This is inconsistent behavior across different platforms, and it would be ideal for %f to work consistently.

CPython versions tested on:

3.8, 3.9, 3.10, 3.11, 3.12

Operating systems tested on:

Linux, macOS, Windows

Metadata

Metadata

Assignees

No one assigned

    Labels

    type-bugAn unexpected behavior, bug, or error

    Projects

    Status

    Done

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions