Detecting new date

I’m planning a new app and will need to know if while using the app the user’s time has changed to the next day. Thinking of using:

Runtime:addEventListener( “enterFrame”, myListener )

and check on each second (for example) and compare it with the previous date table from os.date(), if different then assume the user is in the next date.

Sound good or anyone else have a better experience for this situation?

Thanks in advance.

David

What kind of accuracy do you need?  If within a second is good enough, I’d use an infinite timer with a period of 500 ms instead of enterFrame which is quite often.

That said, either technique will work.

Personally I use enterFrame() as I run a game and compare os.time() to a saved variable to know when a second has passed.

That seems aggressive. You’re going to be checking 86,400 * fps times to determine if you’re in a new day or not. A timer as @roaminggamer suggested will only make 86,400 * 2 checks a day. But I question if you even need that precision. Or consider using a local notification so you just get a new event when the time clicks over. Check during an application resume event if the app has been sleeping. Now you’re down to just a few checks a day.

Rob

What kind of accuracy do you need?  If within a second is good enough, I’d use an infinite timer with a period of 500 ms instead of enterFrame which is quite often.

That said, either technique will work.

Personally I use enterFrame() as I run a game and compare os.time() to a saved variable to know when a second has passed.

That seems aggressive. You’re going to be checking 86,400 * fps times to determine if you’re in a new day or not. A timer as @roaminggamer suggested will only make 86,400 * 2 checks a day. But I question if you even need that precision. Or consider using a local notification so you just get a new event when the time clicks over. Check during an application resume event if the app has been sleeping. Now you’re down to just a few checks a day.

Rob

I would like to determine when the application is first launched on a new day (for the daily bonus in the game). It comes to my mind to use dateVar = os.date ("% d% m") and firstRun = false and write this data to the game settings JSON file. Is this the right approach and can I add information to an existing json file. Or do you need to first read the data into the Lua table, add a key-value pair there and write (overwrite) back to the json file?