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;
34 using NUnit.Framework;
35 namespace MonoTests.System
37 public class TimeZoneInfoTest
40 public class PropertiesTests
43 public void GetLocal ()
45 if (Environment.OSVersion.Platform != PlatformID.Unix)
46 Assert.Ignore ("Not running on Unix.");
47 TimeZoneInfo local = TimeZoneInfo.Local;
48 Assert.IsNotNull (local);
54 public class CreateCustomTimezoneTests
57 [ExpectedException (typeof (ArgumentNullException))]
58 public void IdIsNullException ()
60 TimeZoneInfo.CreateCustomTimeZone (null, new TimeSpan (0), null, null);
64 [ExpectedException (typeof (ArgumentException))]
65 public void IdIsEmptyString ()
67 TimeZoneInfo.CreateCustomTimeZone ("", new TimeSpan (0), null, null);
71 [ExpectedException (typeof (ArgumentException))]
72 public void OffsetIsNotMinutes ()
74 TimeZoneInfo.CreateCustomTimeZone ("mytimezone", new TimeSpan (0, 0, 55), null, null);
78 [ExpectedException (typeof (ArgumentOutOfRangeException))]
79 public void OffsetTooBig ()
81 TimeZoneInfo.CreateCustomTimeZone ("mytimezone", new TimeSpan (14, 1, 0), null, null);
85 [ExpectedException (typeof (ArgumentOutOfRangeException))]
86 public void OffsetTooSmall ()
88 TimeZoneInfo.CreateCustomTimeZone ("mytimezone", - new TimeSpan (14, 1, 0), null, null);
93 [ExpectedException (typeof (ArgumentException))]
94 public void IdLongerThan32 ()
96 TimeZoneInfo.CreateCustomTimeZone ("12345678901234567890123456789012345", new TimeSpan (0), null, null);
101 [ExpectedException (typeof (InvalidTimeZoneException))]
102 public void AdjustmentRulesOverlap ()
104 TimeZoneInfo.TransitionTime s1 = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,4,0,0), 3, 2, DayOfWeek.Sunday);
105 TimeZoneInfo.TransitionTime e1 = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,4,0,0), 10, 2, DayOfWeek.Sunday);
106 TimeZoneInfo.AdjustmentRule r1 = TimeZoneInfo.AdjustmentRule.CreateAdjustmentRule (new DateTime (2000,1,1), new DateTime (2005,1,1), new TimeSpan (1,0,0), s1, e1);
107 TimeZoneInfo.TransitionTime s2 = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,4,0,0), 2, 2, DayOfWeek.Sunday);
108 TimeZoneInfo.TransitionTime e2 = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,4,0,0), 11, 2, DayOfWeek.Sunday);
109 TimeZoneInfo.AdjustmentRule r2 = TimeZoneInfo.AdjustmentRule.CreateAdjustmentRule (new DateTime (2004,1,1), new DateTime (2007,1,1), new TimeSpan (1,0,0), s2, e2);
110 TimeZoneInfo.CreateCustomTimeZone ("mytimezone", new TimeSpan (6,0,0),null,null,null,new TimeZoneInfo.AdjustmentRule[] {r1, r2});
114 [ExpectedException (typeof (InvalidTimeZoneException))]
115 public void RulesNotOrdered ()
117 TimeZoneInfo.TransitionTime s1 = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,4,0,0), 3, 2, DayOfWeek.Sunday);
118 TimeZoneInfo.TransitionTime e1 = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,4,0,0), 10, 2, DayOfWeek.Sunday);
119 TimeZoneInfo.AdjustmentRule r1 = TimeZoneInfo.AdjustmentRule.CreateAdjustmentRule (new DateTime (2000,1,1), new DateTime (2005,1,1), new TimeSpan (1,0,0), s1, e1);
120 TimeZoneInfo.TransitionTime s2 = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,4,0,0), 2, 2, DayOfWeek.Sunday);
121 TimeZoneInfo.TransitionTime e2 = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,4,0,0), 11, 2, DayOfWeek.Sunday);
122 TimeZoneInfo.AdjustmentRule r2 = TimeZoneInfo.AdjustmentRule.CreateAdjustmentRule (new DateTime (2006,1,1), new DateTime (2007,1,1), new TimeSpan (1,0,0), s2, e2);
123 TimeZoneInfo.CreateCustomTimeZone ("mytimezone", new TimeSpan (6,0,0),null,null,null,new TimeZoneInfo.AdjustmentRule[] {r2, r1});
127 [ExpectedException (typeof (InvalidTimeZoneException))]
128 public void OffsetOutOfRange ()
130 TimeZoneInfo.TransitionTime startTransition = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,4,0,0), 3, 2, DayOfWeek.Sunday);
131 TimeZoneInfo.TransitionTime endTransition = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,4,0,0), 10, 2, DayOfWeek.Sunday);
132 TimeZoneInfo.AdjustmentRule rule = TimeZoneInfo.AdjustmentRule.CreateAdjustmentRule (new DateTime (2000,1,1), new DateTime (2005,1,1), new TimeSpan (3,0,0), startTransition, endTransition);
133 TimeZoneInfo.CreateCustomTimeZone ("mytimezone", new TimeSpan (12,0,0),null,null,null,new TimeZoneInfo.AdjustmentRule[] {rule});
137 [ExpectedException (typeof (InvalidTimeZoneException))]
138 public void NullRule ()
140 TimeZoneInfo.CreateCustomTimeZone ("mytimezone", new TimeSpan (12,0,0),null,null,null,new TimeZoneInfo.AdjustmentRule[] {null});
144 [ExpectedException (typeof (InvalidTimeZoneException))]
145 public void MultiplesRulesForDate ()
147 TimeZoneInfo.TransitionTime s1 = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,4,0,0), 3, 2, DayOfWeek.Sunday);
148 TimeZoneInfo.TransitionTime e1 = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,4,0,0), 10, 2, DayOfWeek.Sunday);
149 TimeZoneInfo.AdjustmentRule r1 = TimeZoneInfo.AdjustmentRule.CreateAdjustmentRule (new DateTime (2000,1,1), new DateTime (2005,1,1), new TimeSpan (1,0,0), s1, e1);
150 TimeZoneInfo.TransitionTime s2 = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,4,0,0), 2, 2, DayOfWeek.Sunday);
151 TimeZoneInfo.TransitionTime e2 = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,4,0,0), 11, 2, DayOfWeek.Sunday);
152 TimeZoneInfo.AdjustmentRule r2 = TimeZoneInfo.AdjustmentRule.CreateAdjustmentRule (new DateTime (2005,1,1), new DateTime (2007,1,1), new TimeSpan (1,0,0), s2, e2);
153 TimeZoneInfo.CreateCustomTimeZone ("mytimezone", new TimeSpan (6,0,0),null,null,null,new TimeZoneInfo.AdjustmentRule[] {r1, r2});
157 public void SupportsDaylightSavingTime_NonEmptyAdjustmentRule ()
159 TimeZoneInfo.TransitionTime s1 = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,4,0,0), 3, 2, DayOfWeek.Sunday);
160 TimeZoneInfo.TransitionTime e1 = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,4,0,0), 10, 2, DayOfWeek.Sunday);
161 TimeZoneInfo.AdjustmentRule r1 = TimeZoneInfo.AdjustmentRule.CreateAdjustmentRule (new DateTime (2000,1,1), new DateTime (2005,1,1), new TimeSpan (1,0,0), s1, e1);
162 TimeZoneInfo tz = TimeZoneInfo.CreateCustomTimeZone ("mytimezone", new TimeSpan (6,0,0),null,null,null,new TimeZoneInfo.AdjustmentRule[] {r1});
163 Assert.IsTrue (tz.SupportsDaylightSavingTime);
167 public void SupportsDaylightSavingTime_EmptyAdjustmentRule ()
169 TimeZoneInfo tz = TimeZoneInfo.CreateCustomTimeZone ("mytimezone", new TimeSpan (6,0,0),null,null,null,null);
170 Assert.IsFalse (tz.SupportsDaylightSavingTime);
174 public void SupportsDaylightSavingTime_NonEmptyAdjustmentRule_DisableDaylightSavingTime ()
176 TimeZoneInfo.TransitionTime s1 = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,4,0,0), 3, 2, DayOfWeek.Sunday);
177 TimeZoneInfo.TransitionTime e1 = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,4,0,0), 10, 2, DayOfWeek.Sunday);
178 TimeZoneInfo.AdjustmentRule r1 = TimeZoneInfo.AdjustmentRule.CreateAdjustmentRule (new DateTime (2000,1,1), new DateTime (2005,1,1), new TimeSpan (1,0,0), s1, e1);
179 TimeZoneInfo tz = TimeZoneInfo.CreateCustomTimeZone ("mytimezone", new TimeSpan (6,0,0),null,null,null,new TimeZoneInfo.AdjustmentRule[] {r1}, true);
180 Assert.IsFalse (tz.SupportsDaylightSavingTime);
184 public void SupportsDaylightSavingTime_EmptyAdjustmentRule_DisableDaylightSavingTime ()
186 TimeZoneInfo tz = TimeZoneInfo.CreateCustomTimeZone ("mytimezone", new TimeSpan (6,0,0),null,null,null,null,true);
187 Assert.IsFalse (tz.SupportsDaylightSavingTime);
192 public class IsDaylightSavingTimeTests
197 public void CreateTimeZones ()
199 TimeZoneInfo.TransitionTime start = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,1,0,0), 3, 5, DayOfWeek.Sunday);
200 TimeZoneInfo.TransitionTime end = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,2,0,0), 10, 5, DayOfWeek.Sunday);
201 TimeZoneInfo.AdjustmentRule rule = TimeZoneInfo.AdjustmentRule.CreateAdjustmentRule (DateTime.MinValue.Date, DateTime.MaxValue.Date, new TimeSpan (1,0,0), start, end);
202 london = TimeZoneInfo.CreateCustomTimeZone ("Europe/London", new TimeSpan (0), "Europe/London", "British Standard Time", "British Summer Time", new TimeZoneInfo.AdjustmentRule [] {rule});
206 public void NoDSTInUTC ()
208 DateTime june01 = new DateTime (2007, 06, 01);
209 Assert.IsFalse (TimeZoneInfo.Utc.IsDaylightSavingTime (june01));
213 public void DSTInLondon ()
215 if (Environment.OSVersion.Platform != PlatformID.Unix)
216 Assert.Ignore ("Not running on Unix.");
217 DateTime june01 = new DateTime (2007, 06, 01);
218 DateTime xmas = new DateTime (2007, 12, 25);
219 Assert.IsTrue (london.IsDaylightSavingTime (june01), "June 01 is DST in London");
220 Assert.IsFalse (london.IsDaylightSavingTime (xmas), "Xmas is not DST in London");
224 public void DSTTransisions ()
226 if (Environment.OSVersion.Platform != PlatformID.Unix)
227 Assert.Ignore ("Not running on Unix.");
228 DateTime beforeDST = new DateTime (2007, 03, 25, 0, 59, 59, DateTimeKind.Unspecified);
229 DateTime startDST = new DateTime (2007, 03, 25, 2, 0, 0, DateTimeKind.Unspecified);
230 DateTime endDST = new DateTime (2007, 10, 28, 1, 59, 59, DateTimeKind.Unspecified);
231 DateTime afterDST = new DateTime (2007, 10, 28, 2, 0, 0, DateTimeKind.Unspecified);
232 Assert.IsFalse (london.IsDaylightSavingTime (beforeDST), "Just before DST");
233 Assert.IsTrue (london.IsDaylightSavingTime (startDST), "the first seconds of DST");
234 Assert.IsFalse (london.IsDaylightSavingTime (endDST), "The last seconds of DST");
235 Assert.IsFalse (london.IsDaylightSavingTime (afterDST), "Just after DST");
239 public void DSTTransisionsUTC ()
241 DateTime beforeDST = new DateTime (2007, 03, 25, 0, 59, 59, DateTimeKind.Utc);
242 DateTime startDST = new DateTime (2007, 03, 25, 1, 0, 0, DateTimeKind.Utc);
243 DateTime endDST = new DateTime (2007, 10, 28, 0, 59, 59, DateTimeKind.Utc);
244 DateTime afterDST = new DateTime (2007, 10, 28, 1, 0, 0, DateTimeKind.Utc);
245 Assert.IsFalse (london.IsDaylightSavingTime (beforeDST), "Just before DST");
246 Assert.IsTrue (london.IsDaylightSavingTime (startDST), "the first seconds of DST");
247 Assert.IsTrue (london.IsDaylightSavingTime (endDST), "The last seconds of DST");
248 Assert.IsFalse (london.IsDaylightSavingTime (afterDST), "Just after DST");
253 public void MatchTimeZoneBehavior ()
255 TimeZone tzone = TimeZone.CurrentTimeZone;
256 TimeZoneInfo local = TimeZoneInfo.Local;
257 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)) {
258 date = DateTime.SpecifyKind (date, DateTimeKind.Local);
259 if (local.IsInvalidTime (date))
261 Assert.IsTrue (tzone.IsDaylightSavingTime (date) == local.IsDaylightSavingTime (date));
265 [Test (Description="Description xambug #17155")]
266 public void AdjustmentRuleAfterNewYears ()
269 if (Environment.OSVersion.Platform == PlatformID.Unix)
270 tz = TimeZoneInfo.FindSystemTimeZoneById ("Pacific/Auckland"); // *nix
272 tz = TimeZoneInfo.FindSystemTimeZoneById ("New Zealand Standard Time"); // Windows
274 // DST start: 9/29/2013 2:00:00 AM
275 // DST end: 4/6/2014 3:00:00 AM
276 DateTime dt = new DateTime (2014, 1, 9, 23, 0, 0, DateTimeKind.Utc);
277 Assert.IsTrue (tz.IsDaylightSavingTime (dt), "#1.1");
279 // DST start: 9/29/2014 2:00:00 AM
280 // DST end: 4/6/2015 3:00:00 AM
281 dt = new DateTime (2014, 6, 9, 23, 0, 0, DateTimeKind.Utc);
282 Assert.IsFalse (tz.IsDaylightSavingTime (dt), "#2.1");
284 // DST start: 9/29/2014 2:00:00 AM
285 // DST end: 4/6/2015 3:00:00 AM
286 dt = new DateTime (2014, 10, 9, 23, 0, 0, DateTimeKind.Utc);
287 Assert.IsTrue (tz.IsDaylightSavingTime (dt), "#3.1");
290 [Test] //Covers #26008
291 public void DSTWithFloatingDateRule ()
293 // Construct a custom time zone where daylight saving time starts on the
294 // 2nd Sunday in March.
295 var transitionToDaylight = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1, 1, 1, 2, 0, 0), 3, 2, DayOfWeek.Sunday);
296 var transitionToStandard = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1, 1, 1, 2, 0, 0), 11, 1, DayOfWeek.Sunday);
297 var adjustment = TimeZoneInfo.AdjustmentRule.CreateAdjustmentRule (DateTime.MinValue.Date, DateTime.MaxValue.Date, new TimeSpan (1, 0, 0), transitionToDaylight, transitionToStandard);
298 var timeZone = TimeZoneInfo.CreateCustomTimeZone ("BugCheck", new TimeSpan (-8, 0, 0), "Testing", "Testing Standard", "Testing Daylight", new TimeZoneInfo.AdjustmentRule [] { adjustment });
299 // See if March 7, 2014 is listed as being during daylight saving time.
300 // If it is DST, then the runtime has the bug that we are looking for.
301 Assert.IsFalse (timeZone.IsDaylightSavingTime (new DateTime (2014, 3, 7, 12, 0, 0, DateTimeKind.Unspecified)));
304 [Test] //Covers #25050
305 public void TestAthensDST ()
307 TimeZoneInfo tzi = TimeZoneInfo.FindSystemTimeZoneById ("Europe/Athens");
308 var date = new DateTime (2014, 3, 30 , 2, 0, 0);
309 Assert.IsFalse (tzi.IsDaylightSavingTime (date));
310 Assert.AreEqual (new TimeSpan (2,0,0), tzi.GetUtcOffset (date));
315 public class ConvertTimeTests
320 public void CreateTimeZones ()
322 TimeZoneInfo.TransitionTime start = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,1,0,0), 3, 5, DayOfWeek.Sunday);
323 TimeZoneInfo.TransitionTime end = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,2,0,0), 10, 5, DayOfWeek.Sunday);
324 TimeZoneInfo.AdjustmentRule rule = TimeZoneInfo.AdjustmentRule.CreateAdjustmentRule (DateTime.MinValue.Date, DateTime.MaxValue.Date, new TimeSpan (1,0,0), start, end);
325 london = TimeZoneInfo.CreateCustomTimeZone ("Europe/London", new TimeSpan (0), "Europe/London", "British Standard Time", "British Summer Time", new TimeZoneInfo.AdjustmentRule [] {rule});
329 [ExpectedException (typeof (ArgumentException))]
330 public void ConvertFromUtc_KindIsLocalException ()
332 if (Environment.OSVersion.Platform != PlatformID.Unix)
333 throw new ArgumentException ();
334 TimeZoneInfo.ConvertTimeFromUtc (new DateTime (2007, 5, 3, 11, 8, 0, DateTimeKind.Local), TimeZoneInfo.Local);
338 [ExpectedException (typeof (ArgumentNullException))]
339 public void ConvertFromUtc_DestinationTimeZoneIsNullException ()
341 TimeZoneInfo.ConvertTimeFromUtc (new DateTime (2007, 5, 3, 11, 8, 0), null);
345 public void ConvertFromUtc_DestinationIsUTC ()
347 DateTime now = DateTime.UtcNow;
348 DateTime converted = TimeZoneInfo.ConvertTimeFromUtc (now, TimeZoneInfo.Utc);
349 Assert.AreEqual (now, converted);
353 public void ConvertFromUTC_ConvertInWinter ()
355 if (Environment.OSVersion.Platform != PlatformID.Unix)
356 Assert.Ignore ("Not running on Unix.");
357 DateTime utc = new DateTime (2007, 12, 25, 12, 0, 0);
358 DateTime converted = TimeZoneInfo.ConvertTimeFromUtc (utc, london);
359 Assert.AreEqual (utc, converted);
363 public void ConvertFromUtc_ConvertInSummer ()
365 if (Environment.OSVersion.Platform != PlatformID.Unix)
366 Assert.Ignore ("Not running on Unix.");
367 DateTime utc = new DateTime (2007, 06, 01, 12, 0, 0);
368 DateTime converted = TimeZoneInfo.ConvertTimeFromUtc (utc, london);
369 Assert.AreEqual (utc + new TimeSpan (1,0,0), converted);
373 public void ConvertToUTC_KindIsUtc ()
375 DateTime now = DateTime.UtcNow;
376 Assert.AreEqual (now.Kind, DateTimeKind.Utc);
377 DateTime converted = TimeZoneInfo.ConvertTimeToUtc (now);
378 Assert.AreEqual (now, converted);
382 [ExpectedException (typeof (ArgumentException))]
383 public void ConvertToUTC_KindIsUTCButSourceIsNot ()
385 TimeZoneInfo.ConvertTimeToUtc (new DateTime (2007, 5, 3, 12, 8, 0, DateTimeKind.Utc), london);
389 [ExpectedException (typeof (ArgumentException))]
390 public void ConvertToUTC_KindIsLocalButSourceIsNot ()
392 if (Environment.OSVersion.Platform != PlatformID.Unix)
393 throw new ArgumentException ();
394 TimeZoneInfo.ConvertTimeToUtc (new DateTime (2007, 5, 3, 12, 8, 0, DateTimeKind.Local), london);
398 [ExpectedException (typeof (ArgumentException))]
399 public void ConvertToUTC_InvalidDate ()
401 TimeZoneInfo.ConvertTimeToUtc (new DateTime (2007, 3, 25, 1, 30, 0), london);
405 [ExpectedException (typeof (ArgumentNullException))]
406 public void ConvertToUTC_SourceIsNull ()
408 TimeZoneInfo.ConvertTimeToUtc (new DateTime (2007, 5, 3, 12, 16, 0), null);
413 public void ConvertToUtc_MatchDateTimeBehavior ()
415 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)) {
416 Assert.AreEqual (TimeZoneInfo.ConvertTimeToUtc (date), date.ToUniversalTime ());
422 public void ConvertFromToUtc ()
424 if (Environment.OSVersion.Platform != PlatformID.Unix)
425 Assert.Ignore ("Not running on Unix.");
426 DateTime utc = DateTime.UtcNow;
427 Assert.AreEqual (utc.Kind, DateTimeKind.Utc);
428 DateTime converted = TimeZoneInfo.ConvertTimeFromUtc (utc, london);
429 Assert.AreEqual (converted.Kind, DateTimeKind.Unspecified);
430 DateTime back = TimeZoneInfo.ConvertTimeToUtc (converted, london);
431 Assert.AreEqual (back.Kind, DateTimeKind.Utc);
432 Assert.AreEqual (utc, back);
437 public void ConvertTimeToUtc_Overflow ()
439 var res = TimeZoneInfo.ConvertTimeToUtc (new DateTime (0));
440 Assert.AreEqual (res.Kind, DateTimeKind.Utc, "#1");
442 res = TimeZoneInfo.ConvertTimeToUtc (DateTime.MaxValue);
443 Assert.AreEqual (res.Kind, DateTimeKind.Utc, "#2");
447 public void ConvertFromToLocal ()
449 DateTime utc = DateTime.UtcNow;
450 Assert.AreEqual(utc.Kind, DateTimeKind.Utc);
451 DateTime converted = TimeZoneInfo.ConvertTimeFromUtc(utc, TimeZoneInfo.Local);
453 Assert.AreEqual(DateTimeKind.Local, converted.Kind);
455 Assert.AreEqual(DateTimeKind.Unspecified, converted.Kind);
457 DateTime back = TimeZoneInfo.ConvertTimeToUtc(converted, TimeZoneInfo.Local);
458 Assert.AreEqual(back.Kind, DateTimeKind.Utc);
459 Assert.AreEqual(utc, back);
463 public void ConvertToTimeZone ()
465 if (Environment.OSVersion.Platform != PlatformID.Unix)
466 Assert.Ignore ("Not running on Unix.");
468 TimeZoneInfo.ConvertTime (DateTime.Now, TimeZoneInfo.FindSystemTimeZoneById("Pacific/Auckland"));
472 [ExpectedException (typeof (ArgumentNullException))]
473 public void ConvertTime_DateTime_TimeZoneInfo_DestinationTimeZoneIsNull ()
475 TimeZoneInfo.ConvertTime (DateTime.Now, null);
479 public void ConvertTime_DateTime_TimeZoneInfo_DateTimeKindMatch ()
481 var sdt = new DateTime (2014, 1, 9, 23, 0, 0, DateTimeKind.Utc);
482 var ddt = TimeZoneInfo.ConvertTime (sdt, TimeZoneInfo.Utc);
483 Assert.AreEqual (ddt.Kind, sdt.Kind, "#1.1");
484 Assert.AreEqual (ddt.Kind, DateTimeKind.Utc, "#1.2");
486 sdt = new DateTime (2014, 1, 9, 23, 0, 0, DateTimeKind.Local);
487 ddt = TimeZoneInfo.ConvertTime (sdt, TimeZoneInfo.Local);
488 Assert.AreEqual (ddt.Kind, sdt.Kind, "#2.1");
489 Assert.AreEqual (ddt.Kind, DateTimeKind.Local, "#2.2");
491 sdt = new DateTime (2014, 1, 9, 23, 0, 0);
492 ddt = TimeZoneInfo.ConvertTime (sdt, TimeZoneInfo.Local);
493 Assert.AreEqual (ddt.Kind, sdt.Kind, "#3.1");
494 Assert.AreEqual (ddt.Kind, DateTimeKind.Unspecified, "#3.2");
498 [ExpectedException (typeof (ArgumentNullException))]
499 public void ConverTime_DateTime_TimeZoneInfo_TimeZoneInfo_SourceTimeZoneIsNull ()
501 TimeZoneInfo.ConvertTime (DateTime.Now, null, TimeZoneInfo.Local);
505 [ExpectedException (typeof (ArgumentNullException))]
506 public void ConverTime_DateTime_TimeZoneInfo_TimeZoneInfo_DestinationTimeZoneIsNull ()
508 TimeZoneInfo.ConvertTime (DateTime.Now, TimeZoneInfo.Utc, null);
511 [Test (Description="Fix for xambug https://bugzilla.xamarin.com/show_bug.cgi?id=17155")]
512 public void ConvertTime_AdjustmentRuleAfterNewYears ()
515 if (Environment.OSVersion.Platform == PlatformID.Unix)
516 tz = TimeZoneInfo.FindSystemTimeZoneById ("Pacific/Auckland"); // *nix
518 tz = TimeZoneInfo.FindSystemTimeZoneById ("New Zealand Standard Time"); // Windows
520 // DST start: 9/29/2013 2:00:00 AM
521 // DST end: 4/6/2014 3:00:00 AM
522 DateTime sdt = new DateTime (2014, 1, 9, 23, 0, 0, DateTimeKind.Utc);
523 DateTime ddt = TimeZoneInfo.ConvertTime (sdt, tz);
524 Assert.AreEqual (10, ddt.Day, "#1.1");
525 Assert.AreEqual (1, ddt.Month, "#1.2");
526 Assert.AreEqual (2014, ddt.Year, "#1.3");
527 Assert.AreEqual (12, ddt.Hour, "#1.4");
528 Assert.AreEqual (0, ddt.Minute, "#1.5");
529 Assert.AreEqual (0, ddt.Second, "#1.6");
531 // DST start: 9/29/2014 2:00:00 AM
532 // DST end: 4/6/2015 3:00:00 AM
533 sdt = new DateTime (2014, 6, 9, 23, 0, 0, DateTimeKind.Utc);
534 ddt = TimeZoneInfo.ConvertTime (sdt, tz);
535 Assert.AreEqual (10, ddt.Day, "#2.1");
536 Assert.AreEqual (6, ddt.Month, "#2.2");
537 Assert.AreEqual (2014, ddt.Year, "#2.3");
538 Assert.AreEqual (11, ddt.Hour, "#2.4");
539 Assert.AreEqual (0, ddt.Minute, "#2.5");
540 Assert.AreEqual (0, ddt.Second, "#2.6");
542 // DST start: 9/29/2014 2:00:00 AM
543 // DST end: 4/6/2015 3:00:00 AM
544 sdt = new DateTime (2014, 10, 9, 23, 0, 0, DateTimeKind.Utc);
545 ddt = TimeZoneInfo.ConvertTime (sdt, tz);
546 Assert.AreEqual (10, ddt.Day, "#3.1");
547 Assert.AreEqual (10, ddt.Month, "#3.2");
548 Assert.AreEqual (2014, ddt.Year, "#3.3");
549 Assert.AreEqual (12, ddt.Hour, "#3.4");
550 Assert.AreEqual (0, ddt.Minute, "#3.5");
551 Assert.AreEqual (0, ddt.Second, "#3.6");
554 [Test (Description="Fix the bug https://bugzilla.xamarin.com/show_bug.cgi?id=1849")]
555 public void ConvertTime_AjustmentConvertTimeWithSourceTimeZone () {
557 TimeZoneInfo easternTimeZone;
558 TimeZoneInfo pacificTimeZone;
560 if (Environment.OSVersion.Platform == PlatformID.Unix) {
562 easternTimeZone = TimeZoneInfo.FindSystemTimeZoneById ("US/Eastern");
563 pacificTimeZone = TimeZoneInfo.FindSystemTimeZoneById ("US/Pacific");
567 easternTimeZone = TimeZoneInfo.FindSystemTimeZoneById ("Eastern Standard Time");
568 pacificTimeZone = TimeZoneInfo.FindSystemTimeZoneById ("Pacific Standard Time");
571 DateTime lastMidnight = new DateTime (new DateTime (2012, 06, 13).Ticks, DateTimeKind.Unspecified);
572 DateTime lastMidnightAsEST = TimeZoneInfo.ConvertTime (lastMidnight, pacificTimeZone, easternTimeZone);
573 DateTime lastMidnightAsPST = TimeZoneInfo.ConvertTime (lastMidnightAsEST, easternTimeZone, pacificTimeZone);
575 // Last midnight in PST as EST should be 3AM
576 DateTime expectedDate = new DateTime (2012, 06, 13, 3, 0, 0);
578 Assert.AreEqual (expectedDate, lastMidnightAsEST);
579 Assert.AreEqual (lastMidnight, lastMidnightAsPST);
584 public class IsInvalidTimeTests
589 public void CreateTimeZones ()
591 TimeZoneInfo.TransitionTime start = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,1,0,0), 3, 5, DayOfWeek.Sunday);
592 TimeZoneInfo.TransitionTime end = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,2,0,0), 10, 5, DayOfWeek.Sunday);
593 TimeZoneInfo.AdjustmentRule rule = TimeZoneInfo.AdjustmentRule.CreateAdjustmentRule (DateTime.MinValue.Date, DateTime.MaxValue.Date, new TimeSpan (1,0,0), start, end);
594 london = TimeZoneInfo.CreateCustomTimeZone ("Europe/London", new TimeSpan (0), "Europe/London", "British Standard Time", "British Summer Time", new TimeZoneInfo.AdjustmentRule [] {rule});
599 public void UTCDate ()
601 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)) {
602 date = DateTime.SpecifyKind (date, DateTimeKind.Utc);
603 Assert.IsFalse (london.IsInvalidTime (date));
608 public void InvalidDates ()
610 Assert.IsFalse (london.IsInvalidTime (new DateTime (2007, 03, 25, 0, 59, 59)));
611 Assert.IsTrue (london.IsInvalidTime (new DateTime (2007, 03, 25, 1, 0, 0)));
612 Assert.IsTrue (london.IsInvalidTime (new DateTime (2007, 03, 25, 1, 59, 59)));
613 Assert.IsFalse (london.IsInvalidTime (new DateTime (2007, 03, 25, 2, 0, 0)));
618 public class IsAmbiguousTimeTests
623 public void CreateTimeZones ()
625 TimeZoneInfo.TransitionTime start = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,1,0,0), 3, 5, DayOfWeek.Sunday);
626 TimeZoneInfo.TransitionTime end = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,2,0,0), 10, 5, DayOfWeek.Sunday);
627 TimeZoneInfo.AdjustmentRule rule = TimeZoneInfo.AdjustmentRule.CreateAdjustmentRule (DateTime.MinValue.Date, DateTime.MaxValue.Date, new TimeSpan (1,0,0), start, end);
628 london = TimeZoneInfo.CreateCustomTimeZone ("Europe/London", new TimeSpan (0), "Europe/London", "British Standard Time", "British Summer Time", new TimeZoneInfo.AdjustmentRule [] {rule});
632 public void AmbiguousDates ()
634 if (Environment.OSVersion.Platform != PlatformID.Unix)
635 Assert.Ignore ("Not running on Unix.");
636 Assert.IsFalse (london.IsAmbiguousTime (new DateTime (2007, 10, 28, 1, 0, 0)));
637 Assert.IsTrue (london.IsAmbiguousTime (new DateTime (2007, 10, 28, 1, 0, 1)));
638 Assert.IsTrue (london.IsAmbiguousTime (new DateTime (2007, 10, 28, 2, 0, 0)));
639 Assert.IsFalse (london.IsAmbiguousTime (new DateTime (2007, 10, 28, 2, 0, 1)));
643 public void AmbiguousUTCDates ()
645 if (Environment.OSVersion.Platform != PlatformID.Unix)
646 Assert.Ignore ("Not running on Unix.");
647 Assert.IsFalse (london.IsAmbiguousTime (new DateTime (2007, 10, 28, 0, 0, 0, DateTimeKind.Utc)));
648 Assert.IsTrue (london.IsAmbiguousTime (new DateTime (2007, 10, 28, 0, 0, 1, DateTimeKind.Utc)));
649 Assert.IsTrue (london.IsAmbiguousTime (new DateTime (2007, 10, 28, 0, 59, 59, DateTimeKind.Utc)));
650 Assert.IsFalse (london.IsAmbiguousTime (new DateTime (2007, 10, 28, 1, 0, 0, DateTimeKind.Utc)));
655 public void AmbiguousInUTC ()
657 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)) {
658 Assert.IsFalse (TimeZoneInfo.Utc.IsAmbiguousTime (date));
665 public class GetSystemTimeZonesTests
668 public void NotEmpty ()
670 if (Environment.OSVersion.Platform != PlatformID.Unix)
671 Assert.Ignore ("Not running on Unix.");
672 global::System.Collections.ObjectModel.ReadOnlyCollection<TimeZoneInfo> systemTZ = TimeZoneInfo.GetSystemTimeZones ();
673 Assert.IsNotNull(systemTZ, "SystemTZ is null");
674 Assert.IsFalse (systemTZ.Count == 0, "SystemTZ is empty");
678 public void ContainsBrussels ()
680 if (Environment.OSVersion.Platform != PlatformID.Unix)
681 Assert.Ignore ("Not running on Unix.");
682 global::System.Collections.ObjectModel.ReadOnlyCollection<TimeZoneInfo> systemTZ = TimeZoneInfo.GetSystemTimeZones ();
683 foreach (TimeZoneInfo tz in systemTZ) {
684 if (tz.Id == "Europe/Brussels")
687 Assert.Fail ("Europe/Brussels not found in SystemTZ");
692 public class FindSystemTimeZoneByIdTests
695 [ExpectedException (typeof (ArgumentNullException))]
696 public void NullId ()
698 TimeZoneInfo.FindSystemTimeZoneById (null);
702 [ExpectedException (typeof (TimeZoneNotFoundException))]
703 public void NonSystemTimezone ()
705 if (Environment.OSVersion.Platform != PlatformID.Unix)
706 throw new TimeZoneNotFoundException ();
707 TimeZoneInfo.FindSystemTimeZoneById ("Neverland/The_Lagoon");
711 public void FindBrusselsTZ ()
713 if (Environment.OSVersion.Platform != PlatformID.Unix)
714 Assert.Ignore ("Not running on Unix.");
715 TimeZoneInfo brussels = TimeZoneInfo.FindSystemTimeZoneById ("Europe/Brussels");
716 Assert.IsNotNull (brussels);
720 public void OffsetIsCorrectInKinshasa ()
722 if (Environment.OSVersion.Platform != PlatformID.Unix)
723 Assert.Ignore ("Not running on Unix.");
724 TimeZoneInfo kin = TimeZoneInfo.FindSystemTimeZoneById ("Africa/Kinshasa");
725 Assert.AreEqual (new TimeSpan (1,0,0), kin.BaseUtcOffset, "BaseUtcOffset in Kinshasa is not +1h");
729 public void OffsetIsCorrectInBrussels ()
731 if (Environment.OSVersion.Platform != PlatformID.Unix)
732 Assert.Ignore ("Not running on Unix.");
733 TimeZoneInfo brussels = TimeZoneInfo.FindSystemTimeZoneById ("Europe/Brussels");
734 Assert.AreEqual (new TimeSpan (1,0,0), brussels.BaseUtcOffset, "BaseUtcOffset for Brussels is not +1h");
738 public void NoDSTInKinshasa ()
740 if (Environment.OSVersion.Platform != PlatformID.Unix)
741 Assert.Ignore ("Not running on Unix.");
742 TimeZoneInfo kin = TimeZoneInfo.FindSystemTimeZoneById ("Africa/Kinshasa");
743 Assert.IsFalse (kin.SupportsDaylightSavingTime);
747 public void BrusselsSupportsDST ()
749 if (Environment.OSVersion.Platform != PlatformID.Unix)
750 Assert.Ignore ("Not running on Unix.");
751 TimeZoneInfo brussels = TimeZoneInfo.FindSystemTimeZoneById ("Europe/Brussels");
752 Assert.IsTrue (brussels.SupportsDaylightSavingTime);
756 public void MelbourneSupportsDST ()
758 if (Environment.OSVersion.Platform != PlatformID.Unix)
759 Assert.Ignore ("Not running on Unix.");
760 TimeZoneInfo melbourne = TimeZoneInfo.FindSystemTimeZoneById ("Australia/Melbourne");
761 Assert.IsTrue (melbourne.SupportsDaylightSavingTime);
765 public void RomeAndVaticanSharesTime ()
767 if (Environment.OSVersion.Platform != PlatformID.Unix)
768 Assert.Ignore ("Not running on Unix.");
769 TimeZoneInfo rome = TimeZoneInfo.FindSystemTimeZoneById ("Europe/Rome");
770 TimeZoneInfo vatican = TimeZoneInfo.FindSystemTimeZoneById ("Europe/Vatican");
771 Assert.IsTrue (rome.HasSameRules (vatican));
775 public void FindSystemTimeZoneById_Local_Roundtrip ()
777 Assert.AreEqual (TimeZoneInfo.Local.Id, TimeZoneInfo.FindSystemTimeZoneById (TimeZoneInfo.Local.Id).Id);
781 public void Test326 ()
783 DateTime utc = DateTime.UtcNow;
784 DateTime local = TimeZoneInfo.ConvertTime (utc, TimeZoneInfo.Utc, TimeZoneInfo.FindSystemTimeZoneById (TimeZoneInfo.Local.Id));
785 Assert.AreEqual (local, utc + TimeZoneInfo.Local.GetUtcOffset (utc), "ConvertTime/Local");
790 public void BrusselsAdjustments ()
792 TimeZoneInfo.TransitionTime start = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,2,0,0), 3, 5, DayOfWeek.Sunday);
793 TimeZoneInfo.TransitionTime end = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,3,0,0), 10, 5, DayOfWeek.Sunday);
794 TimeZoneInfo.AdjustmentRule rule = TimeZoneInfo.AdjustmentRule.CreateAdjustmentRule (DateTime.MinValue.Date, DateTime.MaxValue.Date, new TimeSpan (1,0,0), start, end);
795 TimeZoneInfo brussels = TimeZoneInfo.CreateCustomTimeZone ("Europe/Brussels", new TimeSpan (1, 0, 0), "Europe/Brussels", "", "", new TimeZoneInfo.AdjustmentRule [] {rule});
797 TimeZoneInfo brussels_sys = TimeZoneInfo.FindSystemTimeZoneById ("Europe/Brussels");
799 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)) {
800 Assert.AreEqual (brussels.GetUtcOffset (date), brussels_sys.GetUtcOffset (date));
801 Assert.AreEqual (brussels.IsDaylightSavingTime (date), brussels_sys.IsDaylightSavingTime (date));
808 public class GetAmbiguousTimeOffsetsTests
811 [ExpectedException (typeof(ArgumentException))]
812 public void DateIsNotAmbiguous ()
814 if (Environment.OSVersion.Platform != PlatformID.Unix)
815 throw new ArgumentException ();
816 TimeZoneInfo brussels = TimeZoneInfo.FindSystemTimeZoneById ("Europe/Brussels");
817 DateTime date = new DateTime (2007, 05, 11, 11, 40, 00);
818 brussels.GetAmbiguousTimeOffsets (date);
822 public void AmbiguousOffsets ()
824 if (Environment.OSVersion.Platform != PlatformID.Unix)
825 Assert.Ignore ("Not running on Unix.");
826 TimeZoneInfo brussels = TimeZoneInfo.FindSystemTimeZoneById ("Europe/Brussels");
827 DateTime date = new DateTime (2007, 10, 28, 2, 30, 00);
828 Assert.IsTrue (brussels.IsAmbiguousTime (date));
829 Assert.AreEqual (2, brussels.GetAmbiguousTimeOffsets (date).Length);
830 Assert.AreEqual (new TimeSpan[] {new TimeSpan (1, 0, 0), new TimeSpan (2, 0, 0)}, brussels.GetAmbiguousTimeOffsets (date));
835 public class HasSameRulesTests
838 public void NullAdjustments () //bnc #391011
840 TimeZoneInfo utc = TimeZoneInfo.Utc;
841 TimeZoneInfo custom = TimeZoneInfo.CreateCustomTimeZone ("Custom", new TimeSpan (0), "Custom", "Custom");
842 Assert.IsTrue (utc.HasSameRules (custom));
847 public class SerializationTests
850 public void Serialization_Deserialization ()
852 TimeZoneInfo.TransitionTime start = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,1,0,0), 3, 5, DayOfWeek.Sunday);
853 TimeZoneInfo.TransitionTime end = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,2,0,0), 10, 5, DayOfWeek.Sunday);
854 TimeZoneInfo.AdjustmentRule rule = TimeZoneInfo.AdjustmentRule.CreateAdjustmentRule (DateTime.MinValue.Date, DateTime.MaxValue.Date, new TimeSpan (1,0,0), start, end);
855 TimeZoneInfo london = TimeZoneInfo.CreateCustomTimeZone ("Europe/London", new TimeSpan (0), "Europe/London", "British Standard Time", "British Summer Time", new TimeZoneInfo.AdjustmentRule [] {rule});
856 MemoryStream stream = new MemoryStream ();
857 BinaryFormatter formatter = new BinaryFormatter ();
858 formatter.Serialize (stream, london);
860 TimeZoneInfo deserialized = (TimeZoneInfo) formatter.Deserialize (stream);
863 Assert.IsTrue (london.Equals (deserialized));
868 public class MultipleDaylightSavingTimeTests {
869 private TimeZoneInfo cairo;
870 private DateTime dst1Start;
871 private DateTime dst1End;
872 private DateTime dst2Start;
873 private DateTime dst2End;
875 private TimeSpan baseUtcOffset;
876 private TimeSpan dstUtcOffset;
877 private TimeSpan dstOffset;
880 public void CreateTimeZones ()
883 From 1/1/2014 12:00:00 AM to 6/30/2014 12:00:00 AM
885 Begins at 12:00 AM on 16 May
886 Ends at 1:00 AM on 29 June
887 From 7/1/2014 12:00:00 AM to 12/31/2014 12:00:00 AM
889 Begins at 12:00 AM on 29 July
890 Ends at 12:00 AM on 26 September
892 dst1Start = new DateTime (2014, 5, 16);
893 dst1End = new DateTime (2014, 6, 29);
894 dst2Start = new DateTime (2014, 7, 29);
895 dst2End = new DateTime (2014, 9, 26);
897 baseUtcOffset = new TimeSpan (2, 0, 0);
898 dstUtcOffset = new TimeSpan (3, 0, 0);
899 dstOffset = dstUtcOffset - baseUtcOffset;
901 var rule1 = TimeZoneInfo.AdjustmentRule.CreateAdjustmentRule (
902 new DateTime (2014, 1, 1), new DateTime (2014, 6, 30), dstOffset,
903 CreateFixedDateRule (dst1Start), CreateFixedDateRule (dst1End));
905 var rule2 = TimeZoneInfo.AdjustmentRule.CreateAdjustmentRule (
906 new DateTime (2014, 7, 1), new DateTime (2014, 12, 31), dstOffset,
907 CreateFixedDateRule (dst2Start), CreateFixedDateRule (dst2End));
909 cairo = TimeZoneInfo.CreateCustomTimeZone ("Africa/Cairo", baseUtcOffset, "Africa/Cairo", "EET", "EEST",
910 new [] {rule1, rule2});
913 private static TimeZoneInfo.TransitionTime CreateFixedDateRule (DateTime dateTime)
915 var time = new DateTime (dateTime.Ticks - dateTime.Date.Ticks);
916 return TimeZoneInfo.TransitionTime.CreateFixedDateRule (time, dateTime.Month, dateTime.Day);
920 public void GetUtcOffset_FromUTC ()
922 var d = dst1Start.Add (-baseUtcOffset);
923 d = DateTime.SpecifyKind (d, DateTimeKind.Utc);
924 Assert.AreEqual(baseUtcOffset, cairo.GetUtcOffset (d.Add (new TimeSpan(0,0,0,-1))));
925 Assert.AreEqual(dstUtcOffset, cairo.GetUtcOffset (d));
926 Assert.AreEqual(dstUtcOffset, cairo.GetUtcOffset (d.Add (new TimeSpan(0,0,0, 1))));
928 d = dst1End.Add (-baseUtcOffset-dstOffset);
929 d = DateTime.SpecifyKind (d, DateTimeKind.Utc);
930 Assert.AreEqual(dstUtcOffset, cairo.GetUtcOffset (d.Add (new TimeSpan(0,0,0,-1))));
931 Assert.AreEqual(baseUtcOffset, cairo.GetUtcOffset (d));
932 Assert.AreEqual(baseUtcOffset, cairo.GetUtcOffset (d.Add (new TimeSpan(0,0,0, 1))));
934 d = dst2Start.Add (-baseUtcOffset);
935 d = DateTime.SpecifyKind (d, DateTimeKind.Utc);
936 Assert.AreEqual(baseUtcOffset, cairo.GetUtcOffset (d.Add (new TimeSpan(0,0,0,-1))));
937 Assert.AreEqual(dstUtcOffset, cairo.GetUtcOffset (d));
938 Assert.AreEqual(dstUtcOffset, cairo.GetUtcOffset (d.Add (new TimeSpan(0,0,0, 1))));
940 d = dst2End.Add (-baseUtcOffset-dstOffset);
941 d = DateTime.SpecifyKind (d, DateTimeKind.Utc);
942 Assert.AreEqual(dstUtcOffset, cairo.GetUtcOffset (d.Add (new TimeSpan(0,0,0,-1))));
943 Assert.AreEqual(baseUtcOffset, cairo.GetUtcOffset (d));
944 Assert.AreEqual(baseUtcOffset, cairo.GetUtcOffset (d.Add (new TimeSpan(0,0,0, 1))));
948 public void GetUtcOffset_FromLocal ()
950 var d = dst1Start.Add (-baseUtcOffset);
951 d = DateTime.SpecifyKind (d, DateTimeKind.Utc);
952 d = d.ToLocalTime ();
953 Assert.AreEqual(baseUtcOffset, cairo.GetUtcOffset (d.Add (new TimeSpan(0,0,0,-1))));
954 Assert.AreEqual(dstUtcOffset, cairo.GetUtcOffset (d));
955 Assert.AreEqual(dstUtcOffset, cairo.GetUtcOffset (d.Add (new TimeSpan(0,0,0, 1))));
957 d = dst1End.Add (-baseUtcOffset-dstOffset);
958 d = DateTime.SpecifyKind (d, DateTimeKind.Utc);
959 d = d.ToLocalTime ();
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 d = d.ToLocalTime ();
967 Assert.AreEqual(baseUtcOffset, cairo.GetUtcOffset (d.Add (new TimeSpan(0,0,0,-1))));
968 Assert.AreEqual(dstUtcOffset, cairo.GetUtcOffset (d));
969 Assert.AreEqual(dstUtcOffset, cairo.GetUtcOffset (d.Add (new TimeSpan(0,0,0, 1))));
971 d = dst2End.Add (-baseUtcOffset-dstOffset);
972 d = DateTime.SpecifyKind (d, DateTimeKind.Utc);
973 d = d.ToLocalTime ();
974 Assert.AreEqual(dstUtcOffset, cairo.GetUtcOffset (d.Add (new TimeSpan(0,0,0,-1))));
975 Assert.AreEqual(baseUtcOffset, cairo.GetUtcOffset (d));
976 Assert.AreEqual(baseUtcOffset, cairo.GetUtcOffset (d.Add (new TimeSpan(0,0,0, 1))));
980 public void GetUtcOffset_FromUnspecified ()
982 var d = dst1Start.Add (dstOffset);
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 (-dstOffset);
988 Assert.AreEqual(dstUtcOffset, cairo.GetUtcOffset (d.Add (new TimeSpan(0,0,0,-1))));
989 Assert.AreEqual(baseUtcOffset, cairo.GetUtcOffset (d));
990 Assert.AreEqual(baseUtcOffset, cairo.GetUtcOffset (d.Add (new TimeSpan(0,0,0, 1))));
992 d = dst2Start.Add (dstOffset);
993 Assert.AreEqual(baseUtcOffset, cairo.GetUtcOffset (d.Add (new TimeSpan(0,0,0,-1))));
994 Assert.AreEqual(dstUtcOffset, cairo.GetUtcOffset (d));
995 Assert.AreEqual(dstUtcOffset, cairo.GetUtcOffset (d.Add (new TimeSpan(0,0,0, 1))));
997 d = dst2End.Add (-dstOffset);
998 Assert.AreEqual(dstUtcOffset, cairo.GetUtcOffset (d.Add (new TimeSpan(0,0,0,-1))));
999 Assert.AreEqual(baseUtcOffset, cairo.GetUtcOffset (d));
1000 Assert.AreEqual(baseUtcOffset, cairo.GetUtcOffset (d.Add (new TimeSpan(0,0,0, 1))));