[corlib] In a TimeZone test, UtcOffset is taken for the current year, so take Dayligh...
[mono.git] / mcs / class / corlib / Test / System / TimeZoneTest.cs
index 16037987260b263bf88a4ecd80c85b836171e50b..47915f722b425e2200c4b8fb3da81903950d05aa 100644 (file)
@@ -21,23 +21,6 @@ namespace MonoTests.System {
 [TestFixture]
 public class TimeZoneTest {
 
-       private CultureInfo oldcult;
-
-       public TimeZoneTest() {}
-
-       [SetUp]
-       protected void SetUp ()
-       {
-               oldcult = Thread.CurrentThread.CurrentCulture;
-               Thread.CurrentThread.CurrentCulture = new CultureInfo ("");
-       }
-
-       [TearDown]
-       protected void TearDown ()
-       {
-               Thread.CurrentThread.CurrentCulture = oldcult;
-       }
-
        private void CET (TimeZone t1) 
        {
                Assert.AreEqual("CET", t1.StandardName, "A01");
@@ -139,7 +122,34 @@ public class TimeZoneTest {
                Assert.AreEqual(0L, t1.GetUtcOffset (d5).Ticks, "D14");
        }
 
+       private void NZST(TimeZone t1) {
+               Assert.AreEqual("NZST", t1.StandardName, "E01");
+               Assert.AreEqual("NZDT", t1.DaylightName, "E02");
+
+               DaylightTime d1 = t1.GetDaylightChanges (2013);
+               Assert.AreEqual("09/29/2013 02:00:00", d1.Start.ToString ("G"), "E03");
+               Assert.AreEqual("04/07/2013 03:00:00", d1.End.ToString ("G"), "E04");
+               Assert.AreEqual(36000000000L, d1.Delta.Ticks, "E05");
+
+               DaylightTime d2 = t1.GetDaylightChanges (2001);
+               Assert.AreEqual("10/07/2001 02:00:00", d2.Start.ToString ("G"), "E06");
+               Assert.AreEqual("03/18/2001 03:00:00", d2.End.ToString ("G"), "E07");
+               Assert.AreEqual(36000000000L, d2.Delta.Ticks, "E08");
+
+               DateTime d3 = new DateTime(2013,02,15);
+               Assert.AreEqual(true, t1.IsDaylightSavingTime (d3), "E09");
+               DateTime d4 = new DateTime(2013,04,30);
+               Assert.AreEqual(false, t1.IsDaylightSavingTime (d4), "E10");
+               DateTime d5 = new DateTime(2013,11,03);
+               Assert.AreEqual(true, t1.IsDaylightSavingTime (d5), "E11");
+
+               Assert.AreEqual(36000000000L /*hour*/ * 13L, t1.GetUtcOffset (d3).Ticks, "E12");
+               Assert.AreEqual(36000000000L /*hour*/ * 12L, t1.GetUtcOffset (d4).Ticks, "E13");
+               Assert.AreEqual(36000000000L /*hour*/ * 13L, t1.GetUtcOffset (d5).Ticks, "E14");
+       }
+
        [Test]
+       [Culture ("")]
        public void TestCtors ()
        {
                TimeZone t1 = TimeZone.CurrentTimeZone;
@@ -157,6 +167,9 @@ public class TimeZoneTest {
                        case "GMT":
                                GMT (t1);
                                break;
+                       case "NZST":
+                               NZST (t1);
+                               break;
                        default:
                                NUnit.Framework.Assert.Ignore ("Your time zone (" + t1.StandardName + ") isn't defined in the test case");
                                break;
@@ -164,7 +177,6 @@ public class TimeZoneTest {
         }
 
        [Test]
-       [Category ("TargetJvmNotWorking")]
        public void CurrentTimeZone_SerializationRoundtrip ()
        {
                TimeZone tz = TimeZone.CurrentTimeZone;
@@ -259,13 +271,101 @@ public class TimeZoneTest {
                DateTime dst_start_utc = tz.GetDaylightChanges(2007).Start.ToUniversalTime ();
 
                if (dst_start_utc == DateTime.MinValue)
-                       return;
+                       Assert.Ignore ("Couldn't get beginning of daylight saving time in 2007.");
                Assert.IsTrue (tz.ToLocalTime (dst_start_utc.Subtract (new TimeSpan (0, 1, 0))) < tz.ToLocalTime (dst_start_utc), "0:1:59 < 0:3:00");
                Assert.IsTrue (tz.ToLocalTime (dst_start_utc) < tz.ToLocalTime (dst_start_utc.Add (new TimeSpan (0, 1, 0))), "0:3:00 < 0:3:01");
                Assert.IsTrue (tz.ToLocalTime (dst_start_utc.Add (new TimeSpan (0, 1, 0))) < tz.ToLocalTime (dst_start_utc.Add (new TimeSpan (0, 59, 0))), "0:3:01 < 0:3:59");
                Assert.IsTrue (tz.ToLocalTime (dst_start_utc.Add (new TimeSpan (0, 59, 0))) < tz.ToLocalTime (dst_start_utc.Add (new TimeSpan (1, 0, 0))), "0:3:59 < 0:4:00");
                Assert.IsTrue (tz.ToLocalTime (dst_start_utc.Add (new TimeSpan (1, 0, 0))) < tz.ToLocalTime (dst_start_utc.Add (new TimeSpan (1, 1, 0))), "0:4:00 < 0:4:01");
        }
-}
 
+               [Test]
+               public void GetUtcOffsetAtDSTBoundary ()
+               {
+                       /*
+                        * Getting a definitive list of timezones which do or don't observe Daylight
+                        * Savings is difficult (can't say America's or USA definitively) and lengthy see 
+                        *
+                        * http://en.wikipedia.org/wiki/Daylight_saving_time_by_country
+                        *
+                        * as a good starting point for a list.
+                        *
+                        * The following are SOME of the timezones/regions which do support daylight savings.
+                        *
+                        * Pacific/Auckland
+                        * Pacific/Sydney
+                        * USA (EST, CST, MST, PST, AKST) note this does not cover all states or regions
+                        * Europe/London (GMT)
+                        * CET (member states of the European Union)
+                        *
+                        * This test should work in all the above timezones
+                        */
+
+
+                       TimeZone tz = TimeZone.CurrentTimeZone;
+                       int year = DateTime.Now.Year;
+                       DaylightTime daylightChanges = tz.GetDaylightChanges(year);
+                       DateTime dst_end = daylightChanges.End;
+
+                       if (dst_end == DateTime.MinValue)
+                               Assert.Ignore (tz.StandardName + " did not observe daylight saving time during " + year + ".");
+
+                       var standardOffset = tz.GetUtcOffset(daylightChanges.Start.AddMinutes(-1));
+
+                       Assert.AreEqual(standardOffset, tz.GetUtcOffset (dst_end));
+                       Assert.AreEqual(standardOffset, tz.GetUtcOffset (dst_end.Add (daylightChanges.Delta.Negate ().Add (TimeSpan.FromSeconds(1)))));
+                       Assert.AreEqual(standardOffset, tz.GetUtcOffset (dst_end.Add(daylightChanges.Delta.Negate ())));
+                       Assert.AreNotEqual(standardOffset, tz.GetUtcOffset (dst_end.Add(daylightChanges.Delta.Negate ().Add (TimeSpan.FromSeconds(-1)))));
+               }
+
+
+               [Test]
+               public void StaticProperties ()
+               {
+                       Assert.IsNotNull (TimeZoneInfo.Local, "Local");
+                       Assert.IsNotNull (TimeZoneInfo.Utc, "Utc");
+               }
+               
+               [Test]
+               public void FindSystemTimeZoneById ()
+               {
+                       TimeZoneInfo tzi = TimeZoneInfo.FindSystemTimeZoneById ("Canada/Eastern");
+                       Assert.AreEqual ("EDT", tzi.DaylightName, "DaylightName");
+                       Assert.AreEqual ("EST", tzi.StandardName, "StandardName");
+                       Assert.IsTrue (tzi.SupportsDaylightSavingTime, "SupportsDaylightSavingTime");
+               }
+
+#if MOBILE
+               // On device we cannot read the OS file system to look for /etc/localtime
+               // and /usr/share/zoneinfo - so we must initialize the BCL TimeZoneInfo
+               // from NSTimeZoneInfo. The tests here check the code paths between the
+               // two types - if they break then TimeZoneInfo work work at all
+               // ref: http://bugzilla.xamarin.com/show_bug.cgi?id=1790
+               
+               bool incomplete_data_on_simulator_only_bug;
+
+               [Test]
+               public void GetSystemTimeZones ()
+               {
+                       // if test is executed a second time then it report less than 400 (about 127) items available
+                       if (incomplete_data_on_simulator_only_bug)
+                               Assert.Ignore ("known to fail on some iOS simulator versions - see source comments");
+                       
+                       try {
+                               Assert.That (TimeZoneInfo.GetSystemTimeZones ().Count, Is.GreaterThan (400), "GetSystemTimeZones");
+                       } catch (NullReferenceException) {
+                               // that's a weird one. It failed on iOS 5.1 *beta* simulator (on Lion) but it worked on *final*
+                               // now it fails on Snow Leopard the same way (incomplete data) with iOS5 simulator (OS update ?)
+                               // but it *never*ever* failed on devices
+                               incomplete_data_on_simulator_only_bug = true;
+#if XAMCORE_2_0
+                               if (ObjCRuntime.Runtime.Arch == ObjCRuntime.Arch.SIMULATOR)
+#else
+                               if (MonoTouch.ObjCRuntime.Runtime.Arch == MonoTouch.ObjCRuntime.Arch.SIMULATOR)
+#endif
+                                       Assert.Ignore ("known to fail on some iOS simulator versions - see source comments");
+                       }
+               }
+#endif
+       }
 }