[corlib] TimeZoneInfo.GetSystemTimeZones should work as singleton. Fixes #29906
authorMarek Safar <marek.safar@gmail.com>
Mon, 25 May 2015 12:55:29 +0000 (14:55 +0200)
committerMarek Safar <marek.safar@gmail.com>
Mon, 25 May 2015 12:55:29 +0000 (14:55 +0200)
mcs/class/corlib/System/TimeZoneInfo.cs
mcs/class/corlib/Test/System/TimeZoneInfoTest.cs

index 0457583e4c0598c42290a1ef980ab7ef0602fcfc..dc144d38de6eeba2ffe319495dda5b2f98e62b45 100644 (file)
@@ -639,16 +639,17 @@ namespace System
                        info.AddValue ("SupportsDaylightSavingTime", SupportsDaylightSavingTime);
                }
 
-               //FIXME: change this to a generic Dictionary and allow caching for FindSystemTimeZoneById
-               private static List<TimeZoneInfo> systemTimeZones;
+               static ReadOnlyCollection<TimeZoneInfo> systemTimeZones;
+
                public static ReadOnlyCollection<TimeZoneInfo> GetSystemTimeZones ()
                {
                        if (systemTimeZones == null) {
-                               systemTimeZones = new List<TimeZoneInfo> ();
-                               GetSystemTimeZones (systemTimeZones);
+                               var tz = new List<TimeZoneInfo> ();
+                               GetSystemTimeZones (tz);
+                               Interlocked.CompareExchange (ref systemTimeZones, new ReadOnlyCollection<TimeZoneInfo> (tz), null);
                        }
 
-                       return new ReadOnlyCollection<TimeZoneInfo> (systemTimeZones);
+                       return systemTimeZones;
                }
 
                public TimeSpan GetUtcOffset (DateTime dateTime)
index bdce606080e01e90ef49fed05f36b5dfffc472b1..7ed78fc26e3799f1a1377752f240c977b9370943 100644 (file)
@@ -664,6 +664,12 @@ namespace MonoTests.System
                [TestFixture]
                public class GetSystemTimeZonesTests
                {
+                       [Test]
+                       public void Identity ()
+                       {
+                               Assert.AreSame (TimeZoneInfo.GetSystemTimeZones (), TimeZoneInfo.GetSystemTimeZones ());
+                       }
+
                        [Test]
                        public void NotEmpty ()
                        {