Code: Select all
set vFinddateVersion 11.03.03.00.35
bind PUB - !finddate pPubFinddate
proc pPubFinddate {nick uhost hand chan text} {
set vars [split $text]
if {[llength $vars] == 4} {
set day [lindex $vars 0]; set month [lindex $vars 1]; set year [lindex $vars 2]; set number [lindex $vars 3]
switch -- [set date [pParseFinddate $day $month $year $number]] {
e1 {putserv "PRIVMSG $chan :invalid or misspelt full day name"}
e2 {putserv "PRIVMSG $chan :invalid or misspelt full month name"}
e3 {putserv "PRIVMSG $chan :year value was not an integer"}
e4 {putserv "PRIVMSG $chan :year value was not the 4 digits expected"}
e5 {putserv "PRIVMSG $chan :year value was outside the range 1971 through 2037"}
e6 {putserv "PRIVMSG $chan :number value was not an integer"}
e7 {putserv "PRIVMSG $chan :number value was outside the range 1 through 5"}
e8 {putserv "PRIVMSG $chan :cannot find $number [string totitle ${day}'s] in [string totitle $month] $year"}
default {putserv "PRIVMSG $chan :$date"}
}
} else {putserv "PRIVMSG $chan :correct syntax is !finddate <day> <month> <year> <number>"}
return 0
}
proc pParseFinddate {day month year number} {
set dayname [string tolower $day]
set monthname [string tolower $month]
if {[lsearch -exact {sunday monday tuesday wednesday thursday friday saturday} $dayname] == -1} {return e1}
if {[lsearch -exact {january february march april may june july august september october november december} $monthname] == -1} {return e2}
if {![string is integer -strict $year]} {return e3}
if {[string length $year] != 4} {return e4}
if {($year < 1971) || ($year > 2037)} {return e5}
if {![string is integer -strict $number]} {return e6}
if {($number < 1) || ($number > 5)} {return e7}
set target 1
for {set loop 1} {$loop <= 32} {incr loop} {
set date "[format %02i $loop]-[string totitle $monthname]-$year"
if {[catch {set datevalue [clock scan $date -format "%d-%B-%Y"]}]} {return e8}
if {[string equal -nocase [clock format $datevalue -format %A] $day]} {
if {[string equal $target $number]} {
return [clock format $datevalue -format {%A %d %B %Y}]
} else {incr target}
}
}
return 0
}
putlog "finddate.tcl version $vFinddateVersion loaded"
# eof
Code: Select all
switch $number {
1 - 3 {
# something
}
2 - 4 {
# something else
}
}
arfer wrote:Yes I'm aware caesar, but that's only any use if the code for two or more switch values require the same code. All the values in the script above have different code.
I wouldn't mind some explanation of why my Windrop bot rolls extra days over into the next month rather than returning an invalid date. Seems absurd.
Indeed, welcome to the new (improved??!) clock within tcl 8.5. Give it bogus dates, it will return bogus values. Give it the 35th of a month, it will wrap 4 or 5 days into the next month (those with 30/31 days). This is due to tcl8.5 requiring the -format command to implicitly tell it the format you are giving it when using scan. It will no longer allow 'free-form" guesses like tcl8.4 clock did. This is why you see it wrapping into March.tcl 8.4 clock wrote:.tcl [clock scan 31-February-2011]
Tcl error: unable to convert date-time string "31-February-2011"
Code: Select all
#The issue
if {[catch {set datevalue [clock scan $date]}]} {return e8}
Code: Select all
#The solution
if {[catch {set datevalue [clock scan $date -format "%d-%B-%Y"]}]} {return e8}
-format format
Specifies the desired output format for clock format or the expected input format for clock scan. The format string consists of any number of characters other than the per-cent sign (“%”) interspersed with any number of format groups, which are two-character sequences beginning with the per-cent sign. The permissible format groups, and their interpretation, are described under FORMAT GROUPS.
It worked on tcl8.4 unaltered the way you had it. It will trigger error condition #8 (e8) properly and pass it.arfer wrote:Thanks speechless, that does explain things. I have modified the script in my original post according to your recommendations. Hopefully Jagg is still following the thread he created.
One possible problem though, I'm assuming the code no longer works on Tcl 8.4?
Code: Select all
set vFinddateVersion 11.03.07.21.48
bind PUB - !finddate pPubFinddate
proc pPubFinddate {nick uhost hand chan text} {
set vars [split $text]
if {[llength $vars] == 4} {
set day [lindex $vars 0]; set month [lindex $vars 1]; set year [lindex $vars 2]; set number [lindex $vars 3]
switch -- [set date [pParseFinddate $day $month $year $number]] {
e1 {putserv "PRIVMSG $chan :invalid or misspelt full day name"}
e2 {putserv "PRIVMSG $chan :invalid or misspelt full month name"}
e3 {putserv "PRIVMSG $chan :year value was not an integer"}
e4 {putserv "PRIVMSG $chan :year value was not the 4 digits expected"}
e5 {putserv "PRIVMSG $chan :year value was outside the range 1971 through 2037"}
e6 {putserv "PRIVMSG $chan :number value was not an integer"}
e7 {putserv "PRIVMSG $chan :number value was outside the range 1 through 5"}
e8 {putserv "PRIVMSG $chan :cannot find $number [string totitle ${day}'s] in [string totitle $month] $year"}
default {putserv "PRIVMSG $chan :$date"}
}
} else {putserv "PRIVMSG $chan :correct syntax is !finddate <day> <month> <year> <number>"}
return 0
}
proc pParseFinddate {day month year number} {
set dayname [string tolower $day]
set monthname [string tolower $month]
if {[lsearch -exact {sunday monday tuesday wednesday thursday friday saturday} $dayname] == -1} {return e1}
if {[lsearch -exact {january february march april may june july august september october november december} $monthname] == -1} {return e2}
if {![string is integer -strict $year]} {return e3}
if {[string length $year] != 4} {return e4}
if {($year < 1971) || ($year > 2037)} {return e5}
if {![string is integer -strict $number]} {return e6}
if {($number < 1) || ($number > 5)} {return e7}
set target 1
for {set loop 1} {$loop <= 32} {incr loop} {
set date "[format %02i $loop]-[string totitle $monthname]-$year"
if {[catch {set datevalue [clock scan $date]}]} {return e8}
if {![string equal -nocase [clock format $datevalue -format %B] $monthname]} {return e8}
if {[string equal -nocase [clock format $datevalue -format %A] $day]} {
if {[string equal $target $number]} {
return [clock format $datevalue -format {%A %d %B %Y}]
} else {incr target}
}
}
return 0
}
putlog "finddate.tcl version $vFinddateVersion loaded"
# eof
Code: Select all
if {[lsearch -exact {sunday monday tuesday wednesday thursday friday saturday} $dayname] == -1} {return e1}
if {[lsearch -exact {january february march april may june july august september october november december} $monthname] == -1} {return e2}