forked from PuqiAR/Fig-TreeWalker
[VER] v0.3.9-alpha
[Feat] is 操作符现在可以直接判断内置数据类型, 如 10 is Int [Fix] evalMemberExpr的lhs可以为右值,修复原来限制为左值的BUG,如调用一个函数返回结果为struct且访问member触发此bug [Impl] 可更换的std::dynamic_pointer_cast更换为static版本,更快! [Feat] 增加标准库 std.time,以及用到的builtin: __ftime_now_ns,但目前 Time类有点BUG [...] 剩下的忘了
This commit is contained in:
51
src/Module/Library/std/time/time.fig
Normal file
51
src/Module/Library/std/time/time.fig
Normal file
@@ -0,0 +1,51 @@
|
||||
/*
|
||||
Official Module `time`
|
||||
Library/time/time.fig
|
||||
|
||||
Copyright © 2026 PuqiAR. All rights reserved.
|
||||
*/
|
||||
|
||||
import _builtins; // provides __ftime_now_ns (int64_t)
|
||||
|
||||
|
||||
public struct Time
|
||||
{
|
||||
ns: Int; // int64_t, private
|
||||
|
||||
public func toNanos() -> Int
|
||||
{
|
||||
return ns;
|
||||
}
|
||||
|
||||
public func toMicros() -> Int
|
||||
{
|
||||
return __fvalue_int_from(ns / 1000); // convert double to int
|
||||
}
|
||||
|
||||
public func toMillis() -> Double
|
||||
{
|
||||
return ns / 1000 / 1000;
|
||||
}
|
||||
|
||||
public func toSeconds() -> Double
|
||||
{
|
||||
return ns / 1000 / 1000 / 1000;
|
||||
}
|
||||
|
||||
public func since(other: Time) -> Int
|
||||
{
|
||||
const time_ns := other.toNanos();
|
||||
const result := ns - time_ns;
|
||||
if result < 0
|
||||
{
|
||||
throw "time has reversed! 😢";
|
||||
}
|
||||
return result;
|
||||
}
|
||||
// TODO: support `-` operator when Fig supports overload
|
||||
}
|
||||
|
||||
public func now() -> Time
|
||||
{
|
||||
return Time{__ftime_now_ns()};
|
||||
}
|
||||
Reference in New Issue
Block a user