os.time() returns a constant?

Hey, I’m new to Lua so I might be doing this wrong, but I believe that if I call os.time() I should get the current time in seconds. However, I’m getting a constant value (1.33399e+09). I tried passing the current time to it like this: os.time(os.date(‘*t’)) but it returns the same value.

I thought perhaps it was just not printing, but even if I do something like “os.time() % 10”, or os.difftime(…) every frame, I still get a constant.

Is this a bug? And if not, how can I retrieve the time in seconds?

Thanks!

Actually, if I just run:

function draw()
    print(os.time() - 1333990000) 
end

in a loop, it only ever updates every 128 seconds [EDIT: I had written “frames”]. I’m guessing it’s a bug then.

os.time() returns the time from the operating system, measured in seconds since the beginning of 1970.

If you want the time in seconds since the program started running, used the global ElapsedTime variable.

What is it that you’re trying to achieve using the time?

We had a thread about this, but I wasn’t able to find it by searching.

Keep in mind - Codea numbers are only 32 bits (by comparison with desktop Lua at 64 bits). That means that there is no single number that gives you both epoch seconds and the fraction. It’s possible to derive them - I think it was figured out in the thread - but again, can’t find it (I still am getting memory errors when I search), and I’m not going to try to derive it again right this moment.

Ah, thanks guys - I didn’t see ElapsedTime in the docs. That’s what I was implementing!

 in a loop, it only ever updates every 128 frames. I'm guessing it's a bug then

Mr. B… were you looking for this thread?

http://twolivesleft.com/Codea/Talk/discussion/630/unexpected-results-using-os.time-and-os.difftime

The upshot: os.time() doesn’t work in Codea… :frowning:

Yessir, that was the exact thread I was looking for - but I had thought someone had figured out a workaround (which would presumably involve parsing date and so on). Too bad. If nothing else, being able to grab epoch time in seconds is handy.

Plus I thought it was a table… I was having trouble getting it to work as well, thanks for the reference, @Blanchot.

would this be a good place to re-suggest a high resolution (millisec or higher) timer that would reset at each call to draw? With this we would have a way to measure the time some things take in a resolution higher that 1/60th of a second, and would not (be so likely to) hit the 24-bit integral value limit of 32-bit floats.

If I remember correctly, ElapsedTime and DeltaTime are fairly high resolution anyway? 5 or 6 decimal places?

You’re talking about precision. By resolution i mean the minimum difference between 2 consecutive values you can read. In the case of ElapsedTime and DeltaTime, this is 1/60th second at best, which is about 17 msec. Also, by the very nature of the system, this resolution can not be guaranteed.

Oh, sorry - I understand your point now. And exactly, that’s the purpose of having a DeltaTime because it’s never exact.

Why not just get time once by os.time and then set it forward by another high resolution function?i

The page on the Codea Wiki here may help.