How to print absolute time in TCL and relative time in TCL?

How to print absolute time in TCL?

The output would be something like 2018-03-07-14:32:09.040+05:30I-----

I want to create a logic to print epoch microseconds. but I cannot use in build clock milliseconds to print the epoch microseconds.

Upvotes: 0

Views: 5506

Answers (2)

M. D. P
M. D. P

Reputation: 764

This might help you :

set milliseconds [clock clicks]
variable time [format "%s_%03d" [clock format [clock seconds] -format %Y%m%d_%H%M%S] [expr {$milliseconds % 1000}] ]

puts $time

Upvotes: 0

jpo38
jpo38

Reputation: 21604

You should refer to the TCL documentation for clock microseconds:

Returns the current time as an integer number of microseconds.

However, clock format does not support this high resolution clock values. So you'll have to manually extract the microsecond decimal part of the result, use clock format to output the value with seconds precision and later manually add microseconds related decimal value (".XXXXXX") to it.

This scripts will output time with microseconds precision:

# get current time with microseconds precision:
set val [clock microseconds]
# extract time with seconds precision:
set seconds_precision [expr { $val / 1000000 }]
# find number of microseconds:
set microseconds [expr { $val - $seconds_precision*1000000 }]

# output the epoc with microseconds precision:
puts [format "%s.%06d" [clock format $seconds_precision -format "%Y-%m-%d %H:%M:%S"] $microseconds]

Outputs 2018-03-07 11:04:59.946580

You can adapt the script if you prefer milliseconds precision or if you want to show time zone.

Upvotes: 2

Related Questions