Last active
December 22, 2015 01:28
-
-
Save scryptonite/6396424 to your computer and use it in GitHub Desktop.
Millisecond accurate time. I tried several concepts, most of it being wonky-calibration stuff that turned out to be inaccurate (micro leaps back in time). This is the one I am most satisfied with.
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
local function clock() | |
return (math.floor(os.clock() * 1000)/1000) | |
end | |
local _start_time = os.time() | |
local _start_clock = clock() | |
local _last | |
function now() | |
-- Sanity check. See http://lua-users.org/wiki/SleepFunction | |
-- > -- warning: clock can eventually wrap around for sufficiently large n | |
-- > -- (whose value is platform dependent). Even for n == 1, clock() - t0 | |
-- > -- might become negative on the second that clock wraps. | |
local _now_clock = clock(); | |
if _now_clock <= _start_clock then | |
_start_time = os.time() | |
_start_clock = _now_clock | |
end | |
local _return = _start_time + (clock() - _start_clock) | |
-- Another sanity check, which could leave something like animations hanging. | |
-- If it does, you shouldn't be doing anything that is time sensitive on your | |
-- target platform with lua. | |
if _return < _last then | |
return _last | |
end | |
_last = _return | |
return _return | |
end | |
_last = now() | |
return now |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment