ariebovenberg / whenever

⏰ Modern datetime library for Python
https://whenever.rtfd.io
MIT License
898 stars 15 forks source link

Add `add_py_delta` method? #177

Open hongquan opened 1 month ago

hongquan commented 1 month ago

Because add can accept DateDelta / TimeDelta, I think it would be nice it has add_py_delta to accept std timedelta object. Otherwise, we have to convert it to whenever types before passing to add, which makes code verbose.

ariebovenberg commented 1 month ago

Hi @hongquan, thanks for posting this issue. Interesting idea. Although, instead of a new method, it'd be cleaner to simply make add() support timedelta. I don't have many issues with this, as timedelta and TimeDelta are very similar and this could ease migration from the standard library.

Out of curiousity—do you need this because you're migrating away from datetime, or do you have a different use case?

hongquan commented 1 month ago

@ariebovenberg I have this need because I have Django application, where all timedelta is read from PostgreSQL as std timedelta object.

ariebovenberg commented 1 month ago

Thanks for the context. Seems like a common use case.

Agree that it reduces noise in the code quite a bit:

dt.add(delta)
# vs
dt.add(TimeDelta.from_py_timedelta(delta))

One concern I do have is that dt.add(days=1) is in some cases not the same as dt.add(timedelta(days=1)), because the latter always assumes days are exactly 24 hours (which is not the case during DST transitions).