From ffe4640cc14ada5fd731927896d2ed56571b2866 Mon Sep 17 00:00:00 2001 From: sebres Date: Tue, 16 Jul 2024 21:14:40 +0000 Subject: clock: better fix for validation check for scanned day of week in input (normalized input value after scan, Sun is always 7 now); more tests for valid days of weeks covering also format scan with different weekday formats (%a, %u, %w) --- generic/tclClock.c | 2 +- generic/tclDate.c | 2 +- generic/tclGetDate.y | 2 +- tests/clock.test | 55 +++++++++++++++++++++++++++++++++++++--------------- 4 files changed, 42 insertions(+), 19 deletions(-) diff --git a/generic/tclClock.c b/generic/tclClock.c index adf9774..20da59f 100644 --- a/generic/tclClock.c +++ b/generic/tclClock.c @@ -3937,7 +3937,7 @@ ClockValidDate( tempCpyFlg = 1; } GetYearWeekDay(&temp, GREGORIAN_CHANGE_DATE); - if ((temp.dayOfWeek % 7) != (yyDayOfWeek % 7)) { + if (temp.dayOfWeek != yyDayOfWeek) { errMsg = "invalid day of week"; errCode = "day of week"; goto error; diff --git a/generic/tclDate.c b/generic/tclDate.c index fe6f088..961e383 100644 --- a/generic/tclDate.c +++ b/generic/tclDate.c @@ -2126,7 +2126,7 @@ static const TABLE MonthDayTable[] = { { "october", tMONTH, 10 }, { "november", tMONTH, 11 }, { "december", tMONTH, 12 }, - { "sunday", tDAY, 0 }, + { "sunday", tDAY, 7 }, { "monday", tDAY, 1 }, { "tuesday", tDAY, 2 }, { "tues", tDAY, 2 }, diff --git a/generic/tclGetDate.y b/generic/tclGetDate.y index 83331e2..0b25778 100644 --- a/generic/tclGetDate.y +++ b/generic/tclGetDate.y @@ -496,7 +496,7 @@ static const TABLE MonthDayTable[] = { { "october", tMONTH, 10 }, { "november", tMONTH, 11 }, { "december", tMONTH, 12 }, - { "sunday", tDAY, 0 }, + { "sunday", tDAY, 7 }, { "monday", tDAY, 1 }, { "tuesday", tDAY, 2 }, { "tues", tDAY, 2 }, diff --git a/tests/clock.test b/tests/clock.test index bedfb70..5546231 100644 --- a/tests/clock.test +++ b/tests/clock.test @@ -37178,7 +37178,7 @@ if {!$valid_mode} { } {1 {unable to convert input string: invalid time (hour)} 1 {unable to convert input string: invalid time (hour)}} } -proc _invalid_test {testtz args} { +proc _invalid_test {testtz scnargs args} { global valid_mode # ensure validation works TZ independently, since the conversion # of local time to UTC may adjust date/time tokens, depending on TZ: @@ -37186,13 +37186,12 @@ proc _invalid_test {testtz args} { if {$testtz eq ""} { set testtz {:GMT :CET {} :Europe/Berlin :localtime} } + if {!$valid_mode} { # globally -valid 0, so add it explicitely + lappend scnargs -valid 1 + } foreach tz $testtz { foreach {v} $args { - if {$valid_mode} { # globally -valid 1 - lappend res [catch {clock scan $v -timezone $tz} msg] $msg - } else { - lappend res [catch {clock scan $v -valid 1 -timezone $tz} msg] $msg - } + lappend res [catch {clock scan $v {*}$scnargs -timezone $tz} msg] $msg } } set res @@ -37202,49 +37201,73 @@ foreach {idx relstr} {"" "" "+rel" "+ 15 month + 40 days + 30 hours + 80 minutes test clock-46.10$idx {freescan: validation rules: invalid time} \ -body { # 13:00 am/pm are invalid input strings... - _invalid_test {} "13:00 am$relstr" "13:00 pm$relstr" + _invalid_test {} {} "13:00 am$relstr" "13:00 pm$relstr" } -result [lrepeat 10 1 {unable to convert input string: invalid time (hour)}] test clock-46.11$idx {freescan: validation rules: invalid time} \ -body { # invalid minutes in input strings... - _invalid_test {} "23:70$relstr" "11:80 pm$relstr" + _invalid_test {} {} "23:70$relstr" "11:80 pm$relstr" } -result [lrepeat 10 1 {unable to convert input string: invalid time (minutes)}] test clock-46.12$idx {freescan: validation rules: invalid time} \ -body { # invalid seconds in input strings... - _invalid_test {} "23:00:70$relstr" "11:00:80 pm$relstr" + _invalid_test {} {} "23:00:70$relstr" "11:00:80 pm$relstr" } -result [lrepeat 10 1 {unable to convert input string: invalid time}] test clock-46.13$idx {freescan: validation rules: invalid day} \ -body { - _invalid_test {} "29 Feb 2017$relstr" "30 Feb 2016$relstr" + _invalid_test {} {} "29 Feb 2017$relstr" "30 Feb 2016$relstr" } -result [lrepeat 10 1 {unable to convert input string: invalid day}] test clock-46.14$idx {freescan: validation rules: invalid day} \ -body { - _invalid_test {} "0 Feb 2017$relstr" "00 Feb 2017$relstr" + _invalid_test {} {} "0 Feb 2017$relstr" "00 Feb 2017$relstr" } -result [lrepeat 10 1 {unable to convert input string: invalid day}] test clock-46.15$idx {freescan: validation rules: invalid month} \ -body { - _invalid_test {} "13/13/2017$relstr" "00/00/2017$relstr" + _invalid_test {} {} "13/13/2017$relstr" "00/00/2017$relstr" } -result [lrepeat 10 1 {unable to convert input string: invalid month}] test clock-46.16$idx {freescan: validation rules: invalid day of week} \ -body { - _invalid_test {} "Sat Jan 02 00:00:00 1970$relstr" "Thu Jan 04 00:00:00 1970$relstr" + _invalid_test {} {} "Sat Jan 02 00:00:00 1970$relstr" "Thu Jan 04 00:00:00 1970$relstr" } -result [lrepeat 10 1 {unable to convert input string: invalid day of week}] test clock-46.17$idx {scan: validation rules: invalid year} -setup { set orgcfg [list -min-year [::tcl::unsupported::clock::configure -min-year] -max-year [::tcl::unsupported::clock::configure -max-year] \ -year-century [::tcl::unsupported::clock::configure -year-century] -century-switch [::tcl::unsupported::clock::configure -century-switch]] ::tcl::unsupported::clock::configure -min-year 2000 -max-year 2100 -year-century 2000 -century-switch 38 } -body { - _invalid_test {} "70-01-01$relstr" "1870-01-01$relstr" "9570-01-01$relstr" + _invalid_test {} {} "70-01-01$relstr" "1870-01-01$relstr" "9570-01-01$relstr" } -result [lrepeat 15 1 {unable to convert input string: invalid year}] -cleanup { ::tcl::unsupported::clock::configure {*}$orgcfg unset -nocomplain orgcfg } }; # foreach -test clock-46.16-pos {freescan: validation rules: valid day of week (must work for all weekdays)} \ +test clock-46.16-pos-fs {freescan: validation rules: valid day of week (must work for all weekdays)} \ + -body { + _invalid_test {:GMT -12:00 +12:00} {} {Sat, 01 Jan 2000 00:00:00} {Sun, 02 Jan 2000 00:00:00} {Mon, 03 Jan 2000 00:00:00} {Tue, 04 Jan 2000 00:00:00} {Wed, 05 Jan 2000 00:00:00} {Thu, 06 Jan 2000 00:00:00} {Fri, 07 Jan 2000 00:00:00} + } -result [list \ + 0 946684800 0 946771200 0 946857600 0 946944000 0 947030400 0 947116800 0 947203200 \ + 0 946728000 0 946814400 0 946900800 0 946987200 0 947073600 0 947160000 0 947246400 \ + 0 946641600 0 946728000 0 946814400 0 946900800 0 946987200 0 947073600 0 947160000 \ + ] +test clock-46.16-pos-fmt1 {scan with format: validation rules: valid day of week (must work for all weekdays)} \ + -body { + _invalid_test {:GMT -12:00 +12:00} {-format "%a, %d %b %Y %H:%M:%S"} {Sat, 01 Jan 2000 00:00:00} {Sun, 02 Jan 2000 00:00:00} {Mon, 03 Jan 2000 00:00:00} {Tue, 04 Jan 2000 00:00:00} {Wed, 05 Jan 2000 00:00:00} {Thu, 06 Jan 2000 00:00:00} {Fri, 07 Jan 2000 00:00:00} + } -result [list \ + 0 946684800 0 946771200 0 946857600 0 946944000 0 947030400 0 947116800 0 947203200 \ + 0 946728000 0 946814400 0 946900800 0 946987200 0 947073600 0 947160000 0 947246400 \ + 0 946641600 0 946728000 0 946814400 0 946900800 0 946987200 0 947073600 0 947160000 \ + ] +test clock-46.16-pos-fmt2 {scan with format: validation rules: valid day of week (must work for all weekdays)} \ + -body { + _invalid_test {:GMT -12:00 +12:00} {-format "%u, %d %b %Y %H:%M:%S"} {6, 01 Jan 2000 00:00:00} {7, 02 Jan 2000 00:00:00} {1, 03 Jan 2000 00:00:00} {2, 04 Jan 2000 00:00:00} {3, 05 Jan 2000 00:00:00} {4, 06 Jan 2000 00:00:00} {5, 07 Jan 2000 00:00:00} + } -result [list \ + 0 946684800 0 946771200 0 946857600 0 946944000 0 947030400 0 947116800 0 947203200 \ + 0 946728000 0 946814400 0 946900800 0 946987200 0 947073600 0 947160000 0 947246400 \ + 0 946641600 0 946728000 0 946814400 0 946900800 0 946987200 0 947073600 0 947160000 \ + ] +test clock-46.16-pos-fmt2 {scan with format: validation rules: valid day of week (must work for all weekdays)} \ -body { - _invalid_test {:GMT -12:00 +12:00} {Sat, 01 Jan 2000 00:00:00} {Sun, 02 Jan 2000 00:00:00} {Mon, 03 Jan 2000 00:00:00} {Tue, 04 Jan 2000 00:00:00} {Wed, 05 Jan 2000 00:00:00} {Thu, 06 Jan 2000 00:00:00} {Fri, 07 Jan 2000 00:00:00} + _invalid_test {:GMT -12:00 +12:00} {-format "%w, %d %b %Y %H:%M:%S"} {6, 01 Jan 2000 00:00:00} {0, 02 Jan 2000 00:00:00} {1, 03 Jan 2000 00:00:00} {2, 04 Jan 2000 00:00:00} {3, 05 Jan 2000 00:00:00} {4, 06 Jan 2000 00:00:00} {5, 07 Jan 2000 00:00:00} } -result [list \ 0 946684800 0 946771200 0 946857600 0 946944000 0 947030400 0 947116800 0 947203200 \ 0 946728000 0 946814400 0 946900800 0 946987200 0 947073600 0 947160000 0 947246400 \ -- cgit v0.12