Created
September 20, 2022 17:27
-
-
Save Buy-One/f0acecbb83143f82d084bafd6c5e3ecc to your computer and use it in GitHub Desktop.
Convert time in seconds to a formatted string hh:mm:ss.ms
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function format_time_given_in_sec(num_sec) | |
local function add_lead_zero(num) | |
return #(num..'') == 1 and '0'..num or num | |
end | |
local hrs = math.modf(num_sec/3600) -- 3600 sec in an hour | |
local sec = num_sec%3600 -- remainder in sec | |
local mnt = math.modf(sec/60) -- 60 sec in a min | |
local sec_ms = sec%60 -- remainder in sec and ms as a decimal part | |
local dec_places = 10^3 | |
local sec_ms = math.floor(sec_ms * dec_places + 0.5) / dec_places -- round ms down to 3 dec places | |
local col = ':' | |
return add_lead_zero(hrs)..col..add_lead_zero(mnt)..col..sec_ms | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment