-
-
Notifications
You must be signed in to change notification settings - Fork 32.1k
Add __replace__ protocol support to timedelta #135195
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
Comments
It makes sense to add a |
It was not implemented because ambiguous semantic. Let you have >>> from datetime import *
>>> td = timedelta(minutes=5, seconds=102)
>>> td
datetime.timedelta(seconds=402) What should |
I agree with @serhiy-storchaka. Let's close this issue. |
For context, my use case was for cleaning up the formatting time deltas to not have microsecond resolution when printing from a commandline tool. This either required truncating when the date time snapshots with
The public interface to I believe this design choice is the fundamental friction point when trying to add There's not a "normalized" public interface that is allows access to normalized units of time (e.g., days, hours, minutes, seconds, microseconds). In the current API design, I suspect people often misuse Given the backwards incompatibility constraints, I'm not sure there's a path forward on this. |
You can remove microseconds this way: >>> import datetime
>>> td=datetime.timedelta(weeks=10, seconds=3600, microseconds=123_456)
>>> datetime.timedelta(days=td.days, seconds=td.seconds) # remove microseconds
datetime.timedelta(days=70, seconds=3600) |
Uh oh!
There was an error while loading. Please reload this page.
Feature or enhancement
Proposal:
Requirements:
__replace__
support fortimedelta
.replace()
totimedelta
to be consistent withdate
anddatetime
Currently, both
date
anddatetime
have a.replace()
method and supports the__replace__
protocolFor unclear reasons,
timedelta
doesn't follow suit.Has this already been discussed elsewhere?
No response given
Links to previous discussion of this feature:
No response
The text was updated successfully, but these errors were encountered: