Comparing 2 times for a cooldown timer

I’ve been trying to implement a cooldown timer which could persist between application uses.

I can get the time of an event, and store that in a JSON file so that it can be retrieved the next time the app is used. However I am not sure how I would actually compare the 2 times. I thought the easiest way would be to get the time as a unix timestamp and then calculate the number of days/hours/mins etc, but I cannot see a way to get the time in seconds for both iOS and Android.

To give an idea of why I need this, the app will have options that the user can only select once an hour, once a day etc. The different options will have different cooldown times, and obviously the “cooldown end time” will be dynamic so I don’t think I can use something with a predetermined datetime such as http://techority.com/2011/12/07/making-a-countdown-app-with-corona-sdk/

I suspect there is an easy solution, but I’ve been staring at this for a while now and my brain is starting to melt, I’m sure you all know the feeling :wink:

I’d appreciate any help that anyone can give, thanks. [import]uid: 84115 topic_id: 25459 reply_id: 325459[/import]

Just in case anyone needs to do the same thing, here’s what I have done:

local function updateTime()  
local time = os.date( "\*t" )  
local day = time.yday  
local hour = time.hour  
local min = time.min  
local sec = time.sec  
  
--total number of seconds that this time represents  
local time1 = (hour \*60\*60) + (min \*60) + sec  
  
--prevT is previously stored time  
  
if day ~= prevT.yday then  
  
 --new day so reset counter  
 prevTimes[count] = 0  
  
else  
  
 countingDown = true  
 --total number of seconds that this time represents  
 local time2 = (prevT.hour \*60\*60) + (prevT.min \*60) + prevT.sec  
  
 --difference in seconds between the 2 times  
 local diff = time1 - time2  
  
 local limits = {10, 30, 60, 180, 3600}  
 local limit = limits[param.difficulty]  
  
 local remaining = limit - diff  
  
  
 if remaining \> 0 then  
 local numMins = remaining / 60--total number of mins  
 local numSecs = remaining % 60--remaining number of seconds  
 local numHours = numMins / 60--total number of hours  
 numMins = numMins % 60 --remaining number of mins  
  
 hourStr= string.format("%02i", numHours)  
 minStr= string.format("%02i", numMins)  
 secStr= string.format("%02i", numSecs)  
  
 timeToUse = hourStr..":"..minStr..":"..secStr  
  
 elseif remaining \<= 0 then  
 timeToUse = 0  
 prevTimes[count] = 0  
 countingDown = false  
 end  
  
end  
  
return timeToUse  
  
end --end function  
  

All very simple stuff, but you never know, one day someone else may be stuck on the same thing :slight_smile: [import]uid: 84115 topic_id: 25459 reply_id: 103063[/import]