A clock with up to 30 secondsof precision.

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

Update: heres the final code. You are free to use it in your proyects. Please refer me.


-- Clock

-- Use this function to perform your initial setup
function setup()
    -- I like to initialise variables, so her they are.
    intFps = 0
    intMilliseconds = 0
    intSeconds = 0
    intMinutes = 0
    intHours = 0
    longSeconds = 0
    --
    
end

-- This function gets called once every frame
function draw()
    background(0,0,0)
    --You can use intFps to get current FPS
    intFps = math.floor((1/DeltaTime) + 0.5)
    
    --Check the (aproximated) current time
    subTimeCheck()
    
    --intMilliseconds shows a fraction of a second from 0 to 1. It updates every frame.
    longSeconds = longSeconds + 1/intFps
    intMilliseconds = intMilliseconds + 1/intFps
    subAdjustTime()
    --From here you can use intSeconds, intMinutes, intHours and intMillliseconds to obtain these values.
    -- The difference between intMilliseconds and longSeconds is:
    --    intMilliseconds has values bewteen 0 and 1 and reset every seconds. Shows a fragment of a second.
    --    longSeconds doesnt reset. It represents the overall runtime. It has a integer part of seconds with decimals as fragments of a second.
    
    parameter.watch("intHours")
    parameter.watch("intMinutes")
    parameter.watch("intSeconds")
    parameter.watch("intMilliseconds")
    parameter.watch("longSeconds")
    
end

function math.round(num) 
    return math.floor(num+.5) 
end

function subTimeCheck() -- Refresh the time
    local timeValue = os.time()
    if lastCheck == timeValue then 
        --nothing
    else
        intHours = tonumber(os.date('%H', timeValue))
        intMinutes = tonumber(os.date('%M', timeValue))
        intSeconds = tonumber(os.date('%S', timeValue))
        lastCheck = timeValue
    end    
end

function tFormat(arg) -- Add zeros to time if value is < 10
    if string.len(arg) == 1 then
        arg = 0 .. arg
    end
    return arg
end

function subAdjustTime()
    if intMilliseconds >= 1 then 
        intSeconds  = intSeconds + 1 
        intMilliseconds = 0 
    end
    if intSeconds >= 60 then 
        intMinutes = intMinutes + 1
        intSeconds = 0
    end
    if intMinutes >= 60 then
        intHours = intHours + 1
        intMinutes = 0
    end
    if intHours >= 12 then
        intHours = 0
    end
end