+ [Test]
+ public void ToLocalTimeAtDSTBoundaries ()
+ {
+ TimeZone tz = TimeZone.CurrentTimeZone;
+ DateTime dst_start_utc = tz.GetDaylightChanges(2007).Start.ToUniversalTime ();
+
+ if (dst_start_utc == DateTime.MinValue)
+ return;
+ 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;
+ DaylightTime daylightChanges = tz.GetDaylightChanges(2007);
+ DateTime dst_end = daylightChanges.End;
+
+ if (dst_end == DateTime.MinValue)
+ Assert.Ignore (tz.StandardName + " did not observe daylight saving time during 2007.");
+
+ 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 (MonoTouch.ObjCRuntime.Runtime.Arch == MonoTouch.ObjCRuntime.Arch.SIMULATOR)
+ Assert.Ignore ("known to fail on some iOS simulator versions - see source comments");
+ }
+ }
+#endif
+ }