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)));
306 public class ConvertTimeTests
311 public void CreateTimeZones ()
313 TimeZoneInfo.TransitionTime start = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,1,0,0), 3, 5, DayOfWeek.Sunday);
314 TimeZoneInfo.TransitionTime end = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,2,0,0), 10, 5, DayOfWeek.Sunday);
315 TimeZoneInfo.AdjustmentRule rule = TimeZoneInfo.AdjustmentRule.CreateAdjustmentRule (DateTime.MinValue.Date, DateTime.MaxValue.Date, new TimeSpan (1,0,0), start, end);
316 london = TimeZoneInfo.CreateCustomTimeZone ("Europe/London", new TimeSpan (0), "Europe/London", "British Standard Time", "British Summer Time", new TimeZoneInfo.AdjustmentRule [] {rule});
320 [ExpectedException (typeof (ArgumentException))]
321 public void ConvertFromUtc_KindIsLocalException ()
323 if (Environment.OSVersion.Platform != PlatformID.Unix)
324 throw new ArgumentException ();
325 TimeZoneInfo.ConvertTimeFromUtc (new DateTime (2007, 5, 3, 11, 8, 0, DateTimeKind.Local), TimeZoneInfo.Local);
329 [ExpectedException (typeof (ArgumentNullException))]
330 public void ConvertFromUtc_DestinationTimeZoneIsNullException ()
332 TimeZoneInfo.ConvertTimeFromUtc (new DateTime (2007, 5, 3, 11, 8, 0), null);
336 public void ConvertFromUtc_DestinationIsUTC ()
338 DateTime now = DateTime.UtcNow;
339 DateTime converted = TimeZoneInfo.ConvertTimeFromUtc (now, TimeZoneInfo.Utc);
340 Assert.AreEqual (now, converted);
344 public void ConvertFromUTC_ConvertInWinter ()
346 if (Environment.OSVersion.Platform != PlatformID.Unix)
347 Assert.Ignore ("Not running on Unix.");
348 DateTime utc = new DateTime (2007, 12, 25, 12, 0, 0);
349 DateTime converted = TimeZoneInfo.ConvertTimeFromUtc (utc, london);
350 Assert.AreEqual (utc, converted);
354 public void ConvertFromUtc_ConvertInSummer ()
356 if (Environment.OSVersion.Platform != PlatformID.Unix)
357 Assert.Ignore ("Not running on Unix.");
358 DateTime utc = new DateTime (2007, 06, 01, 12, 0, 0);
359 DateTime converted = TimeZoneInfo.ConvertTimeFromUtc (utc, london);
360 Assert.AreEqual (utc + new TimeSpan (1,0,0), converted);
364 public void ConvertToUTC_KindIsUtc ()
366 DateTime now = DateTime.UtcNow;
367 Assert.AreEqual (now.Kind, DateTimeKind.Utc);
368 DateTime converted = TimeZoneInfo.ConvertTimeToUtc (now);
369 Assert.AreEqual (now, converted);
373 [ExpectedException (typeof (ArgumentException))]
374 public void ConvertToUTC_KindIsUTCButSourceIsNot ()
376 TimeZoneInfo.ConvertTimeToUtc (new DateTime (2007, 5, 3, 12, 8, 0, DateTimeKind.Utc), london);
380 [ExpectedException (typeof (ArgumentException))]
381 public void ConvertToUTC_KindIsLocalButSourceIsNot ()
383 if (Environment.OSVersion.Platform != PlatformID.Unix)
384 throw new ArgumentException ();
385 TimeZoneInfo.ConvertTimeToUtc (new DateTime (2007, 5, 3, 12, 8, 0, DateTimeKind.Local), london);
389 [ExpectedException (typeof (ArgumentException))]
390 public void ConvertToUTC_InvalidDate ()
392 TimeZoneInfo.ConvertTimeToUtc (new DateTime (2007, 3, 25, 1, 30, 0), london);
396 [ExpectedException (typeof (ArgumentNullException))]
397 public void ConvertToUTC_SourceIsNull ()
399 TimeZoneInfo.ConvertTimeToUtc (new DateTime (2007, 5, 3, 12, 16, 0), null);
404 public void ConvertToUtc_MatchDateTimeBehavior ()
406 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)) {
407 Assert.AreEqual (TimeZoneInfo.ConvertTimeToUtc (date), date.ToUniversalTime ());
413 public void ConvertFromToUtc ()
415 if (Environment.OSVersion.Platform != PlatformID.Unix)
416 Assert.Ignore ("Not running on Unix.");
417 DateTime utc = DateTime.UtcNow;
418 Assert.AreEqual (utc.Kind, DateTimeKind.Utc);
419 DateTime converted = TimeZoneInfo.ConvertTimeFromUtc (utc, london);
420 Assert.AreEqual (converted.Kind, DateTimeKind.Unspecified);
421 DateTime back = TimeZoneInfo.ConvertTimeToUtc (converted, london);
422 Assert.AreEqual (back.Kind, DateTimeKind.Utc);
423 Assert.AreEqual (utc, back);
429 public void ConvertFromToLocal ()
431 DateTime utc = DateTime.UtcNow;
432 Assert.AreEqual(utc.Kind, DateTimeKind.Utc);
433 DateTime converted = TimeZoneInfo.ConvertTimeFromUtc(utc, TimeZoneInfo.Local);
435 Assert.AreEqual(DateTimeKind.Local, converted.Kind);
437 Assert.AreEqual(DateTimeKind.Unspecified, converted.Kind);
439 DateTime back = TimeZoneInfo.ConvertTimeToUtc(converted, TimeZoneInfo.Local);
440 Assert.AreEqual(back.Kind, DateTimeKind.Utc);
441 Assert.AreEqual(utc, back);
445 public void ConvertToTimeZone ()
447 if (Environment.OSVersion.Platform != PlatformID.Unix)
448 Assert.Ignore ("Not running on Unix.");
450 TimeZoneInfo.ConvertTime (DateTime.Now, TimeZoneInfo.FindSystemTimeZoneById("Pacific/Auckland"));
454 [ExpectedException (typeof (ArgumentNullException))]
455 public void ConvertTime_DateTime_TimeZoneInfo_DestinationTimeZoneIsNull ()
457 TimeZoneInfo.ConvertTime (DateTime.Now, null);
461 public void ConvertTime_DateTime_TimeZoneInfo_DateTimeKindMatch ()
463 var sdt = new DateTime (2014, 1, 9, 23, 0, 0, DateTimeKind.Utc);
464 var ddt = TimeZoneInfo.ConvertTime (sdt, TimeZoneInfo.Utc);
465 Assert.AreEqual (ddt.Kind, sdt.Kind, "#1.1");
466 Assert.AreEqual (ddt.Kind, DateTimeKind.Utc, "#1.2");
468 sdt = new DateTime (2014, 1, 9, 23, 0, 0, DateTimeKind.Local);
469 ddt = TimeZoneInfo.ConvertTime (sdt, TimeZoneInfo.Local);
470 Assert.AreEqual (ddt.Kind, sdt.Kind, "#2.1");
471 Assert.AreEqual (ddt.Kind, DateTimeKind.Local, "#2.2");
473 sdt = new DateTime (2014, 1, 9, 23, 0, 0);
474 ddt = TimeZoneInfo.ConvertTime (sdt, TimeZoneInfo.Local);
475 Assert.AreEqual (ddt.Kind, sdt.Kind, "#3.1");
476 Assert.AreEqual (ddt.Kind, DateTimeKind.Unspecified, "#3.2");
480 [ExpectedException (typeof (ArgumentNullException))]
481 public void ConverTime_DateTime_TimeZoneInfo_TimeZoneInfo_SourceTimeZoneIsNull ()
483 TimeZoneInfo.ConvertTime (DateTime.Now, null, TimeZoneInfo.Local);
487 [ExpectedException (typeof (ArgumentNullException))]
488 public void ConverTime_DateTime_TimeZoneInfo_TimeZoneInfo_DestinationTimeZoneIsNull ()
490 TimeZoneInfo.ConvertTime (DateTime.Now, TimeZoneInfo.Utc, null);
493 [Test (Description="Fix for xambug https://bugzilla.xamarin.com/show_bug.cgi?id=17155")]
494 public void ConvertTime_AdjustmentRuleAfterNewYears ()
497 if (Environment.OSVersion.Platform == PlatformID.Unix)
498 tz = TimeZoneInfo.FindSystemTimeZoneById ("Pacific/Auckland"); // *nix
500 tz = TimeZoneInfo.FindSystemTimeZoneById ("New Zealand Standard Time"); // Windows
502 // DST start: 9/29/2013 2:00:00 AM
503 // DST end: 4/6/2014 3:00:00 AM
504 DateTime sdt = new DateTime (2014, 1, 9, 23, 0, 0, DateTimeKind.Utc);
505 DateTime ddt = TimeZoneInfo.ConvertTime (sdt, tz);
506 Assert.AreEqual (10, ddt.Day, "#1.1");
507 Assert.AreEqual (1, ddt.Month, "#1.2");
508 Assert.AreEqual (2014, ddt.Year, "#1.3");
509 Assert.AreEqual (12, ddt.Hour, "#1.4");
510 Assert.AreEqual (0, ddt.Minute, "#1.5");
511 Assert.AreEqual (0, ddt.Second, "#1.6");
513 // DST start: 9/29/2014 2:00:00 AM
514 // DST end: 4/6/2015 3:00:00 AM
515 sdt = new DateTime (2014, 6, 9, 23, 0, 0, DateTimeKind.Utc);
516 ddt = TimeZoneInfo.ConvertTime (sdt, tz);
517 Assert.AreEqual (10, ddt.Day, "#2.1");
518 Assert.AreEqual (6, ddt.Month, "#2.2");
519 Assert.AreEqual (2014, ddt.Year, "#2.3");
520 Assert.AreEqual (11, ddt.Hour, "#2.4");
521 Assert.AreEqual (0, ddt.Minute, "#2.5");
522 Assert.AreEqual (0, ddt.Second, "#2.6");
524 // DST start: 9/29/2014 2:00:00 AM
525 // DST end: 4/6/2015 3:00:00 AM
526 sdt = new DateTime (2014, 10, 9, 23, 0, 0, DateTimeKind.Utc);
527 ddt = TimeZoneInfo.ConvertTime (sdt, tz);
528 Assert.AreEqual (10, ddt.Day, "#3.1");
529 Assert.AreEqual (10, ddt.Month, "#3.2");
530 Assert.AreEqual (2014, ddt.Year, "#3.3");
531 Assert.AreEqual (12, ddt.Hour, "#3.4");
532 Assert.AreEqual (0, ddt.Minute, "#3.5");
533 Assert.AreEqual (0, ddt.Second, "#3.6");
536 [Test (Description="Fix the bug https://bugzilla.xamarin.com/show_bug.cgi?id=1849")]
537 public void ConvertTime_AjustmentConvertTimeWithSourceTimeZone () {
539 TimeZoneInfo easternTimeZone;
540 TimeZoneInfo pacificTimeZone;
542 if (Environment.OSVersion.Platform == PlatformID.Unix) {
544 easternTimeZone = TimeZoneInfo.FindSystemTimeZoneById ("US/Eastern");
545 pacificTimeZone = TimeZoneInfo.FindSystemTimeZoneById ("US/Pacific");
549 easternTimeZone = TimeZoneInfo.FindSystemTimeZoneById ("Eastern Standard Time");
550 pacificTimeZone = TimeZoneInfo.FindSystemTimeZoneById ("Pacific Standard Time");
553 DateTime lastMidnight = new DateTime (new DateTime (2012, 06, 13).Ticks, DateTimeKind.Unspecified);
554 DateTime lastMidnightAsEST = TimeZoneInfo.ConvertTime (lastMidnight, pacificTimeZone, easternTimeZone);
555 DateTime lastMidnightAsPST = TimeZoneInfo.ConvertTime (lastMidnightAsEST, easternTimeZone, pacificTimeZone);
557 // Last midnight in PST as EST should be 3AM
558 DateTime expectedDate = new DateTime (2012, 06, 13, 3, 0, 0);
560 Assert.AreEqual (expectedDate, lastMidnightAsEST);
561 Assert.AreEqual (lastMidnight, lastMidnightAsPST);
566 public class IsInvalidTimeTests
571 public void CreateTimeZones ()
573 TimeZoneInfo.TransitionTime start = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,1,0,0), 3, 5, DayOfWeek.Sunday);
574 TimeZoneInfo.TransitionTime end = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,2,0,0), 10, 5, DayOfWeek.Sunday);
575 TimeZoneInfo.AdjustmentRule rule = TimeZoneInfo.AdjustmentRule.CreateAdjustmentRule (DateTime.MinValue.Date, DateTime.MaxValue.Date, new TimeSpan (1,0,0), start, end);
576 london = TimeZoneInfo.CreateCustomTimeZone ("Europe/London", new TimeSpan (0), "Europe/London", "British Standard Time", "British Summer Time", new TimeZoneInfo.AdjustmentRule [] {rule});
581 public void UTCDate ()
583 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)) {
584 date = DateTime.SpecifyKind (date, DateTimeKind.Utc);
585 Assert.IsFalse (london.IsInvalidTime (date));
590 public void InvalidDates ()
592 Assert.IsFalse (london.IsInvalidTime (new DateTime (2007, 03, 25, 0, 59, 59)));
593 Assert.IsTrue (london.IsInvalidTime (new DateTime (2007, 03, 25, 1, 0, 0)));
594 Assert.IsTrue (london.IsInvalidTime (new DateTime (2007, 03, 25, 1, 59, 59)));
595 Assert.IsFalse (london.IsInvalidTime (new DateTime (2007, 03, 25, 2, 0, 0)));
600 public class IsAmbiguousTimeTests
605 public void CreateTimeZones ()
607 TimeZoneInfo.TransitionTime start = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,1,0,0), 3, 5, DayOfWeek.Sunday);
608 TimeZoneInfo.TransitionTime end = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,2,0,0), 10, 5, DayOfWeek.Sunday);
609 TimeZoneInfo.AdjustmentRule rule = TimeZoneInfo.AdjustmentRule.CreateAdjustmentRule (DateTime.MinValue.Date, DateTime.MaxValue.Date, new TimeSpan (1,0,0), start, end);
610 london = TimeZoneInfo.CreateCustomTimeZone ("Europe/London", new TimeSpan (0), "Europe/London", "British Standard Time", "British Summer Time", new TimeZoneInfo.AdjustmentRule [] {rule});
614 public void AmbiguousDates ()
616 if (Environment.OSVersion.Platform != PlatformID.Unix)
617 Assert.Ignore ("Not running on Unix.");
618 Assert.IsFalse (london.IsAmbiguousTime (new DateTime (2007, 10, 28, 1, 0, 0)));
619 Assert.IsTrue (london.IsAmbiguousTime (new DateTime (2007, 10, 28, 1, 0, 1)));
620 Assert.IsTrue (london.IsAmbiguousTime (new DateTime (2007, 10, 28, 2, 0, 0)));
621 Assert.IsFalse (london.IsAmbiguousTime (new DateTime (2007, 10, 28, 2, 0, 1)));
625 public void AmbiguousUTCDates ()
627 if (Environment.OSVersion.Platform != PlatformID.Unix)
628 Assert.Ignore ("Not running on Unix.");
629 Assert.IsFalse (london.IsAmbiguousTime (new DateTime (2007, 10, 28, 0, 0, 0, DateTimeKind.Utc)));
630 Assert.IsTrue (london.IsAmbiguousTime (new DateTime (2007, 10, 28, 0, 0, 1, DateTimeKind.Utc)));
631 Assert.IsTrue (london.IsAmbiguousTime (new DateTime (2007, 10, 28, 0, 59, 59, DateTimeKind.Utc)));
632 Assert.IsFalse (london.IsAmbiguousTime (new DateTime (2007, 10, 28, 1, 0, 0, DateTimeKind.Utc)));
637 public void AmbiguousInUTC ()
639 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)) {
640 Assert.IsFalse (TimeZoneInfo.Utc.IsAmbiguousTime (date));
647 public class GetSystemTimeZonesTests
650 public void NotEmpty ()
652 if (Environment.OSVersion.Platform != PlatformID.Unix)
653 Assert.Ignore ("Not running on Unix.");
654 global::System.Collections.ObjectModel.ReadOnlyCollection<TimeZoneInfo> systemTZ = TimeZoneInfo.GetSystemTimeZones ();
655 Assert.IsNotNull(systemTZ, "SystemTZ is null");
656 Assert.IsFalse (systemTZ.Count == 0, "SystemTZ is empty");
660 public void ContainsBrussels ()
662 if (Environment.OSVersion.Platform != PlatformID.Unix)
663 Assert.Ignore ("Not running on Unix.");
664 global::System.Collections.ObjectModel.ReadOnlyCollection<TimeZoneInfo> systemTZ = TimeZoneInfo.GetSystemTimeZones ();
665 foreach (TimeZoneInfo tz in systemTZ) {
666 if (tz.Id == "Europe/Brussels")
669 Assert.Fail ("Europe/Brussels not found in SystemTZ");
674 public class FindSystemTimeZoneByIdTests
677 [ExpectedException (typeof (ArgumentNullException))]
678 public void NullId ()
680 TimeZoneInfo.FindSystemTimeZoneById (null);
684 [ExpectedException (typeof (TimeZoneNotFoundException))]
685 public void NonSystemTimezone ()
687 if (Environment.OSVersion.Platform != PlatformID.Unix)
688 throw new TimeZoneNotFoundException ();
689 TimeZoneInfo.FindSystemTimeZoneById ("Neverland/The_Lagoon");
693 public void FindBrusselsTZ ()
695 if (Environment.OSVersion.Platform != PlatformID.Unix)
696 Assert.Ignore ("Not running on Unix.");
697 TimeZoneInfo brussels = TimeZoneInfo.FindSystemTimeZoneById ("Europe/Brussels");
698 Assert.IsNotNull (brussels);
702 public void OffsetIsCorrectInKinshasa ()
704 if (Environment.OSVersion.Platform != PlatformID.Unix)
705 Assert.Ignore ("Not running on Unix.");
706 TimeZoneInfo kin = TimeZoneInfo.FindSystemTimeZoneById ("Africa/Kinshasa");
707 Assert.AreEqual (new TimeSpan (1,0,0), kin.BaseUtcOffset, "BaseUtcOffset in Kinshasa is not +1h");
711 public void OffsetIsCorrectInBrussels ()
713 if (Environment.OSVersion.Platform != PlatformID.Unix)
714 Assert.Ignore ("Not running on Unix.");
715 TimeZoneInfo brussels = TimeZoneInfo.FindSystemTimeZoneById ("Europe/Brussels");
716 Assert.AreEqual (new TimeSpan (1,0,0), brussels.BaseUtcOffset, "BaseUtcOffset for Brussels is not +1h");
720 public void NoDSTInKinshasa ()
722 if (Environment.OSVersion.Platform != PlatformID.Unix)
723 Assert.Ignore ("Not running on Unix.");
724 TimeZoneInfo kin = TimeZoneInfo.FindSystemTimeZoneById ("Africa/Kinshasa");
725 Assert.IsFalse (kin.SupportsDaylightSavingTime);
729 public void BrusselsSupportsDST ()
731 if (Environment.OSVersion.Platform != PlatformID.Unix)
732 Assert.Ignore ("Not running on Unix.");
733 TimeZoneInfo brussels = TimeZoneInfo.FindSystemTimeZoneById ("Europe/Brussels");
734 Assert.IsTrue (brussels.SupportsDaylightSavingTime);
738 public void MelbourneSupportsDST ()
740 if (Environment.OSVersion.Platform != PlatformID.Unix)
741 Assert.Ignore ("Not running on Unix.");
742 TimeZoneInfo melbourne = TimeZoneInfo.FindSystemTimeZoneById ("Australia/Melbourne");
743 Assert.IsTrue (melbourne.SupportsDaylightSavingTime);
747 public void RomeAndVaticanSharesTime ()
749 if (Environment.OSVersion.Platform != PlatformID.Unix)
750 Assert.Ignore ("Not running on Unix.");
751 TimeZoneInfo rome = TimeZoneInfo.FindSystemTimeZoneById ("Europe/Rome");
752 TimeZoneInfo vatican = TimeZoneInfo.FindSystemTimeZoneById ("Europe/Vatican");
753 Assert.IsTrue (rome.HasSameRules (vatican));
757 public void FindSystemTimeZoneById_Local_Roundtrip ()
759 Assert.AreEqual (TimeZoneInfo.Local.Id, TimeZoneInfo.FindSystemTimeZoneById (TimeZoneInfo.Local.Id).Id);
763 public void Test326 ()
765 DateTime utc = DateTime.UtcNow;
766 DateTime local = TimeZoneInfo.ConvertTime (utc, TimeZoneInfo.Utc, TimeZoneInfo.FindSystemTimeZoneById (TimeZoneInfo.Local.Id));
767 Assert.AreEqual (local, utc + TimeZoneInfo.Local.GetUtcOffset (utc), "ConvertTime/Local");
772 public void BrusselsAdjustments ()
774 TimeZoneInfo.TransitionTime start = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,2,0,0), 3, 5, DayOfWeek.Sunday);
775 TimeZoneInfo.TransitionTime end = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,3,0,0), 10, 5, DayOfWeek.Sunday);
776 TimeZoneInfo.AdjustmentRule rule = TimeZoneInfo.AdjustmentRule.CreateAdjustmentRule (DateTime.MinValue.Date, DateTime.MaxValue.Date, new TimeSpan (1,0,0), start, end);
777 TimeZoneInfo brussels = TimeZoneInfo.CreateCustomTimeZone ("Europe/Brussels", new TimeSpan (1, 0, 0), "Europe/Brussels", "", "", new TimeZoneInfo.AdjustmentRule [] {rule});
779 TimeZoneInfo brussels_sys = TimeZoneInfo.FindSystemTimeZoneById ("Europe/Brussels");
781 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)) {
782 Assert.AreEqual (brussels.GetUtcOffset (date), brussels_sys.GetUtcOffset (date));
783 Assert.AreEqual (brussels.IsDaylightSavingTime (date), brussels_sys.IsDaylightSavingTime (date));
790 public class GetAmbiguousTimeOffsetsTests
793 [ExpectedException (typeof(ArgumentException))]
794 public void DateIsNotAmbiguous ()
796 if (Environment.OSVersion.Platform != PlatformID.Unix)
797 throw new ArgumentException ();
798 TimeZoneInfo brussels = TimeZoneInfo.FindSystemTimeZoneById ("Europe/Brussels");
799 DateTime date = new DateTime (2007, 05, 11, 11, 40, 00);
800 brussels.GetAmbiguousTimeOffsets (date);
804 public void AmbiguousOffsets ()
806 if (Environment.OSVersion.Platform != PlatformID.Unix)
807 Assert.Ignore ("Not running on Unix.");
808 TimeZoneInfo brussels = TimeZoneInfo.FindSystemTimeZoneById ("Europe/Brussels");
809 DateTime date = new DateTime (2007, 10, 28, 2, 30, 00);
810 Assert.IsTrue (brussels.IsAmbiguousTime (date));
811 Assert.AreEqual (2, brussels.GetAmbiguousTimeOffsets (date).Length);
812 Assert.AreEqual (new TimeSpan[] {new TimeSpan (1, 0, 0), new TimeSpan (2, 0, 0)}, brussels.GetAmbiguousTimeOffsets (date));
817 public class HasSameRulesTests
820 public void NullAdjustments () //bnc #391011
822 TimeZoneInfo utc = TimeZoneInfo.Utc;
823 TimeZoneInfo custom = TimeZoneInfo.CreateCustomTimeZone ("Custom", new TimeSpan (0), "Custom", "Custom");
824 Assert.IsTrue (utc.HasSameRules (custom));
829 public class SerializationTests
832 public void Serialization_Deserialization ()
834 TimeZoneInfo.TransitionTime start = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,1,0,0), 3, 5, DayOfWeek.Sunday);
835 TimeZoneInfo.TransitionTime end = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,2,0,0), 10, 5, DayOfWeek.Sunday);
836 TimeZoneInfo.AdjustmentRule rule = TimeZoneInfo.AdjustmentRule.CreateAdjustmentRule (DateTime.MinValue.Date, DateTime.MaxValue.Date, new TimeSpan (1,0,0), start, end);
837 TimeZoneInfo london = TimeZoneInfo.CreateCustomTimeZone ("Europe/London", new TimeSpan (0), "Europe/London", "British Standard Time", "British Summer Time", new TimeZoneInfo.AdjustmentRule [] {rule});
838 MemoryStream stream = new MemoryStream ();
839 BinaryFormatter formatter = new BinaryFormatter ();
840 formatter.Serialize (stream, london);
842 TimeZoneInfo deserialized = (TimeZoneInfo) formatter.Deserialize (stream);
845 Assert.IsTrue (london.Equals (deserialized));
850 public class MultipleDaylightSavingTimeTests {
851 private TimeZoneInfo cairo;
852 private DateTime dst1Start;
853 private DateTime dst1End;
854 private DateTime dst2Start;
855 private DateTime dst2End;
857 private TimeSpan baseUtcOffset;
858 private TimeSpan dstUtcOffset;
859 private TimeSpan dstOffset;
862 public void CreateTimeZones ()
865 From 1/1/2014 12:00:00 AM to 6/30/2014 12:00:00 AM
867 Begins at 12:00 AM on 16 May
868 Ends at 1:00 AM on 29 June
869 From 7/1/2014 12:00:00 AM to 12/31/2014 12:00:00 AM
871 Begins at 12:00 AM on 29 July
872 Ends at 12:00 AM on 26 September
874 dst1Start = new DateTime (2014, 5, 16);
875 dst1End = new DateTime (2014, 6, 29);
876 dst2Start = new DateTime (2014, 7, 29);
877 dst2End = new DateTime (2014, 9, 26);
879 baseUtcOffset = new TimeSpan (2, 0, 0);
880 dstUtcOffset = new TimeSpan (3, 0, 0);
881 dstOffset = dstUtcOffset - baseUtcOffset;
883 var rule1 = TimeZoneInfo.AdjustmentRule.CreateAdjustmentRule (
884 new DateTime (2014, 1, 1), new DateTime (2014, 6, 30), dstOffset,
885 CreateFixedDateRule (dst1Start), CreateFixedDateRule (dst1End));
887 var rule2 = TimeZoneInfo.AdjustmentRule.CreateAdjustmentRule (
888 new DateTime (2014, 7, 1), new DateTime (2014, 12, 31), dstOffset,
889 CreateFixedDateRule (dst2Start), CreateFixedDateRule (dst2End));
891 cairo = TimeZoneInfo.CreateCustomTimeZone ("Africa/Cairo", baseUtcOffset, "Africa/Cairo", "EET", "EEST",
892 new [] {rule1, rule2});
895 private static TimeZoneInfo.TransitionTime CreateFixedDateRule (DateTime dateTime)
897 var time = new DateTime (dateTime.Ticks - dateTime.Date.Ticks);
898 return TimeZoneInfo.TransitionTime.CreateFixedDateRule (time, dateTime.Month, dateTime.Day);
902 public void GetUtcOffset_FromUTC ()
904 var d = dst1Start.Add (-baseUtcOffset);
905 d = DateTime.SpecifyKind (d, DateTimeKind.Utc);
906 Assert.AreEqual(baseUtcOffset, cairo.GetUtcOffset (d.Add (new TimeSpan(0,0,0,-1))));
907 Assert.AreEqual(dstUtcOffset, cairo.GetUtcOffset (d));
908 Assert.AreEqual(dstUtcOffset, cairo.GetUtcOffset (d.Add (new TimeSpan(0,0,0, 1))));
910 d = dst1End.Add (-baseUtcOffset-dstOffset);
911 d = DateTime.SpecifyKind (d, DateTimeKind.Utc);
912 Assert.AreEqual(dstUtcOffset, cairo.GetUtcOffset (d.Add (new TimeSpan(0,0,0,-1))));
913 Assert.AreEqual(baseUtcOffset, cairo.GetUtcOffset (d));
914 Assert.AreEqual(baseUtcOffset, cairo.GetUtcOffset (d.Add (new TimeSpan(0,0,0, 1))));
916 d = dst2Start.Add (-baseUtcOffset);
917 d = DateTime.SpecifyKind (d, DateTimeKind.Utc);
918 Assert.AreEqual(baseUtcOffset, cairo.GetUtcOffset (d.Add (new TimeSpan(0,0,0,-1))));
919 Assert.AreEqual(dstUtcOffset, cairo.GetUtcOffset (d));
920 Assert.AreEqual(dstUtcOffset, cairo.GetUtcOffset (d.Add (new TimeSpan(0,0,0, 1))));
922 d = dst2End.Add (-baseUtcOffset-dstOffset);
923 d = DateTime.SpecifyKind (d, DateTimeKind.Utc);
924 Assert.AreEqual(dstUtcOffset, cairo.GetUtcOffset (d.Add (new TimeSpan(0,0,0,-1))));
925 Assert.AreEqual(baseUtcOffset, cairo.GetUtcOffset (d));
926 Assert.AreEqual(baseUtcOffset, cairo.GetUtcOffset (d.Add (new TimeSpan(0,0,0, 1))));
930 public void GetUtcOffset_FromLocal ()
932 var d = dst1Start.Add (-baseUtcOffset);
933 d = DateTime.SpecifyKind (d, DateTimeKind.Utc);
934 d = d.ToLocalTime ();
935 Assert.AreEqual(baseUtcOffset, cairo.GetUtcOffset (d.Add (new TimeSpan(0,0,0,-1))));
936 Assert.AreEqual(dstUtcOffset, cairo.GetUtcOffset (d));
937 Assert.AreEqual(dstUtcOffset, cairo.GetUtcOffset (d.Add (new TimeSpan(0,0,0, 1))));
939 d = dst1End.Add (-baseUtcOffset-dstOffset);
940 d = DateTime.SpecifyKind (d, DateTimeKind.Utc);
941 d = d.ToLocalTime ();
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))));
946 d = dst2Start.Add (-baseUtcOffset);
947 d = DateTime.SpecifyKind (d, DateTimeKind.Utc);
948 d = d.ToLocalTime ();
949 Assert.AreEqual(baseUtcOffset, cairo.GetUtcOffset (d.Add (new TimeSpan(0,0,0,-1))));
950 Assert.AreEqual(dstUtcOffset, cairo.GetUtcOffset (d));
951 Assert.AreEqual(dstUtcOffset, cairo.GetUtcOffset (d.Add (new TimeSpan(0,0,0, 1))));
953 d = dst2End.Add (-baseUtcOffset-dstOffset);
954 d = DateTime.SpecifyKind (d, DateTimeKind.Utc);
955 d = d.ToLocalTime ();
956 Assert.AreEqual(dstUtcOffset, cairo.GetUtcOffset (d.Add (new TimeSpan(0,0,0,-1))));
957 Assert.AreEqual(baseUtcOffset, cairo.GetUtcOffset (d));
958 Assert.AreEqual(baseUtcOffset, cairo.GetUtcOffset (d.Add (new TimeSpan(0,0,0, 1))));
962 public void GetUtcOffset_FromUnspecified ()
964 var d = dst1Start.Add (dstOffset);
965 Assert.AreEqual(baseUtcOffset, cairo.GetUtcOffset (d.Add (new TimeSpan(0,0,0,-1))));
966 Assert.AreEqual(dstUtcOffset, cairo.GetUtcOffset (d));
967 Assert.AreEqual(dstUtcOffset, cairo.GetUtcOffset (d.Add (new TimeSpan(0,0,0, 1))));
969 d = dst1End.Add (-dstOffset);
970 Assert.AreEqual(dstUtcOffset, cairo.GetUtcOffset (d.Add (new TimeSpan(0,0,0,-1))));
971 Assert.AreEqual(baseUtcOffset, cairo.GetUtcOffset (d));
972 Assert.AreEqual(baseUtcOffset, cairo.GetUtcOffset (d.Add (new TimeSpan(0,0,0, 1))));
974 d = dst2Start.Add (dstOffset);
975 Assert.AreEqual(baseUtcOffset, cairo.GetUtcOffset (d.Add (new TimeSpan(0,0,0,-1))));
976 Assert.AreEqual(dstUtcOffset, cairo.GetUtcOffset (d));
977 Assert.AreEqual(dstUtcOffset, cairo.GetUtcOffset (d.Add (new TimeSpan(0,0,0, 1))));
979 d = dst2End.Add (-dstOffset);
980 Assert.AreEqual(dstUtcOffset, cairo.GetUtcOffset (d.Add (new TimeSpan(0,0,0,-1))));
981 Assert.AreEqual(baseUtcOffset, cairo.GetUtcOffset (d));
982 Assert.AreEqual(baseUtcOffset, cairo.GetUtcOffset (d.Add (new TimeSpan(0,0,0, 1))));