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

Clock-Milliseconds

994 views
Skip to first unread message

pnam

unread,
Sep 12, 2011, 1:29:21 PM9/12/11
to
Hello,

I'm a beginner in TCL programming and am modify an existing code on an
earlier version of tcl. I am interested in extracting the time in
milliseconds. clock milliseconds give an error message. I know we can
get the relative value by using clock click -milliseconds.

Is there any way of extracting absolute time in milliseconds.

Thanks in advance,

-Priya

Gerald W. Lester

unread,
Sep 12, 2011, 1:36:30 PM9/12/11
to pnam
On 9/12/11 12:29 PM, pnam wrote:
> Hello,
>
> I'm a beginner in TCL programming and am modify an existing code on an
> earlier version of tcl. I am interested in extracting the time in
> milliseconds. clock milliseconds give an error message. ...

What version of Tcl are you attempting to use ([clock milliseconds] works
great in 8.5.7 -- I'm pretty sure it was introduced in 8.5.0)?

--
+------------------------------------------------------------------------+
| Gerald W. Lester, President, KNG Consulting LLC |
| Email: Gerald...@kng-consulting.net |
+------------------------------------------------------------------------+

Kevin Kenny

unread,
Sep 12, 2011, 2:28:30 PM9/12/11
to
Is there any possibility of upgrading to Tcl 8.5? 8.4 is quite
obsolete, and you'll find a number of things that won't work if you
try to apply current documentation to it.

If you absolutely must use 8.4, the following code ought to give you
milliseconds that are correctly synchronized with the seconds:


package require Tcl 8.4
proc timestamp {} {
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]
}

But you really ought to upgrade.

--
73 de ke9tv/2, Kevin

Ralf Fassel

unread,
Sep 13, 2011, 7:47:15 AM9/13/11
to
* Kevin Kenny <ken...@acm.org>
| If you absolutely must use 8.4, the following code ought to give you
| milliseconds that are correctly synchronized with the seconds:
>
| package require Tcl 8.4
| proc timestamp {} {
| set secs [clock seconds]
| set ms [clock clicks -milliseconds]

man clock(n)
...
HIGH RESOLUTION TIMERS
...
In addition, there is a clock clicks command that returns a
platform-dependent high-resolution timer. Unlike clock seconds
and clock milliseconds, the value of clock clicks is not
guaranteed to be tied to any fixed epoch; it is simply intended
to be the most precise interval timer available, and is intended
only for relative timing studies such as benchmarks.

So while 'clock milliseconds' is documented to have the same starting
time as 'clock seconds', 'clock clicks -milliseconds' probably does not
share the same starting time as 'clock seconds', and the sub-second part
calculated from the above code might be wrong.

R'

Gerald W. Lester

unread,
Sep 13, 2011, 10:32:52 AM9/13/11
to
Ralf, are you aware that Kevin Kenny (the person you replied to) did most of
the rewrite for the clock command in 8.5?

In other words, I'd trust Kevin before the man page on this one.

Kevin Kenny

unread,
Sep 13, 2011, 10:48:44 AM9/13/11
to
On 09/13/2011 10:32 AM, Gerald W. Lester wrote:

> Ralf, are you aware that Kevin Kenny (the person you replied to) did
> most of the rewrite for the clock command in 8.5?
>
> In other words, I'd trust Kevin before the man page on this one.

Uh, what Gerald said. :)

I wrote the underlying code that 8.4 uses to implement [clock clicks
-milliseconds] - and the Tcl procedure that I supplied works correctly
on both Unix and Windows. [clock clicks] without -milliseconds will
*not* synchronize correctly without more work; see TIP 7 for the
details of disciplining the two oscillators.

The only way in 8.4 that the milliseconds *don't* stay in sync with
the seconds is that the milliseconds are kept in a 32-bit field.
Every 2**32 milliseconds (roughly every 49.7 days), the field overflows
from 2**31-1 to -2**31. The code that I posted in the earlier mail
handles this condition correctly by recognizing that the 32-bit
field is actually the least significant 32 bits of "milliseconds
since the Epoch" and acting accordingly. It works.

It depends on a promise that the documentation doesn't make, but
the code is what it is, and the 8.4 code isn't going to change at
this late date.

8.5 does promise that the milliseconds stay in synchrony with the
seconds. (The procedure will continue to work in 8.5; it just does
needless work checking for nonexistent 2**32-millisecond rollovers.)

Ralf Fassel

