Skip to content

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
...
  • 3 commits
  • 3 files changed
  • 0 commit comments
  • 2 contributors
Commits on May 29, 2012
@gvvaughan gvvaughan gettimeofday: change format to a table with sec and usec fields.
* lposix.c (Pgettimeofday): Return a single table with sec and usec
fields, rather than a pair of numbers.
(Ptimeradd, Ptimercmp, Ptimersub): New functions to emulate the
macros timeradd, timercmp and timersub respectively.
c1a0e7b
Commits on Jun 01, 2012
@gvvaughan gvvaughan gettimeofday: replace C timer macros with lua equivalents.
* lposix.c (Ptimeradd, Ptimercmp, Ptimersub): Remove.
* posix.lua (timeradd, timercmp, timersub): New Lua equivalents
to removed C implementations.
1aa9175
Commits on Jun 04, 2012
@rrthomas Merge pull request #41 from gvvaughan/pull-request/timer-macros
gettimeofday: change format to a table with sec and usec fields.
3b07f2e
Showing with 68 additions and 1 deletion.
  1. +7 −1 lposix.c
  2. +50 −0 posix.lua
  3. +11 −0 tests-posix.lua
View
8 lposix.c
@@ -1832,9 +1832,15 @@ static int Pgettimeofday(lua_State *L) /** gettimeofday() */
struct timeval tv;
if (gettimeofday(&tv, NULL) == -1)
return pusherror(L, "gettimeofday");
+
+ lua_newtable(L);
+ lua_pushstring(L, "sec");
lua_pushinteger(L, tv.tv_sec);
+ lua_settable(L, -3);
+ lua_pushstring(L, "usec");
lua_pushinteger(L, tv.tv_usec);
- return 2;
+ lua_settable(L, -3);
+ return 1;
}
static int Ptime(lua_State *L) /** time() */
View
50 posix.lua
@@ -79,4 +79,54 @@ function M.euidaccess (file, mode)
posix.set_errno (EACCESS)
end
+--- Add one gettimeofday() returned timeval to another.
+-- @param x a timeval
+-- @param y another timeval
+-- @return x + y, adjusted for usec overflow
+function M.timeradd (x,y)
+ local sec, usec = 0, 0
+ if x.sec then sec = sec + x.sec end
+ if y.sec then sec = sec + y.sec end
+ if x.usec then usec = usec + x.usec end
+ if y.usec then usec = usec + y.usec end
+ if usec > 1000000 then
+ sec = sec + 1
+ usec = usec - 1000000
+ end
+
+ return { sec = sec, usec = usec }
+end
+
+--- Compare one gettimeofday() returned timeval with another
+-- @param x a timeval
+-- @param y another timeval
+-- @return 0 if x and y are equal, >0 if x is newer, <0 if y is newer
+function M.timercmp (x, y)
+ local x = { sec = x.sec or 0, usec = x.usec or 0 }
+ local y = { sec = y.sec or 0, usec = y.usec or 0 }
+ if x.sec ~= y.sec then
+ return x.sec - y.sec
+ else
+ return x.usec - y.usec
+ end
+end
+
+--- Subtract one gettimeofday() returned timeval from another.
+-- @param x a timeval
+-- @param y another timeval
+-- @return x - y, adjusted for usec underflow
+function M.timersub (x,y)
+ local sec, usec = 0, 0
+ if x.sec then sec = x.sec end
+ if y.sec then sec = sec - y.sec end
+ if x.usec then usec = x.usec end
+ if y.usec then usec = usec - y.usec end
+ if usec < 0 then
+ sec = sec - 1
+ usec = usec + 1000000
+ end
+
+ return { sec = sec, usec = usec }
+end
+
return M
View
11 tests-posix.lua
@@ -310,4 +310,15 @@ if arg[1] ~= "--no-times" then
end
------------------------------------------------------------------------------
+testing"gettimeofday"
+x=ox.gettimeofday()
+for k,v in pairs(x) do print(k,v) end
+y=ox.timeradd(x,{usec=999999})
+assert (ox.timercmp(x,x) == 0)
+assert (ox.timercmp(x,y) < 0)
+assert (ox.timercmp(y,x) > 0)
+y=ox.timersub(y,{usec=999999})
+assert (ox.timercmp(x,y) == 0)
+
+------------------------------------------------------------------------------
io.stderr:write("\n\n==== ", ox.version, " tests completed ====\n\n")

No commit comments for this range

Something went wrong with that request. Please try again.