Skip to content

Commit

Permalink
Nanosecond unix_now() on Linux. Closes #1
Browse files Browse the repository at this point in the history
  • Loading branch information
ancapdev committed Jul 22, 2020
1 parent 958451c commit d52532a
Showing 1 changed file with 16 additions and 3 deletions.
19 changes: 16 additions & 3 deletions src/UnixTimes.jl
Expand Up @@ -62,9 +62,22 @@ function Base.show(io::IO, x::UnixTime)
nothing
end

function unix_now()
tv = Libc.TimeVal()
UnixTime(Dates.UTInstant(Nanosecond(tv.sec * 1_000_000_000 + tv.usec * 1_000)))
if Sys.islinux()
struct LinuxTimespec
seconds::Clong
nanoseconds::Cuint
end
@inline function unix_now()
ts = Ref{LinuxTimespec}()
ccall(:clock_gettime, Cint, (Cint, Ref{LinuxTimespec}), 0, ts)
x = ts[]
UnixTime(Dates.UTInstant(Nanosecond(x.seconds * 1_000_000_000 + x.nanoseconds)))
end
else
@inline function unix_now()
tv = Libc.TimeVal()
UnixTime(Dates.UTInstant(Nanosecond(tv.sec * 1_000_000_000 + tv.usec * 1_000)))
end
end

end

0 comments on commit d52532a

Please sign in to comment.