Fix daylight saving time issue in shift function
Pull Request Checklist
Thank you for taking the time to improve Arrow! Before submitting your pull request, please check all appropriate boxes:
- [x] 🧪 Added tests for changed code.
- [x] 🛠️ All tests pass when run locally (run
toxormake testto find out!). - [x] 🧹 All linting checks pass when run locally (run
tox -e lintormake lintto find out!). - [x] 📚 Updated documentation for changed code.
- [x] ⏩ Code is up-to-date with the
masterbranch.
If you have any questions about your code changes or any of the points above, please submit your questions along with the pull request and we will try our best to help!
Description of Changes
Fixes #1170: Correct DST Handling in shift Function
This PR addresses the issue with incorrect daylight saving time (DST) handling when using the shift function in the Arrow library.
Changes:
- Modified the
shiftfunction to correctly handle transitions during DST changes. - Added logic to detect positive or negative offset changes when shifting across DST boundaries.
- Adjusted the behavior during spring-forward (to skip the missing hour) and fall-back (to use the second occurrence of a repeated hour).
Issue Link:
Closes: #1170
Explanation:
Previously, the shift function did not account for the changes in offsets that occur during DST transitions, leading to unexpected behavior when shifting times. This fix ensures that:
- For spring-forward transitions, any shifts that would land in the missing hour are adjusted forward.
- For fall-back transitions, the repeated hour is handled properly by using the
foldattribute to select the second occurrence.
Testing:
- Added tests for time shifts that cross DST start and end dates to verify the behavior.
- Ensured consistency when using
timedeltashifts and repeated shifts.
Hi @Shaileshsaravanan thanks for the PR. Mind getting the builds and tests passing? Then we can review :)
@jadchaar
Arrow uses the resolve_imaginary function from dateutil library to handle DST offset. That seems to be the problem here because it only fix the offset if the time is invalid. For example, on 2023-03-12T02:00:00-07:00 in America/Los_Angeles, this time does not exist due to the DST change. In this case resolve_imaginary will correct the time. But if shift function directly shift the time to valid time then this function not going to make any changes
Look into the code for reference https://github.com/dateutil/dateutil/blob/9eaa5de584f9f374c6e4943069925cc53522ad61/src/dateutil/tz/tz.py#L1763