Reading PNG Timestamps

This procedure reads the timestamp (and converts it to normal epoch-relative format) from a PNG file, if present. This functionality is also present in the tcllib png module. --AF


 proc read_png_timestamp {file} {
    set fh [open $file r]
    fconfigure $fh -encoding binary -translation binary -eofchar {}
    if {[read $fh 8] != "\x89PNG\r\n\x1a\n"} { close $fh; return }

    while {[set r [read $fh 8]] != ""} {
        binary scan $r Ia4 len type
        set r [read $fh [expr {$len + 4}]]
        if {[string length $r] != ($len + 4)} { close $fh; return }
        if {$type == "tIME"} {
            binary scan $r Sccccc year month day hour minute second
            close $fh
            return [clock scan "$month/$day/$year $hour:$minute:$second"]
        }
    }
    close $fh
    return
 }