1212#include <stdint.h>
1313#include <time.h>
1414
15- #define PUSH_TIMESPEC (L , t ) \
16- lua_pushnumber((L), (double)(t).tv_sec + (double)(t).tv_nsec * 1e-9)
17-
1815/* time.monotonic() */
1916static int time_monotonic (lua_State * restrict L )
2017{
2118 struct timespec t ;
22- if (clock_monotonic (& t )) {
19+ if (! clock_monotonic (& t )) {
2320 lua_pushinteger (L , -1 );
2421 return 1 ;
2522 }
26- PUSH_TIMESPEC (L , t );
23+ lua_pushnumber (L , TIMESPEC_NANO ( t ) * 1e-9 );
2724 return 1 ;
2825}
2926
3027/* time.process() */
3128static int time_process (lua_State * restrict L )
3229{
3330 struct timespec t ;
34- if (clock_process (& t )) {
31+ if (! clock_process (& t )) {
3532 lua_pushinteger (L , -1 );
3633 return 1 ;
3734 }
38- PUSH_TIMESPEC (L , t );
35+ lua_pushnumber (L , TIMESPEC_NANO ( t ) * 1e-9 );
3936 return 1 ;
4037}
4138
4239/* time.thread() */
4340static int time_thread (lua_State * restrict L )
4441{
4542 struct timespec t ;
46- if (clock_thread (& t )) {
43+ if (! clock_thread (& t )) {
4744 lua_pushinteger (L , -1 );
4845 return 1 ;
4946 }
50- PUSH_TIMESPEC (L , t );
47+ lua_pushnumber (L , TIMESPEC_NANO ( t ) * 1e-9 );
5148 return 1 ;
5249}
5350
5451/* time.wall() */
5552static int time_wall (lua_State * restrict L )
5653{
5754 struct timespec t ;
58- if (clock_realtime (& t )) {
55+ if (! clock_realtime (& t )) {
5956 lua_pushinteger (L , -1 );
6057 return 1 ;
6158 }
62- PUSH_TIMESPEC (L , t );
59+ lua_pushnumber (L , TIMESPEC_NANO ( t ) * 1e-9 );
6360 return 1 ;
6461}
6562
@@ -71,20 +68,18 @@ static int time_measure(lua_State *restrict L)
7168 lua_insert (L , 1 );
7269 bool ok = true;
7370 struct timespec ts0 , ts1 ;
74- if (clock_monotonic (& ts0 )) {
71+ if (! clock_monotonic (& ts0 )) {
7572 ok = false;
7673 }
7774 lua_call (L , nargs , LUA_MULTRET );
78- if (clock_monotonic (& ts1 )) {
75+ if (! clock_monotonic (& ts1 )) {
7976 ok = false;
8077 }
8178 const int nres = lua_gettop (L );
8279 if (!ok ) {
8380 return nres ;
8481 }
85- lua_pushnumber (
86- L , (double )(ts1 .tv_sec - ts0 .tv_sec ) +
87- (double )(ts1 .tv_nsec - ts0 .tv_nsec ) * 1e-9 );
82+ lua_pushnumber (L , TIMESPEC_DIFF (ts0 , ts1 ) * 1e-9 );
8883 lua_replace (L , 1 );
8984 return nres ;
9085}
0 commit comments