我正在捕获 C# 程序中的一些事件,这些事件以时间戳作为系统滴答计数(自开始时间以来的毫秒数)返回。
根据我看到的其他问题,知道我可以从 System.Environment.TickCount
获得相同的号码属性(或其他东西),我如何推断与我收到的 TickCount 对应的 DateTime 对象?
最佳答案
你不能,没有更多信息(即使那样它也可能是模棱两可的)。 Environment.TickCount
返回:
A 32-bit signed integer containing the amount of time in milliseconds that has passed since the last time the computer was started.
...因此,除非您能找出计算机从某个地方启动的时间,否则您就不走运了。很可能您可以通过注册表项或系统调用来找出上次启动时间,但我不知道它们是否在我的脑海中。当然,你可以通过取
Environment.TickCount
得到一个近似值。你自己和DateTime.UtcNow
在此之后(或之前)尽快,并找出两者之间的区别:public static DateTime UnreliableDateTimeFromTickCount(int tickCount)
{
DateTime now = DateTime.UtcNow;
DateTime boot = now - TimeSpan.FromMilliseconds(Environment.TickCount);
return boot + TimeSpan.FromMilliseconds(tickCount);
}
但是,即使这样,该值也会每 24.9 天循环一次,因此如果计算机的运行时间超过此时间,则计数是不明确的。
我建议避免使用
Environment.TickCount
如果可能的话,基本上 - 这完全在你的控制之下吗?
关于c# - 来自 TickCount 的日期时间?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11333907/