Skip to content

Duration

The Duration library provides a convenient way to work with time durations in Lua. It allows for easy creation, manipulation, and conversion of time intervals.

Global Functions

Duration.new()

Creates a new Duration object initialized to zero.

local duration = Duration.new()

Duration.TimeSinceEpoch()

Creates a new Duration object representing the time elapsed since the Unix epoch (January 1, 1970).

local epoch_time = Duration.TimeSinceEpoch()

Duration.FromNanoseconds(nanoseconds)

Creates a new Duration object from a number of nanoseconds.

local duration = Duration.FromNanoseconds(1000000000) -- 1 second

Duration.FromMicroseconds(microseconds)

Creates a new Duration object from a number of microseconds.

local duration = Duration.FromMicroseconds(1000000) -- 1 second

Duration.FromMilliseconds(milliseconds)

Creates a new Duration object from a number of milliseconds.

local duration = Duration.FromMilliseconds(1000) -- 1 second

Duration.FromSeconds(seconds)

Creates a new Duration object from a number of seconds.

local duration = Duration.FromSeconds(60) -- 1 minute

Duration.FromMinutes(minutes)

Creates a new Duration object from a number of minutes.

local duration = Duration.FromMinutes(60) -- 1 hour

Duration.FromHours(hours)

Creates a new Duration object from a number of hours.

local duration = Duration.FromHours(24) -- 1 day

Duration.FromDays(days)

Creates a new Duration object from a number of days.

local duration = Duration.FromDays(7) -- 1 week

Duration.FromMonths(months)

Creates a new Duration object from an approximate number of months (30 days per month).

local duration = Duration.FromMonths(12) -- Approximately 1 year

Duration.FromYears(years)

Creates a new Duration object from an approximate number of years (365 days per year).

local duration = Duration.FromYears(1) -- Approximately 1 year

Duration Object Methods

duration:Nanoseconds()

Returns the total number of nanoseconds in the duration.

local nanoseconds = duration:Nanoseconds()

duration:Microseconds()

Returns the total number of microseconds in the duration.

local microseconds = duration:Microseconds()

duration:Milliseconds()

Returns the total number of milliseconds in the duration.

local milliseconds = duration:Milliseconds()

duration:Seconds()

Returns the total number of seconds in the duration.

local seconds = duration:Seconds()

duration:Minutes()

Returns the total number of minutes in the duration.

local minutes = duration:Minutes()

duration:Hours()

Returns the total number of hours in the duration.

local hours = duration:Hours()

duration:Days()

Returns the total number of days in the duration.

local days = duration:Days()

duration:Months()

Returns the approximate number of months in the duration (assuming 30 days per month).

local months = duration:Months()

duration:Years()

Returns the approximate number of years in the duration (assuming 365 days per year).

local years = duration:Years()

Examples

Measuring elapsed time

local start = Duration.TimeSinceEpoch()
-- Do some work
local end = Duration.TimeSinceEpoch()
local elapsed = Duration.FromNanoseconds(end:Nanoseconds() - start:Nanoseconds())
print("Elapsed time:", elapsed:Milliseconds(), "ms")

Converting between units

local oneDay = Duration.FromDays(1)
print("One day in hours:", oneDay:Hours())
print("One day in minutes:", oneDay:Minutes())
print("One day in seconds:", oneDay:Seconds())

Working with large time spans

local century = Duration.FromYears(100)
print("A century in days:", century:Days())
print("A century in hours:", century:Hours())