How to create RFC 3339 timestamp in TCL?

I inherited a TCL script (I have zero familiarity with the language) and you need to add an RFC 3339 timestamp to it:

2012-04-05T12: 13: 32.123456-08: 00

After googling, I didn't find any means to display microseconds or timeline offset (I found a way to show the timezone name, but that doesn't help).
Is there a way to do this without calling an external process?

+3


source to share


1 answer


In TCL8.5, you can try the following command:

% clock format [clock seconds] -format "%Y-%m-%dT%T%z"
2012-04-05T16:06:07-0500

      

This gives you everything but sub-seasonal permission. The command clock microseconds

will give you the time in microseconds, but I cannot find the format string id that matches it. You can use this to create your own command from scratch:



proc timestamp_rfc3339 {} {
    set us [clock microseconds]
    set sec [expr {$us / 1000000}]
    set micro [expr {$us % 1000000}]
    set ts [clock format $sec -format "%Y-%m-%dT%T"]
    regexp {(...)(..)} [clock format $sec -format "%z"] matched tzh tzm
    return [format "%s.%06s%s:%s" $ts $micro $tzh $tzm]
}

      

Running this result results in a timestamp eg 2012-04-05T16:35:06.366378-05:00

.

Edit: Updated code sample to include user1179884 settings (see comments) and wrap in proc.

+4


source







All Articles