2

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

Blair Conrad
  • 202,794
  • 24
  • 127
  • 110
user156144
  • 2,077
  • 2
  • 26
  • 40

3 Answers3

6

Use the StopWatch class instead...eg

var stopWatch = new StopWatch();
stopWatch.Start();
//code here
stopWatch.Stop()
Console.WriteLine("elapsed millisconds " + stopwatch.ElapsedMilliseconds);
wal
  • 16,300
  • 7
  • 69
  • 104
  • Only correct answer so far. All of the `DateTime` variants do _not_ guarantee monotonic behavior (e.g., if the user sets the system clock). The other option is [`System.Environment.TickCount`](http://msdn.microsoft.com/en-us/library/system.environment.tickcount.aspx), but the StopWatch is better. – Nemo Jul 07 '11 at 23:54
1

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

Bob Vale
  • 17,153
  • 36
  • 47
  • According to http://stackoverflow.com/questions/62151/datetime-now-vs-datetime-utcnow DateTime.UtcNow simply converts current time to UTC. Does it remove any day light saving info when converting to UTC? – user156144 Jul 07 '11 at 23:40
  • Or even some other .NET language. – Blair Conrad Jul 07 '11 at 23:40
  • @user156144, DateTime.UtcNow considers daylight savings – Blair Conrad Jul 07 '11 at 23:41
  • The [Community Content](http://msdn.microsoft.com/en-us/library/system.datetime.utcnow(v=vs.90).aspx#1) on Microsoft's site claims `DateTime.UtcNow` does not necessarily always move forward, either (e.g., if the user sets the system clock). Does C# provide a true monotonic clock? – Nemo Jul 07 '11 at 23:45
0

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
Paul V
  • 1