测量经过的时间 核算夏令时
我有代码可以测量某些操作花费的时间。如果夏令时在操作过程中生效,我会得到不准确的数据。
例如,我正在做
DateTime startAt = DateTime.Now;
DoOperation(); //day light saving takes effect in the middle
int seconds = (DateTime.Now - startAt).TotalSeconds;
//second has wrong seconds
有没有办法衡量这个?
谢谢
I have code that measure how much time some operation took. If day light saving takes effect in the middle of the operation, I get inaccurate data.
So for example, I am doing
DateTime startAt = DateTime.Now;
DoOperation(); //day light saving takes effect in the middle
int seconds = (DateTime.Now - startAt).TotalSeconds;
//second has wrong seconds
Is there a way to measure this?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
更多
发布评论
评论(3)
使用
StopWatch
类代替...例如Use the
StopWatch
class instead...eg假设您使用的是 c#,您可以使用
DateTime.UtcNow
而不是DateTime.Now
Assuming you are using c# you could use
DateTime.UtcNow
instead ofDateTime.Now
另外,在 .NET 中,还有一个 DateTime.Ticks...如果需要的话,它可能更准确,并且可以在 XP 上工作...MSDN 上显示的示例:
Also in .NET there is a DateTime.Ticks... which may be a bit more accurate and work on XP if need be... Example shown on MSDN: