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);
455 Assert.AreEqual(DateTimeKind.Local, converted.Kind);
457 Assert.AreEqual(DateTimeKind.Unspecified, converted.Kind);
459 DateTime back = TimeZoneInfo.ConvertTimeToUtc(converted, TimeZoneInfo.Local);
460 Assert.AreEqual(back.Kind, DateTimeKind.Utc);
461 Assert.AreEqual(utc, back);
465 public void ConvertToTimeZone ()
467 if (Environment.OSVersion.Platform != PlatformID.Unix)
468 Assert.Ignore ("Not running on Unix.");
470 TimeZoneInfo.ConvertTime (DateTime.Now, TimeZoneInfo.FindSystemTimeZoneById("Pacific/Auckland"));
474 [ExpectedException (typeof (ArgumentNullException))]
475 public void ConvertTime_DateTime_TimeZoneInfo_DestinationTimeZoneIsNull ()
477 TimeZoneInfo.ConvertTime (DateTime.Now, null);
481 public void ConvertTime_DateTime_TimeZoneInfo_DateTimeKindMatch ()
483 var sdt = new DateTime (2014, 1, 9, 23, 0, 0, DateTimeKind.Utc);
484 var ddt = TimeZoneInfo.ConvertTime (sdt, TimeZoneInfo.Utc);
485 Assert.AreEqual (ddt.Kind, sdt.Kind, "#1.1");
486 Assert.AreEqual (ddt.Kind, DateTimeKind.Utc, "#1.2");
488 sdt = new DateTime (2014, 1, 9, 23, 0, 0, DateTimeKind.Local);
489 ddt = TimeZoneInfo.ConvertTime (sdt, TimeZoneInfo.Local);
490 Assert.AreEqual (ddt.Kind, sdt.Kind, "#2.1");
491 Assert.AreEqual (ddt.Kind, DateTimeKind.Local, "#2.2");
493 sdt = new DateTime (2014, 1, 9, 23, 0, 0);
494 ddt = TimeZoneInfo.ConvertTime (sdt, TimeZoneInfo.Local);
495 Assert.AreEqual (ddt.Kind, sdt.Kind, "#3.1");
496 Assert.AreEqual (ddt.Kind, DateTimeKind.Unspecified, "#3.2");
500 [ExpectedException (typeof (ArgumentNullException))]
501 public void ConverTime_DateTime_TimeZoneInfo_TimeZoneInfo_SourceTimeZoneIsNull ()
503 TimeZoneInfo.ConvertTime (DateTime.Now, null, TimeZoneInfo.Local);
507 [ExpectedException (typeof (ArgumentNullException))]
508 public void ConverTime_DateTime_TimeZoneInfo_TimeZoneInfo_DestinationTimeZoneIsNull ()
510 TimeZoneInfo.ConvertTime (DateTime.Now, TimeZoneInfo.Utc, null);
513 [Test (Description="Fix for xambug https://bugzilla.xamarin.com/show_bug.cgi?id=17155")]
514 public void ConvertTime_AdjustmentRuleAfterNewYears ()
517 if (Environment.OSVersion.Platform == PlatformID.Unix)
518 tz = TimeZoneInfo.FindSystemTimeZoneById ("Pacific/Auckland"); // *nix
520 tz = TimeZoneInfo.FindSystemTimeZoneById ("New Zealand Standard Time"); // Windows
522 // DST start: 9/29/2013 2:00:00 AM
523 // DST end: 4/6/2014 3:00:00 AM
524 DateTime sdt = new DateTime (2014, 1, 9, 23, 0, 0, DateTimeKind.Utc);
525 DateTime ddt = TimeZoneInfo.ConvertTime (sdt, tz);
526 Assert.AreEqual (10, ddt.Day, "#1.1");
527 Assert.AreEqual (1, ddt.Month, "#1.2");
528 Assert.AreEqual (2014, ddt.Year, "#1.3");
529 Assert.AreEqual (12, ddt.Hour, "#1.4");
530 Assert.AreEqual (0, ddt.Minute, "#1.5");
531 Assert.AreEqual (0, ddt.Second, "#1.6");
533 // DST start: 9/29/2014 2:00:00 AM
534 // DST end: 4/6/2015 3:00:00 AM
535 sdt = new DateTime (2014, 6, 9, 23, 0, 0, DateTimeKind.Utc);
536 ddt = TimeZoneInfo.ConvertTime (sdt, tz);
537 Assert.AreEqual (10, ddt.Day, "#2.1");
538 Assert.AreEqual (6, ddt.Month, "#2.2");
539 Assert.AreEqual (2014, ddt.Year, "#2.3");
540 Assert.AreEqual (11, ddt.Hour, "#2.4");
541 Assert.AreEqual (0, ddt.Minute, "#2.5");
542 Assert.AreEqual (0, ddt.Second, "#2.6");
544 // DST start: 9/29/2014 2:00:00 AM
545 // DST end: 4/6/2015 3:00:00 AM
546 sdt = new DateTime (2014, 10, 9, 23, 0, 0, DateTimeKind.Utc);
547 ddt = TimeZoneInfo.ConvertTime (sdt, tz);
548 Assert.AreEqual (10, ddt.Day, "#3.1");
549 Assert.AreEqual (10, ddt.Month, "#3.2");
550 Assert.AreEqual (2014, ddt.Year, "#3.3");
551 Assert.AreEqual (12, ddt.Hour, "#3.4");
552 Assert.AreEqual (0, ddt.Minute, "#3.5");
553 Assert.AreEqual (0, ddt.Second, "#3.6");
556 [Test (Description="Fix the bug https://bugzilla.xamarin.com/show_bug.cgi?id=1849")]
557 public void ConvertTime_AjustmentConvertTimeWithSourceTimeZone () {
559 TimeZoneInfo easternTimeZone;
560 TimeZoneInfo pacificTimeZone;
562 if (Environment.OSVersion.Platform == PlatformID.Unix) {
564 easternTimeZone = TimeZoneInfo.FindSystemTimeZoneById ("US/Eastern");
565 pacificTimeZone = TimeZoneInfo.FindSystemTimeZoneById ("US/Pacific");
569 easternTimeZone = TimeZoneInfo.FindSystemTimeZoneById ("Eastern Standard Time");
570 pacificTimeZone = TimeZoneInfo.FindSystemTimeZoneById ("Pacific Standard Time");
573 DateTime lastMidnight = new DateTime (new DateTime (2012, 06, 13).Ticks, DateTimeKind.Unspecified);
574 DateTime lastMidnightAsEST = TimeZoneInfo.ConvertTime (lastMidnight, pacificTimeZone, easternTimeZone);
575 DateTime lastMidnightAsPST = TimeZoneInfo.ConvertTime (lastMidnightAsEST, easternTimeZone, pacificTimeZone);
577 // Last midnight in PST as EST should be 3AM
578 DateTime expectedDate = new DateTime (2012, 06, 13, 3, 0, 0);
580 Assert.AreEqual (expectedDate, lastMidnightAsEST);
581 Assert.AreEqual (lastMidnight, lastMidnightAsPST);
586 public class IsInvalidTimeTests
591 public void CreateTimeZones ()
593 TimeZoneInfo.TransitionTime start = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,1,0,0), 3, 5, DayOfWeek.Sunday);
594 TimeZoneInfo.TransitionTime end = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,2,0,0), 10, 5, DayOfWeek.Sunday);
595 TimeZoneInfo.AdjustmentRule rule = TimeZoneInfo.AdjustmentRule.CreateAdjustmentRule (DateTime.MinValue.Date, DateTime.MaxValue.Date, new TimeSpan (1,0,0), start, end);
596 london = TimeZoneInfo.CreateCustomTimeZone ("Europe/London", new TimeSpan (0), "Europe/London", "British Standard Time", "British Summer Time", new TimeZoneInfo.AdjustmentRule [] {rule});
601 public void UTCDate ()
603 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)) {
604 date = DateTime.SpecifyKind (date, DateTimeKind.Utc);
605 Assert.IsFalse (london.IsInvalidTime (date));
610 public void InvalidDates ()
612 Assert.IsFalse (london.IsInvalidTime (new DateTime (2007, 03, 25, 0, 59, 59)));
613 Assert.IsTrue (london.IsInvalidTime (new DateTime (2007, 03, 25, 1, 0, 0)));
614 Assert.IsTrue (london.IsInvalidTime (new DateTime (2007, 03, 25, 1, 59, 59)));
615 Assert.IsFalse (london.IsInvalidTime (new DateTime (2007, 03, 25, 2, 0, 0)));
620 public class IsAmbiguousTimeTests
625 public void CreateTimeZones ()
627 TimeZoneInfo.TransitionTime start = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,1,0,0), 3, 5, DayOfWeek.Sunday);
628 TimeZoneInfo.TransitionTime end = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,2,0,0), 10, 5, DayOfWeek.Sunday);
629 TimeZoneInfo.AdjustmentRule rule = TimeZoneInfo.AdjustmentRule.CreateAdjustmentRule (DateTime.MinValue.Date, DateTime.MaxValue.Date, new TimeSpan (1,0,0), start, end);
630 london = TimeZoneInfo.CreateCustomTimeZone ("Europe/London", new TimeSpan (0), "Europe/London", "British Standard Time", "British Summer Time", new TimeZoneInfo.AdjustmentRule [] {rule});
634 public void AmbiguousDates ()
636 if (Environment.OSVersion.Platform != PlatformID.Unix)
637 Assert.Ignore ("Not running on Unix.");
638 Assert.IsFalse (london.IsAmbiguousTime (new DateTime (2007, 10, 28, 1, 0, 0)));
639 Assert.IsTrue (london.IsAmbiguousTime (new DateTime (2007, 10, 28, 1, 0, 1)));
640 Assert.IsTrue (london.IsAmbiguousTime (new DateTime (2007, 10, 28, 2, 0, 0)));
641 Assert.IsFalse (london.IsAmbiguousTime (new DateTime (2007, 10, 28, 2, 0, 1)));
645 public void AmbiguousUTCDates ()
647 if (Environment.OSVersion.Platform != PlatformID.Unix)
648 Assert.Ignore ("Not running on Unix.");
649 Assert.IsFalse (london.IsAmbiguousTime (new DateTime (2007, 10, 28, 0, 0, 0, DateTimeKind.Utc)));
650 Assert.IsTrue (london.IsAmbiguousTime (new DateTime (2007, 10, 28, 0, 0, 1, DateTimeKind.Utc)));
651 Assert.IsTrue (london.IsAmbiguousTime (new DateTime (2007, 10, 28, 0, 59, 59, DateTimeKind.Utc)));
652 Assert.IsFalse (london.IsAmbiguousTime (new DateTime (2007, 10, 28, 1, 0, 0, DateTimeKind.Utc)));
657 public void AmbiguousInUTC ()
659 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)) {
660 Assert.IsFalse (TimeZoneInfo.Utc.IsAmbiguousTime (date));
667 public class GetSystemTimeZonesTests
670 public void Identity ()
672 Assert.AreSame (TimeZoneInfo.GetSystemTimeZones (), TimeZoneInfo.GetSystemTimeZones ());
676 public void NotEmpty ()
678 if (Environment.OSVersion.Platform != PlatformID.Unix)
679 Assert.Ignore ("Not running on Unix.");
680 global::System.Collections.ObjectModel.ReadOnlyCollection<TimeZoneInfo> systemTZ = TimeZoneInfo.GetSystemTimeZones ();
681 Assert.IsNotNull(systemTZ, "SystemTZ is null");
682 Assert.IsFalse (systemTZ.Count == 0, "SystemTZ is empty");
686 public void ContainsBrussels ()
688 if (Environment.OSVersion.Platform != PlatformID.Unix)
689 Assert.Ignore ("Not running on Unix.");
690 global::System.Collections.ObjectModel.ReadOnlyCollection<TimeZoneInfo> systemTZ = TimeZoneInfo.GetSystemTimeZones ();
691 foreach (TimeZoneInfo tz in systemTZ) {
692 if (tz.Id == "Europe/Brussels")
695 Assert.Fail ("Europe/Brussels not found in SystemTZ");
700 public class FindSystemTimeZoneByIdTests
703 [ExpectedException (typeof (ArgumentNullException))]
704 public void NullId ()
706 TimeZoneInfo.FindSystemTimeZoneById (null);
710 [ExpectedException (typeof (TimeZoneNotFoundException))]
711 public void NonSystemTimezone ()
713 if (Environment.OSVersion.Platform != PlatformID.Unix)
714 throw new TimeZoneNotFoundException ();
715 TimeZoneInfo.FindSystemTimeZoneById ("Neverland/The_Lagoon");
719 public void FindBrusselsTZ ()
721 if (Environment.OSVersion.Platform != PlatformID.Unix)
722 Assert.Ignore ("Not running on Unix.");
723 TimeZoneInfo brussels = TimeZoneInfo.FindSystemTimeZoneById ("Europe/Brussels");
724 Assert.IsNotNull (brussels);
728 public void OffsetIsCorrectInKinshasa ()
730 if (Environment.OSVersion.Platform != PlatformID.Unix)
731 Assert.Ignore ("Not running on Unix.");
732 TimeZoneInfo kin = TimeZoneInfo.FindSystemTimeZoneById ("Africa/Kinshasa");
733 Assert.AreEqual (new TimeSpan (1,0,0), kin.BaseUtcOffset, "BaseUtcOffset in Kinshasa is not +1h");
737 public void OffsetIsCorrectInBrussels ()
739 if (Environment.OSVersion.Platform != PlatformID.Unix)
740 Assert.Ignore ("Not running on Unix.");
741 TimeZoneInfo brussels = TimeZoneInfo.FindSystemTimeZoneById ("Europe/Brussels");
742 Assert.AreEqual (new TimeSpan (1,0,0), brussels.BaseUtcOffset, "BaseUtcOffset for Brussels is not +1h");
746 public void NoDSTInKinshasa ()
748 if (Environment.OSVersion.Platform != PlatformID.Unix)
749 Assert.Ignore ("Not running on Unix.");
750 TimeZoneInfo kin = TimeZoneInfo.FindSystemTimeZoneById ("Africa/Kinshasa");
751 Assert.IsFalse (kin.SupportsDaylightSavingTime);
755 public void BrusselsSupportsDST ()
757 if (Environment.OSVersion.Platform != PlatformID.Unix)
758 Assert.Ignore ("Not running on Unix.");
759 TimeZoneInfo brussels = TimeZoneInfo.FindSystemTimeZoneById ("Europe/Brussels");
760 Assert.IsTrue (brussels.SupportsDaylightSavingTime);
764 public void MelbourneSupportsDST ()
766 if (Environment.OSVersion.Platform != PlatformID.Unix)
767 Assert.Ignore ("Not running on Unix.");
768 TimeZoneInfo melbourne = TimeZoneInfo.FindSystemTimeZoneById ("Australia/Melbourne");
769 Assert.IsTrue (melbourne.SupportsDaylightSavingTime);
773 public void RomeAndVaticanSharesTime ()
775 if (Environment.OSVersion.Platform != PlatformID.Unix)
776 Assert.Ignore ("Not running on Unix.");
777 TimeZoneInfo rome = TimeZoneInfo.FindSystemTimeZoneById ("Europe/Rome");
778 TimeZoneInfo vatican = TimeZoneInfo.FindSystemTimeZoneById ("Europe/Vatican");
779 Assert.IsTrue (rome.HasSameRules (vatican));
783 public void FindSystemTimeZoneById_Local_Roundtrip ()
785 Assert.AreEqual (TimeZoneInfo.Local.Id, TimeZoneInfo.FindSystemTimeZoneById (TimeZoneInfo.Local.Id).Id);
789 public void Test326 ()
791 DateTime utc = DateTime.UtcNow;
792 DateTime local = TimeZoneInfo.ConvertTime (utc, TimeZoneInfo.Utc, TimeZoneInfo.FindSystemTimeZoneById (TimeZoneInfo.Local.Id));
793 Assert.AreEqual (local, utc + TimeZoneInfo.Local.GetUtcOffset (utc), "ConvertTime/Local");
798 public void BrusselsAdjustments ()
800 TimeZoneInfo.TransitionTime start = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,2,0,0), 3, 5, DayOfWeek.Sunday);
801 TimeZoneInfo.TransitionTime end = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,3,0,0), 10, 5, DayOfWeek.Sunday);
802 TimeZoneInfo.AdjustmentRule rule = TimeZoneInfo.AdjustmentRule.CreateAdjustmentRule (DateTime.MinValue.Date, DateTime.MaxValue.Date, new TimeSpan (1,0,0), start, end);
803 TimeZoneInfo brussels = TimeZoneInfo.CreateCustomTimeZone ("Europe/Brussels", new TimeSpan (1, 0, 0), "Europe/Brussels", "", "", new TimeZoneInfo.AdjustmentRule [] {rule});
805 TimeZoneInfo brussels_sys = TimeZoneInfo.FindSystemTimeZoneById ("Europe/Brussels");
807 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)) {
808 Assert.AreEqual (brussels.GetUtcOffset (date), brussels_sys.GetUtcOffset (date));
809 Assert.AreEqual (brussels.IsDaylightSavingTime (date), brussels_sys.IsDaylightSavingTime (date));
815 public void SubminuteDSTOffsets ()
817 if (Environment.OSVersion.Platform != PlatformID.Unix)
820 var subMinuteDSTs = new string [] {
821 "Europe/Dublin", // Europe/Dublin has a DST offset of 34 minutes and 39 seconds in 1916.
824 "Canada/Newfoundland",
827 "N/A", // testing that the test doesn't fail with inexistent TZs
829 foreach (var tz in subMinuteDSTs) {
831 TimeZoneInfo.FindSystemTimeZoneById (tz);
832 } catch (TimeZoneNotFoundException) {
834 } catch (Exception ex) {
835 Assert.Fail (string.Format ("Failed to load TZ {0}: {1}", tz, ex.ToString ()));
842 public class GetAmbiguousTimeOffsetsTests
845 [ExpectedException (typeof(ArgumentException))]
846 public void DateIsNotAmbiguous ()
848 if (Environment.OSVersion.Platform != PlatformID.Unix)
849 throw new ArgumentException ();
850 TimeZoneInfo brussels = TimeZoneInfo.FindSystemTimeZoneById ("Europe/Brussels");
851 DateTime date = new DateTime (2007, 05, 11, 11, 40, 00);
852 brussels.GetAmbiguousTimeOffsets (date);
856 public void AmbiguousOffsets ()
858 if (Environment.OSVersion.Platform != PlatformID.Unix)
859 Assert.Ignore ("Not running on Unix.");
860 TimeZoneInfo brussels = TimeZoneInfo.FindSystemTimeZoneById ("Europe/Brussels");
861 DateTime date = new DateTime (2007, 10, 28, 2, 30, 00);
862 Assert.IsTrue (brussels.IsAmbiguousTime (date));
863 Assert.AreEqual (2, brussels.GetAmbiguousTimeOffsets (date).Length);
864 Assert.AreEqual (new TimeSpan[] {new TimeSpan (1, 0, 0), new TimeSpan (2, 0, 0)}, brussels.GetAmbiguousTimeOffsets (date));
869 public class HasSameRulesTests
872 public void NullAdjustments () //bnc #391011
874 TimeZoneInfo utc = TimeZoneInfo.Utc;
875 TimeZoneInfo custom = TimeZoneInfo.CreateCustomTimeZone ("Custom", new TimeSpan (0), "Custom", "Custom");
876 Assert.IsTrue (utc.HasSameRules (custom));
881 public class SerializationTests
884 public void Serialization_Deserialization ()
886 TimeZoneInfo.TransitionTime start = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,1,0,0), 3, 5, DayOfWeek.Sunday);
887 TimeZoneInfo.TransitionTime end = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,2,0,0), 10, 5, DayOfWeek.Sunday);
888 TimeZoneInfo.AdjustmentRule rule = TimeZoneInfo.AdjustmentRule.CreateAdjustmentRule (DateTime.MinValue.Date, DateTime.MaxValue.Date, new TimeSpan (1,0,0), start, end);
889 TimeZoneInfo london = TimeZoneInfo.CreateCustomTimeZone ("Europe/London", new TimeSpan (0), "Europe/London", "British Standard Time", "British Summer Time", new TimeZoneInfo.AdjustmentRule [] {rule});
890 MemoryStream stream = new MemoryStream ();
891 BinaryFormatter formatter = new BinaryFormatter ();
892 formatter.Serialize (stream, london);
894 TimeZoneInfo deserialized = (TimeZoneInfo) formatter.Deserialize (stream);
897 Assert.IsTrue (london.Equals (deserialized));
902 public class MultipleDaylightSavingTimeTests {
903 private TimeZoneInfo cairo;
904 private DateTime dst1Start;
905 private DateTime dst1End;
906 private DateTime dst2Start;
907 private DateTime dst2End;
909 private TimeSpan baseUtcOffset;
910 private TimeSpan dstUtcOffset;
911 private TimeSpan dstOffset;
914 public void CreateTimeZones ()
917 From 1/1/2014 12:00:00 AM to 6/30/2014 12:00:00 AM
919 Begins at 12:00 AM on 16 May
920 Ends at 1:00 AM on 29 June
921 From 7/1/2014 12:00:00 AM to 12/31/2014 12:00:00 AM
923 Begins at 12:00 AM on 29 July
924 Ends at 12:00 AM on 26 September
926 dst1Start = new DateTime (2014, 5, 16);
927 dst1End = new DateTime (2014, 6, 29);
928 dst2Start = new DateTime (2014, 7, 29);
929 dst2End = new DateTime (2014, 9, 26);
931 baseUtcOffset = new TimeSpan (2, 0, 0);
932 dstUtcOffset = new TimeSpan (3, 0, 0);
933 dstOffset = dstUtcOffset - baseUtcOffset;
935 var rule1 = TimeZoneInfo.AdjustmentRule.CreateAdjustmentRule (
936 new DateTime (2014, 1, 1), new DateTime (2014, 6, 30), dstOffset,
937 CreateFixedDateRule (dst1Start), CreateFixedDateRule (dst1End));
939 var rule2 = TimeZoneInfo.AdjustmentRule.CreateAdjustmentRule (
940 new DateTime (2014, 7, 1), new DateTime (2014, 12, 31), dstOffset,
941 CreateFixedDateRule (dst2Start), CreateFixedDateRule (dst2End));
943 cairo = TimeZoneInfo.CreateCustomTimeZone ("Africa/Cairo", baseUtcOffset, "Africa/Cairo", "EET", "EEST",
944 new [] {rule1, rule2});
947 private static TimeZoneInfo.TransitionTime CreateFixedDateRule (DateTime dateTime)
949 var time = new DateTime (dateTime.Ticks - dateTime.Date.Ticks);
950 return TimeZoneInfo.TransitionTime.CreateFixedDateRule (time, dateTime.Month, dateTime.Day);
954 public void GetUtcOffset_FromUTC ()
956 var d = dst1Start.Add (-baseUtcOffset);
957 d = DateTime.SpecifyKind (d, DateTimeKind.Utc);
958 Assert.AreEqual(baseUtcOffset, cairo.GetUtcOffset (d.Add (new TimeSpan(0,0,0,-1))));
959 Assert.AreEqual(dstUtcOffset, cairo.GetUtcOffset (d));
960 Assert.AreEqual(dstUtcOffset, cairo.GetUtcOffset (d.Add (new TimeSpan(0,0,0, 1))));
962 d = dst1End.Add (-baseUtcOffset-dstOffset);
963 d = DateTime.SpecifyKind (d, DateTimeKind.Utc);
964 Assert.AreEqual(dstUtcOffset, cairo.GetUtcOffset (d.Add (new TimeSpan(0,0,0,-1))));
965 Assert.AreEqual(baseUtcOffset, cairo.GetUtcOffset (d));
966 Assert.AreEqual(baseUtcOffset, cairo.GetUtcOffset (d.Add (new TimeSpan(0,0,0, 1))));
968 d = dst2Start.Add (-baseUtcOffset);
969 d = DateTime.SpecifyKind (d, DateTimeKind.Utc);
970 Assert.AreEqual(baseUtcOffset, cairo.GetUtcOffset (d.Add (new TimeSpan(0,0,0,-1))));
971 Assert.AreEqual(dstUtcOffset, cairo.GetUtcOffset (d));
972 Assert.AreEqual(dstUtcOffset, cairo.GetUtcOffset (d.Add (new TimeSpan(0,0,0, 1))));
974 d = dst2End.Add (-baseUtcOffset-dstOffset);
975 d = DateTime.SpecifyKind (d, DateTimeKind.Utc);
976 Assert.AreEqual(dstUtcOffset, cairo.GetUtcOffset (d.Add (new TimeSpan(0,0,0,-1))));
977 Assert.AreEqual(baseUtcOffset, cairo.GetUtcOffset (d));
978 Assert.AreEqual(baseUtcOffset, cairo.GetUtcOffset (d.Add (new TimeSpan(0,0,0, 1))));
982 public void GetUtcOffset_FromLocal ()
984 var d = dst1Start.Add (-baseUtcOffset);
985 d = DateTime.SpecifyKind (d, DateTimeKind.Utc);
986 d = d.ToLocalTime ();
987 Assert.AreEqual(baseUtcOffset, cairo.GetUtcOffset (d.Add (new TimeSpan(0,0,0,-1))));
988 Assert.AreEqual(dstUtcOffset, cairo.GetUtcOffset (d));
989 Assert.AreEqual(dstUtcOffset, cairo.GetUtcOffset (d.Add (new TimeSpan(0,0,0, 1))));
991 d = dst1End.Add (-baseUtcOffset-dstOffset);
992 d = DateTime.SpecifyKind (d, DateTimeKind.Utc);
993 d = d.ToLocalTime ();
994 Assert.AreEqual(dstUtcOffset, cairo.GetUtcOffset (d.Add (new TimeSpan(0,0,0,-1))));
995 Assert.AreEqual(baseUtcOffset, cairo.GetUtcOffset (d));
996 Assert.AreEqual(baseUtcOffset, cairo.GetUtcOffset (d.Add (new TimeSpan(0,0,0, 1))));
998 d = dst2Start.Add (-baseUtcOffset);
999 d = DateTime.SpecifyKind (d, DateTimeKind.Utc);
1000 d = d.ToLocalTime ();
1001 Assert.AreEqual(baseUtcOffset, cairo.GetUtcOffset (d.Add (new TimeSpan(0,0,0,-1))));
1002 Assert.AreEqual(dstUtcOffset, cairo.GetUtcOffset (d));
1003 Assert.AreEqual(dstUtcOffset, cairo.GetUtcOffset (d.Add (new TimeSpan(0,0,0, 1))));
1005 d = dst2End.Add (-baseUtcOffset-dstOffset);
1006 d = DateTime.SpecifyKind (d, DateTimeKind.Utc);
1007 d = d.ToLocalTime ();
1008 Assert.AreEqual(dstUtcOffset, cairo.GetUtcOffset (d.Add (new TimeSpan(0,0,0,-1))));
1009 Assert.AreEqual(baseUtcOffset, cairo.GetUtcOffset (d));
1010 Assert.AreEqual(baseUtcOffset, cairo.GetUtcOffset (d.Add (new TimeSpan(0,0,0, 1))));
1014 public void GetUtcOffset_FromUnspecified ()
1016 var d = dst1Start.Add (dstOffset);
1017 Assert.AreEqual(baseUtcOffset, cairo.GetUtcOffset (d.Add (new TimeSpan(0,0,0,-1))));
1018 Assert.AreEqual(dstUtcOffset, cairo.GetUtcOffset (d));
1019 Assert.AreEqual(dstUtcOffset, cairo.GetUtcOffset (d.Add (new TimeSpan(0,0,0, 1))));
1021 d = dst1End.Add (-dstOffset);
1022 Assert.AreEqual(dstUtcOffset, cairo.GetUtcOffset (d.Add (new TimeSpan(0,0,0,-1))));
1023 Assert.AreEqual(baseUtcOffset, cairo.GetUtcOffset (d));
1024 Assert.AreEqual(baseUtcOffset, cairo.GetUtcOffset (d.Add (new TimeSpan(0,0,0, 1))));
1026 d = dst2Start.Add (dstOffset);
1027 Assert.AreEqual(baseUtcOffset, cairo.GetUtcOffset (d.Add (new TimeSpan(0,0,0,-1))));
1028 Assert.AreEqual(dstUtcOffset, cairo.GetUtcOffset (d));
1029 Assert.AreEqual(dstUtcOffset, cairo.GetUtcOffset (d.Add (new TimeSpan(0,0,0, 1))));
1031 d = dst2End.Add (-dstOffset);
1032 Assert.AreEqual(dstUtcOffset, cairo.GetUtcOffset (d.Add (new TimeSpan(0,0,0,-1))));
1033 Assert.AreEqual(baseUtcOffset, cairo.GetUtcOffset (d));
1034 Assert.AreEqual(baseUtcOffset, cairo.GetUtcOffset (d.Add (new TimeSpan(0,0,0, 1))));
1039 public class GetDaylightChanges
1041 MethodInfo getChanges;
1044 public void Setup ()
1046 var flags = BindingFlags.Instance | BindingFlags.NonPublic;
1047 getChanges = typeof (TimeZoneInfo).GetMethod ("GetDaylightChanges", flags);
1051 public void TestSydneyDaylightChanges ()
1054 if (Environment.OSVersion.Platform == PlatformID.Unix)
1055 tz = TimeZoneInfo.FindSystemTimeZoneById ("Australia/Sydney");
1057 tz = TimeZoneInfo.FindSystemTimeZoneById ("W. Australia Standard Time");
1059 var changes = (DaylightTime) getChanges.Invoke (tz, new object [] {2014});
1061 Assert.AreEqual (new TimeSpan (1, 0, 0), changes.Delta);
1062 Assert.AreEqual (new DateTime (2014, 10, 5, 2, 0, 0), changes.Start);
1063 Assert.AreEqual (new DateTime (2014, 4, 6, 3, 0, 0), changes.End);