Skip to content

fix: cron should still execute after changing the time back during daylight savings #966

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

Merged
merged 12 commits into from
Mar 26, 2025

Conversation

intcreator
Copy link
Collaborator

@intcreator intcreator commented Feb 24, 2025

Description

This PR will fix the issue where cron did not execute after changing the time back during daylight savings. It also replaces ts-jest with @swc/jest which is a little faster at running tests.

Related Issue

fixes #881

Motivation and Context

I ended up doing a big refactor to fix this issue because everything to do with time zones seemed too complicated to understand as is. I decided that rather than trying to decide based on what the time "should" be after a change compared to what it actually was from Luxon, we should just check if the UTC offset changed. That would indicate a daylight savings jump with very little work on our part. Then we could just check if a cron execution was supposed to happen in the gap we skipped.

Building on that idea, I decided to ultimately create a new fake cron time in UTC based on the timezoned time, do all the manipulations without worrying about time zones, and then convert back into the timezoned time and figure out any adjustments if needed.

As for #881 specifically, it looks like some of our date creations were ambiguous. According to the Luxon docs, you can sometimes create ambiguous times if you try to initialize a DateTime object in the middle of a daylight savings jumping back event. if the time jumps back and hour at 3 am and I create a DateTime at 2:30 am on that day, is it the first 2:30 am or the second 2:30 am? I switched some of our DateTime creations to UTC to make the time they represented unambiguous, and I added some checks to see if the previous hour was ambiguous or not before returning the time.

How Has This Been Tested?

Added a new test case, made sure previous test cases passed (and edited some to make them more clear as to the times being tested), and removed some test cases that tested functions I removed that we no longer needed.

Screenshots (if appropriate):

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)

Checklist:

  • My code follows the code style of this project.
  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • I have added tests to cover my changes.
  • All new and existing tests passed.
  • If my change introduces a breaking change, I have added a ! after the type/scope in the title (see the Conventional Commits standard).

@intcreator intcreator marked this pull request as draft February 24, 2025 19:14
@intcreator intcreator changed the title draft: fix: cron should still execute after changing the time back during daylight savings fix: cron should still execute after changing the time back during daylight savings Feb 24, 2025
@intcreator intcreator marked this pull request as ready for review March 5, 2025 19:53
@intcreator intcreator requested a review from sheerlox March 5, 2025 19:53
@intcreator
Copy link
Collaborator Author

I added a couple of custom types to get around a weird issue where Definitely Typed didn't provide all the subproperties on the Zone object that I needed. should those definitions go in one of the type files? they're only used in time.ts for now...

@intcreator intcreator requested a review from sheerlox March 13, 2025 18:52
Copy link
Collaborator

@sheerlox sheerlox left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm, great work!

sheerlox
sheerlox previously approved these changes Mar 26, 2025
Copy link
Collaborator

@sheerlox sheerlox left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm, great work!

@intcreator intcreator merged commit 8cf0712 into main Mar 26, 2025
18 checks passed
@intcreator intcreator deleted the intcreator/dst-gap branch March 26, 2025 19:24
@ncb000gt
Copy link
Member

🎉 This PR is included in version 4.1.1 🎉

The release is available on:

Your semantic-release bot 📦🚀

sheerlox pushed a commit to sheerlox/node-cron that referenced this pull request Mar 26, 2025
## [4.1.1](kelektiv/node-cron@v4.1.0...v4.1.1) (2025-03-26)

### 🐛 Bug Fixes

* cron should still execute after changing the time back during daylight savings ([kelektiv#966](kelektiv#966)) ([8cf0712](kelektiv@8cf0712)), closes [kelektiv#881](kelektiv#881) [kelektiv#881](kelektiv#881)

### ♻️ Chores

* **action:** update actions/setup-node action to v4.3.0 ([e70709f](kelektiv@e70709f))
* **action:** update actions/upload-artifact action to v4.6.1 ([06ed76c](kelektiv@06ed76c))
* **action:** update actions/upload-artifact action to v4.6.2 ([69ea222](kelektiv@69ea222))
* **action:** update github/codeql-action action to v3.28.10 ([1d14a08](kelektiv@1d14a08))
* **action:** update github/codeql-action action to v3.28.11 ([cd28d4f](kelektiv@cd28d4f))
* **action:** update github/codeql-action action to v3.28.13 ([154f885](kelektiv@154f885))
* **action:** update ossf/scorecard-action action to v2.4.1 ([6a4ec39](kelektiv@6a4ec39))
* **deps:** lock file maintenance ([6742c01](kelektiv@6742c01))
* **deps:** lock file maintenance ([a97cdb1](kelektiv@a97cdb1))
* **deps:** lock file maintenance ([c585973](kelektiv@c585973))
* **deps:** lock file maintenance ([e156aa7](kelektiv@e156aa7))
* **deps:** update dependency [@commitlint](https://github.com/commitlint)/cli to v19.8.0 ([3984884](kelektiv@3984884))
* **deps:** update dependency [@eslint](https://github.com/eslint)/js to v9.22.0 ([7415480](kelektiv@7415480))
* **deps:** update dependency [@eslint](https://github.com/eslint)/js to v9.23.0 ([00fc7ed](kelektiv@00fc7ed))
* **deps:** update dependency [@fast-check](https://github.com/fast-check)/jest to v2.1.0 ([a9a8608](kelektiv@a9a8608))
* **deps:** update dependency [@types](https://github.com/types)/node to v22.13.11 ([38cf6a6](kelektiv@38cf6a6))
* **deps:** update dependency [@types](https://github.com/types)/node to v22.13.5 ([a746320](kelektiv@a746320))
* **deps:** update dependency [@types](https://github.com/types)/node to v22.13.9 ([4ac339f](kelektiv@4ac339f))
* **deps:** update dependency lint-staged to v15.5.0 ([5efb27f](kelektiv@5efb27f))
* **deps:** update dependency prettier to v3.5.3 ([d8f2456](kelektiv@d8f2456))
* **deps:** update dependency sinon to v19.0.4 ([5144f4d](kelektiv@5144f4d))
* **deps:** update dependency ts-jest to v29.2.6 ([3625528](kelektiv@3625528))
* **deps:** update dependency typescript to v5.8.2 ([4ef66e8](kelektiv@4ef66e8))
* **deps:** update linters ([ecbe916](kelektiv@ecbe916))
* **deps:** update node.js to v23.10.0 ([kelektiv#970](kelektiv#970)) ([6775fff](kelektiv@6775fff))
* **deps:** update tests ([5d5e555](kelektiv@5d5e555))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Missing Cron Execution during DST change
3 participants