X Tutup
The Wayback Machine - https://web.archive.org/web/20250617135930/https://github.com/python/cpython/issues/109964
Skip to content

datetime.datetime.strftime() and time.strftime() behave differently about %f directive #109964

Closed
@t-ubukata

Description

@t-ubukata

Bug report

Bug description:

strftime() and strptime() Behavior section says "date, datetime, and time objects all support a strftime(format) method, to create a string representing the time under the control of an explicit format string.", it means these 3 methods behaves as the same, and there is %f directive in the directive list. Otherwise, in time.strftime(format[, t]) section, there is no mention about %f directive in the directive list.

Abuout CPython, datetime.datetime.strftime() and time.strftime() actually behave differently about %f directive.

>>> from datetime import datetime
>>> datetime.now().strftime('%Y-%m-%dT%T.%fZ')
'2023-09-27T19:43:12.168908Z'
>>> import time
>>> time.strftime('%Y-%m-%dT%T.%fZ')
'2023-09-27T19:43:31.%fZ'

Former interprets %f directive, latter doesn't.
I reproduced this issue in CPython 3.10.12 and main branch 2897142, on Ubuntu 22.04.2 LTS.
I'm not sure about this is a documentation issue or CPython implementation issue, or both.

About CPython implementation, it looks like date.strftime() and time.strftime() call wrap_strftime(), and it interprets %f directive, so I couldn't find the cause. I couldn't find a datetime.datetime.strftime() implementation, like datetime_strftime().

About test, there are two tests of time.strftime. One is test_strftime in datetimetester.py, another one is test_strftime.py.
Both don't have a test about %f directive.

I think it's better to fix time.strftime() to interpret %f directive, and duplicated documentations and tests will be merged.

CPython versions tested on:

3.10

Operating systems tested on:

Linux

Metadata

Metadata

Assignees

No one assigned

    Labels

    stdlibPython modules in the Lib dirtype-bugAn unexpected behavior, bug, or error

    Projects

    Status

    Done

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      X Tutup