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.
Duration.TimeSinceEpoch()
Creates a new Duration object representing the time elapsed since the Unix epoch (January 1, 1970).
Duration.FromNanoseconds(nanoseconds)
Creates a new Duration object from a number of nanoseconds.
Duration.FromMicroseconds(microseconds)
Creates a new Duration object from a number of microseconds.
Duration.FromMilliseconds(milliseconds)
Creates a new Duration object from a number of milliseconds.
Duration.FromSeconds(seconds)
Creates a new Duration object from a number of seconds.
Duration.FromMinutes(minutes)
Creates a new Duration object from a number of minutes.
Duration.FromHours(hours)
Creates a new Duration object from a number of hours.
Duration.FromDays(days)
Creates a new Duration object from a number of days.
Duration.FromMonths(months)
Creates a new Duration object from an approximate number of months (30 days per month).
Duration.FromYears(years)
Creates a new Duration object from an approximate number of years (365 days per year).
Duration Object Methods
duration:Nanoseconds()
Returns the total number of nanoseconds in the duration.
duration:Microseconds()
Returns the total number of microseconds in the duration.
duration:Milliseconds()
Returns the total number of milliseconds in the duration.
duration:Seconds()
Returns the total number of seconds in the duration.
duration:Minutes()
Returns the total number of minutes in the duration.
duration:Hours()
Returns the total number of hours in the duration.
duration:Days()
Returns the total number of days in the duration.
duration:Months()
Returns the approximate number of months in the duration (assuming 30 days per month).
duration:Years()
Returns the approximate number of years in the duration (assuming 365 days per year).
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())