From 0d42ef26d02788501e122d94bd58a934b6736b23 Mon Sep 17 00:00:00 2001 From: scher Date: Thu, 3 Sep 2026 20:00:25 +0300 Subject: [PATCH] reject dates that do not exist --- app/flags.v | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/app/flags.v b/app/flags.v index 9cc6768..81c4a44 100644 --- a/app/flags.v +++ b/app/flags.v @@ -12,7 +12,7 @@ mut: role string = 'author' name string email string - limit int = 50 + limit int = 50 by string = 'month' jobs int dry_run bool @@ -111,9 +111,30 @@ fn parse(args []string) !Flags { return f } +// check_date wants a day that exists. fn check_date(s string) !string { if s.len != 10 || s[4] != `-` || s[7] != `-` { return error("'${s}' is not a date; use YYYY-MM-DD") } + for i, c in s { + if i != 4 && i != 7 && !c.is_digit() { + return error("'${s}' is not a date; use YYYY-MM-DD") + } + } + month := s[5..7].int() + day := s[8..10].int() + if month < 1 || month > 12 || day < 1 || day > days_in(s[..4].int(), month) { + return error("there is no such day as '${s}'") + } return s } + +fn days_in(year int, month int) int { + return match month { + 2 { + if year % 4 == 0 && (year % 100 != 0 || year % 400 == 0) { 29 } else { 28 } + } + 4, 6, 9, 11 { 30 } + else { 31 } + } +}