How to build milliseconds value for timestamp from os.clock() reading

Another method, to build a timestamp with milliseconds, is to use the milliseconds from the time reported by os.clock().

The os.clock() function returns the CPU time since Lua started in seconds, with milliseconds precision.

We will use only the milliseconds part of it, and concatenate it with the current timestamp.

A small correction is needed when the milliseconds value is zero. See the example below.

function main(Data)
   local a,b = math.modf(os.clock())
   if b==0 then 
      b='000' 
   else 
      b=tostring(b):sub(3,5) 
   end

   local tf=os.date('%Y-%m-%d %H:%M:%S.',os.time())
   trace(tf..b )

   local tnf=os.date('%Y%m%d%H%M%S.',os.time())
   trace(tnf.. b) 

end

Leave A Comment?

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.