Miscellaneous utility functions

Previous: Interpreting the results that wftk sends back ] [ Top: To-do manager ] [ Next: index ]

These are some functions I end up using a lot for AOLserver/Tcl applications. The now function is pretty obvious, and the sql_safe_string function simply doubles single quotes in order to make strings safe to insert into SQL queries.
 
proc now {{what all}} {
   set time [ns_localtime]
   switch $what {
    all {return [format "%04d-%02d-%02d %02d:%02d:%02d" [expr [lindex $time 5] + 1900] [expr [lindex $time 4] + 1] [lindex $time 3] [lindex $time 2] [lindex $time 1] [lindex $time 0]]}
    date {return [format "%04d-%02d-%02d" [expr [lindex $time 5] + 1900] [expr [lindex $time 4] + 1] [lindex $time 3]]}
    time {return [format "%02d:%02d:%02d" [lindex $time 2] [lindex $time 1] [lindex $time 0]]}
    tag {return [format "%04d%02d%02d%02d%02d%02d" [expr [lindex $time 5] + 1900] [expr [lindex $time 4] + 1] [lindex $time 3] [lindex $time 2] [lindex $time 1] [lindex $time 0]]}
   }
}

proc sql_safe_string {s} {
   set out ""
   while {[string first "'" $s] > -1} {
      set f [string first "'" $s]
      append out [string range $s 0 $f]
      append out "'"
      set s [string range $s [expr $f + 1] end]
   }
   append out $s

   return $out
}
I suppose I could use regsub to implement sql_safe_string but this works and I trust it. (I wrote it very early on in my Tcl days. Ah, gotta love those code fossils.)
Previous: Interpreting the results that wftk sends back ] [ Top: To-do manager ] [ Next: index ]


This code and documentation are released under the terms of the GNU license. They are additionally copyright (c) 2000, Vivtek. All rights reserved except those explicitly granted under the terms of the GNU license.