Attachment "tcl-tz-repro.tcl" to
ticket [7bad415e3e]
added by
kenneth.kienle
2026-09-03 19:55:47.
Also attachment "tcl-tz-repro.tcl" to
ticket [832d098fdf]
added by
kenneth.kienle
2026-09-03 19:47:51.
# Reproduction for three Tcl time-zone bugs, self-contained: no tzdata needed.
# Run: tclsh tcl-tz-repro.tcl (Tcl 8.6 or 9.0)
# Writes five synthetic TZif files into a scratch zoneinfo directory, loads
# them through clock's own reader, and compares against the RFC 9636 meaning
# of each file's footer. Then exercises the free-form clock scan.
puts "Tcl [info patchlevel] library=[info library]"
clock format 0 -timezone :UTC ;# load the Tcl half of clock
# A TZif file: version byte, one time type (offset, abbreviation), no
# transitions in either data block, and a footer TZ string. Per RFC 9636
# section 3: header = magic, version, 15 reserved bytes, six 4-byte counts
# (isutcnt isstdcnt leapcnt timecnt typecnt charcnt); data = transition
# times, type indices, type records (gmtoff isdst abbrind), abbreviations,
# leap records, std/wall and UT/local indicators; then the version-2+
# header/data pair and "\n<TZ string>\n".
proc tzif {version gmtoff abbr footer} {
set abbrs "$abbr\x00"
set hdr [binary format a4a1x15IIIIII TZif $version 0 0 0 0 1 [string length $abbrs]]
set data "[binary format Icc $gmtoff 0 0]$abbrs"
return "$hdr$data$hdr$data\n$footer\n"
}
set tzdir [file join [pwd] repro-zoneinfo-[pid]]
file mkdir [file join $tzdir Test]
foreach {name version gmtoff abbr footer} {
Paris2 2 3600 CET {CET-1CEST,M3.5.0,M10.5.0/3}
Nuuk3 3 -7200 -02 {<-02>2<-01>,M3.5.0/-1,M10.5.0/0}
Israel3 3 7200 IST {IST-2IDT,M3.4.4/26,M10.5.0}
Nuuk4 4 -7200 -02 {<-02>2<-01>,M3.5.0/-1,M10.5.0/0}
} {
set f [open [file join $tzdir Test $name] wb]
puts -nonewline $f [tzif $version $gmtoff $abbr $footer]
close $f
}
set ::tcl::clock::ZoneinfoPaths [linsert $::tcl::clock::ZoneinfoPaths 0 $tzdir]
::tcl::clock::ClearCaches
set fail 0
proc check {what got want} {
global fail
if {$got eq $want} { puts " ok $what -> $got" } else { puts " FAIL $what -> $got (expected $want)"; incr fail }
}
puts "== TZif reader: winter and summer 2030 through each synthetic zone"
set winter 1894060800 ;# 2030-01-11 12:00:00 UTC
set summer 1909742400 ;# 2030-07-11 12:00:00 UTC
foreach {zone wwant swant} {
Paris2 {+0100 CET} {+0200 CEST}
Nuuk3 {-0200 -02} {-0100 -01}
Israel3 {+0200 IST} {+0300 IDT}
Nuuk4 {-0200 -02} {-0100 -01}
} {
if {[catch {::tcl::clock::LoadZoneinfoFile Test/$zone} err]} { puts " FAIL load Test/$zone: $err"; incr fail; continue }
check "Test/$zone winter" [clock format $winter -timezone :Test/$zone -format {%z %Z}] $wwant
check "Test/$zone summer" [clock format $summer -timezone :Test/$zone -format {%z %Z}] $swant
}
puts "== POSIX TZ rules in the Jn (day-of-year) form, as a footer and as a -timezone string"
set f [open [file join $tzdir Test JDay2] wb]
puts -nonewline $f [tzif 2 -18000 EST {EST5EDT,J60,J300}]
close $f
if {[catch {::tcl::clock::LoadZoneinfoFile Test/JDay2} err]} { puts " FAIL load Test/JDay2: $err"; incr fail } else {
check "Test/JDay2 winter" [clock format $winter -timezone :Test/JDay2 -format {%z %Z}] {-0500 EST}
check "Test/JDay2 summer" [clock format $summer -timezone :Test/JDay2 -format {%z %Z}] {-0400 EDT}
}
foreach {tz want} {
{EST5EDT,J60,J300} {-0400 EDT}
{IRST-3:30IRDT,J79/24,J263/24} {+0430 IRDT}
} {
if {[catch {clock format $summer -timezone $tz -format {%z %Z}} got]} { set got "ERROR $got" }
check "-timezone {$tz} summer" $got $want
}
puts "== free-form clock scan of numeric zone abbreviations (tz's own forms)"
foreach {text want} {
{2023-11-15 00:13:20 +02} 1700000000
{2023-11-14 20:13:20 -02} 1700000000
{2023-11-15 01:13:20 +3} 1700000000
{2023-11-15 03:43:20 +0530} 1700000000
{2023-11-15 00:13:20 +0200} 1700000000
{+02 2023-11-15 00:13:20} 1700000000
{+0200 15 Nov 2023 00:13:20} 1700000000
} {
if {[catch {clock scan $text -gmt 1} got]} { set got "ERROR $got" }
check "clock scan {$text}" $got $want
}
check "clock scan {+2 days} from 0" [clock scan {+2 days} -base 0 -gmt 1] 172800
file delete -force $tzdir
puts [expr {$fail ? "$fail FAILED" : "all passed"}]
exit [expr {$fail ? 1 : 0}]