Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How to convert output from [clock clicks] to human readable format?

456 views
Skip to first unread message

Ahmad

unread,
Aug 2, 2010, 4:13:50 PM8/2/10
to
Hi,

I am running a script that involves writing a log file.

I use [clock clicks] at the start and end to get time stamps.

Then, I use: run_time [expr {$end - $start}]

But result is not human meaningful.

How to convert it to a format like: hours : mins : seconds :
milliseconds ?

Thanks for help in advance,
Ahmad

Aric Bills

unread,
Aug 2, 2010, 4:33:48 PM8/2/10
to

According to the clock man page, [clock clicks] is platform-
dependent. If you want milliseconds instead, try [clock
milliseconds]; if you want microseconds, try [clock microseconds].

Joe English

unread,
Aug 2, 2010, 5:40:11 PM8/2/10
to
Aric Bills wrote:
> Ahmad asked:

>> I use [clock clicks] at the start and end to get time stamps.
>> Then, I use: run_time [expr {$end - $start}]
>> But result is not human meaningful.
>> How to convert it to a format like: hours : mins : seconds :
>> milliseconds  ?
>
> According to the clock man page, [clock clicks] is platform-
> dependent. If you want milliseconds instead, try [clock
> milliseconds]; if you want microseconds, try [clock microseconds].


And if (as I suspect) you want seconds, try [clock seconds].

You might also find the following useful:

proc format-elapsed {sec} {
set s [expr {$sec % 60}] ; set rem [expr {$sec / 60}]
set m [expr {$rem % 60}] ; set rem [expr {$rem / 60}]
set h $rem
return [format "%02d:%02d:%02d" $h $m $s]
}
proc tick {} {
variable starttime
set starttime [clock seconds]
}
proc tock {} {
variable starttime
return [format-elapsed [expr {[clock seconds] - $starttime}]]
}


Sample usage:

tick;
# ... some long-running process ...
puts "Elapsed time: [tock]"


--JE

phil

unread,
Aug 6, 2010, 11:24:44 AM8/6/10
to
I prefer floating point.

clicks-to-secs:
set z [format %.05f [expr ($thenclicks - $nowclicks) / 1000000.0]]

millis-to-secs:
set z [format %.03f [expr ($thenmillis - $nowmillis) / 1000.0]]

I've just recently started to use millis and like it.
Precision isn't as high, but I'm sure the precision can't really be
trusted anyway.
Every so often it would go backwards too.

Uwe Klein

unread,
Aug 6, 2010, 12:05:48 PM8/6/10
to

roll over?

I remember having to jump through hoops to get UTC time with fractional
seconds resolution working. i.e. finding the relation (divisor, remainder)
between [clock clicks] and [clock seconds]. What a hassle.

The previous solution was much easier: just encode the DCF signal state
and the utime value from the OS into the header of each data chunk.

uwe

Alexandre Ferrieux

unread,
Aug 7, 2010, 6:05:49 AM8/7/10
to

Then, use (the relatively recent) [clock milliseconds]. It is a
bignum, hence doesn't wrap around, and has the remarkable property of
being consistent with [clock seconds]:

[clock milliseconds]/1000 == [clock seconds]
[clock milliseconds]%1000 == remainder in milliseconds

That makes it the silver bullet for full timestamps with date down to
ms.
(but of course the intrinsic accuracy is what the OS gives you ;-)

-Alex

0 new messages