datetime in milliseconds in lua scripting language

 I wanted to have datetime value  up to milliseconds in a lua script that was called in kamailio using app_lua and here is the way I found how to do it 


function os.capture(cmd, raw)

  local f = assert(io.popen(cmd, 'r'))

  local s = assert(f:read('*a'))

  f:close()

  if raw then return s end

  s = string.gsub(s, '^%s+', '')

  s = string.gsub(s, '%s+$', '')

  s = string.gsub(s, '[\n\r]+', ' ')

  return s

end


now =  os.capture('date +"%Y-%m-%d %H:%M:%S.%3N"')

print(now)


Output : 2023-02-07 10:24:36.648


Comments