Time Methods

Introduction

The Lua language does not provide a native high-resolution time function.

These supplementary time-based methods are provided for convenience and compatibility.

These methods are accessed via the “n2.n2svcd” module:

    local n2svcd = require "n2.n2svcd"

.wait [Asynchronous]

This method sets a timer and hands control back to the LogicApp. When the timer expires the Lua script will be resumed.

The wait method takes the following parameters.

Parameter Type Description
seconds Number The number of seconds to wait.

Example:

    n2svcd.wait (5)

The wait method returns true.

.gettimeofday [Synchronous]

The get_time_of_day method takes no parameters.

The get_time_of_day method returns a two-element Lua list.

Parameter Type Description
[1] Integer The total number of Epoch seconds.
[2] Integer The microseconds associated with our number of Epoch seconds.
    local tod = n2svcd.gettimeofday ()
    n2svcd.debug ("NOW = %d.%d", tod[1], tod[2])

.get_time_of_day [Synchronous]

The get_time_of_day method is a minor variant which returns a Lua table with named attributes.

Parameter Type Description
seconds Integer The total number of Epoch seconds.
microseconds Integer The microseconds associated with our number of Epoch seconds.
    local tod = n2svcd.get_time_of_day ()
    n2svcd.debug ("NOW = %d.%d", tod.seconds, tod.microseconds)

.tv_interval [Pure Lua]

The tv_interval method accepts either one or two two-element Lua lists as returned from gettimeofday.

Parameter Type Description
from Two-Element Integer List [Required] The start time of the interval.
to Two-Element Integer List The end time of the interval.
(Default: The current time "now")

The tv_interval method returns the floating-point number difference in seconds between the times represented by from and to. If the from time is later than the to time then the result will be negative.

    local from = n2svcd.gettimeofday ()
    n2svcd.wait (3.5)
    local to = n2svcd.gettimeofday ()
    n2svcd.debug ("ELAPSED = %f", n2svcd.tv_interval (from, to))