This topic contains 0 replies, has 1 voice, and was last updated by  lev 4 years, 9 months ago.

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

  • Simple method to build timestamp with milliseconds is to take the milliseconds part of time reported by os.clock() function.

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

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

    Small adjustment has been added to cover instances when milliseconds value is zero.

    See 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())
       print (tf..b )
       
       local tnf=os.date('%Y%m%d%H%M%S.',os.time())
       print (tnf.. b) 
       
    end

You must be logged in to reply to this topic.