]> git.madduck.net Git - etc/awesome.git/blob - luatz/gettime.lua

madduck's git repository

Every one of the projects in this repository is available at the canonical URL git://git.madduck.net/madduck/pub/<projectpath> — see each project's metadata for the exact URL.

All patches and comments are welcome. Please squash your changes to logical commits before using git-format-patch and git-send-email to patches@git.madduck.net. If you'd read over the Git project's submission guidelines and adhered to them, I'd be especially grateful.

SSH access, as well as push access can be individually arranged.

If you use my repositories frequently, consider adding the following snippet to ~/.gitconfig and using the third clone URL listed for each project:

[url "git://git.madduck.net/madduck/"]
  insteadOf = madduck:

examples/date_arithmetic.lua: Remove unused c2 var
[etc/awesome.git] / luatz / gettime.lua
1 local _M = { }
2
3 _M.source , _M.resolution , _M.gettime = (function()
4         local has_syscall , syscall = pcall ( require , "syscall" )
5         if has_syscall and syscall.clock_gettime and syscall.c.CLOCK then
6                 local clock_id = syscall.c.CLOCK.REALTIME
7                 local function timespec_to_number ( timespec )
8                         return tonumber ( timespec.tv_sec ) + tonumber ( timespec.tv_nsec ) * 1e-9
9                 end
10                 return "syscall.clock_gettime(CLOCK_REALTIME)" ,
11                         syscall.clock_getres and timespec_to_number ( syscall.clock_getres ( clock_id ) ) or 1e-9 ,
12                         function ( )
13                                 return timespec_to_number ( syscall.clock_gettime ( clock_id ) )
14                         end
15         end
16
17         local has_unix , unix = pcall ( require , "unix" )
18         -- On Apple devices lunix only uses gettimeofday()
19         if has_unix and unix.clock_gettime and unix.uname and unix.uname().sysname ~= "Darwin" then
20                 return "unix.clock_gettime(CLOCK_REALTIME)" , 1e-9 , function ( )
21                         return unix.clock_gettime ( )
22                 end
23         end
24
25         if has_syscall and syscall.gettimeofday then
26                 local function timeval_to_number ( timeval )
27                         return tonumber ( timeval.tv_sec ) + tonumber ( timeval.tv_nsec ) * 1e-6
28                 end
29                 return "syscall.gettimeofday()" , 1e-6 ,
30                         function ( )
31                                 return timeval_to_number ( syscall.gettimeofday ( ) )
32                         end
33         end
34
35         if has_unix and unix.gettimeofday then
36                 return "unix.gettimeofday()", 1e-6, unix.gettimeofday
37         end
38
39         local has_socket , socket = pcall ( require , "socket" )
40         if has_socket and socket.gettime then
41                 -- on windows, this uses GetSystemTimeAsFileTime, which has resolution of 1e-7
42                 -- on linux, this uses gettimeofday, which has resolution of 1e-6
43                 return "socket.gettime()" , 1e-6 , socket.gettime
44         end
45
46         if ngx and ngx.now then -- luacheck: ignore 113
47                 return "ngx.now()" , 1e-3 , ngx.now -- luacheck: ignore 113
48         end
49
50         return "os.time()" , 1 , os.time
51 end)()
52
53 return _M