#!/bin/sh # \ exec tclsh "$0" if {[info tclversion] < 7.5} { puts stderr "Sorry, you need at least Tcl version 7.5 (this script uses the 'clock' command)" exit } # The calc routine takes a time (as the number of seconds since the epoch) and prints the week number # according to ISO8601:1988 # # 'clock format' uses strftime(), which often has problems around the year transitions, hence the # extra checks # # Andrew Benham, June 1998 proc calc {time} { set secs_per_week [expr 7*24*60*60] set year [clock format $time -format "%Y"] set week [clock format $time -format "%V"] if {$week == 53} { # If strftime() returns week 53 then we need to check, because it's often wrong. # Check the week numbers for the previous and next weeks set last_week [clock format [expr $time - $secs_per_week] -format "%V"] set last_year [clock format [expr $time - $secs_per_week] -format "%Y"] if {$last_week == 51} { # If last week was week 51, then this must be week 52 not 53. # Force the year to be that of last week too. set week 52 set year $last_year } else { set next_week [clock format [expr $time + $secs_per_week] -format "%V"] set next_year [clock format [expr $time + $secs_per_week] -format "%Y"] if {$next_week == 2} { # If next week is week 02, then this must be week 01 not 53 set week 01 set year $next_year } else { # OK, this really is week 53 # Force the year to be that of last week too. set year $last_year } } } puts "${year}W$week" } calc [clock seconds]