unread,
Sep 13, 2011, 2:17:13 PM9/13/11
to
* "Gerald W. Lester" <Gerald...@KnG-Consulting.net>
| Ralf, are you aware that Kevin Kenny (the person you replied to) did
| most of the rewrite for the clock command in 8.5?

Of course not...thanks for pointing that out. :-/

At least I used 'probably' and 'might' in my posting :-), since I have
not looked up the C code to see how 'clock' was implemented in 8.4...

Kenny, sorry for the confusion.

R'

pnam

unread,
Sep 26, 2011, 11:58:15 AM9/26/11
to
On Sep 12, 2:28 pm, Kevin Kenny <kenn...@acm.org> wrote:
> On 09/12/2011 01:29 PM, pnam wrote:
>
> > Hello,
>
> > I'm a beginner in TCL programming and am modify an existing code on an
> > earlier version of tcl. I am interested in extracting the time in
> > milliseconds.clockmilliseconds give an error message. I know we can
> > get the relative value by usingclockclick -milliseconds.
>
> > Is there any way of  extracting absolute time in milliseconds.
>
> > Thanks in advance,
>
> > -Priya
>
> Is there any possibility of upgrading to Tcl 8.5?  8.4 is quite
> obsolete, and you'll find a number of things that won't work if you
> try to apply current documentation to it.
>
> If you absolutely must use 8.4, the following code ought to give you
> milliseconds that are correctly synchronized with the seconds:
>
>    package require Tcl 8.4
>    proc timestamp {} {
>      set secs [clockseconds]
>      set ms [clockclicks -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]
>    }
>
> But you really ought to upgrade.
>
> --
> 73 de ke9tv/2, Kevin

Also I would like to use microseconds instead of milliseconds.
Final output: Human readable date in microseconds resolution

pnam

unread,
Sep 26, 2011, 11:52:00 AM9/26/11
to
On Sep 12, 2:28 pm, Kevin Kenny <kenn...@acm.org> wrote:
> On 09/12/2011 01:29 PM, pnam wrote:
>
> > Hello,
>
> > I'm a beginner in TCL programming and am modify an existing code on an
> > earlier version of tcl. I am interested in extracting the time in
> > milliseconds.clockmilliseconds give an error message. I know we can
> > get the relative value by usingclockclick -milliseconds.
>
> > Is there any way of  extracting absolute time in milliseconds.
>
> > Thanks in advance,
>
> > -Priya
>
> Is there any possibility of upgrading to Tcl 8.5?  8.4 is quite
> obsolete, and you'll find a number of things that won't work if you
> try to apply current documentation to it.
>
> If you absolutely must use 8.4, the following code ought to give you
> milliseconds that are correctly synchronized with the seconds:
>
>    package require Tcl 8.4
>    proc timestamp {} {
>      set secs [clockseconds]
>      set ms [clockclicks -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]
>    }
>
> But you really ought to upgrade.
>
> --
> 73 de ke9tv/2, Kevin

Hi Kevin
How do we convert this to human readable date.

-P

Kevin Kenny

unread,
Sep 27, 2011, 8:53:55 AM9/27/11
to
In 8.4 or 8.5?

In 8.5, do [clock microseconds], divide the result by 1000000,
and pass the result to [clock format]. Format the remainder of the
division separately with a group like %06d. (And be aware that at
least on Windows, the microseconds are more-or-less fictitious;
see TIP #7 for the attempt that we make to keep them in line.)

In 8.4, as long as you're on MacOSX. Linux, Solaris, or Windows,
clicks happen to be microseconds, so you can get away with code
like the 'timestamp' procedure above. But the ice is getting very
thin here. You really need to be on 8.5.

aureu...@gmail.com

unread,
Sep 24, 2013, 9:40:07 AM9/24/13
to
What do you mean by works ? It is possible to compare the value with another machine with milliseconds timestamp ?

Kevin Kenny

unread,
Sep 29, 2013, 7:05:48 PM9/29/13
to
On 09/24/2013 09:40 AM, aureu...@gmail.com wrote:

> What do you mean by works ? It is possible to compare the value with another machine with milliseconds timestamp ?
>

If the machines' clocks are synchronized to that level of accuracy.

That's possible over at least short distances (or wires with predictable
latency) using NTP, or by deriving your time from a reference like GPS,
or by having an in-house Stratum 1 time server distributing time over a
local network. Millisecond-level synchronization among remote hosts
is still tricky.

But this is outside the realm of what Tcl is responsible for: if
the underlying system returns time-of-day to millisecond accuracy,
so will Tcl.
0 new messages