os

A reduced operating system library. Only time and clock functions remain: clock, date, difftime and time.


Functions

os.clock(): number

os.clock(): number

  • Returns the CPU time used by the process, in seconds. Useful to measure elapsed work, not wall-clock time.
local t0 = os.clock()
local dt = os.clock() - t0

os.date(format?: string = "%c", time?: number = os.time()): string | table

os.date(format?: string = "%c", time?: number = os.time()): string | table

  • Formats time (seconds since 1970-01-01 UTC) according to format.
  • A leading ! formats in UTC; otherwise local time is used.
  • "*t" (or "!*t") returns a table with the fields year, month, day, hour, min, sec, wday (1 is Sunday), yday (1 to 366) and isdst (boolean).
  • Otherwise the format is a C strftime string; directives below are supported. An invalid directive raises an error.
Directive Meaning
%a abbreviated weekday name
%A full weekday name
%b abbreviated month name
%B full month name
%c date and time, locale format
%d day of the month (01-31)
%H hour, 24-hour clock (00-23)
%I hour, 12-hour clock (01-12)
%j day of the year (001-366)
%m month (01-12)
%M minute (00-59)
%p AM or PM
%S second (00-61)
%U week of the year, Sunday first (00-53)
%w weekday, Sunday is 0 (0-6)
%W week of the year, Monday first (00-53)
%x date, locale format
%X time, locale format
%y year without century (00-99)
%Y year with century
%% a literal %
print(os.date("%Y-%m-%d %H:%M:%S"))
local t = os.date("*t")
print(t.year, t.month, t.day)

os.difftime(t2: number, t1: number): number

os.difftime(t2: number, t1: number): number

  • Returns the number of seconds from time t1 to time t2.

os.time(t?: table): number

os.time(t?: table): number

  • With no argument, returns the current time in seconds since 1970-01-01 UTC.
  • With a table, reads the fields year, month, day, hour (default 12), min (default 0), sec (default 0) and isdst (optional boolean) as local time and returns the corresponding timestamp.
  • year, month and day are required. Out-of-range fields are normalized (month 13 is January of the next year).
  • Returns nil if the time cannot be represented.
print(os.time({ year = 2026, month = 1, day = 1, hour = 0 }))