From b996c34763930fb1ed51e59c46d2453b7e012acf Mon Sep 17 00:00:00 2001 From: Mark Grimes Date: Mon, 17 Apr 2023 10:05:39 -0400 Subject: [PATCH 1/2] test: add failing tests for future dd/mm rules If no year is specified, then nil is returned for: - current date July 15 in test - future months --- rules/common/slash_dmy_test.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/rules/common/slash_dmy_test.go b/rules/common/slash_dmy_test.go index d2b09f9..60144a6 100644 --- a/rules/common/slash_dmy_test.go +++ b/rules/common/slash_dmy_test.go @@ -27,6 +27,10 @@ func TestSlashDMY(t *testing.T) { // prev day will be added to the future {"The Deadline is 14/07", 16, "14/07", (195 + 366 - OFFSET) * 24 * time.Hour}, + + // Existing doesn't work for a month in the future + {"The Deadline is 14/08", 16, "14/08", time.Date(2016, 8, 14, 0, 0, 0, 0, time.UTC).Sub(null)}, + {"The Deadline is 15/07", 16, "15/07", time.Date(2016, 7, 15, 0, 0, 0, 0, time.UTC).Sub(null)}, } w := when.New(nil) From f732bc8fc2eebaa9f383267ed00d1e007f0f9964 Mon Sep 17 00:00:00 2001 From: Mark Grimes Date: Mon, 17 Apr 2023 10:10:23 -0400 Subject: [PATCH 2/2] fix: update slash_dym to accomodate current date and future months --- rules/common/slash_dmy.go | 27 ++++++++++++--------------- 1 file changed, 12 insertions(+), 15 deletions(-) diff --git a/rules/common/slash_dmy.go b/rules/common/slash_dmy.go index 31d0799..6c6668e 100644 --- a/rules/common/slash_dmy.go +++ b/rules/common/slash_dmy.go @@ -70,27 +70,24 @@ func SlashDMY(s rules.Strategy) rules.Rule { return true, nil } - if int(ref.Month()) > month { + if month < int(ref.Month()) { year = ref.Year() + 1 - goto WithYear - } + } else if month == int(ref.Month()) { + if day > getDays(ref.Year(), month) { + // invalid date: day is after last day of the month + return false, nil + } - if int(ref.Month()) == month { - if getDays(ref.Year(), month) >= day { - if day > ref.Day() { - year = ref.Year() - } else if day < ref.Day() { - year = ref.Year() + 1 - } else { - return false, nil - } - goto WithYear + if day >= ref.Day() { + year = ref.Year() } else { - return false, nil + year = ref.Year() + 1 } + } else { + year = ref.Year() } - return true, nil + goto WithYear }, } }