gpt4 book ai didi

node.js - 是否有任何类似于 c# 上的 Environment.TickCount 的 NodeJs 类/函数?

转载 作者:行者123 更新时间:2023-12-04 08:42:54 29 4
gpt4 key购买 nike

此代码在 c# 上运行

int x = Environment.TickCount;
docs for Environment.TickCount

Gets the number of milliseconds elapsed since the system started. TickCount cycles between Int32.MinValue, which is a negative number, and Int32.MaxValue once every 49.8 days.


TickCount will increment from Zero to (2147483647) for approximately 24.9 days, then jump back to (-2147483648), which is a negative number, then increment back to zero during the next 24.9 days.


We can use int result = Environment.TickCount & Int32.MaxValue; to make it rotate between (0) and (2147483647) for every 24.9 days


我想要 NodeJS 中的等效方法,它会产生相同的结果。
我在 NodeJS 上进行了搜索 npmjs但没有找到类似的功能

最佳答案

os.uptime()是最接近您需要的方法

Returns the system uptime in number of seconds


NodeJS docs
但这是一个有效的问题,即上述方法的最大限制是多少。?
在 NodeJS 中,最大安全整数是 Number.MAX_SAFE_INTEGER9007199254740991 .基本上是 289583309.373 years .所以我想我们将不得不假设这是所述方法的最大值。
如果你想要 c# 的功能 TickCount ,您将需要创建自己的自定义方法,可能如下所示:
// this method will cycle between 0 and 2147483647
function TickCount() {
const miliseconds_elapsed = os.uptime() * 1000; // convert the time in miliseconds
return miliseconds_elapsed % 2147483647;
}

// this method will cycle between -2147483648 to 2147483647
// note: it will not start from 0
function TickCount() {
const miliseconds_elapsed = os.uptime() * 1000; // convert the time in miliseconds
return (miliseconds_elapsed % 4294967296) - 2147483648;
}

// this method will cycle between -2147483648 to 2147483647
// note: it will start from 0 goes to 2147483647
// then comes back to -2147483648 and starts the cycle
function TickCount() {
const miliseconds_elapsed = os.uptime() * 1000; // convert the time in miliseconds
if (miliseconds_elapsed <= 2147483647) {
return miliseconds_elapsed;
}
return ((miliseconds_elapsed - 2147483648) % 4294967296) - 2147483648;
}

关于node.js - 是否有任何类似于 c# 上的 Environment.TickCount 的 NodeJs 类/函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64476280/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com