See http://purl.org/tcl/home/man/tcl8.4/TclCmd/clock.htm for the formal man page. Beyond that, try the %Q format: % clock format [clock scan now] -format %Q Stardate 56657.7 See also [Star Trek], [A little A/D clock] ---- [AM] The clock command manipulates an integer number that starts at zero at the "epoch" (1 january 1970, IIRC, on UNIX and many other systems). This limits the valid range of dates a bit. Some experimentation however shows that the range ''1 january 1902'' to "31 december 2038'' is acceptable on UNIX and Windows. This may not include all dates of birth of now-living people, but it goes a long way. For the Mac this may be a slightly different range (1904-2039). ---- [LV] To use clock to provide today's date and time formatted, type clock format [clock scan now] ---- From news:comp.lang.tcl, I see: Date: Mon Mar 19 15:41:16 EST 2001 From: Jeff Hobbs Organization: ActiveState - Programming for the People atsekhanovsky@kraft.com wrote: Is there a way to do the following by using the clock command: If I have a date in the future, for example Mar 26, 2001 I need to find out what the date for that Saturday is. (Sat Mar 31, 2001) Apr 30, 2001 = Sat May 5, 2001 Mar 28, 2001 = Sat May 5, 2001 (hobbs) 50 % clock format [clock scan "Saturday" \ -base [clock scan "Apr 30, 2001"]] Sat May 05 00:00:00 Pacific Daylight Time 2001 (hobbs) 51 % clock format [clock scan "Saturday" \ -base [clock scan "Mar 28, 2001"]] Sat Mar 31 00:00:00 Pacific Standard Time 2001 ---- You can format relative time in seconds to h:m:s format. Just make sure you pretend to be in Greenwich( -gmt 1), otherwise your local timezone may be added. ''(RS)'' clock format 711 -format %H:%M:%S -gmt 1 01:11:51 See [Formatting durations] also. ---- rmax: Is there any chance to tell [[clock format]] to use another timezone than local or GMT? patthoyts: % set env(TZ) GMT ; clock format [clock seconds] Wed Jan 30 16:07:47 GMT 2002 % set env(TZ) GMT+2 ; clock format [clock seconds] Wed Jan 30 14:07:52 GMT 2002 "Use" means several different things; here's a different example of '''clock''''s intelligence about [timezone]s: clock format [clock seconds] -format "%X %x" -locale fi_FI -timezone EET ---- '''Traditional degrees''': clock format can be put to un-timely uses. As degrees especially in geography are also subdivided in minutes and seconds, how's this one-liner for formatting decimal degrees: proc dec2deg x {concat [expr int($x)]\u00B0 [clock format [expr round($x*3600)] -format "%M' %S\"" -gmt 1]} The additional -gmt 1 switch is needed if you happen to live in a non-integer timezone. (RS) ---- Want to format the modification time of a specific file? Try clock format [file mtime "/path/to/my/file"] where you replace /path/to/my/file with the path you want. Or replace it with a variable containing the path. ---- To get back, from clock, yesterday's date, use clock format [clock scan yesterday] ---- See "[timezone]" for a list of recognized timezones. ---- In the year 2002, the first week reported by %w is 00. To get more natural numbers starting from 1 (and without leading zero), use expr [scan [clock format [clock sec] -format %W] %d]+1 ;#RS This was not the case in the years {1973 1979 1990 1996 2001 2007 2018 2024 2029 2035}, a distance sequence of 6-5-6-11.., where the first week was 01... Hence this cleanup routine, where Jan 1 is always in week 1: proc week {date} { set week [scan [clock format $date -format %W] %d] set year [clock format $date -format %Y] if {[lsearch { 1973 1979 1990 1996 2001 2007 2018 2024 2029 2035 } $year] < 0} { incr week } set week } ;# RS - but better just use %V: ''[Kevin Kenny] wrote on this question in [the comp.lang.tcl newsgroup] on 2001-01-07, so in week 01 ;-):'' The format group, %V, yields the ISO8601 fiscal week number. It's inexplicably missing from the documentation in 8.3; this has been fixed in 8.4. By the way, lots of companies in the US (my employer is NOT one) use a fiscal week numbering system where week 1 begins on the first Sunday of the year rather than on the Monday before the first Thursday. ---- [NEM] - From a question in comp.lang.tcl and a session on the chat, here is a proc for getting the start and end days of a week given a week number (0-51): proc dates {date} { set date [clock scan "$date weeks" -base [clock scan "01/01/2002"]] set st [clock scan "sunday" -base [expr {$date - 436800}]] set end [clock scan "saturday" -base $date] return [list [clock format $st] [clock format $end]] } Thus, dates 48 gives {Sun Dec 01 00:00:00 GMT 2002} {Sat Dec 07 00:00:00 GMT 2002} [NEM] - After some heckling on comp.lang.tcl for not allowing you to specify a year, or what day the week starts on, here is an improvement: proc dates {date {year now} {day "Sunday"}} { set days [list sun mon tue wed thu fri sat] set day [string tolower [string range $day 0 2]] if {[string equal $year now]} { set year [clock format [clock scan now] -format %Y] } set date [clock scan "$date weeks" -base [clock scan "01/01/$year"]] set st [clock scan $day -base [expr {$date - 436800}]] set endday [lsearch $days $day] set endday [expr {($endday - 1) % 7}] set endday [lindex $days $endday] set end [clock scan $endday -base $date] return [list $st $end] } It does all the necessary magic. It also returns the numbers rather than pre-formatting them, as this is more flexible. One improvement would be to allow specifying the end day of the week as well (for working weeks, etc). You could do this with an extra parameter, and only calculating the endday if it was not set. ---- Here is a little procedure that will let you do timestamping accurate to the millisecond. It requires Tcl 8.4 or greater; while 8.3 supports [[clock clicks -milliseconds]], the 'clicks' and the 'seconds' are not properly synchronized on all platforms. The procedure was originally sent by [Kevin Kenny] on comp.lang.tcl. I have modified it a bit so that it can give back the second and millisecond information separately to the caller. [EF] package require Tcl 8.4 proc timestamp { { tv_sec_p "" } { tv_msec_p "" } } { if { $tv_sec_p != "" } { upvar $tv_sec_p secs } if { $tv_msec_p != "" } { upvar $tv_msec_p fract } set secs [clock seconds] set ms [clock clicks -milliseconds] set base [expr { $secs * 1000 }] set fract [expr { $ms - $base }] if { $fract > 1000 } { set diff [expr { $fract / 1000 }] incr secs $diff incr fract [expr { -1000 * $diff }] } return $secs.[format %03d $fract] } [MH] (2005-10-04) Never mind.. Should have tested it first. Works as advertised :-) ---- '''Relative time access by hours, minutes, seconds''': If you work with relative times in seconds, you can extract its components with [[clock scan]] and [[clock format]], but you can also do it with direct arithmetics ([RS]): proc time'h time {expr {($time%86400)/3600}} proc time'm time {expr {($time%3600)/60}} proc time's time {expr {$time%60}} ---- [KBK] (2004-11-29) wishes to open a discussion on the subject of [Parsing ISO8601 dates and times] ---- [DKF] (2005-05-30): Working out someone's age can be tricky. Luckily, the [clock] command (with some helping [expr] trickery) can provide a lot of support for this. # Date-of-birth (in seconds relative to epoch) in $dob foreach {Ynow CMPnow} [clock format [clock seconds] -format "%Y 1%m%d"] {} foreach {Ydob CMPdob} [clock format $dob -format "%Y 1%m%d"] {} set ageInYears [expr { $Ynow-$Ydob-($CMPnow<$CMPdob) }] ---- [KBK]: "http://www.cl.cam.ac.uk/~mgk25/uts.txt is Tcl's model of time ..." [jmn] 2006-06-24 So presumably this means that Tcl is adjusting the output of [[clock seconds]], [[clock milliseconds]] etc with a slight offset for a period of 1000 seconds prior to each leap second? Is this then on top of a separate skewing that may already be occuring as an NTP client adjusts the system clock to keep in sync with the leap second? I've heard for example than some windows NTP clients will start skewing the clock an hour before the leap second. Do we really have two separate adjustments being made around this time? (I assume the NTP one to the actual system clock, the Tcl 'adjustment' merely being to reported output?) How on earth then would one compare timestamps generated on a system that uses TAI? This is a complicated issue - and I think my understanding of it is pretty limited, but what are the possibilities say of extending [[clock seconds]] etc so that we know exactly what timestamps we're actually dealing with? e.g [[clock seconds UTS]] - presumably the Tcl skewed implementation that exists now? [[clock seconds UTC]] - tracks UTC - including ambiguities around leap seconds [[clock seconds TAI]] - I guess it would require leap-second lookup tables if the system clock isn't already directly in TAI Anyone care to comment on the feasability and/or desirability of this? ---- [MG] See also [Bag of number/time spellers] ---- * [A base-5 clock] * [A little countdown clock] * [An analog clock in Tk] * [an analogue countdown clock-face] * [An arc clock] * [Binary clock] * [clock and daylight saving time] * [clock forward compatibility] * [clock milliseconds] * [clock seconds] * [clock.tcl] * [curses digital clock] * [Date and Time Issues] * [Localized clock] * [Reworking the clock command] ---- [[ [Arts and crafts of Tcl-Tk programming] [Tcl syntax help] | [Category Command] | [Category Date and Time] ]]