Hello. Im new in Lua programmin but i loved to code on VB6. On another thread i saw a code to get the current time, but it refreshes every two mnutes and some seconds. So, i made a algorythm to calculate the time passing between two frames, and adjusting the obtained time to advance the cloc.
Pros: -you get the sensaion the time passes with a ver good precision: i compared with a stopwatch and at several minutes it still synced
-every time the os.time changes, the program updates the time
-you can use the seconds variable to do a lot of things
Cons -it could be used only on not clock dependant software (alarms, stopwatches, etc; but thatones you want just to show what time is)
-the program lost the count when pauses, background of even when you call the notification center and only can readjust time when os.time refreshes
-os.time is not so precise⦠Idn how much but it could be up to 30 seconds to a minuteā¦
Sorry my english. There is some junk in between, and the code is not optimised, but is for testing. Please, put it on overloading projects and tell me about how precise is.
-- Clock
-- Use this function to perform your initial setup
function setup()
print("Hello World!")
displayMode(FULLSCREEN)
a=1
b=1
c=1
intRed = 0
intGreen = 0
intBlue = 0
currStat = ""
fps = 0
milliseconds = 0
seconds = 0
minutes = 0
hours = 0
lastCheck = 0
end
-- This function gets called once every frame
function draw()
-- This function approach time in seconds passed
intGreen = math.abs(a)
intBlue = intGreen / 2
background(0,intGreen,intBlue)
-- Some memory overload to make the program busyer over time
-- this is just junk and must be removed; its here for testing purposes.
--print (a)
--print ("b")
--print ("c")
fps = math.floor((1/DeltaTime) + 0.5)
text(fps .. "fps",100,300)
-- Every frame takes when 60 fps : 1/60 of a second
-- Every frame takes when 30 fps : 2/60 of a second, that is 1/30
-- so..
milliseconds = milliseconds + 1/fps
text(milliseconds,100,280)
-- now rounded
text(math.round(milliseconds),100,260)
--
-- Now we need to get the time and refresh it every when Codea can (aprox 125 seconds)
-- First check the time (function thanks to Chronon)
timeCheck()
-- now time adjusting (im using a slow and long system just to test. There are better ways to do this.
if milliseconds >= 1 then
seconds = seconds + 1
milliseconds = 0
end
if seconds >= 60 then
minutes = minutes + 1
seconds = 0
end
if minutes >= 60 then
hours = hours + 1
minutes = 0
end
if hours >= 12 then
hours = 0
end
--now print the time
textAlign(LEFT)
text("Current time is: " .. hours .. ":" .. minutes .. ":" .. seconds .. "." .. milliseconds,100,240)
text(tFormat(hours) .. ":" .. tFormat(minutes) .. ":" .. tFormat(seconds),300,300)
end
-- some auxiliary functions
function math.round(num)
return math.floor(num+.5)
end
function timeCheck()
local timeValue = os.time()
if lastCheck == timeValue then
-- do nothing here. Only refresh the time if os.time have changed.
else
hours = os.date('%H', timeValue)
minutes = os.date('%M', timeValue)
seconds = os.date('%S', timeValue)
-- it returns string values
print (hours)
print (seconds)
print (minutes)
-- now we convert in an old fashioned way...
hours = tonumber(hours)
minutes = tonumber(minutes)
seconds = tonumber(seconds)
lastCheck = timeValue
end
end
function tFormat(arg)
if string.len(arg) == 1 then
arg = 0 .. arg
end
return arg
end