Question

os.time() in Luaj returns time in milliseconds, but according to lua documentation, it should return time in seconds.

  1. Is this a bug in Luaj?
  2. And Can you suggest a workaround that will work with Luaj(for java) and real Lua(c/c++)? because i have to use the same lua source for both applications.(cant simply divide it with 1000, as they both have return different time scale)

example in my lua file:

local start = os.time()
while(true) do
    print(os.time() - start)
end

in c++ , i received output:

1
1
1
...(1 seconds passed)
2
2
2

in java (using Luaj), i got:

1
...(terminate in eclipse as fast as my finger can)
659
659
659
659

fyi, i try this on windows

Was it helpful?

Solution 2

Lua manual about os.time():

The returned value is a number, whose meaning depends on your system. In POSIX, Windows, and some other systems, this number counts the number of seconds since some given start time (the "epoch"). In other systems, the meaning is not specified, and the number returned by time can be used only as an argument to os.date and os.difftime.

So, any Lua implementation could freely change the meaning of os.time() value.

OTHER TIPS

Yes there's a bug in luaj.

The implementation just returns System.currentTimeMillis() when you call os.time(). It should really return something like (long)(System.currentTimeMillis()/1000.)

It's also worth pointing out that the os.date and os.time handling in luaj is almost completely missing. I would recommend that you assume that they've not been implemented yet.

It appears like you've already confirmed that it's a bug in LuaJ; as for the workaround you can replace os.time() with your own version:

if (runningunderluaj) then
  local ostime = os.time
  os.time = function(...) return ostime(...)/1000 end
end

where runningunderluaj can check for some global variable that is only set under luaj. If that's not available, you can probably come up with your own check by comparing the results from calls to os.clock and os.time that measure time difference:

local s = os.clock()
local t = os.time()
while true do
  if os.clock()-s > 0.1 then break end
end
-- (at least) 100ms has passed
local runningunderluaj = os.time() - t > 1

Note: It's possible that os.clock() is "broken" as well. I don't have access to luaj to test this...

In luaj-3.0-beta2, this has been fixed to return time in seconds.

This was a bug in all versions of luaj up to and including luaj-3.0-beta1.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top