Using 2 digit Millisecond

Purpose:  Lua does not have milliseconds and sometimes you have a need to output a two digit millisecond. I built this particularly for when outputting HL7 messages to a file i needed the files to be in sequence based upon milliseconds.  If you are just trying to benchmark then you can use os.clock but it gets a little trickier when trying to get a valid two digit millisecond.

Links:

Steps to Build:

  • Open a Translator
  • Create a Shared module and name it ‘milliseconds’
  • Paste code into module
    function ms()
    mil = tostring(string.format("%.2f", os.clock()))
       mil = mil:sub(mil:find('%.'),mil:len())
       mil = 60 * mil
       mil = tostring(mil)
       subd = mil:find('%.')
    
       if mil:len() == 4 then 
          mil = mil:sub(0, subd -1)
       elseif mil:len() == 3 then 
          mil = '0' .. mil:sub(0, subd-1)
       elseif mil:len() == 2 then 
          mil = mil:sub(0, subd)
       elseif mil:len() == 1 then
          mil = '0'..mil:sub(0, subd)
       end
       return mil
    
    end
  • Import shared module from main script by entering “require ‘millisecond'”
  • Test it out by typing trace(ms()) in an active function

Leave A Comment?

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