[corlib] Don't use DateTime.Now for measuring elapsed time
authorAlexander Köplinger <alex.koeplinger@outlook.com>
Sun, 2 Jul 2017 11:02:31 +0000 (13:02 +0200)
committerAlexander Köplinger <alex.koeplinger@outlook.com>
Sun, 2 Jul 2017 21:31:34 +0000 (23:31 +0200)
Use Stopwatch or DateTime.UtcNow instead which aren't affected
by things like daylight saving time

mcs/class/corlib/System.Runtime.Remoting.Lifetime/Lease.cs
mcs/class/corlib/Test/Microsoft.Win32/RegistryKeyTest.cs
mcs/class/corlib/Test/System.IO/DirectoryInfoTest.cs
mcs/class/corlib/Test/System.Threading/ThreadPoolTest.cs
mcs/class/corlib/Test/System.Threading/ThreadTest.cs
mcs/class/corlib/Test/System.Threading/WaitHandleTest.cs

index 8261ce287f9bd9627f05363f339dbdb472a697d9..e954640a138c2849cddb47a61da18ca722e49a32 100644 (file)
@@ -54,12 +54,12 @@ namespace System.Runtime.Remoting.Lifetime
                        _initialLeaseTime = LifetimeServices.LeaseTime;
                        _renewOnCallTime = LifetimeServices.RenewOnCallTime;
                        _sponsorshipTimeout = LifetimeServices.SponsorshipTimeout;
-                       _leaseExpireTime = DateTime.Now + _initialLeaseTime;
+                       _leaseExpireTime = DateTime.UtcNow + _initialLeaseTime;
                }
 
                public TimeSpan CurrentLeaseTime 
                { 
-                       get { return _leaseExpireTime - DateTime.Now; }
+                       get { return _leaseExpireTime - DateTime.UtcNow; }
                }
 
                public LeaseState CurrentState 
@@ -82,7 +82,7 @@ namespace System.Runtime.Remoting.Lifetime
                                        throw new RemotingException ("InitialLeaseTime property can only be set when the lease is in initial state; state is " + _currentState + ".");
 
                                _initialLeaseTime = value; 
-                               _leaseExpireTime = DateTime.Now + _initialLeaseTime;
+                               _leaseExpireTime = DateTime.UtcNow + _initialLeaseTime;
                                if (value == TimeSpan.Zero) _currentState = LeaseState.Null;
                        }
                }
@@ -130,7 +130,7 @@ namespace System.Runtime.Remoting.Lifetime
 
                public TimeSpan Renew (TimeSpan renewalTime)
                {
-                       DateTime newTime = DateTime.Now + renewalTime;
+                       DateTime newTime = DateTime.UtcNow + renewalTime;
                        if (newTime > _leaseExpireTime) _leaseExpireTime = newTime;
                        return CurrentLeaseTime;
                }
