测量经过的时间 核算夏令时

发布于 2024-11-19 09:17:55 字数 296 浏览 3 评论 0原文

我有代码可以测量某些操作花费的时间。如果夏令时在操作过程中生效,我会得到不准确的数据。

例如,我正在做

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

对风讲故事 2024-11-26 09:17:55

使用 StopWatch 类代替...例如

var stopWatch = new StopWatch();
stopWatch.Start();
//code here
stopWatch.Stop()
Console.WriteLine("elapsed millisconds " + stopwatch.ElapsedMilliseconds);

Use the StopWatch class instead...eg

var stopWatch = new StopWatch();
stopWatch.Start();
//code here
stopWatch.Stop()
Console.WriteLine("elapsed millisconds " + stopwatch.ElapsedMilliseconds);
反差帅 2024-11-26 09:17:55

假设您使用的是 c#,您可以使用 DateTime.UtcNow 而不是 DateTime.Now

Assuming you are using c# you could use DateTime.UtcNow instead of DateTime.Now

飘然心甜 2024-11-26 09:17:55

另外,在 .NET 中,还有一个 DateTime.Ticks...如果需要的话,它可能更准确,并且可以在 XP 上工作...MSDN 上显示的示例:

DateTime centuryBegin = new DateTime(2001, 1, 1);
DateTime currentDate = DateTime.Now;

long elapsedTicks = currentDate.Ticks - centuryBegin.Ticks;
TimeSpan elapsedSpan = new TimeSpan(elapsedTicks);

Console.WriteLine("Elapsed from the beginning of the century to {0:f}:", 
                   currentDate);
Console.WriteLine("   {0:N0} nanoseconds", elapsedTicks * 100);
Console.WriteLine("   {0:N0} ticks", elapsedTicks);
Console.WriteLine("   {0:N2} seconds", elapsedSpan.TotalSeconds);
Console.WriteLine("   {0:N2} minutes", elapsedSpan.TotalMinutes);
Console.WriteLine("   {0:N0} days, {1} hours, {2} minutes, {3} seconds", 
                  elapsedSpan.Days, elapsedSpan.Hours, 
                  elapsedSpan.Minutes, elapsedSpan.Seconds);

// If run on December 14, 2007, at 15:23, this example displays the
// following output to the console:
//    Elapsed from the beginning of the century to Friday, December 14, 2007 3:23 PM:
//          219,338,580,000,000,000 nanoseconds
//          2,193,385,800,000,000 ticks
//          219,338,580.00 seconds
//          3,655,643.00 minutes
//          2,538 days, 15 hours, 23 minutes, 0 seconds

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:

DateTime centuryBegin = new DateTime(2001, 1, 1);
DateTime currentDate = DateTime.Now;

long elapsedTicks = currentDate.Ticks - centuryBegin.Ticks;
TimeSpan elapsedSpan = new TimeSpan(elapsedTicks);

Console.WriteLine("Elapsed from the beginning of the century to {0:f}:", 
                   currentDate);
Console.WriteLine("   {0:N0} nanoseconds", elapsedTicks * 100);
Console.WriteLine("   {0:N0} ticks", elapsedTicks);
Console.WriteLine("   {0:N2} seconds", elapsedSpan.TotalSeconds);
Console.WriteLine("   {0:N2} minutes", elapsedSpan.TotalMinutes);
Console.WriteLine("   {0:N0} days, {1} hours, {2} minutes, {3} seconds", 
                  elapsedSpan.Days, elapsedSpan.Hours, 
                  elapsedSpan.Minutes, elapsedSpan.Seconds);

// If run on December 14, 2007, at 15:23, this example displays the
// following output to the console:
//    Elapsed from the beginning of the century to Friday, December 14, 2007 3:23 PM:
//          219,338,580,000,000,000 nanoseconds
//          2,193,385,800,000,000 ticks
//          219,338,580.00 seconds
//          3,655,643.00 minutes
//          2,538 days, 15 hours, 23 minutes, 0 seconds
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文