Nanosecond epoch time - POSIXClock.jl refresh

These are just notes on updating from a conversation on Slack:

const librt = Sys.isunix() ? Libdl.find_library(["librt.so"]) : nothing
mutable struct Clock{IsUnix}
    sec::Int
    nsec::Int
end
function Clock()
    return Clock{Sys.isunix()}(0,0)
end
const GlobalClock = Clock()
function get_time(c::Clock{true})::Int
    ccall((:clock_gettime, librt), Int32, (Int32, Ptr{Clock}), Int32(0), pointer_from_objref(c))
    return c.sec * 1000000000 + c.nsec
end
function get_time(::Clock{false})::Int
    return round(Int,time() * 1e9)
end
# not thread safe to use GlobalClock so be careful
function get_time()
    return get_time(GlobalClock)
end
2 Likes