hudac
hudac

Reputation: 2798

getting time in milliseconds from a date

Is it possible to take a string of date, such as 2013-02-26 10:45:54,082 and get the time in milliseconds from the "epoch" ? or something similar?

I know clock scan returns the time in seconds. Also clock scan returns an error for this specific format.

I couldn't find something as I want, and I want to make sure it doesn't exists before I start creating it...

Thanks

Upvotes: 1

Views: 2743

Answers (1)

Donal Fellows
Donal Fellows

Reputation: 137637

The millisecond-scale offset isn't supported by Tcl (as it is a fairly rare format in practice), but you can work around that:

set str "2013-02-26 10:45:54,082"
regexp {^([^\.,]*)[\.,](\d+)$} $str -> datepart millipart; # split on the last occurrence of , or . 
set timestamp [clock scan $datepart -format "%Y-%m-%d %H:%M:%S" -gmt 1]
scan $millipart "%d" millis
set millistamp [expr {$timestamp*1000 + $millis}]

(You don't supply timezone info, so I've assumed it is UTC, i.e., “GMT”. I've also allowed for using . instead of , as a millisecond separator, as which you get depends on the locale that created the time string in the first place.)

Upvotes: 4

Related Questions