Skip to main content

std:time

The std:time module provides functions for working with timestamps, formatting dates and times, and controlling delays. It is useful for logging, scheduling, time calculations, and formatting dates for display or storage.


Functions

time.now(): integer

Returns the current Unix timestamp (seconds since January 1, 1970).

  • Parameters: None
  • Returns:
    • (integer) the current timestamp in seconds.

Example:

local time = require("std:time")

local now = time.now()
print("Current timestamp:", now)

time.format(format_string: string, timestamp: integer): string

Formats a given timestamp into a human-readable string according to the specified format. Format strings follow a strftime-style syntax.

  • Parameters:

    • format_string (string): formatting pattern (like %Y-%m-%d %H:%M:%S).
    • timestamp (integer): Unix timestamp to format.
  • Returns:

    • (string) formatted date and time.

Example:

local time = require("std:time")

local now = time.now()
local formatted = time.format("%A, %B %d, %Y", now)
print(formatted)
-- "Saturday, August 10, 2025"

time.sleep(milliseconds: integer)

Pauses execution for a specified amount of time.

  • Parameters:

    • milliseconds (integer): duration to sleep in milliseconds.
  • Returns: None

Example:

local time = require("std:time")

print("Sleeping for 1 second...")
time.sleep(1000)
print("Awake!")

Notes

  • All timestamps are in seconds since Unix epoch.
  • The format function supports standard strftime tokens.
  • sleep may not be perfectly precise for very short durations due to system scheduling.

Supported Format Tokens

TokenExample ValueMeaning
%Y2006Full year (4 digits)
%y06Year (2 digits)
%m01Month (01-12)
%d02Day of month (01-31)
%H15Hour (24-hour)
%I03Hour (12-hour)
%M04Minute
%S05Second
%pPMAM/PM
%z-0500Timezone offset
%ZLocalTimezone abbreviation
%ASundayFull weekday name (manual)
%aSunAbbreviated weekday name
%BAugustFull month name
%bAugAbbreviated month name
%F2006-01-02Equivalent to %Y-%m-%d
%T15:04:05Equivalent to %H:%M:%S
%r03:04:05 PM12-hour clock time
%R15:0424-hour clock time without seconds