Wiki

Clone wiki

Core / os.time

Back to Beyond the Codea in-app reference


FunctionIconFunction-Small.png os.time ([table]) function

Introduction

The Lua version 5.1 reference and Codea's in-app reference documents most of the os.time([table]) function. The function returns a Lua number that represents the number of seconds since 00:00:00 1 January 1970.

Precision of the result

By early 2013, there were over 1,358,600,000 seconds since 1 January 1970. Lua's number type as implemented by Codea version 1.5.1 cannot present integers greater than 16,777,216 precisely. As a consequence, the result of `os.time()` is only currently accurate to within 128 seconds.

For example:

function setup()
    print("os.time() returns the number of seconds since "..
        "00:00:00 1 January 1970.")
    print("Lua's number type in Codea version 1.5.1 can record current times only "..
        "to within an accuracy of 128 seconds.")
    timeCheck()
    print("Touch the Viewer for an update. Up to two minutes and "..
        "eight seconds must pass before the reported time and "..
        "date changes.")
end

local trialCount = 0
function timeCheck()
    local timeValue = os.time()
    local timeValueString = string.format("%.1f", timeValue)
    local dateString = os.date("%c", timeValue)
    trialCount = trialCount + 1
    print(trialCount..": "..timeValueString.." seconds is "..dateString)
end

function draw() background(0) end

function touched(touch)
    if touch.state == BEGAN then timeCheck() end
end

Updated