Forum Discussion

Eduardo_Saito_1's avatar
Eduardo_Saito_1
Icon for Nimbostratus rankNimbostratus
Feb 05, 2009

Timestamp in ms.

Hello guys.

 

 

 

Does anybody know how can I create a timestamp that can be used in ms?

 

I'm using [clock seconds] but it leaves me with a 1 second accuracy problem.

 

 

 

Thanks!

 

 

 

Eduardo Saito

1 Reply

  • Hi Eduardo,

     

     

    Are you trying to log deltas between events with ms accuracy? Or are you trying to just log the current time with ms accuracy? For the former, you can use [clock clicks -milliseconds].

     

     

    The caveat with this on the TCL man page is:

     

     

     

    http://www.tcl.tk/man/tcl8.4/TclCmd/clock.htmM5

     

     

    clock clicks ?-milliseconds?

     

    Return a high-resolution time value as a system-dependent integer value. The unit of the value is system-dependent but should be the highest resolution clock available on the system such as a CPU cycle counter. If -milliseconds is specified, then the value is guaranteed to be of millisecond granularity. This value should only be used for the relative measurement of elapsed time.

     

     

     

     

    So I'm not sure whether it's possible to get the current time with millisecond accuracy in an efficient manner.

     

     

    There was a suggestion from a poster on this TCL wiki page:

     

     

     

    http://wiki.tcl.tk/1035

     

     

    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]

     

    }

     

     

     

     

    9.x doesn't support proc's so you'd need to convert this to standard code and run it every time you want to log the current time with ms accuracy. I think 10 might support procs, so maybe it would be more feasible to use this soon.

     

     

    Aaron