This module contains functions for working with time, benchmarks, and date values.

Include using

var time = use("std:time");

clock

clock()

Returns the current Unix timestamp as a number representing the number of seconds elapsed since January 1, 1970 (UTC).

  • Parameters: None
  • Returns: (Number) The current Unix timestamp in seconds.
var time = use("std:time");

var ts = time.clock();
echo ts;

clock_ms

clock_ms()

Returns the current Unix timestamp in high-precision milliseconds.

  • Parameters: None
  • Returns: (Number) The current Unix timestamp in milliseconds.
var time = use("std:time");

var ms = time.clock_ms();
echo ms; // e.g. 1698421500123

now

now()

Returns the current date and time as a dictionary containing year, month, day, hour, minute, and second.

  • Parameters: None
  • Returns: (Dict) A dictionary containing calendar components.
var time = use("std:time");

var current_time = time.now();
echo current_time["year"];
echo current_time["month"];
echo current_time["day"];

format

format(timestamp = clock(), format = "%Y-%m-%d %H:%M:%S")

Formats a Unix timestamp using a template format string. Throws TypeError if parameters are invalid.

Supported format specifiers: * %Y - four-digit year * %m - two-digit month * %d - two-digit day * %H - two-digit hour * %M - two-digit minute * %S - two-digit second

var time = use("std:time");

echo time.format(); // "2023-10-27 15:45:00"
echo time.format(1698421500.0, format = "%d/%m/%Y"); // "27/10/2023"

is_leap_year

is_leap_year(year)

Checks if a given calendar year is a leap year. Throws TypeError if year is not a number.

  • Parameters:
    • year (Number): The four-digit year to check.
  • Returns: (Bool) true if a leap year, otherwise false.
var time = use("std:time");

echo time.is_leap_year(2024); // true
echo time.is_leap_year(2023); // false

time_execution

time_execution(func)

Measures the execution time of a callback function and returns the duration in milliseconds. Throws TypeError if func is not callable.

  • Parameters:
    • func (Callable): The function to benchmark.
  • Returns: (Number) Execution duration in milliseconds.
var time = use("std:time");

fn heavy_task() {
    for (var i = 0; i < 100000; i += 1) {}
}

var duration_ms = time.time_execution(heavy_task);
echo "Elapsed: ${duration_ms} ms";

sleep

sleep(ms)

Pauses execution of the current thread for the specified duration in milliseconds. Throws ValueError if ms is negative.

  • Parameters:
    • ms (Number): Milliseconds to sleep.
  • Returns: (Nil)
var time = use("std:time");

echo "waiting...";
time.sleep(1500); // Sleep for 1.5 seconds
echo "done!";