Skip to content

Commit

Permalink
better fix for validation check for scanned day of week in input (nor…
Browse files Browse the repository at this point in the history
…malized 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)
  • Loading branch information
sebres committed Jul 16, 2024
1 parent 922dedc commit 6f178cf
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 19 deletions.
2 changes: 1 addition & 1 deletion generic/tclClock.c
Original file line number Diff line number Diff line change
Expand Up @@ -3956,7 +3956,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;
}
}
Expand Down
2 changes: 1 addition & 1 deletion generic/tclDate.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 },
Expand Down
2 changes: 1 addition & 1 deletion generic/tclGetDate.y
Original file line number Diff line number Diff line change
Expand Up @@ -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 },
Expand Down
55 changes: 39 additions & 16 deletions tests/clock.test
Original file line number Diff line number Diff line change
Expand Up @@ -37159,21 +37159,20 @@ 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:
set res {}
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
Expand All @@ -37183,49 +37182,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 [clock configure -min-year] -max-year [clock configure -max-year] \
-year-century [clock configure -year-century] -century-switch [clock configure -century-switch]]
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 {
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 \
Expand Down

0 comments on commit 6f178cf

Please sign in to comment.