5 * Stephane Delcroix <stephane@delcroix.org>
7 * Copyright 2011 Xamarin Inc.
9 * Permission is hereby granted, free of charge, to any person obtaining
10 * a copy of this software and associated documentation files (the
11 * "Software"), to deal in the Software without restriction, including
12 * without limitation the rights to use, copy, modify, merge, publish,
13 * distribute, sublicense, and/or sell copies of the Software, and to
14 * permit persons to whom the Software is furnished to do so, subject to
15 * the following conditions:
17 * The above copyright notice and this permission notice shall be
18 * included in all copies or substantial portions of the Software.
20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 using System.Runtime.Serialization.Formatters.Binary;
32 using System.Collections;
33 using System.Reflection;
34 using System.Globalization;
36 using NUnit.Framework;
37 namespace MonoTests.System
39 public class TimeZoneInfoTest
42 public class PropertiesTests
45 public void GetLocal ()
47 if (Environment.OSVersion.Platform != PlatformID.Unix)
48 Assert.Ignore ("Not running on Unix.");
49 TimeZoneInfo local = TimeZoneInfo.Local;
50 Assert.IsNotNull (local);
56 public class CreateCustomTimezoneTests
59 [ExpectedException (typeof (ArgumentNullException))]
60 public void IdIsNullException ()
62 TimeZoneInfo.CreateCustomTimeZone (null, new TimeSpan (0), null, null);
66 [ExpectedException (typeof (ArgumentException))]
67 public void IdIsEmptyString ()
69 TimeZoneInfo.CreateCustomTimeZone ("", new TimeSpan (0), null, null);
73 [ExpectedException (typeof (ArgumentException))]
74 public void OffsetIsNotMinutes ()
76 TimeZoneInfo.CreateCustomTimeZone ("mytimezone", new TimeSpan (0, 0, 55), null, null);
80 [ExpectedException (typeof (ArgumentOutOfRangeException))]
81 public void OffsetTooBig ()
83 TimeZoneInfo.CreateCustomTimeZone ("mytimezone", new TimeSpan (14, 1, 0), null, null);
87 [ExpectedException (typeof (ArgumentOutOfRangeException))]
88 public void OffsetTooSmall ()
90 TimeZoneInfo.CreateCustomTimeZone ("mytimezone", - new TimeSpan (14, 1, 0), null, null);
95 [ExpectedException (typeof (ArgumentException))]
96 public void IdLongerThan32 ()
98 TimeZoneInfo.CreateCustomTimeZone ("12345678901234567890123456789012345", new TimeSpan (0), null, null);
103 [ExpectedException (typeof (InvalidTimeZoneException))]
104 public void AdjustmentRulesOverlap ()
106 TimeZoneInfo.TransitionTime s1 = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,4,0,0), 3, 2, DayOfWeek.Sunday);
107 TimeZoneInfo.TransitionTime e1 = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,4,0,0), 10, 2, DayOfWeek.Sunday);
108 TimeZoneInfo.AdjustmentRule r1 = TimeZoneInfo.AdjustmentRule.CreateAdjustmentRule (new DateTime (2000,1,1), new DateTime (2005,1,1), new TimeSpan (1,0,0), s1, e1);
109 TimeZoneInfo.TransitionTime s2 = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,4,0,0), 2, 2, DayOfWeek.Sunday);
110 TimeZoneInfo.TransitionTime e2 = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,4,0,0), 11, 2, DayOfWeek.Sunday);
111 TimeZoneInfo.AdjustmentRule r2 = TimeZoneInfo.AdjustmentRule.CreateAdjustmentRule (new DateTime (2004,1,1), new DateTime (2007,1,1), new TimeSpan (1,0,0), s2, e2);
112 TimeZoneInfo.CreateCustomTimeZone ("mytimezone", new TimeSpan (6,0,0),null,null,null,new TimeZoneInfo.AdjustmentRule[] {r1, r2});
116 [ExpectedException (typeof (InvalidTimeZoneException))]
117 public void RulesNotOrdered ()
119 TimeZoneInfo.TransitionTime s1 = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,4,0,0), 3, 2, DayOfWeek.Sunday);
120 TimeZoneInfo.TransitionTime e1 = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,4,0,0), 10, 2, DayOfWeek.Sunday);
121 TimeZoneInfo.AdjustmentRule r1 = TimeZoneInfo.AdjustmentRule.CreateAdjustmentRule (new DateTime (2000,1,1), new DateTime (2005,1,1), new TimeSpan (1,0,0), s1, e1);
122 TimeZoneInfo.TransitionTime s2 = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,4,0,0), 2, 2, DayOfWeek.Sunday);
123 TimeZoneInfo.TransitionTime e2 = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,4,0,0), 11, 2, DayOfWeek.Sunday);
124 TimeZoneInfo.AdjustmentRule r2 = TimeZoneInfo.AdjustmentRule.CreateAdjustmentRule (new DateTime (2006,1,1), new DateTime (2007,1,1), new TimeSpan (1,0,0), s2, e2);
125 TimeZoneInfo.CreateCustomTimeZone ("mytimezone", new TimeSpan (6,0,0),null,null,null,new TimeZoneInfo.AdjustmentRule[] {r2, r1});
129 [ExpectedException (typeof (InvalidTimeZoneException))]
130 public void OffsetOutOfRange ()
132 TimeZoneInfo.TransitionTime startTransition = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,4,0,0), 3, 2, DayOfWeek.Sunday);
133 TimeZoneInfo.TransitionTime endTransition = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,4,0,0), 10, 2, DayOfWeek.Sunday);
134 TimeZoneInfo.AdjustmentRule rule = TimeZoneInfo.AdjustmentRule.CreateAdjustmentRule (new DateTime (2000,1,1), new DateTime (2005,1,1), new TimeSpan (3,0,0), startTransition, endTransition);
135 TimeZoneInfo.CreateCustomTimeZone ("mytimezone", new TimeSpan (12,0,0),null,null,null,new TimeZoneInfo.AdjustmentRule[] {rule});
139 [ExpectedException (typeof (InvalidTimeZoneException))]
140 public void NullRule ()
142 TimeZoneInfo.CreateCustomTimeZone ("mytimezone", new TimeSpan (12,0,0),null,null,null,new TimeZoneInfo.AdjustmentRule[] {null});
146 [ExpectedException (typeof (InvalidTimeZoneException))]
147 public void MultiplesRulesForDate ()
149 TimeZoneInfo.TransitionTime s1 = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,4,0,0), 3, 2, DayOfWeek.Sunday);
150 TimeZoneInfo.TransitionTime e1 = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,4,0,0), 10, 2, DayOfWeek.Sunday);
151 TimeZoneInfo.AdjustmentRule r1 = TimeZoneInfo.AdjustmentRule.CreateAdjustmentRule (new DateTime (2000,1,1), new DateTime (2005,1,1), new TimeSpan (1,0,0), s1, e1);
152 TimeZoneInfo.TransitionTime s2 = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,4,0,0), 2, 2, DayOfWeek.Sunday);
153 TimeZoneInfo.TransitionTime e2 = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,4,0,0), 11, 2, DayOfWeek.Sunday);
154 TimeZoneInfo.AdjustmentRule r2 = TimeZoneInfo.AdjustmentRule.CreateAdjustmentRule (new DateTime (2005,1,1), new DateTime (2007,1,1), new TimeSpan (1,0,0), s2, e2);
155 TimeZoneInfo.CreateCustomTimeZone ("mytimezone", new TimeSpan (6,0,0),null,null,null,new TimeZoneInfo.AdjustmentRule[] {r1, r2});
159 public void SupportsDaylightSavingTime_NonEmptyAdjustmentRule ()
161 TimeZoneInfo.TransitionTime s1 = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,4,0,0), 3, 2, DayOfWeek.Sunday);
162 TimeZoneInfo.TransitionTime e1 = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,4,0,0), 10, 2, DayOfWeek.Sunday);
163 TimeZoneInfo.AdjustmentRule r1 = TimeZoneInfo.AdjustmentRule.CreateAdjustmentRule (new DateTime (2000,1,1), new DateTime (2005,1,1), new TimeSpan (1,0,0), s1, e1);
164 TimeZoneInfo tz = TimeZoneInfo.CreateCustomTimeZone ("mytimezone", new TimeSpan (6,0,0),null,null,null,new TimeZoneInfo.AdjustmentRule[] {r1});
165 Assert.IsTrue (tz.SupportsDaylightSavingTime);
169 public void SupportsDaylightSavingTime_EmptyAdjustmentRule ()
171 TimeZoneInfo tz = TimeZoneInfo.CreateCustomTimeZone ("mytimezone", new TimeSpan (6,0,0),null,null,null,null);
172 Assert.IsFalse (tz.SupportsDaylightSavingTime);
176 public void SupportsDaylightSavingTime_NonEmptyAdjustmentRule_DisableDaylightSavingTime ()
178 TimeZoneInfo.TransitionTime s1 = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,4,0,0), 3, 2, DayOfWeek.Sunday);
179 TimeZoneInfo.TransitionTime e1 = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,4,0,0), 10, 2, DayOfWeek.Sunday);
180 TimeZoneInfo.AdjustmentRule r1 = TimeZoneInfo.AdjustmentRule.CreateAdjustmentRule (new DateTime (2000,1,1), new DateTime (2005,1,1), new TimeSpan (1,0,0), s1, e1);
181 TimeZoneInfo tz = TimeZoneInfo.CreateCustomTimeZone ("mytimezone", new TimeSpan (6,0,0),null,null,null,new TimeZoneInfo.AdjustmentRule[] {r1}, true);
182 Assert.IsFalse (tz.SupportsDaylightSavingTime);
186 public void SupportsDaylightSavingTime_EmptyAdjustmentRule_DisableDaylightSavingTime ()
188 TimeZoneInfo tz = TimeZoneInfo.CreateCustomTimeZone ("mytimezone", new TimeSpan (6,0,0),null,null,null,null,true);
189 Assert.IsFalse (tz.SupportsDaylightSavingTime);
194 public class IsDaylightSavingTimeTests
199 public void CreateTimeZones ()
201 TimeZoneInfo.TransitionTime start = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,1,0,0), 3, 5, DayOfWeek.Sunday);
202 TimeZoneInfo.TransitionTime end = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,2,0,0), 10, 5, DayOfWeek.Sunday);
203 TimeZoneInfo.AdjustmentRule rule = TimeZoneInfo.AdjustmentRule.CreateAdjustmentRule (DateTime.MinValue.Date, DateTime.MaxValue.Date, new TimeSpan (1,0,0), start, end);
204 london = TimeZoneInfo.CreateCustomTimeZone ("Europe/London", new TimeSpan (0), "Europe/London", "British Standard Time", "British Summer Time", new TimeZoneInfo.AdjustmentRule [] {rule});
208 public void NoDSTInUTC ()
210 DateTime june01 = new DateTime (2007, 06, 01);
211 Assert.IsFalse (TimeZoneInfo.Utc.IsDaylightSavingTime (june01));
215 public void DSTInLondon ()
217 if (Environment.OSVersion.Platform != PlatformID.Unix)
218 Assert.Ignore ("Not running on Unix.");
219 DateTime june01 = new DateTime (2007, 06, 01);
220 DateTime xmas = new DateTime (2007, 12, 25);
221 Assert.IsTrue (london.IsDaylightSavingTime (june01), "June 01 is DST in London");
222 Assert.IsFalse (london.IsDaylightSavingTime (xmas), "Xmas is not DST in London");
226 public void DSTTransisions ()
228 if (Environment.OSVersion.Platform != PlatformID.Unix)
229 Assert.Ignore ("Not running on Unix.");
230 DateTime beforeDST = new DateTime (2007, 03, 25, 0, 59, 59, DateTimeKind.Unspecified);
231 DateTime startDST = new DateTime (2007, 03, 25, 2, 0, 0, DateTimeKind.Unspecified);
232 DateTime endDST = new DateTime (2007, 10, 28, 1, 59, 59, DateTimeKind.Unspecified);
233 DateTime afterDST = new DateTime (2007, 10, 28, 2, 0, 0, DateTimeKind.Unspecified);
234 Assert.IsFalse (london.IsDaylightSavingTime (beforeDST), "Just before DST");
235 Assert.IsTrue (london.IsDaylightSavingTime (startDST), "the first seconds of DST");
236 Assert.IsFalse (london.IsDaylightSavingTime (endDST), "The last seconds of DST");
237 Assert.IsFalse (london.IsDaylightSavingTime (afterDST), "Just after DST");
241 public void DSTTransisionsUTC ()
243 DateTime beforeDST = new DateTime (2007, 03, 25, 0, 59, 59, DateTimeKind.Utc);
244 DateTime startDST = new DateTime (2007, 03, 25, 1, 0, 0, DateTimeKind.Utc);
245 DateTime endDST = new DateTime (2007, 10, 28, 0, 59, 59, DateTimeKind.Utc);
246 DateTime afterDST = new DateTime (2007, 10, 28, 1, 0, 0, DateTimeKind.Utc);
247 Assert.IsFalse (london.IsDaylightSavingTime (beforeDST), "Just before DST");
248 Assert.IsTrue (london.IsDaylightSavingTime (startDST), "the first seconds of DST");
249 Assert.IsTrue (london.IsDaylightSavingTime (endDST), "The last seconds of DST");
250 Assert.IsFalse (london.IsDaylightSavingTime (afterDST), "Just after DST");
255 public void MatchTimeZoneBehavior ()
257 TimeZone tzone = TimeZone.CurrentTimeZone;
258 TimeZoneInfo local = TimeZoneInfo.Local;
259 for (DateTime date = new DateTime (2007, 01, 01, 0, 0, 0, DateTimeKind.Local); date < new DateTime (2007, 12, 31, 23, 59, 59); date += new TimeSpan (0,1,0)) {
260 date = DateTime.SpecifyKind (date, DateTimeKind.Local);
261 if (local.IsInvalidTime (date))
263 Assert.IsTrue (tzone.IsDaylightSavingTime (date) == local.IsDaylightSavingTime (date));
267 [Test (Description="Description xambug #17155")]
268 public void AdjustmentRuleAfterNewYears ()
271 if (Environment.OSVersion.Platform == PlatformID.Unix)
272 tz = TimeZoneInfo.FindSystemTimeZoneById ("Pacific/Auckland"); // *nix
274 tz = TimeZoneInfo.FindSystemTimeZoneById ("New Zealand Standard Time"); // Windows
276 // DST start: 9/29/2013 2:00:00 AM
277 // DST end: 4/6/2014 3:00:00 AM
278 DateTime dt = new DateTime (2014, 1, 9, 23, 0, 0, DateTimeKind.Utc);
279 Assert.IsTrue (tz.IsDaylightSavingTime (dt), "#1.1");
281 // DST start: 9/29/2014 2:00:00 AM
282 // DST end: 4/6/2015 3:00:00 AM
283 dt = new DateTime (2014, 6, 9, 23, 0, 0, DateTimeKind.Utc);
284 Assert.IsFalse (tz.IsDaylightSavingTime (dt), "#2.1");
286 // DST start: 9/29/2014 2:00:00 AM
287 // DST end: 4/6/2015 3:00:00 AM
288 dt = new DateTime (2014, 10, 9, 23, 0, 0, DateTimeKind.Utc);
289 Assert.IsTrue (tz.IsDaylightSavingTime (dt), "#3.1");
292 [Test] //Covers #26008
293 public void DSTWithFloatingDateRule ()
295 // Construct a custom time zone where daylight saving time starts on the
296 // 2nd Sunday in March.
297 var transitionToDaylight = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1, 1, 1, 2, 0, 0), 3, 2, DayOfWeek.Sunday);
298 var transitionToStandard = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1, 1, 1, 2, 0, 0), 11, 1, DayOfWeek.Sunday);
299 var adjustment = TimeZoneInfo.AdjustmentRule.CreateAdjustmentRule (DateTime.MinValue.Date, DateTime.MaxValue.Date, new TimeSpan (1, 0, 0), transitionToDaylight, transitionToStandard);
300 var timeZone = TimeZoneInfo.CreateCustomTimeZone ("BugCheck", new TimeSpan (-8, 0, 0), "Testing", "Testing Standard", "Testing Daylight", new TimeZoneInfo.AdjustmentRule [] { adjustment });
301 // See if March 7, 2014 is listed as being during daylight saving time.
302 // If it is DST, then the runtime has the bug that we are looking for.
303 Assert.IsFalse (timeZone.IsDaylightSavingTime (new DateTime (2014, 3, 7, 12, 0, 0, DateTimeKind.Unspecified)));
306 [Test] //Covers #25050
307 public void TestAthensDST ()
309 TimeZoneInfo tzi = TimeZoneInfo.FindSystemTimeZoneById ("Europe/Athens");
310 var date = new DateTime (2014, 3, 30 , 2, 0, 0);
311 Assert.IsFalse (tzi.IsDaylightSavingTime (date));
312 Assert.AreEqual (new TimeSpan (2,0,0), tzi.GetUtcOffset (date));
317 public class ConvertTimeTests
322 public void CreateTimeZones ()
324 TimeZoneInfo.TransitionTime start = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,1,0,0), 3, 5, DayOfWeek.Sunday);
325 TimeZoneInfo.TransitionTime end = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,2,0,0), 10, 5, DayOfWeek.Sunday);
326 TimeZoneInfo.AdjustmentRule rule = TimeZoneInfo.AdjustmentRule.CreateAdjustmentRule (DateTime.MinValue.Date, DateTime.MaxValue.Date, new TimeSpan (1,0,0), start, end);
327 london = TimeZoneInfo.CreateCustomTimeZone ("Europe/London", new TimeSpan (0), "Europe/London", "British Standard Time", "British Summer Time", new TimeZoneInfo.AdjustmentRule [] {rule});
331 [ExpectedException (typeof (ArgumentException))]
332 public void ConvertFromUtc_KindIsLocalException ()
334 if (Environment.OSVersion.Platform != PlatformID.Unix)
335 throw new ArgumentException ();
336 TimeZoneInfo.ConvertTimeFromUtc (new DateTime (2007, 5, 3, 11, 8, 0, DateTimeKind.Local), TimeZoneInfo.Local);
340 [ExpectedException (typeof (ArgumentNullException))]
341 public void ConvertFromUtc_DestinationTimeZoneIsNullException ()
343 TimeZoneInfo.ConvertTimeFromUtc (new DateTime (2007, 5, 3, 11, 8, 0), null);
347 public void ConvertFromUtc_DestinationIsUTC ()
349 DateTime now = DateTime.UtcNow;
350 DateTime converted = TimeZoneInfo.ConvertTimeFromUtc (now, TimeZoneInfo.Utc);
351 Assert.AreEqual (now, converted);
355 public void ConvertFromUTC_ConvertInWinter ()
357 if (Environment.OSVersion.Platform != PlatformID.Unix)
358 Assert.Ignore ("Not running on Unix.");
359 DateTime utc = new DateTime (2007, 12, 25, 12, 0, 0);
360 DateTime converted = TimeZoneInfo.ConvertTimeFromUtc (utc, london);
361 Assert.AreEqual (utc, converted);
365 public void ConvertFromUtc_ConvertInSummer ()
367 if (Environment.OSVersion.Platform != PlatformID.Unix)
368 Assert.Ignore ("Not running on Unix.");
369 DateTime utc = new DateTime (2007, 06, 01, 12, 0, 0);
370 DateTime converted = TimeZoneInfo.ConvertTimeFromUtc (utc, london);
371 Assert.AreEqual (utc + new TimeSpan (1,0,0), converted);
375 public void ConvertToUTC_KindIsUtc ()
377 DateTime now = DateTime.UtcNow;
378 Assert.AreEqual (now.Kind, DateTimeKind.Utc);
379 DateTime converted = TimeZoneInfo.ConvertTimeToUtc (now);
380 Assert.AreEqual (now, converted);
384 [ExpectedException (typeof (ArgumentException))]
385 public void ConvertToUTC_KindIsUTCButSourceIsNot ()
387 TimeZoneInfo.ConvertTimeToUtc (new DateTime (2007, 5, 3, 12, 8, 0, DateTimeKind.Utc), london);
391 [ExpectedException (typeof (ArgumentException))]
392 public void ConvertToUTC_KindIsLocalButSourceIsNot ()
394 if (Environment.OSVersion.Platform != PlatformID.Unix)
395 throw new ArgumentException ();
396 TimeZoneInfo.ConvertTimeToUtc (new DateTime (2007, 5, 3, 12, 8, 0, DateTimeKind.Local), london);
400 [ExpectedException (typeof (ArgumentException))]
401 public void ConvertToUTC_InvalidDate ()
403 TimeZoneInfo.ConvertTimeToUtc (new DateTime (2007, 3, 25, 1, 30, 0), london);
407 [ExpectedException (typeof (ArgumentNullException))]
408 public void ConvertToUTC_SourceIsNull ()
410 TimeZoneInfo.ConvertTimeToUtc (new DateTime (2007, 5, 3, 12, 16, 0), null);
415 public void ConvertToUtc_MatchDateTimeBehavior ()
417 for (DateTime date = new DateTime (2007, 01, 01, 0, 0, 0); date < new DateTime (2007, 12, 31, 23, 59, 59); date += new TimeSpan (0,1,0)) {
418 Assert.AreEqual (TimeZoneInfo.ConvertTimeToUtc (date), date.ToUniversalTime ());
424 public void ConvertFromToUtc ()
426 if (Environment.OSVersion.Platform != PlatformID.Unix)
427 Assert.Ignore ("Not running on Unix.");
428 DateTime utc = DateTime.UtcNow;
429 Assert.AreEqual (utc.Kind, DateTimeKind.Utc);
430 DateTime converted = TimeZoneInfo.ConvertTimeFromUtc (utc, london);
431 Assert.AreEqual (converted.Kind, DateTimeKind.Unspecified);
432 DateTime back = TimeZoneInfo.ConvertTimeToUtc (converted, london);
433 Assert.AreEqual (back.Kind, DateTimeKind.Utc);
434 Assert.AreEqual (utc, back);
439 public void ConvertTimeToUtc_Overflow ()
441 var res = TimeZoneInfo.ConvertTimeToUtc (new DateTime (0));
442 Assert.AreEqual (res.Kind, DateTimeKind.Utc, "#1");
444 res = TimeZoneInfo.ConvertTimeToUtc (DateTime.MaxValue);
445 Assert.AreEqual (res.Kind, DateTimeKind.Utc, "#2");
449 public void ConvertFromToLocal ()
451 DateTime utc = DateTime.UtcNow;
452 Assert.AreEqual(utc.Kind, DateTimeKind.Utc);
453 DateTime converted = TimeZoneInfo.ConvertTimeFromUtc(utc, TimeZoneInfo.Local);
454 Assert.AreEqual(DateTimeKind.Local, converted.Kind);
455 DateTime back = TimeZoneInfo.ConvertTimeToUtc(converted, TimeZoneInfo.Local);
456 Assert.AreEqual(back.Kind, DateTimeKind.Utc);
457 Assert.AreEqual(utc, back);
461 public void ConvertToTimeZone ()
463 if (Environment.OSVersion.Platform != PlatformID.Unix)
464 Assert.Ignore ("Not running on Unix.");
466 TimeZoneInfo.ConvertTime (DateTime.Now, TimeZoneInfo.FindSystemTimeZoneById("Pacific/Auckland"));
470 [ExpectedException (typeof (ArgumentNullException))]
471 public void ConvertTime_DateTime_TimeZoneInfo_DestinationTimeZoneIsNull ()
473 TimeZoneInfo.ConvertTime (DateTime.Now, null);
477 public void ConvertTime_DateTime_TimeZoneInfo_DateTimeKindMatch ()
479 var sdt = new DateTime (2014, 1, 9, 23, 0, 0, DateTimeKind.Utc);
480 var ddt = TimeZoneInfo.ConvertTime (sdt, TimeZoneInfo.Utc);
481 Assert.AreEqual (ddt.Kind, sdt.Kind, "#1.1");
482 Assert.AreEqual (ddt.Kind, DateTimeKind.Utc, "#1.2");
484 sdt = new DateTime (2014, 1, 9, 23, 0, 0, DateTimeKind.Local);
485 ddt = TimeZoneInfo.ConvertTime (sdt, TimeZoneInfo.Local);
486 Assert.AreEqual (ddt.Kind, sdt.Kind, "#2.1");
487 Assert.AreEqual (ddt.Kind, DateTimeKind.Local, "#2.2");
489 sdt = new DateTime (2014, 1, 9, 23, 0, 0);
490 ddt = TimeZoneInfo.ConvertTime (sdt, TimeZoneInfo.Local);
491 Assert.AreEqual (ddt.Kind, sdt.Kind, "#3.1");
492 Assert.AreEqual (ddt.Kind, DateTimeKind.Unspecified, "#3.2");
496 [ExpectedException (typeof (ArgumentNullException))]
497 public void ConverTime_DateTime_TimeZoneInfo_TimeZoneInfo_SourceTimeZoneIsNull ()
499 TimeZoneInfo.ConvertTime (DateTime.Now, null, TimeZoneInfo.Local);
503 [ExpectedException (typeof (ArgumentNullException))]
504 public void ConverTime_DateTime_TimeZoneInfo_TimeZoneInfo_DestinationTimeZoneIsNull ()
506 TimeZoneInfo.ConvertTime (DateTime.Now, TimeZoneInfo.Utc, null);
509 [Test (Description="Fix for xambug https://bugzilla.xamarin.com/show_bug.cgi?id=17155")]
510 public void ConvertTime_AdjustmentRuleAfterNewYears ()
513 if (Environment.OSVersion.Platform == PlatformID.Unix)
514 tz = TimeZoneInfo.FindSystemTimeZoneById ("Pacific/Auckland"); // *nix
516 tz = TimeZoneInfo.FindSystemTimeZoneById ("New Zealand Standard Time"); // Windows
518 // DST start: 9/29/2013 2:00:00 AM
519 // DST end: 4/6/2014 3:00:00 AM
520 DateTime sdt = new DateTime (2014, 1, 9, 23, 0, 0, DateTimeKind.Utc);
521 DateTime ddt = TimeZoneInfo.ConvertTime (sdt, tz);
522 Assert.AreEqual (10, ddt.Day, "#1.1");
523 Assert.AreEqual (1, ddt.Month, "#1.2");
524 Assert.AreEqual (2014, ddt.Year, "#1.3");
525 Assert.AreEqual (12, ddt.Hour, "#1.4");
526 Assert.AreEqual (0, ddt.Minute, "#1.5");
527 Assert.AreEqual (0, ddt.Second, "#1.6");
529 // DST start: 9/29/2014 2:00:00 AM
530 // DST end: 4/6/2015 3:00:00 AM
531 sdt = new DateTime (2014, 6, 9, 23, 0, 0, DateTimeKind.Utc);
532 ddt = TimeZoneInfo.ConvertTime (sdt, tz);
533 Assert.AreEqual (10, ddt.Day, "#2.1");
534 Assert.AreEqual (6, ddt.Month, "#2.2");
535 Assert.AreEqual (2014, ddt.Year, "#2.3");
536 Assert.AreEqual (11, ddt.Hour, "#2.4");
537 Assert.AreEqual (0, ddt.Minute, "#2.5");
538 Assert.AreEqual (0, ddt.Second, "#2.6");
540 // DST start: 9/29/2014 2:00:00 AM
541 // DST end: 4/6/2015 3:00:00 AM
542 sdt = new DateTime (2014, 10, 9, 23, 0, 0, DateTimeKind.Utc);
543 ddt = TimeZoneInfo.ConvertTime (sdt, tz);
544 Assert.AreEqual (10, ddt.Day, "#3.1");
545 Assert.AreEqual (10, ddt.Month, "#3.2");
546 Assert.AreEqual (2014, ddt.Year, "#3.3");
547 Assert.AreEqual (12, ddt.Hour, "#3.4");
548 Assert.AreEqual (0, ddt.Minute, "#3.5");
549 Assert.AreEqual (0, ddt.Second, "#3.6");
552 [Test (Description="Fix the bug https://bugzilla.xamarin.com/show_bug.cgi?id=1849")]
553 public void ConvertTime_AjustmentConvertTimeWithSourceTimeZone () {
555 TimeZoneInfo easternTimeZone;
556 TimeZoneInfo pacificTimeZone;
558 if (Environment.OSVersion.Platform == PlatformID.Unix) {
560 easternTimeZone = TimeZoneInfo.FindSystemTimeZoneById ("US/Eastern");
561 pacificTimeZone = TimeZoneInfo.FindSystemTimeZoneById ("US/Pacific");
565 easternTimeZone = TimeZoneInfo.FindSystemTimeZoneById ("Eastern Standard Time");
566 pacificTimeZone = TimeZoneInfo.FindSystemTimeZoneById ("Pacific Standard Time");
569 DateTime lastMidnight = new DateTime (new DateTime (2012, 06, 13).Ticks, DateTimeKind.Unspecified);
570 DateTime lastMidnightAsEST = TimeZoneInfo.ConvertTime (lastMidnight, pacificTimeZone, easternTimeZone);
571 DateTime lastMidnightAsPST = TimeZoneInfo.ConvertTime (lastMidnightAsEST, easternTimeZone, pacificTimeZone);
573 // Last midnight in PST as EST should be 3AM
574 DateTime expectedDate = new DateTime (2012, 06, 13, 3, 0, 0);
576 Assert.AreEqual (expectedDate, lastMidnightAsEST);
577 Assert.AreEqual (lastMidnight, lastMidnightAsPST);
582 public class IsInvalidTimeTests
587 public void CreateTimeZones ()
589 TimeZoneInfo.TransitionTime start = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,1,0,0), 3, 5, DayOfWeek.Sunday);
590 TimeZoneInfo.TransitionTime end = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,2,0,0), 10, 5, DayOfWeek.Sunday);
591 TimeZoneInfo.AdjustmentRule rule = TimeZoneInfo.AdjustmentRule.CreateAdjustmentRule (DateTime.MinValue.Date, DateTime.MaxValue.Date, new TimeSpan (1,0,0), start, end);
592 london = TimeZoneInfo.CreateCustomTimeZone ("Europe/London", new TimeSpan (0), "Europe/London", "British Standard Time", "British Summer Time", new TimeZoneInfo.AdjustmentRule [] {rule});
597 public void UTCDate ()
599 for (DateTime date = new DateTime (2007, 01, 01, 0, 0, 0); date < new DateTime (2007, 12, 31, 23, 59, 59); date += new TimeSpan (0,1,0)) {
600 date = DateTime.SpecifyKind (date, DateTimeKind.Utc);
601 Assert.IsFalse (london.IsInvalidTime (date));
606 public void InvalidDates ()
608 Assert.IsFalse (london.IsInvalidTime (new DateTime (2007, 03, 25, 0, 59, 59)));
609 Assert.IsTrue (london.IsInvalidTime (new DateTime (2007, 03, 25, 1, 0, 0)));
610 Assert.IsTrue (london.IsInvalidTime (new DateTime (2007, 03, 25, 1, 59, 59)));
611 Assert.IsFalse (london.IsInvalidTime (new DateTime (2007, 03, 25, 2, 0, 0)));
616 public class IsAmbiguousTimeTests
621 public void CreateTimeZones ()
623 TimeZoneInfo.TransitionTime start = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,1,0,0), 3, 5, DayOfWeek.Sunday);
624 TimeZoneInfo.TransitionTime end = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,2,0,0), 10, 5, DayOfWeek.Sunday);
625 TimeZoneInfo.AdjustmentRule rule = TimeZoneInfo.AdjustmentRule.CreateAdjustmentRule (DateTime.MinValue.Date, DateTime.MaxValue.Date, new TimeSpan (1,0,0), start, end);
626 london = TimeZoneInfo.CreateCustomTimeZone ("Europe/London", new TimeSpan (0), "Europe/London", "British Standard Time", "British Summer Time", new TimeZoneInfo.AdjustmentRule [] {rule});
630 public void AmbiguousDates ()
632 if (Environment.OSVersion.Platform != PlatformID.Unix)
633 Assert.Ignore ("Not running on Unix.");
634 Assert.IsFalse (london.IsAmbiguousTime (new DateTime (2007, 10, 28, 1, 0, 0)));
635 Assert.IsTrue (london.IsAmbiguousTime (new DateTime (2007, 10, 28, 1, 0, 1)));
636 Assert.IsTrue (london.IsAmbiguousTime (new DateTime (2007, 10, 28, 2, 0, 0)));
637 Assert.IsFalse (london.IsAmbiguousTime (new DateTime (2007, 10, 28, 2, 0, 1)));
641 public void AmbiguousUTCDates ()
643 if (Environment.OSVersion.Platform != PlatformID.Unix)
644 Assert.Ignore ("Not running on Unix.");
645 Assert.IsFalse (london.IsAmbiguousTime (new DateTime (2007, 10, 28, 0, 0, 0, DateTimeKind.Utc)));
646 Assert.IsTrue (london.IsAmbiguousTime (new DateTime (2007, 10, 28, 0, 0, 1, DateTimeKind.Utc)));
647 Assert.IsTrue (london.IsAmbiguousTime (new DateTime (2007, 10, 28, 0, 59, 59, DateTimeKind.Utc)));
648 Assert.IsFalse (london.IsAmbiguousTime (new DateTime (2007, 10, 28, 1, 0, 0, DateTimeKind.Utc)));
653 public void AmbiguousInUTC ()
655 for (DateTime date = new DateTime (2007, 01, 01, 0, 0, 0); date < new DateTime (2007, 12, 31, 23, 59, 59); date += new TimeSpan (0,1,0)) {
656 Assert.IsFalse (TimeZoneInfo.Utc.IsAmbiguousTime (date));
663 public class GetSystemTimeZonesTests
666 public void Identity ()
668 Assert.AreSame (TimeZoneInfo.GetSystemTimeZones (), TimeZoneInfo.GetSystemTimeZones ());
672 public void NotEmpty ()
674 if (Environment.OSVersion.Platform != PlatformID.Unix)
675 Assert.Ignore ("Not running on Unix.");
676 global::System.Collections.ObjectModel.ReadOnlyCollection<TimeZoneInfo> systemTZ = TimeZoneInfo.GetSystemTimeZones ();
677 Assert.IsNotNull(systemTZ, "SystemTZ is null");
678 Assert.IsFalse (systemTZ.Count == 0, "SystemTZ is empty");
682 public void ContainsBrussels ()
684 if (Environment.OSVersion.Platform != PlatformID.Unix)
685 Assert.Ignore ("Not running on Unix.");
686 global::System.Collections.ObjectModel.ReadOnlyCollection<TimeZoneInfo> systemTZ = TimeZoneInfo.GetSystemTimeZones ();
687 foreach (TimeZoneInfo tz in systemTZ) {
688 if (tz.Id == "Europe/Brussels")
691 Assert.Fail ("Europe/Brussels not found in SystemTZ");
696 public class FindSystemTimeZoneByIdTests
699 [ExpectedException (typeof (ArgumentNullException))]
700 public void NullId ()
702 TimeZoneInfo.FindSystemTimeZoneById (null);
706 [ExpectedException (typeof (TimeZoneNotFoundException))]
707 public void NonSystemTimezone ()
709 if (Environment.OSVersion.Platform != PlatformID.Unix)
710 throw new TimeZoneNotFoundException ();
711 TimeZoneInfo.FindSystemTimeZoneById ("Neverland/The_Lagoon");
715 public void FindBrusselsTZ ()
717 if (Environment.OSVersion.Platform != PlatformID.Unix)
718 Assert.Ignore ("Not running on Unix.");
719 TimeZoneInfo brussels = TimeZoneInfo.FindSystemTimeZoneById ("Europe/Brussels");
720 Assert.IsNotNull (brussels);
724 public void OffsetIsCorrectInKinshasa ()
726 if (Environment.OSVersion.Platform != PlatformID.Unix)
727 Assert.Ignore ("Not running on Unix.");
728 TimeZoneInfo kin = TimeZoneInfo.FindSystemTimeZoneById ("Africa/Kinshasa");
729 Assert.AreEqual (new TimeSpan (1,0,0), kin.BaseUtcOffset, "BaseUtcOffset in Kinshasa is not +1h");
733 public void OffsetIsCorrectInBrussels ()
735 if (Environment.OSVersion.Platform != PlatformID.Unix)
736 Assert.Ignore ("Not running on Unix.");
737 TimeZoneInfo brussels = TimeZoneInfo.FindSystemTimeZoneById ("Europe/Brussels");
738 Assert.AreEqual (new TimeSpan (1,0,0), brussels.BaseUtcOffset, "BaseUtcOffset for Brussels is not +1h");
742 public void NoDSTInKinshasa ()
744 if (Environment.OSVersion.Platform != PlatformID.Unix)
745 Assert.Ignore ("Not running on Unix.");
746 TimeZoneInfo kin = TimeZoneInfo.FindSystemTimeZoneById ("Africa/Kinshasa");
747 Assert.IsFalse (kin.SupportsDaylightSavingTime);
751 public void BrusselsSupportsDST ()
753 if (Environment.OSVersion.Platform != PlatformID.Unix)
754 Assert.Ignore ("Not running on Unix.");
755 TimeZoneInfo brussels = TimeZoneInfo.FindSystemTimeZoneById ("Europe/Brussels");
756 Assert.IsTrue (brussels.SupportsDaylightSavingTime);
760 public void MelbourneSupportsDST ()
762 if (Environment.OSVersion.Platform != PlatformID.Unix)
763 Assert.Ignore ("Not running on Unix.");
764 TimeZoneInfo melbourne = TimeZoneInfo.FindSystemTimeZoneById ("Australia/Melbourne");
765 Assert.IsTrue (melbourne.SupportsDaylightSavingTime);
769 public void RomeAndVaticanSharesTime ()
771 if (Environment.OSVersion.Platform != PlatformID.Unix)
772 Assert.Ignore ("Not running on Unix.");
773 TimeZoneInfo rome = TimeZoneInfo.FindSystemTimeZoneById ("Europe/Rome");
774 TimeZoneInfo vatican = TimeZoneInfo.FindSystemTimeZoneById ("Europe/Vatican");
775 Assert.IsTrue (rome.HasSameRules (vatican));
779 public void FindSystemTimeZoneById_Local_Roundtrip ()
781 Assert.AreEqual (TimeZoneInfo.Local.Id, TimeZoneInfo.FindSystemTimeZoneById (TimeZoneInfo.Local.Id).Id);
785 public void Test326 ()
787 DateTime utc = DateTime.UtcNow;
788 DateTime local = TimeZoneInfo.ConvertTime (utc, TimeZoneInfo.Utc, TimeZoneInfo.FindSystemTimeZoneById (TimeZoneInfo.Local.Id));
789 Assert.AreEqual (local, utc + TimeZoneInfo.Local.GetUtcOffset (utc), "ConvertTime/Local");
794 public void BrusselsAdjustments ()
796 TimeZoneInfo.TransitionTime start = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,2,0,0), 3, 5, DayOfWeek.Sunday);
797 TimeZoneInfo.TransitionTime end = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,3,0,0), 10, 5, DayOfWeek.Sunday);
798 TimeZoneInfo.AdjustmentRule rule = TimeZoneInfo.AdjustmentRule.CreateAdjustmentRule (DateTime.MinValue.Date, DateTime.MaxValue.Date, new TimeSpan (1,0,0), start, end);
799 TimeZoneInfo brussels = TimeZoneInfo.CreateCustomTimeZone ("Europe/Brussels", new TimeSpan (1, 0, 0), "Europe/Brussels", "", "", new TimeZoneInfo.AdjustmentRule [] {rule});
801 TimeZoneInfo brussels_sys = TimeZoneInfo.FindSystemTimeZoneById ("Europe/Brussels");
803 for (DateTime date = new DateTime (2006, 01, 01, 0, 0, 0, DateTimeKind.Local); date < new DateTime (2007, 12, 31, 23, 59, 59); date += new TimeSpan (0,30,0)) {
804 Assert.AreEqual (brussels.GetUtcOffset (date), brussels_sys.GetUtcOffset (date));
805 Assert.AreEqual (brussels.IsDaylightSavingTime (date), brussels_sys.IsDaylightSavingTime (date));
811 public void SubminuteDSTOffsets ()
813 if (Environment.OSVersion.Platform != PlatformID.Unix)
816 var subMinuteDSTs = new string [] {
817 "Europe/Dublin", // Europe/Dublin has a DST offset of 34 minutes and 39 seconds in 1916.
820 "Canada/Newfoundland",
823 "N/A", // testing that the test doesn't fail with inexistent TZs
825 foreach (var tz in subMinuteDSTs) {
827 TimeZoneInfo.FindSystemTimeZoneById (tz);
828 } catch (TimeZoneNotFoundException) {
830 } catch (Exception ex) {
831 Assert.Fail (string.Format ("Failed to load TZ {0}: {1}", tz, ex.ToString ()));
838 public class GetAmbiguousTimeOffsetsTests
841 [ExpectedException (typeof(ArgumentException))]
842 public void DateIsNotAmbiguous ()
844 if (Environment.OSVersion.Platform != PlatformID.Unix)
845 throw new ArgumentException ();
846 TimeZoneInfo brussels = TimeZoneInfo.FindSystemTimeZoneById ("Europe/Brussels");
847 DateTime date = new DateTime (2007, 05, 11, 11, 40, 00);
848 brussels.GetAmbiguousTimeOffsets (date);
852 public void AmbiguousOffsets ()
854 if (Environment.OSVersion.Platform != PlatformID.Unix)
855 Assert.Ignore ("Not running on Unix.");
856 TimeZoneInfo brussels = TimeZoneInfo.FindSystemTimeZoneById ("Europe/Brussels");
857 DateTime date = new DateTime (2007, 10, 28, 2, 30, 00);
858 Assert.IsTrue (brussels.IsAmbiguousTime (date));
859 Assert.AreEqual (2, brussels.GetAmbiguousTimeOffsets (date).Length);
860 Assert.AreEqual (new TimeSpan[] {new TimeSpan (1, 0, 0), new TimeSpan (2, 0, 0)}, brussels.GetAmbiguousTimeOffsets (date));
865 public class HasSameRulesTests
868 public void NullAdjustments () //bnc #391011
870 TimeZoneInfo utc = TimeZoneInfo.Utc;
871 TimeZoneInfo custom = TimeZoneInfo.CreateCustomTimeZone ("Custom", new TimeSpan (0), "Custom", "Custom");
872 Assert.IsTrue (utc.HasSameRules (custom));
877 public class SerializationTests
880 public void Serialization_Deserialization ()
882 TimeZoneInfo.TransitionTime start = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,1,0,0), 3, 5, DayOfWeek.Sunday);
883 TimeZoneInfo.TransitionTime end = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,2,0,0), 10, 5, DayOfWeek.Sunday);
884 TimeZoneInfo.AdjustmentRule rule = TimeZoneInfo.AdjustmentRule.CreateAdjustmentRule (DateTime.MinValue.Date, DateTime.MaxValue.Date, new TimeSpan (1,0,0), start, end);
885 TimeZoneInfo london = TimeZoneInfo.CreateCustomTimeZone ("Europe/London", new TimeSpan (0), "Europe/London", "British Standard Time", "British Summer Time", new TimeZoneInfo.AdjustmentRule [] {rule});
886 MemoryStream stream = new MemoryStream ();
887 BinaryFormatter formatter = new BinaryFormatter ();
888 formatter.Serialize (stream, london);
890 TimeZoneInfo deserialized = (TimeZoneInfo) formatter.Deserialize (stream);
893 Assert.IsTrue (london.Equals (deserialized));
898 public class MultipleDaylightSavingTimeTests {
899 private TimeZoneInfo cairo;
900 private DateTime dst1Start;
901 private DateTime dst1End;
902 private DateTime dst2Start;
903 private DateTime dst2End;
905 private TimeSpan baseUtcOffset;
906 private TimeSpan dstUtcOffset;
907 private TimeSpan dstOffset;
910 public void CreateTimeZones ()
913 From 1/1/2014 12:00:00 AM to 6/30/2014 12:00:00 AM
915 Begins at 12:00 AM on 16 May
916 Ends at 1:00 AM on 29 June
917 From 7/1/2014 12:00:00 AM to 12/31/2014 12:00:00 AM
919 Begins at 12:00 AM on 29 July
920 Ends at 12:00 AM on 26 September
922 dst1Start = new DateTime (2014, 5, 16);
923 dst1End = new DateTime (2014, 6, 29);
924 dst2Start = new DateTime (2014, 7, 29);
925 dst2End = new DateTime (2014, 9, 26);
927 baseUtcOffset = new TimeSpan (2, 0, 0);
928 dstUtcOffset = new TimeSpan (3, 0, 0);
929 dstOffset = dstUtcOffset - baseUtcOffset;
931 var rule1 = TimeZoneInfo.AdjustmentRule.CreateAdjustmentRule (
932 new DateTime (2014, 1, 1), new DateTime (2014, 6, 30), dstOffset,
933 CreateFixedDateRule (dst1Start), CreateFixedDateRule (dst1End));
935 var rule2 = TimeZoneInfo.AdjustmentRule.CreateAdjustmentRule (
936 new DateTime (2014, 7, 1), new DateTime (2014, 12, 31), dstOffset,
937 CreateFixedDateRule (dst2Start), CreateFixedDateRule (dst2End));
939 cairo = TimeZoneInfo.CreateCustomTimeZone ("Africa/Cairo", baseUtcOffset, "Africa/Cairo", "EET", "EEST",
940 new [] {rule1, rule2});
943 private static TimeZoneInfo.TransitionTime CreateFixedDateRule (DateTime dateTime)
945 var time = new DateTime (dateTime.Ticks - dateTime.Date.Ticks);
946 return TimeZoneInfo.TransitionTime.CreateFixedDateRule (time, dateTime.Month, dateTime.Day);
950 public void GetUtcOffset_FromUTC ()
952 var d = dst1Start.Add (-baseUtcOffset);
953 d = DateTime.SpecifyKind (d, DateTimeKind.Utc);
954 Assert.AreEqual(baseUtcOffset, cairo.GetUtcOffset (d.Add (new TimeSpan(0,0,0,-1))));
955 Assert.AreEqual(dstUtcOffset, cairo.GetUtcOffset (d));
956 Assert.AreEqual(dstUtcOffset, cairo.GetUtcOffset (d.Add (new TimeSpan(0,0,0, 1))));
958 d = dst1End.Add (-baseUtcOffset-dstOffset);
959 d = DateTime.SpecifyKind (d, DateTimeKind.Utc);
960 Assert.AreEqual(dstUtcOffset, cairo.GetUtcOffset (d.Add (new TimeSpan(0,0,0,-1))));
961 Assert.AreEqual(baseUtcOffset, cairo.GetUtcOffset (d));
962 Assert.AreEqual(baseUtcOffset, cairo.GetUtcOffset (d.Add (new TimeSpan(0,0,0, 1))));
964 d = dst2Start.Add (-baseUtcOffset);
965 d = DateTime.SpecifyKind (d, DateTimeKind.Utc);
966 Assert.AreEqual(baseUtcOffset, cairo.GetUtcOffset (d.Add (new TimeSpan(0,0,0,-1))));
967 Assert.AreEqual(dstUtcOffset, cairo.GetUtcOffset (d));
968 Assert.AreEqual(dstUtcOffset, cairo.GetUtcOffset (d.Add (new TimeSpan(0,0,0, 1))));
970 d = dst2End.Add (-baseUtcOffset-dstOffset);
971 d = DateTime.SpecifyKind (d, DateTimeKind.Utc);
972 Assert.AreEqual(dstUtcOffset, cairo.GetUtcOffset (d.Add (new TimeSpan(0,0,0,-1))));
973 Assert.AreEqual(baseUtcOffset, cairo.GetUtcOffset (d));
974 Assert.AreEqual(baseUtcOffset, cairo.GetUtcOffset (d.Add (new TimeSpan(0,0,0, 1))));
978 public void GetUtcOffset_FromLocal ()
980 var d = dst1Start.Add (-baseUtcOffset);
981 d = DateTime.SpecifyKind (d, DateTimeKind.Utc);
982 d = d.ToLocalTime ();
983 Assert.AreEqual(baseUtcOffset, cairo.GetUtcOffset (d.Add (new TimeSpan(0,0,0,-1))));
984 Assert.AreEqual(dstUtcOffset, cairo.GetUtcOffset (d));
985 Assert.AreEqual(dstUtcOffset, cairo.GetUtcOffset (d.Add (new TimeSpan(0,0,0, 1))));
987 d = dst1End.Add (-baseUtcOffset-dstOffset);
988 d = DateTime.SpecifyKind (d, DateTimeKind.Utc);
989 d = d.ToLocalTime ();
990 Assert.AreEqual(dstUtcOffset, cairo.GetUtcOffset (d.Add (new TimeSpan(0,0,0,-1))));
991 Assert.AreEqual(baseUtcOffset, cairo.GetUtcOffset (d));
992 Assert.AreEqual(baseUtcOffset, cairo.GetUtcOffset (d.Add (new TimeSpan(0,0,0, 1))));
994 d = dst2Start.Add (-baseUtcOffset);
995 d = DateTime.SpecifyKind (d, DateTimeKind.Utc);
996 d = d.ToLocalTime ();
997 Assert.AreEqual(baseUtcOffset, cairo.GetUtcOffset (d.Add (new TimeSpan(0,0,0,-1))));
998 Assert.AreEqual(dstUtcOffset, cairo.GetUtcOffset (d));
999 Assert.AreEqual(dstUtcOffset, cairo.GetUtcOffset (d.Add (new TimeSpan(0,0,0, 1))));
1001 d = dst2End.Add (-baseUtcOffset-dstOffset);
1002 d = DateTime.SpecifyKind (d, DateTimeKind.Utc);
1003 d = d.ToLocalTime ();
1004 Assert.AreEqual(dstUtcOffset, cairo.GetUtcOffset (d.Add (new TimeSpan(0,0,0,-1))));
1005 Assert.AreEqual(baseUtcOffset, cairo.GetUtcOffset (d));
1006 Assert.AreEqual(baseUtcOffset, cairo.GetUtcOffset (d.Add (new TimeSpan(0,0,0, 1))));
1010 public void GetUtcOffset_FromUnspecified ()
1012 var d = dst1Start.Add (dstOffset);
1013 Assert.AreEqual(baseUtcOffset, cairo.GetUtcOffset (d.Add (new TimeSpan(0,0,0,-1))));
1014 Assert.AreEqual(dstUtcOffset, cairo.GetUtcOffset (d));
1015 Assert.AreEqual(dstUtcOffset, cairo.GetUtcOffset (d.Add (new TimeSpan(0,0,0, 1))));
1017 d = dst1End.Add (-dstOffset);
1018 Assert.AreEqual(dstUtcOffset, cairo.GetUtcOffset (d.Add (new TimeSpan(0,0,0,-1))));
1019 Assert.AreEqual(baseUtcOffset, cairo.GetUtcOffset (d));
1020 Assert.AreEqual(baseUtcOffset, cairo.GetUtcOffset (d.Add (new TimeSpan(0,0,0, 1))));
1022 d = dst2Start.Add (dstOffset);
1023 Assert.AreEqual(baseUtcOffset, cairo.GetUtcOffset (d.Add (new TimeSpan(0,0,0,-1))));
1024 Assert.AreEqual(dstUtcOffset, cairo.GetUtcOffset (d));
1025 Assert.AreEqual(dstUtcOffset, cairo.GetUtcOffset (d.Add (new TimeSpan(0,0,0, 1))));
1027 d = dst2End.Add (-dstOffset);
1028 Assert.AreEqual(dstUtcOffset, cairo.GetUtcOffset (d.Add (new TimeSpan(0,0,0,-1))));
1029 Assert.AreEqual(baseUtcOffset, cairo.GetUtcOffset (d));
1030 Assert.AreEqual(baseUtcOffset, cairo.GetUtcOffset (d.Add (new TimeSpan(0,0,0, 1))));
1035 public class GetDaylightChanges
1037 MethodInfo getChanges;
1040 public void Setup ()
1042 var flags = BindingFlags.Instance | BindingFlags.NonPublic;
1043 getChanges = typeof (TimeZoneInfo).GetMethod ("GetDaylightChanges", flags);
1047 public void TestSydneyDaylightChanges ()
1050 if (Environment.OSVersion.Platform == PlatformID.Unix)
1051 tz = TimeZoneInfo.FindSystemTimeZoneById ("Australia/Sydney");
1053 tz = TimeZoneInfo.FindSystemTimeZoneById ("W. Australia Standard Time");
1055 var changes = (DaylightTime) getChanges.Invoke (tz, new object [] {2014});
1057 Assert.AreEqual (new TimeSpan (1, 0, 0), changes.Delta);
1058 Assert.AreEqual (new DateTime (2014, 10, 5, 2, 0, 0), changes.Start);
1059 Assert.AreEqual (new DateTime (2014, 4, 6, 3, 0, 0), changes.End);