Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## 0.48.1 - Unreleased

- Tasks: keep monthly and yearly `tasks add --repeat` series on one task per period. A series due on the 31st now uses the last day of shorter months (January 31 is followed by February 28, March 31 and April 30), and a February 29 series falls on February 28 in common years, instead of skipping February and creating two March tasks. (#1309) — thanks @saariuslystoned.

## 0.48.0 - 2026-11-05

**Highlights:** New backups stay local until you configure a remote; existing configurations keep working. Store service-account keys in the keyring, search Gmail IDs efficiently, and use environment-backed watch tokens, with Calendar fixes and security dependency updates.
Expand Down
19 changes: 17 additions & 2 deletions internal/cmd/tasks_repeat.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,14 +157,29 @@ func addRepeat(t time.Time, unit repeatUnit, n int) time.Time {
case repeatWeekly:
return t.AddDate(0, 0, 7*n)
case repeatMonthly:
return t.AddDate(0, n, 0)
return addMonthsClamped(t, n)
case repeatYearly:
return t.AddDate(n, 0, 0)
return addMonthsClamped(t, 12*n)
default:
return t
}
}

// addMonthsClamped adds n calendar months and keeps the day of month, using
// the target month's last day when it is shorter. time.AddDate would carry the
// overflow into the next month, so a monthly series from January 31 would skip
// February and create two March tasks. The target month is found in UTC, so a
// daylight-saving gap at midnight in t's location cannot move it.
func addMonthsClamped(t time.Time, n int) time.Time {
year, month, day := t.Date()
hour, minute, sec := t.Clock()
first := time.Date(year, month+time.Month(n), 1, 0, 0, 0, 0, time.UTC)
if last := first.AddDate(0, 1, -1).Day(); day > last {
day = last
}
return time.Date(first.Year(), first.Month(), day, hour, minute, sec, t.Nanosecond(), t.Location())
}

func formatTaskDue(t time.Time, hasTime bool) string {
if hasTime {
return t.Format(time.RFC3339)
Expand Down
36 changes: 36 additions & 0 deletions internal/cmd/tasks_repeat_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"slices"
"sync/atomic"
"testing"
"time"
)

type tasksRepeatTestResult struct {
Expand Down Expand Up @@ -89,6 +90,21 @@ func TestTasksAddCmd_RecurRRuleIntervalCreatesMultiple(t *testing.T) {
assertTaskDueOrder(t, result.due, []string{"2025-01-01T00:00:00Z", "2025-01-03T00:00:00Z", "2025-01-05T00:00:00Z"})
}

func TestTasksAddCmd_RepeatMonthlyKeepsMonthEnd(t *testing.T) {
result := runTasksRepeatTest(t, "--due", "2026-01-31", "--repeat", "monthly", "--repeat-count", "4")
assertTaskDueOrder(t, result.due, []string{"2026-01-31T00:00:00Z", "2026-02-28T00:00:00Z", "2026-03-31T00:00:00Z", "2026-04-30T00:00:00Z"})
}

func TestTasksAddCmd_RepeatMonthlyUntilKeepsOneTaskPerMonth(t *testing.T) {
result := runTasksRepeatTest(t, "--due", "2026-01-31T09:00:00Z", "--recur-rrule", "FREQ=MONTHLY", "--repeat-until", "2026-04-30")
assertTaskDueOrder(t, result.due, []string{"2026-01-31T09:00:00Z", "2026-02-28T09:00:00Z", "2026-03-31T09:00:00Z", "2026-04-30T09:00:00Z"})
}

func TestTasksAddCmd_RepeatYearlyFromLeapDay(t *testing.T) {
result := runTasksRepeatTest(t, "--due", "2028-02-29", "--repeat", "yearly", "--repeat-count", "3")
assertTaskDueOrder(t, result.due, []string{"2028-02-29T00:00:00Z", "2029-02-28T00:00:00Z", "2030-02-28T00:00:00Z"})
}

func assertTaskDueOrder(t *testing.T, got, want []string) {
t.Helper()
if !slices.Equal(got, want) {
Expand Down Expand Up @@ -172,3 +188,23 @@ func TestParseRepeatRRule(t *testing.T) {
})
}
}

func TestAddMonthsClampedIgnoresMidnightDSTGap(t *testing.T) {
t.Parallel()
// Paraguay started daylight saving at midnight on 2023-10-01, so that
// day has no 00:00. A local --due such as "2023-09-15 00:00" parses in
// time.Local, and the next month must still be October.
loc, err := time.LoadLocation("America/Asuncion")
if err != nil {
t.Skipf("tzdata: %v", err)
}
start := time.Date(2023, time.September, 15, 0, 0, 0, 0, loc)
got := addMonthsClamped(start, 1)
if got.Year() != 2023 || got.Month() != time.October || got.Day() != 15 {
t.Fatalf("addMonthsClamped(%s, 1) = %s, want 2023-10-15", start, got)
}
end := time.Date(2023, time.August, 31, 0, 30, 0, 0, loc)
if got := addMonthsClamped(end, 1); got.Month() != time.September || got.Day() != 30 {
t.Fatalf("addMonthsClamped(%s, 1) = %s, want 2023-09-30", end, got)
}
}
Loading