]> 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:

luatz/timetable: Check argument to `new_from_timestamp`
[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 then
6                 if syscall.clock_gettime then
7                         local clock_id = syscall.c.CLOCK.REALTIME
8                         local function timespec_to_number ( timespec )
9                                 return tonumber ( timespec.tv_sec ) + tonumber ( timespec.tv_nsec ) * 1e-9
10                         end
11                         return "syscall.clock_gettime(CLOCK_REALTIME)" ,
12                                 syscall.clock_getres and timespec_to_number ( syscall.clock_getres ( clock_id ) ) or 1e-9 ,
13                                 function ( )
14                                         return timespec_to_number ( syscall.clock_gettime ( clock_id ) )
15                                 end
16
17                 elseif syscall.gettimeofday then
18                         local function timeval_to_number ( timeval )
19                                 return tonumber ( timeval.tv_sec ) + tonumber ( timeval.tv_nsec ) * 1e-6
20                         end
21                         return "syscall.gettimeofday()" , 1e-6 ,
22                                 function ( )
23                                         return timeval_to_number ( syscall.gettimeofday ( ) )
24                                 end
25                 end
26         end
27
28         local has_socket , socket = pcall ( require , "socket" )
29         if has_socket and socket.gettime then
30                 -- on windows, this uses GetSystemTimeAsFileTime, which has resolution of 1e-7
31                 -- on linux, this uses gettimeofday, which has resolution of 1e-6
32                 return "socket.gettime()" , 1e-6 , socket.gettime
33         end
34
35         return "os.time()" , 1 , os.time
36 end)()
37
38 return _M