Synchronizing System Time

2006-06-06 VI A rather simplistic approach to setting the system time. Plain Tcl. Requires administrator privileges and works on SunOS Linux and Windows. The script downloads a web page from the nist web site and parses it to set the time. The URL is of the form: / <s for standard> or <d for daylight savings> / <offset-from-gmt>

The default (/d/-8) is correct for the pacific timezone in the US:


    package require http
    set url "http://www.time.gov/timezone.cgi?/d/-8"
    set idx [::http::geturl $url]
    set r [::http::data $idx]
    ::http::cleanup $idx
    regsub -all {<script.*?/script>} $r {} r
    regsub -all {<style.*?/style>} $r {} r
    regsub -all {<[^>]*>} $r "\n" r
    regsub -all {&[^;]*;} $r "\n" r
    set found 0
    foreach line [split $r "\n"] {
        set line [string trim $line]
        if {[regexp {^\d\d:\d\d:\d\d$} $line]} {
            set time $line
        } elseif {[regexp {.*day,\s+([A-Za-z]+\s\d+,\s+20\d\d)} $line -> t]} {
            set time "$time $t"
            set found 1
        }
    }
    if {$found == 0} {
        error "Could not parse out time"
    }
    set c [clock scan $time]
    puts "Time is [clock format $c]"
    switch $::tcl_platform(os) {
        Linux - SunOS {
            exec date [clock format $c -format %m%d%H%M%Y.%S]
        }
        "Windows NT" {
            exec cmd /c date [clock format $c -format %m-%d-%y]
            exec cmd /c time [clock format $c -format %H:%M:%S]
        }
        default {
            error "Unknown platform"
        }
    }