-
-
Notifications
You must be signed in to change notification settings - Fork 31.7k
Add to/from string methods to datetime.timedelta #85426
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
I suggest adding datetime.timedelta methods that convert to/from str. The reason is that I have several places where configuration contains various timeouts. I'd like to write '50ms' and not 0.05 which is more human readable. See https://golang.org/pkg/time/#ParseDuration for how Go does it. |
I don't know how much support this will get since there is already a str(timedelta) operation defined with a different format. But I don't like that format much. The day[s] part is too verbose, the H:MM:SS part could too easily be interpreted as D:HH:MM, and the result for negative deltas is horrible (-43 days, 23:59:55; see bpo-38701). My favourite format for a duration is usually the HTML 5 format like 1w 0d 12h 0m 27s Another option is ISO 8601 format: P1W0DT12H0M27S But both of these standards only go down to the seconds unit, so 50 ms has to be 0.05s or PT0.05S. |
I think the lack sub second parts in these formats is a disadvantage. |
I think it is unlikely that we'll want to experiment with this directly in CPython. I don't think a fixed format (other than the annoying one that you already get from calling It is annoyingly complicated to do this, and I'd rather it be tried out in other libraries with more flexibility to make breaking changes and a shorter release cadence. Dateutil is a good choice, but a clear and thorough proposal (or at least examples of this done well in other ecosystems we can crib from) is necessary. |
I think it would be nice to have a from str method that could reverse the behavior of str(timedelta). I'm trying to parse files that contain the output of str(timedelta), and there's no easy way to get them back to timedelta's |
On second thought it may be better to close the other one instead. This needs the extension-modules label ;-) |
I think the other has more discussion so I don't know why we should close the other one. The disgressions were needed IMO. In addition, the other one is focused on |
I originally though of that, but I realized most of the discussion is off topic. (3rd party implementation links really, and the approach itself did not receive much support.) Same request, its just the other is proposes a certain way of doing it, if they were implemented it would close this. See the second post here. |
3rd-party implementations are useful because they give you a precedent. So I think it's better to keep the other one rather than this one. |
You are the core dev, I guess it's up to you anyway, close whichever one you think should be. |
I think we can keep both opened because they are still different. Having |
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:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: