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: isoformat function drops microseconds part if its value is 000000
Type: behavior Stage: resolved
Components: Library (Lib) Versions: Python 3.9, Python 3.8, Python 3.7, Python 3.6, Python 3.5, Python 2.7
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: Alexander Bolshakov, SilentGhost, belopolsky, p-ganssle, xtreak
Priority: normal Keywords:

Created on 2020-03-26 14:14 by Alexander Bolshakov, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Messages (3)
msg365077 - (view) Author: Alexander Bolshakov (Alexander Bolshakov) Date: 2020-03-26 14:14
isoformat function does not conform to the ISO 8601 and drops microseconds part if its value is 000000.

The issue can be reproduced using the following code snippet:

for i in range(1,10000000):
     timestamp=datetime.datetime.utcnow().isoformat()
     if len(timestamp)!=26:
         print(timestamp)
msg365089 - (view) Author: Karthikeyan Singaravelan (xtreak) * (Python committer) Date: 2020-03-26 16:14
Does timespec fulfill this use case to always return microseconds?

https://docs.python.org/3/library/datetime.html#datetime.datetime.isoformat

Return a string representing the date and time in ISO 8601 format:
    YYYY-MM-DDTHH:MM:SS.ffffff, if microsecond is not 0
    YYYY-MM-DDTHH:MM:SS, if microsecond is 0

The optional argument timespec specifies the number of additional components of the time to include (the default is 'auto'). It can be one of the following:

    'auto': Same as 'seconds' if microsecond is 0, same as 'microseconds' otherwise.
    'hours': Include the hour in the two-digit HH format.
    'minutes': Include hour and minute in HH:MM format.
    'seconds': Include hour, minute, and second in HH:MM:SS format.
    'milliseconds': Include full time, but truncate fractional second part to milliseconds. HH:MM:SS.sss format.
    'microseconds': Include full time in HH:MM:SS.ffffff format.



./python
Python 3.9.0a4+ (heads/master:6723e933c4, Mar 21 2020, 06:54:01) 
[GCC 7.5.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import datetime
>>> datetime.datetime(1, 1, 1).isoformat(timespec='auto')
'0001-01-01T00:00:00'
>>> datetime.datetime(1, 1, 1).isoformat(timespec='microseconds')
'0001-01-01T00:00:00.000000'
msg365111 - (view) Author: Paul Ganssle (p-ganssle) * (Python committer) Date: 2020-03-26 21:11
> isoformat function does not conform to the ISO 8601 and drops microseconds part if its value is 000000.

I'm not sure why you think that this does not conform to ISO 8601 - ISO 8601 is a sprawling beast of a spec and allows some crazy formats. Some examples of perfectly valid ISO 8601 strings:

--03-26
2020-W13-4T03
2020-03-26T03.5
2020-03-26T03,5
2020-03-26T03:30:40.334


There are *hundreds* of valid formats encompassed by ISO 8601.

Anyway, that's an aside. The behavior of .isoformat() is pretty clearly documented. These are the first three line of the documentation:

    Return a string representing the date and time in ISO 8601 format:

      - YYYY-MM-DDTHH:MM:SS.ffffff, if microsecond is not 0
      - YYYY-MM-DDTHH:MM:SS, if microsecond is 0

I believe Karthikeyan has adequately explained how to get the behavior you want, so I am going to go ahead and close this as working as intended.
History
Date User Action Args
2022-04-11 14:59:28adminsetgithub: 84257
2020-03-26 21:11:20p-gansslesetmessages: + msg365111
2020-03-26 21:07:35Alexander Bolshakovsetstatus: open -> closed
resolution: not a bug
stage: resolved
2020-03-26 21:03:20SilentGhostsetnosy: + SilentGhost
2020-03-26 16:14:20xtreaksetnosy: + xtreak
messages: + msg365089
2020-03-26 16:03:51xtreaksetnosy: + belopolsky, p-ganssle
2020-03-26 14:14:48Alexander Bolshakovcreate