Dlang 实现 PHP microtime 函数
以下是使用 Dlang 语言实现 PHP 的 microtime 函数的代码:
import std.datetime;
import std.string;
string microtime(bool get_as_float = false)
{
auto now = Clock.currTime();
auto sec = now.toUnixTime();
auto msec = (now - Clock.fromUnixTime(sec)).total!'microseconds' / 1000000.0;
if (get_as_float) {
return to!string(sec + msec);
} else {
return format('%d %.6f', sec, msec);
}
}
这里使用了 Dlang 标准库中的 std.datetime 和 std.string 模块。Clock.currTime() 函数返回当前时间,toUnixTime() 方法将时间转换为自 Unix 纪元以来的秒数,total!'microseconds' 方法返回自 Unix 纪元以来的总微秒数,format() 方法用于格式化输出字符串。
使用 microtime() 函数,可以像下面这样获取当前时间的 microtime 值:
auto time_str = microtime(); // 返回形如 '1609459828 0.627035' 的字符串
auto time_float = microtime(true); // 返回形如 1609459828.627035 的浮点数
原文地址: https://www.cveoy.top/t/topic/oniQ 著作权归作者所有。请勿转载和采集!