diff --git a/bootstrap.php b/bootstrap.php index bd796fa..7891748 100644 --- a/bootstrap.php +++ b/bootstrap.php @@ -21,6 +21,7 @@ function ml_get_lisp(): array (new MadLisp\Lib\Compare())->register($env); (new MadLisp\Lib\Math())->register($env); (new MadLisp\Lib\Strings())->register($env); + (new MadLisp\Lib\Time())->register($env); (new MadLisp\Lib\Types())->register($env); return [$lisp, $env]; diff --git a/src/Lib/Time.php b/src/Lib/Time.php new file mode 100644 index 0000000..908ee66 --- /dev/null +++ b/src/Lib/Time.php @@ -0,0 +1,30 @@ +set('time', new CoreFunc('time', 'Return the current time as unix timestamp.', 0, 0, + fn () => time() + )); + + $env->set('date', new CoreFunc('date', 'Format the time according to first argument.', 1, 2, + fn (string $format, ?int $time = null) => date($format, $time !== null ? $time : time()) + )); + + $env->set('strtotime', new CoreFunc('strtotime', 'Parse datetime string into unix timestamp. Optional second argument can be used to give time for relative formats.', 1, 2, + fn (string $format, ?int $time = null) => strtotime($format, $time !== null ? $time : time()) + )); + + $env->set('sleep', new CoreFunc('sleep', 'Sleep (wait) for the specified time in milliseconds.', 1, 1, + function (int $time) { + usleep($time * 1000); + return null; + } + )); + } +}