This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: datetime.time object incorrectly shows associated date in strftime() output
Type: behavior Stage: resolved
Components: Library (Lib) Versions: Python 3.7
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: Abhisek Maiti, belopolsky, p-ganssle
Priority: normal Keywords:

Created on 2019-09-10 07:03 by Abhisek Maiti, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 15882 closed pierreglaser, 2019-09-10 15:58
Messages (2)
msg351580 - (view) Author: Abhisek Maiti (Abhisek Maiti) Date: 2019-09-10 07:03
`
from datetime import time

t = time(13, 5, 45)
`
While `t.year`, `t.month` and `t.day` results into `AttributeError` the following:
`
t.strftime('%Y : %B : %d - %H : %M : %S')
`
generates:

`
1900 : January : 01 - 13 : 05 : 45
`
This is unexpected behavior since `datetime.time` object does not have those attributes. The documentation at https://docs.python.org/3/library/datetime.html specifically states that 

"
class datetime.time

    An idealized time, independent of any particular day, assuming that every day has exactly 24*60*60 seconds (there is no notion of “leap seconds” here). Attributes: hour, minute, second, microsecond, and tzinfo.
"
msg351601 - (view) Author: Paul Ganssle (p-ganssle) * (Python committer) Date: 2019-09-10 09:34
Hi Abhisek,

This is actually the expected / intended behavior, and it is documented under "strptime() and strftime() behavior": https://docs.python.org/3/library/datetime.html#strftime-strptime-behavior (which is linked to by the time.strftime documentation: https://docs.python.org/3/library/datetime.html#datetime.datetime.strftime . The relevant section is:

  For time objects, the format codes for year, month, and day should not be used, as time objects have no such values. If they’re used anyway, 1900 is substituted for the year, and 1 for the month and day.

If I were designing `datetime.time.strftime` from scratch, my instinct would be to throw an error in this case, but my philosophy on parsing interfaces tends towards the stricter side. At this point, I think it would do more harm than good to change this behavior. I imagine that the motivation is something like the Robustness Principle ( https://en.wikipedia.org/wiki/Robustness_principle ), but I wasn't involved in the original design so I can't be sure.

Thank you for taking the time to make a bug report, it's very appreciated even when it turns out to not be a bug.
History
Date User Action Args
2022-04-11 14:59:20adminsetgithub: 82263
2019-09-10 15:58:38pierreglasersetpull_requests: + pull_request15523
2019-09-10 09:34:09p-gansslesetstatus: open -> closed
resolution: not a bug
messages: + msg351601

stage: resolved
2019-09-10 07:36:11xtreaksetnosy: + belopolsky, p-ganssle
2019-09-10 07:03:53Abhisek Maiticreate