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.InteropServices;
32 using System.Runtime.Serialization.Formatters.Binary;
33 using System.Collections;
34 using System.Reflection;
35 using System.Globalization;
37 using NUnit.Framework;
38 namespace MonoTests.System
40 public class TimeZoneInfoTest
43 public class PropertiesTests
46 public void GetLocal ()
48 if (Environment.OSVersion.Platform != PlatformID.Unix)
49 Assert.Ignore ("Not running on Unix.");
50 TimeZoneInfo local = TimeZoneInfo.Local;
51 Assert.IsNotNull (local);
56 private static extern int readlink (string path, byte[] buffer, int buflen);
58 [Test] // Covers #24958
59 public void LocalId ()
61 byte[] buf = new byte [512];
63 var path = "/etc/localtime";
65 var ret = readlink (path, buf, buf.Length);
67 return; // path is not a symbolic link, nothing to test
68 } catch (DllNotFoundException e) {
72 Assert.IsTrue (TimeZoneInfo.Local.Id != "Local", "Local timezone id should not be \"Local\"");
77 public class CreateCustomTimezoneTests
80 [ExpectedException (typeof (ArgumentNullException))]
81 public void IdIsNullException ()
83 TimeZoneInfo.CreateCustomTimeZone (null, new TimeSpan (0), null, null);
87 [ExpectedException (typeof (ArgumentException))]
88 public void IdIsEmptyString ()
90 TimeZoneInfo.CreateCustomTimeZone ("", new TimeSpan (0), null, null);
94 [ExpectedException (typeof (ArgumentException))]
95 public void OffsetIsNotMinutes ()
97 TimeZoneInfo.CreateCustomTimeZone ("mytimezone", new TimeSpan (0, 0, 55), null, null);
101 [ExpectedException (typeof (ArgumentOutOfRangeException))]
102 public void OffsetTooBig ()
104 TimeZoneInfo.CreateCustomTimeZone ("mytimezone", new TimeSpan (14, 1, 0), null, null);
108 [ExpectedException (typeof (ArgumentOutOfRangeException))]
109 public void OffsetTooSmall ()
111 TimeZoneInfo.CreateCustomTimeZone ("mytimezone", - new TimeSpan (14, 1, 0), null, null);
116 [ExpectedException (typeof (ArgumentException))]
117 public void IdLongerThan32 ()
119 TimeZoneInfo.CreateCustomTimeZone ("12345678901234567890123456789012345", new TimeSpan (0), null, null);
124 [ExpectedException (typeof (InvalidTimeZoneException))]
125 public void AdjustmentRulesOverlap ()
127 TimeZoneInfo.TransitionTime s1 = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,4,0,0), 3, 2, DayOfWeek.Sunday);
128 TimeZoneInfo.TransitionTime e1 = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,4,0,0), 10, 2, DayOfWeek.Sunday);
129 TimeZoneInfo.AdjustmentRule r1 = TimeZoneInfo.AdjustmentRule.CreateAdjustmentRule (new DateTime (2000,1,1), new DateTime (2005,1,1), new TimeSpan (1,0,0), s1, e1);
130 TimeZoneInfo.TransitionTime s2 = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,4,0,0), 2, 2, DayOfWeek.Sunday);
131 TimeZoneInfo.TransitionTime e2 = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,4,0,0), 11, 2, DayOfWeek.Sunday);
132 TimeZoneInfo.AdjustmentRule r2 = TimeZoneInfo.AdjustmentRule.CreateAdjustmentRule (new DateTime (2004,1,1), new DateTime (2007,1,1), new TimeSpan (1,0,0), s2, e2);
133 TimeZoneInfo.CreateCustomTimeZone ("mytimezone", new TimeSpan (6,0,0),null,null,null,new TimeZoneInfo.AdjustmentRule[] {r1, r2});
137 [ExpectedException (typeof (InvalidTimeZoneException))]
138 public void RulesNotOrdered ()
140 TimeZoneInfo.TransitionTime s1 = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,4,0,0), 3, 2, DayOfWeek.Sunday);
141 TimeZoneInfo.TransitionTime e1 = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,4,0,0), 10, 2, DayOfWeek.Sunday);
142 TimeZoneInfo.AdjustmentRule r1 = TimeZoneInfo.AdjustmentRule.CreateAdjustmentRule (new DateTime (2000,1,1), new DateTime (2005,1,1), new TimeSpan (1,0,0), s1, e1);
143 TimeZoneInfo.TransitionTime s2 = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,4,0,0), 2, 2, DayOfWeek.Sunday);
144 TimeZoneInfo.TransitionTime e2 = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,4,0,0), 11, 2, DayOfWeek.Sunday);
145 TimeZoneInfo.AdjustmentRule r2 = TimeZoneInfo.AdjustmentRule.CreateAdjustmentRule (new DateTime (2006,1,1), new DateTime (2007,1,1), new TimeSpan (1,0,0), s2, e2);
146 TimeZoneInfo.CreateCustomTimeZone ("mytimezone", new TimeSpan (6,0,0),null,null,null,new TimeZoneInfo.AdjustmentRule[] {r2, r1});
150 [ExpectedException (typeof (InvalidTimeZoneException))]
151 public void OffsetOutOfRange ()
153 TimeZoneInfo.TransitionTime startTransition = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,4,0,0), 3, 2, DayOfWeek.Sunday);
154 TimeZoneInfo.TransitionTime endTransition = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,4,0,0), 10, 2, DayOfWeek.Sunday);
155 TimeZoneInfo.AdjustmentRule rule = TimeZoneInfo.AdjustmentRule.CreateAdjustmentRule (new DateTime (2000,1,1), new DateTime (2005,1,1), new TimeSpan (3,0,0), startTransition, endTransition);
156 TimeZoneInfo.CreateCustomTimeZone ("mytimezone", new TimeSpan (12,0,0),null,null,null,new TimeZoneInfo.AdjustmentRule[] {rule});
160 [ExpectedException (typeof (InvalidTimeZoneException))]
161 public void NullRule ()
163 TimeZoneInfo.CreateCustomTimeZone ("mytimezone", new TimeSpan (12,0,0),null,null,null,new TimeZoneInfo.AdjustmentRule[] {null});
167 [ExpectedException (typeof (InvalidTimeZoneException))]
168 public void MultiplesRulesForDate ()
170 TimeZoneInfo.TransitionTime s1 = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,4,0,0), 3, 2, DayOfWeek.Sunday);
171 TimeZoneInfo.TransitionTime e1 = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,4,0,0), 10, 2, DayOfWeek.Sunday);
172 TimeZoneInfo.AdjustmentRule r1 = TimeZoneInfo.AdjustmentRule.CreateAdjustmentRule (new DateTime (2000,1,1), new DateTime (2005,1,1), new TimeSpan (1,0,0), s1, e1);
173 TimeZoneInfo.TransitionTime s2 = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,4,0,0), 2, 2, DayOfWeek.Sunday);
174 TimeZoneInfo.TransitionTime e2 = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,4,0,0), 11, 2, DayOfWeek.Sunday);
175 TimeZoneInfo.AdjustmentRule r2 = TimeZoneInfo.AdjustmentRule.CreateAdjustmentRule (new DateTime (2005,1,1), new DateTime (2007,1,1), new TimeSpan (1,0,0), s2, e2);
176 TimeZoneInfo.CreateCustomTimeZone ("mytimezone", new TimeSpan (6,0,0),null,null,null,new TimeZoneInfo.AdjustmentRule[] {r1, r2});
180 public void SupportsDaylightSavingTime_NonEmptyAdjustmentRule ()
182 TimeZoneInfo.TransitionTime s1 = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,4,0,0), 3, 2, DayOfWeek.Sunday);
183 TimeZoneInfo.TransitionTime e1 = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,4,0,0), 10, 2, DayOfWeek.Sunday);
184 TimeZoneInfo.AdjustmentRule r1 = TimeZoneInfo.AdjustmentRule.CreateAdjustmentRule (new DateTime (2000,1,1), new DateTime (2005,1,1), new TimeSpan (1,0,0), s1, e1);
185 TimeZoneInfo tz = TimeZoneInfo.CreateCustomTimeZone ("mytimezone", new TimeSpan (6,0,0),null,null,null,new TimeZoneInfo.AdjustmentRule[] {r1});
186 Assert.IsTrue (tz.SupportsDaylightSavingTime);
190 public void SupportsDaylightSavingTime_EmptyAdjustmentRule ()
192 TimeZoneInfo tz = TimeZoneInfo.CreateCustomTimeZone ("mytimezone", new TimeSpan (6,0,0),null,null,null,null);
193 Assert.IsFalse (tz.SupportsDaylightSavingTime);
197 public void SupportsDaylightSavingTime_NonEmptyAdjustmentRule_DisableDaylightSavingTime ()
199 TimeZoneInfo.TransitionTime s1 = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,4,0,0), 3, 2, DayOfWeek.Sunday);
200 TimeZoneInfo.TransitionTime e1 = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,4,0,0), 10, 2, DayOfWeek.Sunday);
201 TimeZoneInfo.AdjustmentRule r1 = TimeZoneInfo.AdjustmentRule.CreateAdjustmentRule (new DateTime (2000,1,1), new DateTime (2005,1,1), new TimeSpan (1,0,0), s1, e1);
202 TimeZoneInfo tz = TimeZoneInfo.CreateCustomTimeZone ("mytimezone", new TimeSpan (6,0,0),null,null,null,new TimeZoneInfo.AdjustmentRule[] {r1}, true);
203 Assert.IsFalse (tz.SupportsDaylightSavingTime);
207 public void SupportsDaylightSavingTime_EmptyAdjustmentRule_DisableDaylightSavingTime ()
209 TimeZoneInfo tz = TimeZoneInfo.CreateCustomTimeZone ("mytimezone", new TimeSpan (6,0,0),null,null,null,null,true);
210 Assert.IsFalse (tz.SupportsDaylightSavingTime);
215 public class IsDaylightSavingTimeTests
220 public void CreateTimeZones ()
222 TimeZoneInfo.TransitionTime start = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,1,0,0), 3, 5, DayOfWeek.Sunday);
223 TimeZoneInfo.TransitionTime end = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,2,0,0), 10, 5, DayOfWeek.Sunday);
224 TimeZoneInfo.AdjustmentRule rule = TimeZoneInfo.AdjustmentRule.CreateAdjustmentRule (DateTime.MinValue.Date, DateTime.MaxValue.Date, new TimeSpan (1,0,0), start, end);
225 london = TimeZoneInfo.CreateCustomTimeZone ("Europe/London", new TimeSpan (0), "Europe/London", "British Standard Time", "British Summer Time", new TimeZoneInfo.AdjustmentRule [] {rule});
229 public void NoDSTInUTC ()
231 DateTime june01 = new DateTime (2007, 06, 01);
232 Assert.IsFalse (TimeZoneInfo.Utc.IsDaylightSavingTime (june01));
236 public void DSTInLondon ()
238 if (Environment.OSVersion.Platform != PlatformID.Unix)
239 Assert.Ignore ("Not running on Unix.");
240 DateTime june01 = new DateTime (2007, 06, 01);
241 DateTime xmas = new DateTime (2007, 12, 25);
242 Assert.IsTrue (london.IsDaylightSavingTime (june01), "June 01 is DST in London");
243 Assert.IsFalse (london.IsDaylightSavingTime (xmas), "Xmas is not DST in London");
247 public void DSTTransisions ()
249 if (Environment.OSVersion.Platform != PlatformID.Unix)
250 Assert.Ignore ("Not running on Unix.");
251 DateTime beforeDST = new DateTime (2007, 03, 25, 0, 59, 59, DateTimeKind.Unspecified);
252 DateTime startDST = new DateTime (2007, 03, 25, 2, 0, 0, DateTimeKind.Unspecified);
253 DateTime endDST = new DateTime (2007, 10, 28, 1, 59, 59, DateTimeKind.Unspecified);
254 DateTime afterDST = new DateTime (2007, 10, 28, 2, 0, 0, DateTimeKind.Unspecified);
255 Assert.IsFalse (london.IsDaylightSavingTime (beforeDST), "Just before DST");
256 Assert.IsTrue (london.IsDaylightSavingTime (startDST), "the first seconds of DST");
257 Assert.IsFalse (london.IsDaylightSavingTime (endDST), "The last seconds of DST");
258 Assert.IsFalse (london.IsDaylightSavingTime (afterDST), "Just after DST");
262 public void DSTTransisionsUTC ()
264 DateTime beforeDST = new DateTime (2007, 03, 25, 0, 59, 59, DateTimeKind.Utc);
265 DateTime startDST = new DateTime (2007, 03, 25, 1, 0, 0, DateTimeKind.Utc);
266 DateTime endDST = new DateTime (2007, 10, 28, 0, 59, 59, DateTimeKind.Utc);
267 DateTime afterDST = new DateTime (2007, 10, 28, 1, 0, 0, DateTimeKind.Utc);
268 Assert.IsFalse (london.IsDaylightSavingTime (beforeDST), "Just before DST");
269 Assert.IsTrue (london.IsDaylightSavingTime (startDST), "the first seconds of DST");
270 Assert.IsTrue (london.IsDaylightSavingTime (endDST), "The last seconds of DST");
271 Assert.IsFalse (london.IsDaylightSavingTime (afterDST), "Just after DST");
276 public void MatchTimeZoneBehavior ()
278 TimeZone tzone = TimeZone.CurrentTimeZone;
279 TimeZoneInfo local = TimeZoneInfo.Local;
280 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)) {
281 date = DateTime.SpecifyKind (date, DateTimeKind.Local);
282 if (local.IsInvalidTime (date))
284 Assert.IsTrue (tzone.IsDaylightSavingTime (date) == local.IsDaylightSavingTime (date));
288 [Test (Description="Description xambug #17155")]
289 public void AdjustmentRuleAfterNewYears ()
292 if (Environment.OSVersion.Platform == PlatformID.Unix)
293 tz = TimeZoneInfo.FindSystemTimeZoneById ("Pacific/Auckland"); // *nix
295 tz = TimeZoneInfo.FindSystemTimeZoneById ("New Zealand Standard Time"); // Windows
297 // DST start: 9/29/2013 2:00:00 AM
298 // DST end: 4/6/2014 3:00:00 AM
299 DateTime dt = new DateTime (2014, 1, 9, 23, 0, 0, DateTimeKind.Utc);
300 Assert.IsTrue (tz.IsDaylightSavingTime (dt), "#1.1");
302 // DST start: 9/29/2014 2:00:00 AM
303 // DST end: 4/6/2015 3:00:00 AM
304 dt = new DateTime (2014, 6, 9, 23, 0, 0, DateTimeKind.Utc);
305 Assert.IsFalse (tz.IsDaylightSavingTime (dt), "#2.1");
307 // DST start: 9/29/2014 2:00:00 AM
308 // DST end: 4/6/2015 3:00:00 AM
309 dt = new DateTime (2014, 10, 9, 23, 0, 0, DateTimeKind.Utc);
310 Assert.IsTrue (tz.IsDaylightSavingTime (dt), "#3.1");
313 [Test] //Covers #26008
314 public void DSTWithFloatingDateRule ()
316 // Construct a custom time zone where daylight saving time starts on the
317 // 2nd Sunday in March.
318 var transitionToDaylight = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1, 1, 1, 2, 0, 0), 3, 2, DayOfWeek.Sunday);
319 var transitionToStandard = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1, 1, 1, 2, 0, 0), 11, 1, DayOfWeek.Sunday);
320 var adjustment = TimeZoneInfo.AdjustmentRule.CreateAdjustmentRule (DateTime.MinValue.Date, DateTime.MaxValue.Date, new TimeSpan (1, 0, 0), transitionToDaylight, transitionToStandard);
321 var timeZone = TimeZoneInfo.CreateCustomTimeZone ("BugCheck", new TimeSpan (-8, 0, 0), "Testing", "Testing Standard", "Testing Daylight", new TimeZoneInfo.AdjustmentRule [] { adjustment });
322 // See if March 7, 2014 is listed as being during daylight saving time.
323 // If it is DST, then the runtime has the bug that we are looking for.
324 Assert.IsFalse (timeZone.IsDaylightSavingTime (new DateTime (2014, 3, 7, 12, 0, 0, DateTimeKind.Unspecified)));
327 [Test] //Covers #25050
328 public void TestAthensDST ()
330 TimeZoneInfo tzi = TimeZoneInfo.FindSystemTimeZoneById ("Europe/Athens");
331 var date = new DateTime (2014, 3, 30 , 2, 0, 0);
332 Assert.IsFalse (tzi.IsDaylightSavingTime (date));
333 Assert.AreEqual (new TimeSpan (2,0,0), tzi.GetUtcOffset (date));
338 public class ConvertTimeTests
343 public void CreateTimeZones ()
345 TimeZoneInfo.TransitionTime start = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,1,0,0), 3, 5, DayOfWeek.Sunday);
346 TimeZoneInfo.TransitionTime end = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,2,0,0), 10, 5, DayOfWeek.Sunday);
347 TimeZoneInfo.AdjustmentRule rule = TimeZoneInfo.AdjustmentRule.CreateAdjustmentRule (DateTime.MinValue.Date, DateTime.MaxValue.Date, new TimeSpan (1,0,0), start, end);
348 london = TimeZoneInfo.CreateCustomTimeZone ("Europe/London", new TimeSpan (0), "Europe/London", "British Standard Time", "British Summer Time", new TimeZoneInfo.AdjustmentRule [] {rule});
352 [ExpectedException (typeof (ArgumentException))]
353 public void ConvertFromUtc_KindIsLocalException ()
355 if (Environment.OSVersion.Platform != PlatformID.Unix)
356 throw new ArgumentException ();
357 TimeZoneInfo.ConvertTimeFromUtc (new DateTime (2007, 5, 3, 11, 8, 0, DateTimeKind.Local), TimeZoneInfo.Local);
361 [ExpectedException (typeof (ArgumentNullException))]
362 public void ConvertFromUtc_DestinationTimeZoneIsNullException ()
364 TimeZoneInfo.ConvertTimeFromUtc (new DateTime (2007, 5, 3, 11, 8, 0), null);
368 public void ConvertFromUtc_DestinationIsUTC ()
370 DateTime now = DateTime.UtcNow;
371 DateTime converted = TimeZoneInfo.ConvertTimeFromUtc (now, TimeZoneInfo.Utc);
372 Assert.AreEqual (now, converted);
376 public void ConvertFromUTC_ConvertInWinter ()
378 if (Environment.OSVersion.Platform != PlatformID.Unix)
379 Assert.Ignore ("Not running on Unix.");
380 DateTime utc = new DateTime (2007, 12, 25, 12, 0, 0);
381 DateTime converted = TimeZoneInfo.ConvertTimeFromUtc (utc, london);
382 Assert.AreEqual (utc, converted);
386 public void ConvertFromUtc_ConvertInSummer ()
388 if (Environment.OSVersion.Platform != PlatformID.Unix)
389 Assert.Ignore ("Not running on Unix.");
390 DateTime utc = new DateTime (2007, 06, 01, 12, 0, 0);
391 DateTime converted = TimeZoneInfo.ConvertTimeFromUtc (utc, london);
392 Assert.AreEqual (utc + new TimeSpan (1,0,0), converted);
396 public void ConvertToUTC_KindIsUtc ()
398 DateTime now = DateTime.UtcNow;
399 Assert.AreEqual (now.Kind, DateTimeKind.Utc);
400 DateTime converted = TimeZoneInfo.ConvertTimeToUtc (now);
401 Assert.AreEqual (now, converted);
405 [ExpectedException (typeof (ArgumentException))]
406 public void ConvertToUTC_KindIsUTCButSourceIsNot ()
408 TimeZoneInfo.ConvertTimeToUtc (new DateTime (2007, 5, 3, 12, 8, 0, DateTimeKind.Utc), london);
412 [ExpectedException (typeof (ArgumentException))]
413 public void ConvertToUTC_KindIsLocalButSourceIsNot ()
415 if (Environment.OSVersion.Platform != PlatformID.Unix)
416 throw new ArgumentException ();
417 TimeZoneInfo.ConvertTimeToUtc (new DateTime (2007, 5, 3, 12, 8, 0, DateTimeKind.Local), london);
421 [ExpectedException (typeof (ArgumentException))]
422 public void ConvertToUTC_InvalidDate ()
424 TimeZoneInfo.ConvertTimeToUtc (new DateTime (2007, 3, 25, 1, 30, 0), london);
428 [ExpectedException (typeof (ArgumentNullException))]
429 public void ConvertToUTC_SourceIsNull ()
431 TimeZoneInfo.ConvertTimeToUtc (new DateTime (2007, 5, 3, 12, 16, 0), null);
436 public void ConvertToUtc_MatchDateTimeBehavior ()
438 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)) {
439 Assert.AreEqual (TimeZoneInfo.ConvertTimeToUtc (date), date.ToUniversalTime ());
445 public void ConvertFromToUtc ()
447 if (Environment.OSVersion.Platform != PlatformID.Unix)
448 Assert.Ignore ("Not running on Unix.");
449 DateTime utc = DateTime.UtcNow;
450 Assert.AreEqual (utc.Kind, DateTimeKind.Utc);
451 DateTime converted = TimeZoneInfo.ConvertTimeFromUtc (utc, london);
452 Assert.AreEqual (converted.Kind, DateTimeKind.Unspecified);
453 DateTime back = TimeZoneInfo.ConvertTimeToUtc (converted, london);
454 Assert.AreEqual (back.Kind, DateTimeKind.Utc);
455 Assert.AreEqual (utc, back);
460 public void ConvertTimeToUtc_Overflow ()
462 var res = TimeZoneInfo.ConvertTimeToUtc (new DateTime (0));
463 Assert.AreEqual (res.Kind, DateTimeKind.Utc, "#1");
465 res = TimeZoneInfo.ConvertTimeToUtc (DateTime.MaxValue);
466 Assert.AreEqual (res.Kind, DateTimeKind.Utc, "#2");
470 public void ConvertFromToLocal ()
472 DateTime utc = DateTime.UtcNow;
473 Assert.AreEqual(utc.Kind, DateTimeKind.Utc);
474 DateTime converted = TimeZoneInfo.ConvertTimeFromUtc(utc, TimeZoneInfo.Local);
475 Assert.AreEqual(DateTimeKind.Local, converted.Kind);
476 DateTime back = TimeZoneInfo.ConvertTimeToUtc(converted, TimeZoneInfo.Local);
477 Assert.AreEqual(back.Kind, DateTimeKind.Utc);
478 Assert.AreEqual(utc, back);
482 public void ConvertToTimeZone ()
484 if (Environment.OSVersion.Platform != PlatformID.Unix)
485 Assert.Ignore ("Not running on Unix.");
487 TimeZoneInfo.ConvertTime (DateTime.Now, TimeZoneInfo.FindSystemTimeZoneById("Pacific/Auckland"));
491 [ExpectedException (typeof (ArgumentNullException))]
492 public void ConvertTime_DateTime_TimeZoneInfo_DestinationTimeZoneIsNull ()
494 TimeZoneInfo.ConvertTime (DateTime.Now, null);
498 public void ConvertTime_DateTime_TimeZoneInfo_DateTimeKindMatch ()
500 var sdt = new DateTime (2014, 1, 9, 23, 0, 0, DateTimeKind.Utc);
501 var ddt = TimeZoneInfo.ConvertTime (sdt, TimeZoneInfo.Utc);
502 Assert.AreEqual (ddt.Kind, sdt.Kind, "#1.1");
503 Assert.AreEqual (ddt.Kind, DateTimeKind.Utc, "#1.2");
505 sdt = new DateTime (2014, 1, 9, 23, 0, 0, DateTimeKind.Local);
506 ddt = TimeZoneInfo.ConvertTime (sdt, TimeZoneInfo.Local);
507 Assert.AreEqual (ddt.Kind, sdt.Kind, "#2.1");
508 Assert.AreEqual (ddt.Kind, DateTimeKind.Local, "#2.2");
510 sdt = new DateTime (2014, 1, 9, 23, 0, 0);
511 ddt = TimeZoneInfo.ConvertTime (sdt, TimeZoneInfo.Local);
512 Assert.AreEqual (ddt.Kind, sdt.Kind, "#3.1");
513 Assert.AreEqual (ddt.Kind, DateTimeKind.Unspecified, "#3.2");
517 [ExpectedException (typeof (ArgumentNullException))]
518 public void ConverTime_DateTime_TimeZoneInfo_TimeZoneInfo_SourceTimeZoneIsNull ()
520 TimeZoneInfo.ConvertTime (DateTime.Now, null, TimeZoneInfo.Local);
524 [ExpectedException (typeof (ArgumentNullException))]
525 public void ConverTime_DateTime_TimeZoneInfo_TimeZoneInfo_DestinationTimeZoneIsNull ()
527 TimeZoneInfo.ConvertTime (DateTime.Now, TimeZoneInfo.Utc, null);
530 [Test (Description="Fix for xambug https://bugzilla.xamarin.com/show_bug.cgi?id=17155")]
531 public void ConvertTime_AdjustmentRuleAfterNewYears ()
534 if (Environment.OSVersion.Platform == PlatformID.Unix)
535 tz = TimeZoneInfo.FindSystemTimeZoneById ("Pacific/Auckland"); // *nix
537 tz = TimeZoneInfo.FindSystemTimeZoneById ("New Zealand Standard Time"); // Windows
539 // DST start: 9/29/2013 2:00:00 AM
540 // DST end: 4/6/2014 3:00:00 AM
541 DateTime sdt = new DateTime (2014, 1, 9, 23, 0, 0, DateTimeKind.Utc);
542 DateTime ddt = TimeZoneInfo.ConvertTime (sdt, tz);
543 Assert.AreEqual (10, ddt.Day, "#1.1");
544 Assert.AreEqual (1, ddt.Month, "#1.2");
545 Assert.AreEqual (2014, ddt.Year, "#1.3");
546 Assert.AreEqual (12, ddt.Hour, "#1.4");
547 Assert.AreEqual (0, ddt.Minute, "#1.5");
548 Assert.AreEqual (0, ddt.Second, "#1.6");
550 // DST start: 9/29/2014 2:00:00 AM
551 // DST end: 4/6/2015 3:00:00 AM
552 sdt = new DateTime (2014, 6, 9, 23, 0, 0, DateTimeKind.Utc);
553 ddt = TimeZoneInfo.ConvertTime (sdt, tz);
554 Assert.AreEqual (10, ddt.Day, "#2.1");
555 Assert.AreEqual (6, ddt.Month, "#2.2");
556 Assert.AreEqual (2014, ddt.Year, "#2.3");
557 Assert.AreEqual (11, ddt.Hour, "#2.4");
558 Assert.AreEqual (0, ddt.Minute, "#2.5");
559 Assert.AreEqual (0, ddt.Second, "#2.6");
561 // DST start: 9/29/2014 2:00:00 AM
562 // DST end: 4/6/2015 3:00:00 AM
563 sdt = new DateTime (2014, 10, 9, 23, 0, 0, DateTimeKind.Utc);
564 ddt = TimeZoneInfo.ConvertTime (sdt, tz);
565 Assert.AreEqual (10, ddt.Day, "#3.1");
566 Assert.AreEqual (10, ddt.Month, "#3.2");
567 Assert.AreEqual (2014, ddt.Year, "#3.3");
568 Assert.AreEqual (12, ddt.Hour, "#3.4");
569 Assert.AreEqual (0, ddt.Minute, "#3.5");
570 Assert.AreEqual (0, ddt.Second, "#3.6");
573 [Test (Description="Fix the bug https://bugzilla.xamarin.com/show_bug.cgi?id=1849")]
574 public void ConvertTime_AjustmentConvertTimeWithSourceTimeZone () {
576 TimeZoneInfo easternTimeZone;
577 TimeZoneInfo pacificTimeZone;
579 if (Environment.OSVersion.Platform == PlatformID.Unix) {
581 easternTimeZone = TimeZoneInfo.FindSystemTimeZoneById ("US/Eastern");
582 pacificTimeZone = TimeZoneInfo.FindSystemTimeZoneById ("US/Pacific");
586 easternTimeZone = TimeZoneInfo.FindSystemTimeZoneById ("Eastern Standard Time");
587 pacificTimeZone = TimeZoneInfo.FindSystemTimeZoneById ("Pacific Standard Time");
590 DateTime lastMidnight = new DateTime (new DateTime (2012, 06, 13).Ticks, DateTimeKind.Unspecified);
591 DateTime lastMidnightAsEST = TimeZoneInfo.ConvertTime (lastMidnight, pacificTimeZone, easternTimeZone);
592 DateTime lastMidnightAsPST = TimeZoneInfo.ConvertTime (lastMidnightAsEST, easternTimeZone, pacificTimeZone);
594 // Last midnight in PST as EST should be 3AM
595 DateTime expectedDate = new DateTime (2012, 06, 13, 3, 0, 0);
597 Assert.AreEqual (expectedDate, lastMidnightAsEST);
598 Assert.AreEqual (lastMidnight, lastMidnightAsPST);
603 public class IsInvalidTimeTests
608 public void CreateTimeZones ()
610 TimeZoneInfo.TransitionTime start = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,1,0,0), 3, 5, DayOfWeek.Sunday);
611 TimeZoneInfo.TransitionTime end = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,2,0,0), 10, 5, DayOfWeek.Sunday);
612 TimeZoneInfo.AdjustmentRule rule = TimeZoneInfo.AdjustmentRule.CreateAdjustmentRule (DateTime.MinValue.Date, DateTime.MaxValue.Date, new TimeSpan (1,0,0), start, end);
613 london = TimeZoneInfo.CreateCustomTimeZone ("Europe/London", new TimeSpan (0), "Europe/London", "British Standard Time", "British Summer Time", new TimeZoneInfo.AdjustmentRule [] {rule});
618 public void UTCDate ()
620 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)) {
621 date = DateTime.SpecifyKind (date, DateTimeKind.Utc);
622 Assert.IsFalse (london.IsInvalidTime (date));
627 public void InvalidDates ()
629 Assert.IsFalse (london.IsInvalidTime (new DateTime (2007, 03, 25, 0, 59, 59)));
630 Assert.IsTrue (london.IsInvalidTime (new DateTime (2007, 03, 25, 1, 0, 0)));
631 Assert.IsTrue (london.IsInvalidTime (new DateTime (2007, 03, 25, 1, 59, 59)));
632 Assert.IsFalse (london.IsInvalidTime (new DateTime (2007, 03, 25, 2, 0, 0)));
637 public class IsAmbiguousTimeTests
642 public void CreateTimeZones ()
644 TimeZoneInfo.TransitionTime start = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,1,0,0), 3, 5, DayOfWeek.Sunday);
645 TimeZoneInfo.TransitionTime end = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,2,0,0), 10, 5, DayOfWeek.Sunday);
646 TimeZoneInfo.AdjustmentRule rule = TimeZoneInfo.AdjustmentRule.CreateAdjustmentRule (DateTime.MinValue.Date, DateTime.MaxValue.Date, new TimeSpan (1,0,0), start, end);
647 london = TimeZoneInfo.CreateCustomTimeZone ("Europe/London", new TimeSpan (0), "Europe/London", "British Standard Time", "British Summer Time", new TimeZoneInfo.AdjustmentRule [] {rule});
651 public void AmbiguousDates ()
653 if (Environment.OSVersion.Platform != PlatformID.Unix)
654 Assert.Ignore ("Not running on Unix.");
655 Assert.IsFalse (london.IsAmbiguousTime (new DateTime (2007, 10, 28, 1, 0, 0)));
656 Assert.IsTrue (london.IsAmbiguousTime (new DateTime (2007, 10, 28, 1, 0, 1)));
657 Assert.IsTrue (london.IsAmbiguousTime (new DateTime (2007, 10, 28, 2, 0, 0)));
658 Assert.IsFalse (london.IsAmbiguousTime (new DateTime (2007, 10, 28, 2, 0, 1)));
662 public void AmbiguousUTCDates ()
664 if (Environment.OSVersion.Platform != PlatformID.Unix)
665 Assert.Ignore ("Not running on Unix.");
666 Assert.IsFalse (london.IsAmbiguousTime (new DateTime (2007, 10, 28, 0, 0, 0, DateTimeKind.Utc)));
667 Assert.IsTrue (london.IsAmbiguousTime (new DateTime (2007, 10, 28, 0, 0, 1, DateTimeKind.Utc)));
668 Assert.IsTrue (london.IsAmbiguousTime (new DateTime (2007, 10, 28, 0, 59, 59, DateTimeKind.Utc)));
669 Assert.IsFalse (london.IsAmbiguousTime (new DateTime (2007, 10, 28, 1, 0, 0, DateTimeKind.Utc)));
674 public void AmbiguousInUTC ()
676 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)) {
677 Assert.IsFalse (TimeZoneInfo.Utc.IsAmbiguousTime (date));
684 public class GetSystemTimeZonesTests
687 public void Identity ()
689 Assert.AreSame (TimeZoneInfo.GetSystemTimeZones (), TimeZoneInfo.GetSystemTimeZones ());
693 public void NotEmpty ()
695 if (Environment.OSVersion.Platform != PlatformID.Unix)
696 Assert.Ignore ("Not running on Unix.");
697 global::System.Collections.ObjectModel.ReadOnlyCollection<TimeZoneInfo> systemTZ = TimeZoneInfo.GetSystemTimeZones ();
698 Assert.IsNotNull(systemTZ, "SystemTZ is null");
699 Assert.IsFalse (systemTZ.Count == 0, "SystemTZ is empty");
703 public void ContainsBrussels ()
705 if (Environment.OSVersion.Platform != PlatformID.Unix)
706 Assert.Ignore ("Not running on Unix.");
707 global::System.Collections.ObjectModel.ReadOnlyCollection<TimeZoneInfo> systemTZ = TimeZoneInfo.GetSystemTimeZones ();
708 foreach (TimeZoneInfo tz in systemTZ) {
709 if (tz.Id == "Europe/Brussels")
712 Assert.Fail ("Europe/Brussels not found in SystemTZ");
716 public void ReflectionReturnsTheCorrectMethod ()
718 var method = (MethodInfo) typeof (TimeZoneInfo).GetMember ("GetSystemTimeZones", MemberTypes.Method, BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic)[0];
720 var timeZones = (global::System.Collections.ObjectModel.ReadOnlyCollection<TimeZoneInfo>) method.Invoke (null, null);
721 Assert.IsTrue (timeZones.Count > 0, "GetSystemTimeZones should not return an empty collection.");
726 public class FindSystemTimeZoneByIdTests
729 [ExpectedException (typeof (ArgumentNullException))]
730 public void NullId ()
732 TimeZoneInfo.FindSystemTimeZoneById (null);
736 [ExpectedException (typeof (TimeZoneNotFoundException))]
737 public void NonSystemTimezone ()
739 if (Environment.OSVersion.Platform != PlatformID.Unix)
740 throw new TimeZoneNotFoundException ();
741 TimeZoneInfo.FindSystemTimeZoneById ("Neverland/The_Lagoon");
745 public void FindBrusselsTZ ()
747 if (Environment.OSVersion.Platform != PlatformID.Unix)
748 Assert.Ignore ("Not running on Unix.");
749 TimeZoneInfo brussels = TimeZoneInfo.FindSystemTimeZoneById ("Europe/Brussels");
750 Assert.IsNotNull (brussels);
754 public void OffsetIsCorrectInKinshasa ()
756 if (Environment.OSVersion.Platform != PlatformID.Unix)
757 Assert.Ignore ("Not running on Unix.");
758 TimeZoneInfo kin = TimeZoneInfo.FindSystemTimeZoneById ("Africa/Kinshasa");
759 Assert.AreEqual (new TimeSpan (1,0,0), kin.BaseUtcOffset, "BaseUtcOffset in Kinshasa is not +1h");
763 public void OffsetIsCorrectInBrussels ()
765 if (Environment.OSVersion.Platform != PlatformID.Unix)
766 Assert.Ignore ("Not running on Unix.");
767 TimeZoneInfo brussels = TimeZoneInfo.FindSystemTimeZoneById ("Europe/Brussels");
768 Assert.AreEqual (new TimeSpan (1,0,0), brussels.BaseUtcOffset, "BaseUtcOffset for Brussels is not +1h");
772 public void NoDSTInKinshasa ()
774 if (Environment.OSVersion.Platform != PlatformID.Unix)
775 Assert.Ignore ("Not running on Unix.");
776 TimeZoneInfo kin = TimeZoneInfo.FindSystemTimeZoneById ("Africa/Kinshasa");
777 Assert.IsFalse (kin.SupportsDaylightSavingTime);
781 public void BrusselsSupportsDST ()
783 if (Environment.OSVersion.Platform != PlatformID.Unix)
784 Assert.Ignore ("Not running on Unix.");
785 TimeZoneInfo brussels = TimeZoneInfo.FindSystemTimeZoneById ("Europe/Brussels");
786 Assert.IsTrue (brussels.SupportsDaylightSavingTime);
790 public void MelbourneSupportsDST ()
792 if (Environment.OSVersion.Platform != PlatformID.Unix)
793 Assert.Ignore ("Not running on Unix.");
794 TimeZoneInfo melbourne = TimeZoneInfo.FindSystemTimeZoneById ("Australia/Melbourne");
795 Assert.IsTrue (melbourne.SupportsDaylightSavingTime);
799 public void RomeAndVaticanSharesTime ()
801 if (Environment.OSVersion.Platform != PlatformID.Unix)
802 Assert.Ignore ("Not running on Unix.");
803 TimeZoneInfo rome = TimeZoneInfo.FindSystemTimeZoneById ("Europe/Rome");
804 TimeZoneInfo vatican = TimeZoneInfo.FindSystemTimeZoneById ("Europe/Vatican");
805 Assert.IsTrue (rome.HasSameRules (vatican));
809 public void FindSystemTimeZoneById_Local_Roundtrip ()
811 Assert.AreEqual (TimeZoneInfo.Local.Id, TimeZoneInfo.FindSystemTimeZoneById (TimeZoneInfo.Local.Id).Id);
815 public void Test326 ()
817 DateTime utc = DateTime.UtcNow;
818 DateTime local = TimeZoneInfo.ConvertTime (utc, TimeZoneInfo.Utc, TimeZoneInfo.FindSystemTimeZoneById (TimeZoneInfo.Local.Id));
819 Assert.AreEqual (local, utc + TimeZoneInfo.Local.GetUtcOffset (utc), "ConvertTime/Local");
824 public void BrusselsAdjustments ()
826 TimeZoneInfo.TransitionTime start = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,2,0,0), 3, 5, DayOfWeek.Sunday);
827 TimeZoneInfo.TransitionTime end = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,3,0,0), 10, 5, DayOfWeek.Sunday);
828 TimeZoneInfo.AdjustmentRule rule = TimeZoneInfo.AdjustmentRule.CreateAdjustmentRule (DateTime.MinValue.Date, DateTime.MaxValue.Date, new TimeSpan (1,0,0), start, end);
829 TimeZoneInfo brussels = TimeZoneInfo.CreateCustomTimeZone ("Europe/Brussels", new TimeSpan (1, 0, 0), "Europe/Brussels", "", "", new TimeZoneInfo.AdjustmentRule [] {rule});
831 TimeZoneInfo brussels_sys = TimeZoneInfo.FindSystemTimeZoneById ("Europe/Brussels");
833 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)) {
834 Assert.AreEqual (brussels.GetUtcOffset (date), brussels_sys.GetUtcOffset (date));
835 Assert.AreEqual (brussels.IsDaylightSavingTime (date), brussels_sys.IsDaylightSavingTime (date));
841 public void SubminuteDSTOffsets ()
843 if (Environment.OSVersion.Platform != PlatformID.Unix)
846 var subMinuteDSTs = new string [] {
847 "Europe/Dublin", // Europe/Dublin has a DST offset of 34 minutes and 39 seconds in 1916.
850 "Canada/Newfoundland",
853 "N/A", // testing that the test doesn't fail with inexistent TZs
855 foreach (var tz in subMinuteDSTs) {
857 TimeZoneInfo.FindSystemTimeZoneById (tz);
858 } catch (TimeZoneNotFoundException) {
860 } catch (Exception ex) {
861 Assert.Fail (string.Format ("Failed to load TZ {0}: {1}", tz, ex.ToString ()));
868 public class GetAmbiguousTimeOffsetsTests
871 [ExpectedException (typeof(ArgumentException))]
872 public void DateIsNotAmbiguous ()
874 if (Environment.OSVersion.Platform != PlatformID.Unix)
875 throw new ArgumentException ();
876 TimeZoneInfo brussels = TimeZoneInfo.FindSystemTimeZoneById ("Europe/Brussels");
877 DateTime date = new DateTime (2007, 05, 11, 11, 40, 00);
878 brussels.GetAmbiguousTimeOffsets (date);
882 public void AmbiguousOffsets ()
884 if (Environment.OSVersion.Platform != PlatformID.Unix)
885 Assert.Ignore ("Not running on Unix.");
886 TimeZoneInfo brussels = TimeZoneInfo.FindSystemTimeZoneById ("Europe/Brussels");
887 DateTime date = new DateTime (2007, 10, 28, 2, 30, 00);
888 Assert.IsTrue (brussels.IsAmbiguousTime (date));
889 Assert.AreEqual (2, brussels.GetAmbiguousTimeOffsets (date).Length);
890 Assert.AreEqual (new TimeSpan[] {new TimeSpan (1, 0, 0), new TimeSpan (2, 0, 0)}, brussels.GetAmbiguousTimeOffsets (date));
895 public class HasSameRulesTests
898 public void NullAdjustments () //bnc #391011
900 TimeZoneInfo utc = TimeZoneInfo.Utc;
901 TimeZoneInfo custom = TimeZoneInfo.CreateCustomTimeZone ("Custom", new TimeSpan (0), "Custom", "Custom");
902 Assert.IsTrue (utc.HasSameRules (custom));
907 public class SerializationTests
910 public void Serialization_Deserialization ()
912 TimeZoneInfo.TransitionTime start = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,1,0,0), 3, 5, DayOfWeek.Sunday);
913 TimeZoneInfo.TransitionTime end = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,2,0,0), 10, 5, DayOfWeek.Sunday);
914 TimeZoneInfo.AdjustmentRule rule = TimeZoneInfo.AdjustmentRule.CreateAdjustmentRule (DateTime.MinValue.Date, DateTime.MaxValue.Date, new TimeSpan (1,0,0), start, end);
915 TimeZoneInfo london = TimeZoneInfo.CreateCustomTimeZone ("Europe/London", new TimeSpan (0), "Europe/London", "British Standard Time", "British Summer Time", new TimeZoneInfo.AdjustmentRule [] {rule});
916 MemoryStream stream = new MemoryStream ();
917 BinaryFormatter formatter = new BinaryFormatter ();
918 formatter.Serialize (stream, london);
920 TimeZoneInfo deserialized = (TimeZoneInfo) formatter.Deserialize (stream);
923 Assert.IsTrue (london.Equals (deserialized));
928 public class MultipleDaylightSavingTimeTests {
929 private TimeZoneInfo cairo;
930 private DateTime dst1Start;
931 private DateTime dst1End;
932 private DateTime dst2Start;
933 private DateTime dst2End;
935 private TimeSpan baseUtcOffset;
936 private TimeSpan dstUtcOffset;
937 private TimeSpan dstOffset;
940 public void CreateTimeZones ()
943 From 1/1/2014 12:00:00 AM to 6/30/2014 12:00:00 AM
945 Begins at 12:00 AM on 16 May
946 Ends at 1:00 AM on 29 June
947 From 7/1/2014 12:00:00 AM to 12/31/2014 12:00:00 AM
949 Begins at 12:00 AM on 29 July
950 Ends at 12:00 AM on 26 September
952 dst1Start = new DateTime (2014, 5, 16);
953 dst1End = new DateTime (2014, 6, 29);
954 dst2Start = new DateTime (2014, 7, 29);
955 dst2End = new DateTime (2014, 9, 26);
957 baseUtcOffset = new TimeSpan (2, 0, 0);
958 dstUtcOffset = new TimeSpan (3, 0, 0);
959 dstOffset = dstUtcOffset - baseUtcOffset;
961 var rule1 = TimeZoneInfo.AdjustmentRule.CreateAdjustmentRule (
962 new DateTime (2014, 1, 1), new DateTime (2014, 6, 30), dstOffset,
963 CreateFixedDateRule (dst1Start), CreateFixedDateRule (dst1End));
965 var rule2 = TimeZoneInfo.AdjustmentRule.CreateAdjustmentRule (
966 new DateTime (2014, 7, 1), new DateTime (2014, 12, 31), dstOffset,
967 CreateFixedDateRule (dst2Start), CreateFixedDateRule (dst2End));
969 cairo = TimeZoneInfo.CreateCustomTimeZone ("Africa/Cairo", baseUtcOffset, "Africa/Cairo", "EET", "EEST",
970 new [] {rule1, rule2});
973 private static TimeZoneInfo.TransitionTime CreateFixedDateRule (DateTime dateTime)
975 var time = new DateTime (dateTime.Ticks - dateTime.Date.Ticks);
976 return TimeZoneInfo.TransitionTime.CreateFixedDateRule (time, dateTime.Month, dateTime.Day);
980 public void GetUtcOffset_FromUTC ()
982 var d = dst1Start.Add (-baseUtcOffset);
983 d = DateTime.SpecifyKind (d, DateTimeKind.Utc);
984 Assert.AreEqual(baseUtcOffset, cairo.GetUtcOffset (d.Add (new TimeSpan(0,0,0,-1))));
985 Assert.AreEqual(dstUtcOffset, cairo.GetUtcOffset (d));
986 Assert.AreEqual(dstUtcOffset, cairo.GetUtcOffset (d.Add (new TimeSpan(0,0,0, 1))));
988 d = dst1End.Add (-baseUtcOffset-dstOffset);
989 d = DateTime.SpecifyKind (d, DateTimeKind.Utc);
990 Assert.AreEqual(dstUtcOffset, cairo.GetUtcOffset (d.Add (new TimeSpan(0,0,0,-1))));
991 Assert.AreEqual(baseUtcOffset, cairo.GetUtcOffset (d));
992 Assert.AreEqual(baseUtcOffset, cairo.GetUtcOffset (d.Add (new TimeSpan(0,0,0, 1))));
994 d = dst2Start.Add (-baseUtcOffset);
995 d = DateTime.SpecifyKind (d, DateTimeKind.Utc);
996 Assert.AreEqual(baseUtcOffset, cairo.GetUtcOffset (d.Add (new TimeSpan(0,0,0,-1))));
997 Assert.AreEqual(dstUtcOffset, cairo.GetUtcOffset (d));
998 Assert.AreEqual(dstUtcOffset, cairo.GetUtcOffset (d.Add (new TimeSpan(0,0,0, 1))));
1000 d = dst2End.Add (-baseUtcOffset-dstOffset);
1001 d = DateTime.SpecifyKind (d, DateTimeKind.Utc);
1002 Assert.AreEqual(dstUtcOffset, cairo.GetUtcOffset (d.Add (new TimeSpan(0,0,0,-1))));
1003 Assert.AreEqual(baseUtcOffset, cairo.GetUtcOffset (d));
1004 Assert.AreEqual(baseUtcOffset, cairo.GetUtcOffset (d.Add (new TimeSpan(0,0,0, 1))));
1008 public void GetUtcOffset_FromLocal ()
1010 var d = dst1Start.Add (-baseUtcOffset);
1011 d = DateTime.SpecifyKind (d, DateTimeKind.Utc);
1012 d = d.ToLocalTime ();
1013 Assert.AreEqual(baseUtcOffset, cairo.GetUtcOffset (d.Add (new TimeSpan(0,0,0,-1))));
1014 Assert.AreEqual(dstUtcOffset, cairo.GetUtcOffset (d));
1015 Assert.AreEqual(dstUtcOffset, cairo.GetUtcOffset (d.Add (new TimeSpan(0,0,0, 1))));
1017 d = dst1End.Add (-baseUtcOffset-dstOffset);
1018 d = DateTime.SpecifyKind (d, DateTimeKind.Utc);
1019 d = d.ToLocalTime ();
1020 Assert.AreEqual(dstUtcOffset, cairo.GetUtcOffset (d.Add (new TimeSpan(0,0,0,-1))));
1021 Assert.AreEqual(baseUtcOffset, cairo.GetUtcOffset (d));
1022 Assert.AreEqual(baseUtcOffset, cairo.GetUtcOffset (d.Add (new TimeSpan(0,0,0, 1))));
1024 d = dst2Start.Add (-baseUtcOffset);
1025 d = DateTime.SpecifyKind (d, DateTimeKind.Utc);
1026 d = d.ToLocalTime ();
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 (-baseUtcOffset-dstOffset);
1032 d = DateTime.SpecifyKind (d, DateTimeKind.Utc);
1033 d = d.ToLocalTime ();
1034 Assert.AreEqual(dstUtcOffset, cairo.GetUtcOffset (d.Add (new TimeSpan(0,0,0,-1))));
1035 Assert.AreEqual(baseUtcOffset, cairo.GetUtcOffset (d));
1036 Assert.AreEqual(baseUtcOffset, cairo.GetUtcOffset (d.Add (new TimeSpan(0,0,0, 1))));
1040 public void GetUtcOffset_FromUnspecified ()
1042 var d = dst1Start.Add (dstOffset);
1043 Assert.AreEqual(baseUtcOffset, cairo.GetUtcOffset (d.Add (new TimeSpan(0,0,0,-1))));
1044 Assert.AreEqual(dstUtcOffset, cairo.GetUtcOffset (d));
1045 Assert.AreEqual(dstUtcOffset, cairo.GetUtcOffset (d.Add (new TimeSpan(0,0,0, 1))));
1047 d = dst1End.Add (-dstOffset);
1048 Assert.AreEqual(dstUtcOffset, cairo.GetUtcOffset (d.Add (new TimeSpan(0,0,0,-1))));
1049 Assert.AreEqual(baseUtcOffset, cairo.GetUtcOffset (d));
1050 Assert.AreEqual(baseUtcOffset, cairo.GetUtcOffset (d.Add (new TimeSpan(0,0,0, 1))));
1052 d = dst2Start.Add (dstOffset);
1053 Assert.AreEqual(baseUtcOffset, cairo.GetUtcOffset (d.Add (new TimeSpan(0,0,0,-1))));
1054 Assert.AreEqual(dstUtcOffset, cairo.GetUtcOffset (d));
1055 Assert.AreEqual(dstUtcOffset, cairo.GetUtcOffset (d.Add (new TimeSpan(0,0,0, 1))));
1057 d = dst2End.Add (-dstOffset);
1058 Assert.AreEqual(dstUtcOffset, cairo.GetUtcOffset (d.Add (new TimeSpan(0,0,0,-1))));
1059 Assert.AreEqual(baseUtcOffset, cairo.GetUtcOffset (d));
1060 Assert.AreEqual(baseUtcOffset, cairo.GetUtcOffset (d.Add (new TimeSpan(0,0,0, 1))));
1065 public class GetDaylightChanges
1067 MethodInfo getChanges;
1070 public void Setup ()
1072 var flags = BindingFlags.Instance | BindingFlags.NonPublic;
1073 getChanges = typeof (TimeZoneInfo).GetMethod ("GetDaylightChanges", flags);
1077 public void TestSydneyDaylightChanges ()
1080 if (Environment.OSVersion.Platform == PlatformID.Unix)
1081 tz = TimeZoneInfo.FindSystemTimeZoneById ("Australia/Sydney");
1083 tz = TimeZoneInfo.FindSystemTimeZoneById ("W. Australia Standard Time");
1085 var changes = (DaylightTime) getChanges.Invoke (tz, new object [] {2014});
1087 Assert.AreEqual (new TimeSpan (1, 0, 0), changes.Delta);
1088 Assert.AreEqual (new DateTime (2014, 10, 5, 2, 0, 0), changes.Start);
1089 Assert.AreEqual (new DateTime (2014, 4, 6, 3, 0, 0), changes.End);