Does os.clock() resets after a certain point?

I am building a timer function in my programme that needs to track time in milliseconds. I am using os.clock() to track my time, and time elapsed as (os.clock() - starttime). It works 99.99% of the time.

However, what I noticed is that when os.clock() reaches about ~4200 seconds, it resets to zero. This screws up the time elapsed function as the starttime was stored using the pre-reset time. Is anyone else noticing this too?

Is there any other time function that I can use to track milliseconds and would not reset? Or is there a method to deliberately reset os.clock()?

You could use Elapsedtime build in value, which gives you the time a beginng of each draw() call, and synchronize it with the clock() function.
Or if Elapsedtime also saturates, you could use os.time() to count the seconds, and get the ms from os.clock().
Or you could simply detect the reset moment and count it into n, and note Tmax. Then time = os.clock() +n*Tmax, but at some point float format will not be accurate enough.
But You don’t really need to know a time bigger than 1 hour with an accuracy of 1 ms, do you? This would be an accuracy better than 10-6! What application whould need that? Are you sending a radio signal to Pluto, waiting for the echo, and trying to measure the earth-to-Pluto-distance with a 1km accuracy? :wink:

Haha. Actually, any of your methods would work, I just wanted to know that it’s an inherent feature of os.clock() and not a buggy code. I use os.clock() to sync and time my animated sprite. As the clock does not reset unless I close-reopen Codea, after 1.25 hour of de-bugging, the problem will crop up even for short animation.


Hello @zapaper. I think Lua’s os.clock() calls the C library function clock() and that that function returns a 32-bit unsigned long which increments by 1,000,000 each second. That would explain why it cycles through zero every 2^32/1,000,000 seconds.

Thanks mpilgrem. You are right on the dot. Works out to be 4,294secs!

Hello @zapaper. Having discussed this topic with @Simeon, I would avoid os.clock() - its ‘seconds’ appear to have a complex relationship with the duration of elapsed time on an iPad. ElapsedTime is better suited to elapsed time.