On 02Feb2023 21:42, Thomas Mc Kay <thomas.d.mckay@gmail.com> wrote:
I was looking for a simple intuitive way to parse a timedelta from a string. The best I could find feels clunky at best: https://stackoverflow.com/questions/4628122/
Solution 1 looks like this: ``` from datetime import datetime, timedelta t = datetime.strptime('05:20:25', '%H:%M:%S') delta = timedelta(hours=t.hour, minutes=t.minute, seconds=t.second) ```
Looks ok, semanticly.
Solution 2 looks like this: ``` from datetime import datetime, timedelta t = datetime.strptime('05:20:25', '%H:%M:%S') delta = t - datetime.combine(t.date(), time.min) ```
Feels more indirect and less intuitive.
Solution 3 looks like this: ``` from datetime import timedelta import re match = re.match(r'(?P<hours>\d{2}):(?P<minutes>\d{2}):(?P<seconds>\d{2})', '05:20:25') delta = timedelta(**match.groupdict()) ```
Ugh.
Formatting back to strings is as verbose: ``` string = f'{delta.hours:02d}:{delta.minutes:02d}:{delta.seconds:02d}' ```
That's not too bad. I was hoping for funky `format()` support, but some experiments suggest not.
I think it would be nicer and more intuitive if timedelta had strptime and strftime like datetime has. ``` delta = timedelta.strptime('05:20:25', '%H:%M:%S') # timedelta(hours=5, minutes=20, seconds=25) string = delta.strftime('%H:%M:%S') # '05:20:25' ```
Aye, sounds nice.
The reason I would like this in the standard lib (vs maintaining my own helpers) is that : a) it feels very natural, especially after years of using these methods on datetime. b) there's currently three ways to do it that I've seen in the wild (documented above). Having this in the std lib would help with the ZoP "There should be one-- and preferably only one --obvious way to do it." (the "obvious" part of this statement is covered in (a)).
+1 Have you looked at arrow or dateutil? https://pypi.org/project/arrow/ https://pypi.org/project/python-dateutil/ They might have this kind of generality. I've used the above for some things. Personally, I _also_ keep my own helpers around, usually for special niche cases (more niche than your suggested `timedelta.strptime`): https://pypi.org/project/cs.dateutils/ https://github.com/cameron-simpson/css/blob/main/lib/python/cs/dateutils.py It's totally special to my personal stuff and is on PyPI basicly to support other more useful modules (so that `pip install`) can pull it in. Anyway, my point here is that you might well want to provide your own helpers. If nothing else it avoids reimplementing them every time you want them. _Also_, you could implement a `timedelta.strptime` and lobby for inclusion in the stdlib with an implementation to bolster your case. You could even monkey patch your method into the `timedelta` class at runtime :-) It may be that some of the difficulty is that `datetime.strptime` is a shim to the C library `strptime` (hence the platform dependent comment in the docs). But if you do an implementation which effectively uses `datetime.strptime` and just sanity checks the result for non-timedelta fields like timezones that might work well. Unsure. Cheers, Cameron Simpson <cs@cskk.id.au>