index 048091d2d7cb9dbb624836c4833bfa52d79b89cc..bef88b0c599515481b21854be4e0073c3fea3617 100755 (executable)
@@ -2512,7 +2512,7 @@ namespace MonoTests.Microsoft.Win32
                        string subKeyName = Guid.NewGuid ().ToString ();
 
                        try {
-                               object rawValue = DateTime.Now;
+                               object rawValue = DateTime.UtcNow;
 
                                using (RegistryKey createdKey = Registry.CurrentUser.CreateSubKey (subKeyName)) {
                                        // we created a new subkey, so value should not exist
index 34a431aabd715880e7c6962ead01cbeb150e68d2..925a481b93ed44bceeb130199b4ee719034a094e 100644 (file)
@@ -978,7 +978,7 @@ namespace MonoTests.System.IO
                public void LastAccessTimeUtc ()
                {
                        DirectoryInfo info = new DirectoryInfo (TempFolder);
-                       info.LastAccessTimeUtc = DateTime.Now;
+                       info.LastAccessTimeUtc = DateTime.UtcNow;
                }
 
                [Test]
@@ -992,7 +992,7 @@ namespace MonoTests.System.IO
                public void CreationTimeUtc ()
                {
                        DirectoryInfo info = new DirectoryInfo (TempFolder);
-                       info.CreationTimeUtc = DateTime.Now;
+                       info.CreationTimeUtc = DateTime.UtcNow;
                }
 
                [Test]
index 98c939312eadb5c37dea947046bf681ae0ae4459..9dace8484e5e06c64f6c7a83e3e896cebe2a24f7 100644 (file)
@@ -27,6 +27,7 @@
 //
 
 using System;
+using System.Diagnostics;
 using System.Threading;
 using NUnit.Framework;
 
@@ -91,8 +92,8 @@ namespace MonoTests.System.Threading
                                else
                                        ThreadPool.QueueUserWorkItem (_ => { Interlocked.Increment (ref sum); Interlocked.Increment (ref total); });
                        }
-                       var start = DateTime.Now;
-                       while ((total != n || sum != 0) && (DateTime.Now - start).TotalSeconds < 60)
+                       var sw = Stopwatch.StartNew ();
+                       while ((total != n || sum != 0) && sw.Elapsed.TotalSeconds < 60)
                                Thread.Sleep (1000);
                        Assert.IsTrue (total == n, "#1");
                        Assert.IsTrue (sum   == 0, "#2");
@@ -160,7 +161,7 @@ namespace MonoTests.System.Threading
                public void GetAvailableThreads ()
                {
                        ManualResetEvent mre = new ManualResetEvent (false);
-                       DateTime start = DateTime.Now;
+                       var sw = Stopwatch.StartNew ();
                        int i, workerThreads, completionPortThreads;
 
                        try {
@@ -173,7 +174,7 @@ namespace MonoTests.System.Threading
 
                                        Console.WriteLine ("workerThreads = {0}, completionPortThreads = {1}", workerThreads, completionPortThreads);
 
-                                       if ((DateTime.Now - start).TotalSeconds >= 10)
+                                       if (sw.Elapsed.TotalSeconds >= 10)
                                                Assert.Fail ("did not reach 0 available threads");
 
                                        ThreadPool.QueueUserWorkItem (GetAvailableThreads_Callback, mre);
index a84fa27a77d742a5faaebda98b46758d2bff70e6..c45938e5be7af4c6976fe79ed12e25eead9ab123 100644 (file)
@@ -1349,9 +1349,9 @@ namespace MonoTests.System.Threading
                
                public static void WhileAlive (Thread t, bool alive, string s)
                {
-                       DateTime ti = DateTime.Now;
+                       var sw = SD.Stopwatch.StartNew ();
                        while (t.IsAlive == alive) {
-                               if ((DateTime.Now - ti).TotalSeconds > 10) {
+                               if (sw.Elapsed.TotalSeconds > 10) {
                                        if (alive) Assert.Fail ("Timeout while waiting for not alive state. " + s);
                                        else Assert.Fail ("Timeout while waiting for alive state. " + s);
                                }
@@ -1360,12 +1360,12 @@ namespace MonoTests.System.Threading
 
                public static bool WhileAliveOrStop (Thread t, bool alive, string s)
                {
-                       DateTime ti = DateTime.Now;
+                       var sw = SD.Stopwatch.StartNew ();
                        while (t.IsAlive == alive) {
                                if (t.ThreadState == ThreadState.Stopped)
                                        return false;
 
-                               if ((DateTime.Now - ti).TotalSeconds > 10) {
+                               if (sw.Elapsed.TotalSeconds > 10) {
                                        if (alive) Assert.Fail ("Timeout while waiting for not alive state. " + s);
                                        else Assert.Fail ("Timeout while waiting for alive state. " + s);
                                }
index 7ceec51affc0b047af1602c85b591cd4a386e0aa..723da25bebfb3f4e028f93c9f100a886f2b21cd9 100644 (file)
@@ -28,6 +28,7 @@
 
 
 using System;
+using System.Diagnostics;
 using System.Collections.Generic;
 using System.Threading;
 
@@ -558,8 +559,8 @@ namespace MonoTests.System.Threading {
 
                                Thread.Sleep (10); // wait a bit so we enter mre.WaitOne
 
-                               DateTime end = DateTime.Now.AddMilliseconds (500);
-                               while (DateTime.Now < end) {
+                               var sw = Stopwatch.StartNew ();
+                               while (sw.ElapsedMilliseconds <= 500) {
                                        thread.Suspend ();
                                        thread.Resume ();
                                }
@@ -588,8 +589,8 @@ namespace MonoTests.System.Threading {
 
                                Thread.Sleep (10); // wait a bit so we enter WaitHandle.WaitAny ({mre1, mre2})
 
-                               DateTime end = DateTime.Now.AddMilliseconds (500);
-                               while (DateTime.Now < end) {
+                               var sw = Stopwatch.StartNew ();
+                               while (sw.ElapsedMilliseconds <= 500) {
                                        thread.Suspend ();
                                        thread.Resume ();
                                }
@@ -618,8 +619,8 @@ namespace MonoTests.System.Threading {
 
                                Thread.Sleep (10); // wait a bit so we enter WaitHandle.WaitAll ({mre1, mre2})
 
-                               DateTime end = DateTime.Now.AddMilliseconds (500);
-                               while (DateTime.Now < end) {
+                               var sw = Stopwatch.StartNew ();
+                               while (sw.ElapsedMilliseconds <= 500) {
                                        thread.Suspend ();
                                        thread.Resume ();
                                }