add some time functions

This commit is contained in:
Pekka Laiho 2020-06-03 18:15:36 +07:00
parent a0a049fe91
commit 53730d16ef
2 changed files with 31 additions and 0 deletions

View File

@ -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];

30
src/Lib/Time.php Normal file
View File

@ -0,0 +1,30 @@
<?php
namespace MadLisp\Lib;
use MadLisp\CoreFunc;
use MadLisp\Env;
class Time implements ILib
{
public function register(Env $env): void
{
$env->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;
}
));
}
}