c# - 来自 TickCount 的日期时间?

标签 c# datetime

我正在捕获 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/

相关文章:

c# - 如何查看一个字符串是否包含另一个带引号的字符串?

c# - 带圆形边框的 Stackpanel

c# - WinForms Livecharts 图表标题

c# - 通过键盘按下输入文本的最短路径

r - 使用 R 的 ggplot2 定义日期时间的第一个轴断点

C# - 两个 DateTimes 之间的持续时间(以分钟为单位)

vb.net - 如何在 DataGridViewColumn 中使用服务器日期时间格式而不是系统日期时间格式

c# - 如何使用 Machine.Fakes (Moq) 模拟 Function<>?

c# - 将两个日期相加

php - mysql代码中的时间变化