Skip to content

Commit

Permalink
fix month duration calculation error (#295)
Browse files Browse the repository at this point in the history
* fix month duration calculation error

* kick off actions

* Update scheduler.go

Co-authored-by: John Roesler <johnrroesler@gmail.com>
  • Loading branch information
xwjdsh and JohnRoesler authored Feb 16, 2022
1 parent 2d070e5 commit 2319102
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 1 deletion.
2 changes: 1 addition & 1 deletion scheduler.go
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,7 @@ func calculateNextRunForMonth(s *Scheduler, job *Job, lastRun time.Time, dayOfMo
next = next.AddDate(0, job.interval, -0)
next = next.Add(-difference)
} else {
if job.interval == 1 { // every month counts current month
if job.interval == 1 && !jobDay.Equal(lastRun) { // every month counts current month
next = next.AddDate(0, job.interval-1, 0)
} else { // should run next month interval
next = next.AddDate(0, job.interval, 0)
Expand Down
1 change: 1 addition & 0 deletions scheduler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -891,6 +891,7 @@ func TestScheduler_CalculateNextRun(t *testing.T) {
{name: "every month at day should consider at days", job: &Job{interval: 1, unit: months, daysOfTheMonth: []int{2}, lastRun: januaryFirst2020At(0, 0, 0)}, wantTimeUntilNextRun: 1 * day},
{name: "every month at day should consider at hours", job: &Job{interval: 1, unit: months, atTime: _getHours(9) + _getMinutes(30), lastRun: januaryFirst2020At(0, 0, 0)}, wantTimeUntilNextRun: 31*day + _getHours(9) + _getMinutes(30)},
{name: "every month on the first day, but started on january 8th, should run February 1st", job: &Job{interval: 1, unit: months, daysOfTheMonth: []int{1}, lastRun: januaryFirst2020At(0, 0, 0).AddDate(0, 0, 7)}, wantTimeUntilNextRun: 24 * day},
{name: "every month same as lastRun, should run February 1st", job: &Job{interval: 1, unit: months, daysOfTheMonth: []int{1}, lastRun: januaryFirst2020At(0, 0, 0)}, wantTimeUntilNextRun: 31 * day},
{name: "every 2 months at day 1, starting at day 1, should run in 2 months", job: &Job{interval: 2, unit: months, daysOfTheMonth: []int{1}, lastRun: januaryFirst2020At(0, 0, 0)}, wantTimeUntilNextRun: 31*day + 29*day}, // 2020 january and february
{name: "every 2 months at day 2, starting at day 1, should run in 2 months + 1 day", job: &Job{interval: 2, unit: months, daysOfTheMonth: []int{2}, lastRun: januaryFirst2020At(0, 0, 0)}, wantTimeUntilNextRun: 31*day + 29*day + 1*day}, // 2020 january and february
{name: "every 2 months at day 1, starting at day 2, should run in 2 months - 1 day", job: &Job{interval: 2, unit: months, daysOfTheMonth: []int{1}, lastRun: januaryFirst2020At(0, 0, 0).AddDate(0, 0, 1)}, wantTimeUntilNextRun: 30*day + 29*day}, // 2020 january and february
Expand Down

0 comments on commit 2319102

Please sign in to comment.