Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

isoformat function drops microseconds part if its value is 000000 #84257

Closed
AlexanderBolshakov mannequin opened this issue Mar 26, 2020 · 3 comments
Closed

isoformat function drops microseconds part if its value is 000000 #84257

AlexanderBolshakov mannequin opened this issue Mar 26, 2020 · 3 comments
Labels
3.7 (EOL) end of life 3.8 only security fixes 3.9 only security fixes stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error

Comments

@AlexanderBolshakov
Copy link
Mannequin

AlexanderBolshakov mannequin commented Mar 26, 2020

BPO 40076
Nosy @abalkin, @pganssle, @tirkarthi

Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

Show more details

GitHub fields:

assignee = None
closed_at = <Date 2020-03-26.21:07:35.837>
created_at = <Date 2020-03-26.14:14:48.206>
labels = ['type-bug', '3.8', '3.9', 'invalid', '3.7', 'library']
title = 'isoformat function drops microseconds part if its value is 000000'
updated_at = <Date 2020-03-26.21:11:20.750>
user = 'https://bugs.python.org/AlexanderBolshakov'

bugs.python.org fields:

activity = <Date 2020-03-26.21:11:20.750>
actor = 'p-ganssle'
assignee = 'none'
closed = True
closed_date = <Date 2020-03-26.21:07:35.837>
closer = 'Alexander Bolshakov'
components = ['Library (Lib)']
creation = <Date 2020-03-26.14:14:48.206>
creator = 'Alexander Bolshakov'
dependencies = []
files = []
hgrepos = []
issue_num = 40076
keywords = []
message_count = 3.0
messages = ['365077', '365089', '365111']
nosy_count = 5.0
nosy_names = ['belopolsky', 'SilentGhost', 'p-ganssle', 'xtreak', 'Alexander Bolshakov']
pr_nums = []
priority = 'normal'
resolution = 'not a bug'
stage = 'resolved'
status = 'closed'
superseder = None
type = 'behavior'
url = 'https://bugs.python.org/issue40076'
versions = ['Python 2.7', 'Python 3.5', 'Python 3.6', 'Python 3.7', 'Python 3.8', 'Python 3.9']

@AlexanderBolshakov
Copy link
Mannequin Author

AlexanderBolshakov mannequin commented Mar 26, 2020

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)

@AlexanderBolshakov AlexanderBolshakov mannequin added 3.7 (EOL) end of life 3.8 only security fixes 3.9 only security fixes stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error labels Mar 26, 2020
@tirkarthi
Copy link
Member

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'

@AlexanderBolshakov AlexanderBolshakov mannequin closed this as completed Mar 26, 2020
@AlexanderBolshakov AlexanderBolshakov mannequin added the invalid label Mar 26, 2020
@AlexanderBolshakov AlexanderBolshakov mannequin closed this as completed Mar 26, 2020
@AlexanderBolshakov AlexanderBolshakov mannequin added the invalid label Mar 26, 2020
@pganssle
Copy link
Member

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.

@ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
3.7 (EOL) end of life 3.8 only security fixes 3.9 only security fixes stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

2 participants