Skip to content

string representation of a timedelta objects cannot be read by time class #135176

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

Closed
lenneman opened this issue Jun 5, 2025 · 5 comments
Closed
Labels
type-feature A feature request or enhancement

Comments

@lenneman
Copy link

lenneman commented Jun 5, 2025

Feature or enhancement

Proposal:

The __str__ method of the datetime.timediff class returns a string representation of a time difference in days, hours, minutes, seconds, microseconds. The representation of minutes and seconds is with a leading zero, which is fine. But the hours is not with a leading zero.
Example:

>>> tstart = datetime.now()
...
>>> tend = datetime.now()
>>> tend - tstart
datetime.timedelta(seconds=5, microseconds=737300)
>>> str(tend - tstart)
'0:00:05.737300'

The problem is now that the method fromisoformat of the datetime.time class does not recognize this string as a valid iso-format string because of the missing leading zero:

>>> str(tend-tstart)
'0:00:05.737300'
>>> time.fromisoformat(str(tend-tstart))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: Invalid isoformat string: '0:00:05.737300'

adding a leading zero works:

>>> time.fromisoformat('00:00:05.737300')
datetime.time(0, 0, 5, 737300)
>>> str(time.fromisoformat('00:00:05.737300'))
'00:00:05.737300'

interestingly the __str__ method of time has a leading zero for the hours.

So the __str__ method of timedelta shall add a leading zero as well to be conform to the ISO format.

Has this already been discussed elsewhere?

This is a minor feature, which does not need previous discussion elsewhere

Links to previous discussion of this feature:

No response

@lenneman lenneman added the type-feature A feature request or enhancement label Jun 5, 2025
@terryjreedy
Copy link
Member

Seems sensible. Do you wish to a PR? Comparing the 2 __str__ methods, assuming in Python, should make this easy.

@lenneman
Copy link
Author

lenneman commented Jun 5, 2025

I've no experience doing a PR, so I would prefer someone else does it.

@vstinner
Copy link
Member

vstinner commented Jun 5, 2025

Changing datetime.timedelta.__str__() can break a lot of projects. I would prefer to add a datetime.timedelta.isoformat() function.

@StanFromIreland
Copy link
Contributor

This will require changing the C implementation too I'm afraid.

@vstinner That issue has also had quite a lot of extensive discussion before:

@serhiy-storchaka
Copy link
Member

time and timedelta different types that represent different entities. They are not expected to have interoperable string representation.

>>> str(timedelta(seconds=1000000))
'11 days, 13:46:40'

As for adding methods to convert the string representation back to timedelta, there are already existing issues.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type-feature A feature request or enhancement
Projects
Development

No branches or pull requests

5 participants