-
-
Notifications
You must be signed in to change notification settings - Fork 31.8k
Document how to format a timedelta
in human-readable form
#132642
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
The reason is because of normalization of timedelta objects. It's the expected (and documented) behavior, albeit surprsing. See https://docs.python.org/3/library/datetime.html#datetime.timedelta. The relevant paragraph is:
The representation is meant to be unique under the above constraints. |
However, I'm willing to consider the Note that it's possible to just recover the time delta in terms of (signed) hours by taking the corresponding components (namely, you don't need to parse the output, just use the attributes directly namely |
Even better, just do division on the result of I don't think def human_friendly_td_str(td):
if td.days >= 0:
return str(td)
return f'-({-td!s})' |
We could add this kind of recipe in the docs. I'm converting this into a docs issue first (adding more methods would require another discussion but I don't think we should only consider adding it to timedelta; other classes may need this but we'll need to decide first which ones). |
timedelta
in human-readable form
thx for reply quote i did tried terminal honestly, totally unexpected, although i did read py datetime docs and did catch that 'weird' conversion explanation consider this solved, my workaround does what i need best regards |
I still think we can make a docs improvement for that so I'll keep this one open. At least mention a recipe where we show how to make it more readable (we do have a small example of "surprsing" normalization so it's fine to have a recipe that follows IMO) |
in a current state, it would need be converted in any case, via string (my solution), or via something else what a user expects when it sees 'tzoffset', is -ve hours for western, & +ve hours for eastern longitudes while we are at it happy easter holidays godspeed |
@picnixz There are 1 (2) duplicates of this. |
Oh... right... I forgot about them. Anyway, we can still use this one for the docs. |
|
The issue isn't about whether they are fine or not, it's about the possible negative output which for a human is not necessarily useful (especially, if one needs to do -1 day + X hours) |
Bug description:
terminal : expected / correct
timenow : dt_utc_str : 2025-04-17 15:46:10
timenow : timezone : Europe/Vienna
timenow : dt_event_str : 2025-04-17 17:46:10
timenow : tzoffstr : 2:00:00
timenow : tz_offset_ : 2:00:00 | type : <class 'datetime.timedelta'>
timenow : tz_offset : 2.0
terminal : unexpected / wrong
timenow : dt_utc_str : 2025-04-17 15:46:36
timenow : timezone : America/Lima
timenow : dt_event_str : 2025-04-17 10:46:36
timenow : tzoffstr : -1 day, 19:00:00
timenow : tz_offset_ : -1 day, 19:00:00 | type : <class 'datetime.timedelta'>
timenow : tz_offset : -5.0
seems western longitudes return '-1 day, hh:mm:ss' offset
tested with usa, peru, chile, venezuela : all west longitude (-ve) return '-1 day...'
with slovenia, austria, china, australia : works ok, as expected, no 'day' in return
the workaround i am using (bottom of above py code) is working properly > tz_offset is correct
basically it is -24 (hours) + hh:mm:ss (as decimal number)
os : linux mint22 (latest, greatest)
python : 3.11.12 in virtual env (Python 3.11.12 (main, Apr 9 2025, 08:55:55) [GCC 13.3.0] on linux)
guest os (with dev venv) is running in virual machine on identical host (mint22)
have fun
CPython versions tested on:
3.11
Operating systems tested on:
Linux
The text was updated successfully, but these errors were encountered: