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
42 static FieldInfo localField;
43 static FieldInfo cachedDataField;
44 static object localFieldObj;
46 public static string MapTimeZoneId (string id)
48 if (Environment.OSVersion.Platform == PlatformID.Unix)
52 case "Pacific/Auckland":
53 return "New Zealand Standard Time";
55 return "GTB Standard Time";
57 return "Eastern Standard Time";
59 return "Pacific Standard Time";
60 case "Australia/Sydney":
61 case "Australia/Melbourne":
62 return "AUS Eastern Standard Time";
63 case "Europe/Brussels":
64 return "Romance Standard Time";
65 case "Africa/Kinshasa":
66 return "W. Central Africa Standard Time";
68 case "Europe/Vatican":
69 return "W. Europe Standard Time";
70 case "Canada/Eastern":
71 return "Eastern Standard Time";
73 Assert.Fail ($"No mapping defined for zone id '{id}'");
79 public static void SetLocal (TimeZoneInfo val)
81 if (localField == null) {
82 if (Type.GetType ("Mono.Runtime") != null) {
83 localField = typeof (TimeZoneInfo).GetField ("local",
84 BindingFlags.Static | BindingFlags.GetField | BindingFlags.NonPublic);
86 cachedDataField = typeof (TimeZoneInfo).GetField ("s_cachedData",
87 BindingFlags.Static | BindingFlags.GetField | BindingFlags.NonPublic);
89 localField = cachedDataField.FieldType.GetField ("m_localTimeZone",
90 BindingFlags.Instance | BindingFlags.GetField | BindingFlags.NonPublic);
94 if (cachedDataField != null)
95 localFieldObj = cachedDataField.GetValue (null);
97 localField.SetValue (localFieldObj, val);
101 public class PropertiesTests
104 public void GetLocal ()
106 TimeZoneInfo local = TimeZoneInfo.Local;
107 Assert.IsNotNull (local);
108 Assert.IsTrue (true);
112 private static extern int readlink (string path, byte[] buffer, int buflen);
114 [Test] // Covers #24958
115 public void LocalId ()
117 byte[] buf = new byte [512];
119 var path = "/etc/localtime";
121 var ret = readlink (path, buf, buf.Length);
123 return; // path is not a symbolic link, nothing to test
124 } catch (DllNotFoundException e) {
127 #if !MONOTOUCH && !XAMMAC
128 // this assumption is incorrect for iOS, tvO, watchOS and OSX
129 Assert.IsTrue (TimeZoneInfo.Local.Id != "Local", "Local timezone id should not be \"Local\"");
135 public class CreateCustomTimezoneTests
138 [ExpectedException (typeof (ArgumentNullException))]
139 public void IdIsNullException ()
141 TimeZoneInfo.CreateCustomTimeZone (null, new TimeSpan (0), null, null);
145 [ExpectedException (typeof (ArgumentException))]
146 public void IdIsEmptyString ()
148 TimeZoneInfo.CreateCustomTimeZone ("", new TimeSpan (0), null, null);
152 [ExpectedException (typeof (ArgumentException))]
153 public void OffsetIsNotMinutes ()
155 TimeZoneInfo.CreateCustomTimeZone ("mytimezone", new TimeSpan (0, 0, 55), null, null);
159 [ExpectedException (typeof (ArgumentOutOfRangeException))]
160 public void OffsetTooBig ()
162 TimeZoneInfo.CreateCustomTimeZone ("mytimezone", new TimeSpan (14, 1, 0), null, null);
166 [ExpectedException (typeof (ArgumentOutOfRangeException))]
167 public void OffsetTooSmall ()
169 TimeZoneInfo.CreateCustomTimeZone ("mytimezone", - new TimeSpan (14, 1, 0), null, null);
174 [ExpectedException (typeof (ArgumentException))]
175 public void IdLongerThan32 ()
177 TimeZoneInfo.CreateCustomTimeZone ("12345678901234567890123456789012345", new TimeSpan (0), null, null);
182 [ExpectedException (typeof (InvalidTimeZoneException))]
183 public void AdjustmentRulesOverlap ()
185 TimeZoneInfo.TransitionTime s1 = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,4,0,0), 3, 2, DayOfWeek.Sunday);
186 TimeZoneInfo.TransitionTime e1 = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,4,0,0), 10, 2, DayOfWeek.Sunday);
187 TimeZoneInfo.AdjustmentRule r1 = TimeZoneInfo.AdjustmentRule.CreateAdjustmentRule (new DateTime (2000,1,1), new DateTime (2005,1,1), new TimeSpan (1,0,0), s1, e1);
188 TimeZoneInfo.TransitionTime s2 = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,4,0,0), 2, 2, DayOfWeek.Sunday);
189 TimeZoneInfo.TransitionTime e2 = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,4,0,0), 11, 2, DayOfWeek.Sunday);
190 TimeZoneInfo.AdjustmentRule r2 = TimeZoneInfo.AdjustmentRule.CreateAdjustmentRule (new DateTime (2004,1,1), new DateTime (2007,1,1), new TimeSpan (1,0,0), s2, e2);
191 TimeZoneInfo.CreateCustomTimeZone ("mytimezone", new TimeSpan (6,0,0),null,null,null,new TimeZoneInfo.AdjustmentRule[] {r1, r2});
195 [ExpectedException (typeof (InvalidTimeZoneException))]
196 public void RulesNotOrdered ()
198 TimeZoneInfo.TransitionTime s1 = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,4,0,0), 3, 2, DayOfWeek.Sunday);
199 TimeZoneInfo.TransitionTime e1 = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,4,0,0), 10, 2, DayOfWeek.Sunday);
200 TimeZoneInfo.AdjustmentRule r1 = TimeZoneInfo.AdjustmentRule.CreateAdjustmentRule (new DateTime (2000,1,1), new DateTime (2005,1,1), new TimeSpan (1,0,0), s1, e1);
201 TimeZoneInfo.TransitionTime s2 = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,4,0,0), 2, 2, DayOfWeek.Sunday);
202 TimeZoneInfo.TransitionTime e2 = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,4,0,0), 11, 2, DayOfWeek.Sunday);
203 TimeZoneInfo.AdjustmentRule r2 = TimeZoneInfo.AdjustmentRule.CreateAdjustmentRule (new DateTime (2006,1,1), new DateTime (2007,1,1), new TimeSpan (1,0,0), s2, e2);
204 TimeZoneInfo.CreateCustomTimeZone ("mytimezone", new TimeSpan (6,0,0),null,null,null,new TimeZoneInfo.AdjustmentRule[] {r2, r1});
208 [ExpectedException (typeof (InvalidTimeZoneException))]
209 public void OffsetOutOfRange ()
211 TimeZoneInfo.TransitionTime startTransition = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,4,0,0), 3, 2, DayOfWeek.Sunday);
212 TimeZoneInfo.TransitionTime endTransition = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,4,0,0), 10, 2, DayOfWeek.Sunday);
213 TimeZoneInfo.AdjustmentRule rule = TimeZoneInfo.AdjustmentRule.CreateAdjustmentRule (new DateTime (2000,1,1), new DateTime (2005,1,1), new TimeSpan (3,0,0), startTransition, endTransition);
214 TimeZoneInfo.CreateCustomTimeZone ("mytimezone", new TimeSpan (12,0,0),null,null,null,new TimeZoneInfo.AdjustmentRule[] {rule});
218 [ExpectedException (typeof (InvalidTimeZoneException))]
219 public void NullRule ()
221 TimeZoneInfo.CreateCustomTimeZone ("mytimezone", new TimeSpan (12,0,0),null,null,null,new TimeZoneInfo.AdjustmentRule[] {null});
225 [ExpectedException (typeof (InvalidTimeZoneException))]
226 public void MultiplesRulesForDate ()
228 TimeZoneInfo.TransitionTime s1 = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,4,0,0), 3, 2, DayOfWeek.Sunday);
229 TimeZoneInfo.TransitionTime e1 = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,4,0,0), 10, 2, DayOfWeek.Sunday);
230 TimeZoneInfo.AdjustmentRule r1 = TimeZoneInfo.AdjustmentRule.CreateAdjustmentRule (new DateTime (2000,1,1), new DateTime (2005,1,1), new TimeSpan (1,0,0), s1, e1);
231 TimeZoneInfo.TransitionTime s2 = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,4,0,0), 2, 2, DayOfWeek.Sunday);
232 TimeZoneInfo.TransitionTime e2 = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,4,0,0), 11, 2, DayOfWeek.Sunday);
233 TimeZoneInfo.AdjustmentRule r2 = TimeZoneInfo.AdjustmentRule.CreateAdjustmentRule (new DateTime (2005,1,1), new DateTime (2007,1,1), new TimeSpan (1,0,0), s2, e2);
234 TimeZoneInfo.CreateCustomTimeZone ("mytimezone", new TimeSpan (6,0,0),null,null,null,new TimeZoneInfo.AdjustmentRule[] {r1, r2});
238 public void SupportsDaylightSavingTime_NonEmptyAdjustmentRule ()
240 TimeZoneInfo.TransitionTime s1 = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,4,0,0), 3, 2, DayOfWeek.Sunday);
241 TimeZoneInfo.TransitionTime e1 = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,4,0,0), 10, 2, DayOfWeek.Sunday);
242 TimeZoneInfo.AdjustmentRule r1 = TimeZoneInfo.AdjustmentRule.CreateAdjustmentRule (new DateTime (2000,1,1), new DateTime (2005,1,1), new TimeSpan (1,0,0), s1, e1);
243 TimeZoneInfo tz = TimeZoneInfo.CreateCustomTimeZone ("mytimezone", new TimeSpan (6,0,0),null,null,null,new TimeZoneInfo.AdjustmentRule[] {r1});
244 Assert.IsTrue (tz.SupportsDaylightSavingTime);
248 public void SupportsDaylightSavingTime_EmptyAdjustmentRule ()
250 TimeZoneInfo tz = TimeZoneInfo.CreateCustomTimeZone ("mytimezone", new TimeSpan (6,0,0),null,null,null,null);
251 Assert.IsFalse (tz.SupportsDaylightSavingTime);
255 public void SupportsDaylightSavingTime_NonEmptyAdjustmentRule_DisableDaylightSavingTime ()
257 TimeZoneInfo.TransitionTime s1 = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,4,0,0), 3, 2, DayOfWeek.Sunday);
258 TimeZoneInfo.TransitionTime e1 = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,4,0,0), 10, 2, DayOfWeek.Sunday);
259 TimeZoneInfo.AdjustmentRule r1 = TimeZoneInfo.AdjustmentRule.CreateAdjustmentRule (new DateTime (2000,1,1), new DateTime (2005,1,1), new TimeSpan (1,0,0), s1, e1);
260 TimeZoneInfo tz = TimeZoneInfo.CreateCustomTimeZone ("mytimezone", new TimeSpan (6,0,0),null,null,null,new TimeZoneInfo.AdjustmentRule[] {r1}, true);
261 Assert.IsFalse (tz.SupportsDaylightSavingTime);
265 public void SupportsDaylightSavingTime_EmptyAdjustmentRule_DisableDaylightSavingTime ()
267 TimeZoneInfo tz = TimeZoneInfo.CreateCustomTimeZone ("mytimezone", new TimeSpan (6,0,0),null,null,null,null,true);
268 Assert.IsFalse (tz.SupportsDaylightSavingTime);
273 public class IsDaylightSavingTimeTests
278 public void CreateTimeZones ()
280 TimeZoneInfo.TransitionTime start = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,1,0,0), 3, 5, DayOfWeek.Sunday);
281 TimeZoneInfo.TransitionTime end = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,2,0,0), 10, 5, DayOfWeek.Sunday);
282 TimeZoneInfo.AdjustmentRule rule = TimeZoneInfo.AdjustmentRule.CreateAdjustmentRule (DateTime.MinValue.Date, DateTime.MaxValue.Date, new TimeSpan (1,0,0), start, end);
283 london = TimeZoneInfo.CreateCustomTimeZone ("Europe/London", new TimeSpan (0), "Europe/London", "British Standard Time", "British Summer Time", new TimeZoneInfo.AdjustmentRule [] {rule});
287 public void NoDSTInUTC ()
289 DateTime june01 = new DateTime (2007, 06, 01);
290 Assert.IsFalse (TimeZoneInfo.Utc.IsDaylightSavingTime (june01));
294 public void DSTInLondon ()
296 DateTime june01 = new DateTime (2007, 06, 01);
297 DateTime xmas = new DateTime (2007, 12, 25);
298 Assert.IsTrue (london.IsDaylightSavingTime (june01), "June 01 is DST in London");
299 Assert.IsFalse (london.IsDaylightSavingTime (xmas), "Xmas is not DST in London");
303 public void DSTTransitions ()
305 DateTime beforeDST = new DateTime (2007, 03, 25, 0, 59, 59, DateTimeKind.Unspecified);
306 DateTime startDST = new DateTime (2007, 03, 25, 2, 0, 0, DateTimeKind.Unspecified);
307 DateTime endDST = new DateTime (2007, 10, 28, 1, 59, 59, DateTimeKind.Unspecified);
308 DateTime afterDST = new DateTime (2007, 10, 28, 2, 0, 0, DateTimeKind.Unspecified);
309 Assert.IsFalse (london.IsDaylightSavingTime (beforeDST), "Just before DST");
310 Assert.IsTrue (london.IsDaylightSavingTime (startDST), "the first seconds of DST");
311 Assert.IsTrue (london.IsDaylightSavingTime (endDST), "The last seconds of DST");
312 Assert.IsFalse (london.IsDaylightSavingTime (afterDST), "Just after DST");
316 public void DSTTransitionsUTC ()
318 DateTime beforeDST = new DateTime (2007, 03, 25, 0, 59, 59, DateTimeKind.Utc);
319 DateTime startDST = new DateTime (2007, 03, 25, 1, 0, 0, DateTimeKind.Utc);
320 DateTime endDST = new DateTime (2007, 10, 28, 0, 59, 59, DateTimeKind.Utc);
321 DateTime afterDST = new DateTime (2007, 10, 28, 1, 0, 0, DateTimeKind.Utc);
322 Assert.IsFalse (london.IsDaylightSavingTime (beforeDST), "Just before DST");
323 Assert.IsTrue (london.IsDaylightSavingTime (startDST), "the first seconds of DST");
324 Assert.IsTrue (london.IsDaylightSavingTime (endDST), "The last seconds of DST");
325 Assert.IsFalse (london.IsDaylightSavingTime (afterDST), "Just after DST");
330 public void MatchTimeZoneBehavior ()
332 TimeZone tzone = TimeZone.CurrentTimeZone;
333 TimeZoneInfo local = TimeZoneInfo.Local;
334 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)) {
335 date = DateTime.SpecifyKind (date, DateTimeKind.Local);
336 if (local.IsInvalidTime (date))
338 Assert.IsTrue (tzone.IsDaylightSavingTime (date) == local.IsDaylightSavingTime (date));
342 [Test (Description="Description xambug #17155")]
343 public void AdjustmentRuleAfterNewYears ()
345 TimeZoneInfo tz = TimeZoneInfo.FindSystemTimeZoneById (MapTimeZoneId ("Pacific/Auckland"));
346 // DST start: 9/29/2013 2:00:00 AM
347 // DST end: 4/6/2014 3:00:00 AM
348 DateTime dt = new DateTime (2014, 1, 9, 23, 0, 0, DateTimeKind.Utc);
349 Assert.IsTrue (tz.IsDaylightSavingTime (dt), "#1.1");
351 // DST start: 9/29/2014 2:00:00 AM
352 // DST end: 4/6/2015 3:00:00 AM
353 dt = new DateTime (2014, 6, 9, 23, 0, 0, DateTimeKind.Utc);
354 Assert.IsFalse (tz.IsDaylightSavingTime (dt), "#2.1");
356 // DST start: 9/29/2014 2:00:00 AM
357 // DST end: 4/6/2015 3:00:00 AM
358 dt = new DateTime (2014, 10, 9, 23, 0, 0, DateTimeKind.Utc);
359 Assert.IsTrue (tz.IsDaylightSavingTime (dt), "#3.1");
362 [Test] //Covers #26008
363 public void DSTWithFloatingDateRule ()
365 // Construct a custom time zone where daylight saving time starts on the
366 // 2nd Sunday in March.
367 var transitionToDaylight = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1, 1, 1, 2, 0, 0), 3, 2, DayOfWeek.Sunday);
368 var transitionToStandard = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1, 1, 1, 2, 0, 0), 11, 1, DayOfWeek.Sunday);
369 var adjustment = TimeZoneInfo.AdjustmentRule.CreateAdjustmentRule (DateTime.MinValue.Date, DateTime.MaxValue.Date, new TimeSpan (1, 0, 0), transitionToDaylight, transitionToStandard);
370 var timeZone = TimeZoneInfo.CreateCustomTimeZone ("BugCheck", new TimeSpan (-8, 0, 0), "Testing", "Testing Standard", "Testing Daylight", new TimeZoneInfo.AdjustmentRule [] { adjustment });
371 // See if March 7, 2014 is listed as being during daylight saving time.
372 // If it is DST, then the runtime has the bug that we are looking for.
373 Assert.IsFalse (timeZone.IsDaylightSavingTime (new DateTime (2014, 3, 7, 12, 0, 0, DateTimeKind.Unspecified)));
376 [Test] //Covers #25050
377 public void TestAthensDST ()
379 TimeZoneInfo tzi = TimeZoneInfo.FindSystemTimeZoneById (MapTimeZoneId ("Europe/Athens"));
380 var date = new DateTime (2014, 3, 30 , 2, 0, 0);
381 Assert.IsFalse (tzi.IsDaylightSavingTime (date));
382 Assert.AreEqual (new TimeSpan (2,0,0), tzi.GetUtcOffset (date));
386 public void TestAthensDST_InDSTDelta ()
388 // In .NET GetUtcOffset() returns the BaseUtcOffset for times within the hour
389 // lost when DST starts but IsDaylightSavingTime() returns true.
391 TimeZoneInfo tzi = TimeZoneInfo.FindSystemTimeZoneById (MapTimeZoneId ("Europe/Athens"));
393 var date = new DateTime (2014, 3, 30 , 3, 0, 0);
394 Assert.IsTrue (tzi.IsDaylightSavingTime (date));
395 Assert.AreEqual (new TimeSpan (2, 0, 0), tzi.GetUtcOffset (date));
396 Assert.IsTrue (tzi.IsDaylightSavingTime (new DateTimeOffset (date, tzi.GetUtcOffset (date))));
398 date = new DateTime (2014, 3, 30 , 3, 1, 0);
399 Assert.IsTrue (tzi.IsDaylightSavingTime (date));
400 Assert.AreEqual (new TimeSpan (2, 0, 0), tzi.GetUtcOffset (date));
401 Assert.IsTrue (tzi.IsDaylightSavingTime (new DateTimeOffset (date, tzi.GetUtcOffset (date))));
403 date = new DateTime (2014, 3, 30 , 3, 59, 0);
404 Assert.IsTrue (tzi.IsDaylightSavingTime (date));
405 Assert.AreEqual (new TimeSpan (2, 0, 0), tzi.GetUtcOffset (date));
406 Assert.IsTrue (tzi.IsDaylightSavingTime (new DateTimeOffset (date, tzi.GetUtcOffset (date))));
408 date = new DateTime (2014, 3, 30 , 4, 0, 0);
409 Assert.IsTrue (tzi.IsDaylightSavingTime (date));
410 Assert.AreEqual (new TimeSpan (3, 0, 0), tzi.GetUtcOffset (date));
411 Assert.IsTrue (tzi.IsDaylightSavingTime (new DateTimeOffset (date, tzi.GetUtcOffset (date))));
415 public void TestAthensDST_InDSTDelta_NoTransitions ()
417 if (Environment.OSVersion.Platform != PlatformID.Unix)
418 Assert.Ignore ("TimeZoneInfo on Mono on Windows and .NET has no transitions");
420 // Repeat the previous test but this time force using AdjustmentRules by nulling out TimeZoneInfo.transitions
422 TimeZoneInfo tzi = TimeZoneInfo.FindSystemTimeZoneById ("Europe/Athens");
424 var transitionsField = typeof (TimeZoneInfo).GetField ("transitions", BindingFlags.Instance | BindingFlags.NonPublic);
425 var transitions = transitionsField.GetValue (tzi);
426 Assert.IsNotNull (transitions, "Expected Athens TimeZoneInfo.transitions to be non-null");
427 transitionsField.SetValue (tzi, null);
431 var date = new DateTime (2014, 3, 30 , 3, 0, 0);
432 Assert.IsTrue (tzi.IsDaylightSavingTime (date));
433 Assert.AreEqual (new TimeSpan (2, 0, 0), tzi.GetUtcOffset (date));
434 Assert.IsTrue (tzi.IsDaylightSavingTime (new DateTimeOffset (date, tzi.GetUtcOffset (date))));
436 date = new DateTime (2014, 3, 30 , 3, 1, 0);
437 Assert.IsTrue (tzi.IsDaylightSavingTime (date));
438 Assert.AreEqual (new TimeSpan (2, 0, 0), tzi.GetUtcOffset (date));
439 Assert.IsTrue (tzi.IsDaylightSavingTime (new DateTimeOffset (date, tzi.GetUtcOffset (date))));
441 date = new DateTime (2014, 3, 30 , 3, 59, 0);
442 Assert.IsTrue (tzi.IsDaylightSavingTime (date));
443 Assert.AreEqual (new TimeSpan (2, 0, 0), tzi.GetUtcOffset (date));
444 Assert.IsTrue (tzi.IsDaylightSavingTime (new DateTimeOffset (date, tzi.GetUtcOffset (date))));
446 date = new DateTime (2014, 3, 30 , 4, 0, 0);
447 Assert.IsTrue (tzi.IsDaylightSavingTime (date));
448 Assert.AreEqual (new TimeSpan (3, 0, 0), tzi.GetUtcOffset (date));
449 Assert.IsTrue (tzi.IsDaylightSavingTime (new DateTimeOffset (date, tzi.GetUtcOffset (date))));
452 transitionsField.SetValue (tzi, transitions);
456 [Test] //Covers #41349
457 public void TestIsDST_DateTimeOffset ()
459 TimeZoneInfo tzi = TimeZoneInfo.FindSystemTimeZoneById (MapTimeZoneId ("Europe/Athens"));
460 var date = new DateTime (2014, 3, 30 , 2, 0, 0);
461 var offset = tzi.GetUtcOffset (date);
462 var dateOffset = new DateTimeOffset (date, offset);
463 Assert.IsFalse (tzi.IsDaylightSavingTime (dateOffset));
465 date = new DateTime (2014, 3, 30 , 3, 0, 0);
466 offset = tzi.GetUtcOffset (date);
467 dateOffset = new DateTimeOffset (date, offset);
468 Assert.IsTrue (tzi.IsDaylightSavingTime (dateOffset));
473 public class ConvertTimeTests_LocalUtc : ConvertTimeTests
475 static TimeZoneInfo oldLocal;
478 public void SetLocal ()
480 base.CreateTimeZones ();
482 oldLocal = TimeZoneInfo.Local;
483 TimeZoneInfoTest.SetLocal (TimeZoneInfo.Utc);
487 public void RestoreLocal ()
489 TimeZoneInfoTest.SetLocal (oldLocal);
494 public class ConvertTimeTests
499 public void CreateTimeZones ()
501 TimeZoneInfo.TransitionTime start = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,1,0,0), 3, 5, DayOfWeek.Sunday);
502 TimeZoneInfo.TransitionTime end = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,2,0,0), 10, 5, DayOfWeek.Sunday);
503 TimeZoneInfo.AdjustmentRule rule = TimeZoneInfo.AdjustmentRule.CreateAdjustmentRule (DateTime.MinValue.Date, DateTime.MaxValue.Date, new TimeSpan (1,0,0), start, end);
504 london = TimeZoneInfo.CreateCustomTimeZone ("Europe/London", new TimeSpan (0), "Europe/London", "British Standard Time", "British Summer Time", new TimeZoneInfo.AdjustmentRule [] {rule});
508 [ExpectedException (typeof (ArgumentException))]
509 public void ConvertFromUtc_KindIsLocalException ()
511 TimeZoneInfo.ConvertTimeFromUtc (new DateTime (2007, 5, 3, 11, 8, 0, DateTimeKind.Local), TimeZoneInfo.Local);
515 [ExpectedException (typeof (ArgumentNullException))]
516 public void ConvertFromUtc_DestinationTimeZoneIsNullException ()
518 TimeZoneInfo.ConvertTimeFromUtc (new DateTime (2007, 5, 3, 11, 8, 0), null);
522 public void ConvertFromUtc_DestinationIsUTC ()
524 DateTime now = DateTime.UtcNow;
525 DateTime converted = TimeZoneInfo.ConvertTimeFromUtc (now, TimeZoneInfo.Utc);
526 Assert.AreEqual (now, converted);
530 public void ConvertFromUTC_ConvertInWinter ()
532 DateTime utc = new DateTime (2007, 12, 25, 12, 0, 0);
533 DateTime converted = TimeZoneInfo.ConvertTimeFromUtc (utc, london);
534 Assert.AreEqual (utc, converted);
538 public void ConvertFromUtc_ConvertInSummer ()
540 DateTime utc = new DateTime (2007, 06, 01, 12, 0, 0);
541 DateTime converted = TimeZoneInfo.ConvertTimeFromUtc (utc, london);
542 Assert.AreEqual (utc + new TimeSpan (1,0,0), converted);
546 public void ConvertToUTC_KindIsUtc ()
548 DateTime now = DateTime.UtcNow;
549 Assert.AreEqual (now.Kind, DateTimeKind.Utc);
550 DateTime converted = TimeZoneInfo.ConvertTimeToUtc (now);
551 Assert.AreEqual (now, converted);
555 [ExpectedException (typeof (ArgumentException))]
556 public void ConvertToUTC_KindIsUTCButSourceIsNot ()
558 TimeZoneInfo.ConvertTimeToUtc (new DateTime (2007, 5, 3, 12, 8, 0, DateTimeKind.Utc), london);
562 [ExpectedException (typeof (ArgumentException))]
563 public void ConvertToUTC_KindIsLocalButSourceIsNot ()
565 TimeZoneInfo.ConvertTimeToUtc (new DateTime (2007, 5, 3, 12, 8, 0, DateTimeKind.Local), london);
569 [ExpectedException (typeof (ArgumentException))]
570 public void ConvertToUTC_InvalidDate ()
572 TimeZoneInfo.ConvertTimeToUtc (new DateTime (2007, 3, 25, 1, 30, 0), london);
576 [ExpectedException (typeof (ArgumentNullException))]
577 public void ConvertToUTC_SourceIsNull ()
579 TimeZoneInfo.ConvertTimeToUtc (new DateTime (2007, 5, 3, 12, 16, 0), null);
584 public void ConvertToUtc_MatchDateTimeBehavior ()
586 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)) {
587 Assert.AreEqual (TimeZoneInfo.ConvertTimeToUtc (date), date.ToUniversalTime ());
593 public void ConvertFromToUtc ()
595 DateTime utc = DateTime.UtcNow;
596 Assert.AreEqual (utc.Kind, DateTimeKind.Utc);
597 DateTime converted = TimeZoneInfo.ConvertTimeFromUtc (utc, london);
598 Assert.AreEqual (converted.Kind, DateTimeKind.Unspecified);
599 DateTime back = TimeZoneInfo.ConvertTimeToUtc (converted, london);
600 Assert.AreEqual (back.Kind, DateTimeKind.Utc);
601 Assert.AreEqual (utc, back);
606 public void ConvertTimeToUtc_Overflow ()
608 var res = TimeZoneInfo.ConvertTimeToUtc (new DateTime (0));
609 Assert.AreEqual (res.Kind, DateTimeKind.Utc, "#1");
611 res = TimeZoneInfo.ConvertTimeToUtc (DateTime.MaxValue);
612 Assert.AreEqual (res.Kind, DateTimeKind.Utc, "#2");
616 public void ConvertFromToUtc_Utc ()
618 DateTime utc = DateTime.UtcNow;
619 Assert.AreEqual (utc.Kind, DateTimeKind.Utc);
620 DateTime converted = TimeZoneInfo.ConvertTimeFromUtc (utc, TimeZoneInfo.Utc);
621 Assert.AreEqual (DateTimeKind.Utc, converted.Kind);
622 DateTime back = TimeZoneInfo.ConvertTimeToUtc (converted, TimeZoneInfo.Utc);
623 Assert.AreEqual (back.Kind, DateTimeKind.Utc);
624 Assert.AreEqual (utc, back);
628 public void ConvertFromToLocal ()
630 DateTime utc = DateTime.UtcNow;
631 Assert.AreEqual (utc.Kind, DateTimeKind.Utc);
632 DateTime converted = TimeZoneInfo.ConvertTimeFromUtc (utc, TimeZoneInfo.Local);
633 var expectedKind = (TimeZoneInfo.Local == TimeZoneInfo.Utc)? DateTimeKind.Utc : DateTimeKind.Local;
634 Assert.AreEqual (expectedKind, converted.Kind);
635 DateTime back = TimeZoneInfo.ConvertTimeToUtc (converted, TimeZoneInfo.Local);
636 Assert.AreEqual (back.Kind, DateTimeKind.Utc);
637 Assert.AreEqual (utc, back);
641 public void ConvertToTimeZone ()
643 TimeZoneInfo.ConvertTime (DateTime.Now, TimeZoneInfo.FindSystemTimeZoneById (MapTimeZoneId ("Pacific/Auckland")));
647 [ExpectedException (typeof (ArgumentNullException))]
648 public void ConvertTime_DateTime_TimeZoneInfo_DestinationTimeZoneIsNull ()
650 TimeZoneInfo.ConvertTime (DateTime.Now, null);
654 public void ConvertTime_DateTime_TimeZoneInfo_DateTimeKindMatch ()
656 var sdt = new DateTime (2014, 1, 9, 23, 0, 0, DateTimeKind.Utc);
657 var ddt = TimeZoneInfo.ConvertTime (sdt, TimeZoneInfo.Utc);
658 Assert.AreEqual (ddt.Kind, sdt.Kind, "#1.1");
659 Assert.AreEqual (ddt.Kind, DateTimeKind.Utc, "#1.2");
661 sdt = new DateTime (2014, 1, 9, 23, 0, 0, DateTimeKind.Local);
662 ddt = TimeZoneInfo.ConvertTime (sdt, TimeZoneInfo.Local);
663 Assert.AreEqual (ddt.Kind, sdt.Kind, "#2.1");
664 Assert.AreEqual (ddt.Kind, DateTimeKind.Local, "#2.2");
666 sdt = new DateTime (2014, 1, 9, 23, 0, 0);
667 ddt = TimeZoneInfo.ConvertTime (sdt, TimeZoneInfo.Local);
668 var expectedKind = (TimeZoneInfo.Local == TimeZoneInfo.Utc)? DateTimeKind.Utc : sdt.Kind;
669 Assert.AreEqual (expectedKind, ddt.Kind, "#3.1");
670 Assert.AreEqual (DateTimeKind.Unspecified, sdt.Kind, "#3.2");
674 [ExpectedException (typeof (ArgumentNullException))]
675 public void ConverTime_DateTime_TimeZoneInfo_TimeZoneInfo_SourceTimeZoneIsNull ()
677 TimeZoneInfo.ConvertTime (DateTime.Now, null, TimeZoneInfo.Local);
681 [ExpectedException (typeof (ArgumentNullException))]
682 public void ConverTime_DateTime_TimeZoneInfo_TimeZoneInfo_DestinationTimeZoneIsNull ()
684 TimeZoneInfo.ConvertTime (DateTime.Now, TimeZoneInfo.Utc, null);
687 [Test (Description="Fix for xambug https://bugzilla.xamarin.com/show_bug.cgi?id=17155")]
688 public void ConvertTime_AdjustmentRuleAfterNewYears ()
690 TimeZoneInfo tz = TimeZoneInfo.FindSystemTimeZoneById (MapTimeZoneId ("Pacific/Auckland"));
692 // DST start: 9/29/2013 2:00:00 AM
693 // DST end: 4/6/2014 3:00:00 AM
694 DateTime sdt = new DateTime (2014, 1, 9, 23, 0, 0, DateTimeKind.Utc);
695 DateTime ddt = TimeZoneInfo.ConvertTime (sdt, tz);
696 Assert.AreEqual (10, ddt.Day, "#1.1");
697 Assert.AreEqual (1, ddt.Month, "#1.2");
698 Assert.AreEqual (2014, ddt.Year, "#1.3");
699 Assert.AreEqual (12, ddt.Hour, "#1.4");
700 Assert.AreEqual (0, ddt.Minute, "#1.5");
701 Assert.AreEqual (0, ddt.Second, "#1.6");
703 // DST start: 9/29/2014 2:00:00 AM
704 // DST end: 4/6/2015 3:00:00 AM
705 sdt = new DateTime (2014, 6, 9, 23, 0, 0, DateTimeKind.Utc);
706 ddt = TimeZoneInfo.ConvertTime (sdt, tz);
707 Assert.AreEqual (10, ddt.Day, "#2.1");
708 Assert.AreEqual (6, ddt.Month, "#2.2");
709 Assert.AreEqual (2014, ddt.Year, "#2.3");
710 Assert.AreEqual (11, ddt.Hour, "#2.4");
711 Assert.AreEqual (0, ddt.Minute, "#2.5");
712 Assert.AreEqual (0, ddt.Second, "#2.6");
714 // DST start: 9/29/2014 2:00:00 AM
715 // DST end: 4/6/2015 3:00:00 AM
716 sdt = new DateTime (2014, 10, 9, 23, 0, 0, DateTimeKind.Utc);
717 ddt = TimeZoneInfo.ConvertTime (sdt, tz);
718 Assert.AreEqual (10, ddt.Day, "#3.1");
719 Assert.AreEqual (10, ddt.Month, "#3.2");
720 Assert.AreEqual (2014, ddt.Year, "#3.3");
721 Assert.AreEqual (12, ddt.Hour, "#3.4");
722 Assert.AreEqual (0, ddt.Minute, "#3.5");
723 Assert.AreEqual (0, ddt.Second, "#3.6");
726 [Test (Description="Fix the bug https://bugzilla.xamarin.com/show_bug.cgi?id=1849")]
727 public void ConvertTime_AjustmentConvertTimeWithSourceTimeZone () {
729 TimeZoneInfo easternTimeZone = TimeZoneInfo.FindSystemTimeZoneById (MapTimeZoneId ("US/Eastern"));
730 TimeZoneInfo pacificTimeZone = TimeZoneInfo.FindSystemTimeZoneById (MapTimeZoneId ("US/Pacific"));
732 DateTime lastMidnight = new DateTime (new DateTime (2012, 06, 13).Ticks, DateTimeKind.Unspecified);
733 DateTime lastMidnightAsEST = TimeZoneInfo.ConvertTime (lastMidnight, pacificTimeZone, easternTimeZone);
734 DateTime lastMidnightAsPST = TimeZoneInfo.ConvertTime (lastMidnightAsEST, easternTimeZone, pacificTimeZone);
736 // Last midnight in PST as EST should be 3AM
737 DateTime expectedDate = new DateTime (2012, 06, 13, 3, 0, 0);
739 Assert.AreEqual (expectedDate, lastMidnightAsEST);
740 Assert.AreEqual (lastMidnight, lastMidnightAsPST);
744 public void ConvertTimeBySystemTimeZoneId_UtcId ()
746 DateTime localTime = TimeZoneInfo.ConvertTime (DateTime.UtcNow, TimeZoneInfo.Utc, TimeZoneInfo.Local);
748 TimeZoneInfo.ConvertTimeBySystemTimeZoneId (DateTime.UtcNow, TimeZoneInfo.Utc.Id, TimeZoneInfo.Local.Id);
753 public class IsInvalidTimeTests
758 public void CreateTimeZones ()
760 TimeZoneInfo.TransitionTime start = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,1,0,0), 3, 5, DayOfWeek.Sunday);
761 TimeZoneInfo.TransitionTime end = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,2,0,0), 10, 5, DayOfWeek.Sunday);
762 TimeZoneInfo.AdjustmentRule rule = TimeZoneInfo.AdjustmentRule.CreateAdjustmentRule (DateTime.MinValue.Date, DateTime.MaxValue.Date, new TimeSpan (1,0,0), start, end);
763 london = TimeZoneInfo.CreateCustomTimeZone ("Europe/London", new TimeSpan (0), "Europe/London", "British Standard Time", "British Summer Time", new TimeZoneInfo.AdjustmentRule [] {rule});
768 public void UTCDate ()
770 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)) {
771 date = DateTime.SpecifyKind (date, DateTimeKind.Utc);
772 Assert.IsFalse (london.IsInvalidTime (date));
777 public void InvalidDates ()
779 Assert.IsFalse (london.IsInvalidTime (new DateTime (2007, 03, 25, 0, 59, 59)));
780 Assert.IsTrue (london.IsInvalidTime (new DateTime (2007, 03, 25, 1, 0, 0)));
781 Assert.IsTrue (london.IsInvalidTime (new DateTime (2007, 03, 25, 1, 59, 59)));
782 Assert.IsFalse (london.IsInvalidTime (new DateTime (2007, 03, 25, 2, 0, 0)));
787 public class IsAmbiguousTimeTests
792 public void CreateTimeZones ()
794 TimeZoneInfo.TransitionTime start = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,1,0,0), 3, 5, DayOfWeek.Sunday);
795 TimeZoneInfo.TransitionTime end = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,2,0,0), 10, 5, DayOfWeek.Sunday);
796 TimeZoneInfo.AdjustmentRule rule = TimeZoneInfo.AdjustmentRule.CreateAdjustmentRule (DateTime.MinValue.Date, DateTime.MaxValue.Date, new TimeSpan (1,0,0), start, end);
797 london = TimeZoneInfo.CreateCustomTimeZone ("Europe/London", new TimeSpan (0), "Europe/London", "British Standard Time", "British Summer Time", new TimeZoneInfo.AdjustmentRule [] {rule});
801 public void AmbiguousDates ()
803 Assert.IsFalse (london.IsAmbiguousTime (new DateTime (2007, 10, 28, 1, 0, 0)));
804 Assert.IsTrue (london.IsAmbiguousTime (new DateTime (2007, 10, 28, 1, 0, 1)));
805 Assert.IsTrue (london.IsAmbiguousTime (new DateTime (2007, 10, 28, 2, 0, 0)));
806 Assert.IsFalse (london.IsAmbiguousTime (new DateTime (2007, 10, 28, 2, 0, 1)));
810 public void AmbiguousUTCDates ()
812 Assert.IsFalse (london.IsAmbiguousTime (new DateTime (2007, 10, 28, 0, 0, 0, DateTimeKind.Utc)));
813 Assert.IsTrue (london.IsAmbiguousTime (new DateTime (2007, 10, 28, 0, 0, 1, DateTimeKind.Utc)));
814 Assert.IsTrue (london.IsAmbiguousTime (new DateTime (2007, 10, 28, 0, 59, 59, DateTimeKind.Utc)));
815 Assert.IsFalse (london.IsAmbiguousTime (new DateTime (2007, 10, 28, 1, 0, 0, DateTimeKind.Utc)));
820 public void AmbiguousInUTC ()
822 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)) {
823 Assert.IsFalse (TimeZoneInfo.Utc.IsAmbiguousTime (date));
830 public class GetSystemTimeZonesTests
833 public void Identity ()
835 Assert.AreSame (TimeZoneInfo.GetSystemTimeZones (), TimeZoneInfo.GetSystemTimeZones ());
839 public void NotEmpty ()
841 global::System.Collections.ObjectModel.ReadOnlyCollection<TimeZoneInfo> systemTZ = TimeZoneInfo.GetSystemTimeZones ();
842 Assert.IsNotNull(systemTZ, "SystemTZ is null");
843 Assert.IsFalse (systemTZ.Count == 0, "SystemTZ is empty");
847 public void ContainsBrussels ()
849 global::System.Collections.ObjectModel.ReadOnlyCollection<TimeZoneInfo> systemTZ = TimeZoneInfo.GetSystemTimeZones ();
850 foreach (TimeZoneInfo tz in systemTZ) {
851 if (tz.Id == MapTimeZoneId ("Europe/Brussels"))
854 Assert.Fail ("Europe/Brussels not found in SystemTZ");
858 public void ReflectionReturnsTheCorrectMethod ()
860 var method = (MethodInfo) typeof (TimeZoneInfo).GetMember ("GetSystemTimeZones", MemberTypes.Method, BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic)[0];
862 var timeZones = (global::System.Collections.ObjectModel.ReadOnlyCollection<TimeZoneInfo>) method.Invoke (null, null);
863 Assert.IsTrue (timeZones.Count > 0, "GetSystemTimeZones should not return an empty collection.");
868 public void WindowsRegistryTimezoneWithParentheses ()
870 var method = (MethodInfo) typeof (TimeZoneInfo).GetMember ("TrimSpecial", MemberTypes.Method, BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic)[0];
872 var name = method.Invoke (null, new object [] { " <---> Central Standard Time (Mexico) ||<<>>" });
873 Assert.AreEqual (name, "Central Standard Time (Mexico)", "#1");
879 public class FindSystemTimeZoneByIdTests
882 [ExpectedException (typeof (ArgumentNullException))]
883 public void NullId ()
885 TimeZoneInfo.FindSystemTimeZoneById (null);
889 [ExpectedException (typeof (TimeZoneNotFoundException))]
890 public void NonSystemTimezone ()
892 TimeZoneInfo.FindSystemTimeZoneById ("Neverland/The_Lagoon");
896 public void FindBrusselsTZ ()
898 TimeZoneInfo brussels = TimeZoneInfo.FindSystemTimeZoneById (MapTimeZoneId ("Europe/Brussels"));
899 Assert.IsNotNull (brussels);
903 public void OffsetIsCorrectInKinshasa ()
905 TimeZoneInfo kin = TimeZoneInfo.FindSystemTimeZoneById (MapTimeZoneId ("Africa/Kinshasa"));
906 Assert.AreEqual (new TimeSpan (1,0,0), kin.BaseUtcOffset, "BaseUtcOffset in Kinshasa is not +1h");
910 public void OffsetIsCorrectInBrussels ()
912 TimeZoneInfo brussels = TimeZoneInfo.FindSystemTimeZoneById (MapTimeZoneId ("Europe/Brussels"));
913 Assert.AreEqual (new TimeSpan (1,0,0), brussels.BaseUtcOffset, "BaseUtcOffset for Brussels is not +1h");
917 public void NoDSTInKinshasa ()
919 TimeZoneInfo kin = TimeZoneInfo.FindSystemTimeZoneById (MapTimeZoneId ("Africa/Kinshasa"));
920 Assert.IsFalse (kin.SupportsDaylightSavingTime);
924 public void BrusselsSupportsDST ()
926 TimeZoneInfo brussels = TimeZoneInfo.FindSystemTimeZoneById (MapTimeZoneId ("Europe/Brussels"));
927 Assert.IsTrue (brussels.SupportsDaylightSavingTime);
931 public void MelbourneSupportsDST ()
933 TimeZoneInfo melbourne = TimeZoneInfo.FindSystemTimeZoneById (MapTimeZoneId ("Australia/Melbourne"));
934 Assert.IsTrue (melbourne.SupportsDaylightSavingTime);
938 public void RomeAndVaticanSharesTime ()
940 TimeZoneInfo rome = TimeZoneInfo.FindSystemTimeZoneById (MapTimeZoneId ("Europe/Rome"));
941 TimeZoneInfo vatican = TimeZoneInfo.FindSystemTimeZoneById (MapTimeZoneId ("Europe/Vatican"));
942 Assert.IsTrue (rome.HasSameRules (vatican));
946 public void FindSystemTimeZoneById_Local_Roundtrip ()
948 Assert.AreEqual (TimeZoneInfo.Local.Id, TimeZoneInfo.FindSystemTimeZoneById (TimeZoneInfo.Local.Id).Id);
952 public void Test326 ()
954 DateTime utc = DateTime.UtcNow;
955 DateTime local = TimeZoneInfo.ConvertTime (utc, TimeZoneInfo.Utc, TimeZoneInfo.FindSystemTimeZoneById (TimeZoneInfo.Local.Id));
956 Assert.AreEqual (local, utc + TimeZoneInfo.Local.GetUtcOffset (utc), "ConvertTime/Local");
961 public void BrusselsAdjustments ()
963 TimeZoneInfo.TransitionTime start = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,2,0,0), 3, 5, DayOfWeek.Sunday);
964 TimeZoneInfo.TransitionTime end = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,3,0,0), 10, 5, DayOfWeek.Sunday);
965 TimeZoneInfo.AdjustmentRule rule = TimeZoneInfo.AdjustmentRule.CreateAdjustmentRule (DateTime.MinValue.Date, DateTime.MaxValue.Date, new TimeSpan (1,0,0), start, end);
966 TimeZoneInfo brussels = TimeZoneInfo.CreateCustomTimeZone ("Europe/Brussels", new TimeSpan (1, 0, 0), "Europe/Brussels", "", "", new TimeZoneInfo.AdjustmentRule [] {rule});
968 TimeZoneInfo brussels_sys = TimeZoneInfo.FindSystemTimeZoneById ("Europe/Brussels");
970 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)) {
971 Assert.AreEqual (brussels.GetUtcOffset (date), brussels_sys.GetUtcOffset (date));
972 Assert.AreEqual (brussels.IsDaylightSavingTime (date), brussels_sys.IsDaylightSavingTime (date));
978 public void SubminuteDSTOffsets ()
980 if (Environment.OSVersion.Platform != PlatformID.Unix)
983 var subMinuteDSTs = new string [] {
984 "Europe/Dublin", // Europe/Dublin has a DST offset of 34 minutes and 39 seconds in 1916.
987 "Canada/Newfoundland",
991 foreach (var tz in subMinuteDSTs) {
992 TimeZoneInfo.FindSystemTimeZoneById (tz);
997 [ExpectedException (typeof (TimeZoneNotFoundException))]
998 public void InvalidName ()
1000 TimeZoneInfo.FindSystemTimeZoneById ("N/A");
1005 public class GetAmbiguousTimeOffsetsTests
1008 [ExpectedException (typeof(ArgumentException))]
1009 public void DateIsNotAmbiguous ()
1011 TimeZoneInfo brussels = TimeZoneInfo.FindSystemTimeZoneById (MapTimeZoneId ("Europe/Brussels"));
1012 DateTime date = new DateTime (2007, 05, 11, 11, 40, 00);
1013 brussels.GetAmbiguousTimeOffsets (date);
1017 public void AmbiguousOffsets ()
1019 TimeZoneInfo brussels = TimeZoneInfo.FindSystemTimeZoneById (MapTimeZoneId ("Europe/Brussels"));
1020 DateTime date = new DateTime (2007, 10, 28, 2, 30, 00);
1021 Assert.IsTrue (brussels.IsAmbiguousTime (date));
1022 Assert.AreEqual (2, brussels.GetAmbiguousTimeOffsets (date).Length);
1023 Assert.AreEqual (new TimeSpan[] {new TimeSpan (1, 0, 0), new TimeSpan (2, 0, 0)}, brussels.GetAmbiguousTimeOffsets (date));
1028 public class HasSameRulesTests
1031 public void NullAdjustments () //bnc #391011
1033 TimeZoneInfo utc = TimeZoneInfo.Utc;
1034 TimeZoneInfo custom = TimeZoneInfo.CreateCustomTimeZone ("Custom", new TimeSpan (0), "Custom", "Custom");
1035 Assert.IsTrue (utc.HasSameRules (custom));
1040 public class SerializationTests
1043 public void Serialization_Deserialization ()
1045 TimeZoneInfo.TransitionTime start = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,1,0,0), 3, 5, DayOfWeek.Sunday);
1046 TimeZoneInfo.TransitionTime end = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,2,0,0), 10, 5, DayOfWeek.Sunday);
1047 TimeZoneInfo.AdjustmentRule rule = TimeZoneInfo.AdjustmentRule.CreateAdjustmentRule (DateTime.MinValue.Date, DateTime.MaxValue.Date, new TimeSpan (1,0,0), start, end);
1048 TimeZoneInfo london = TimeZoneInfo.CreateCustomTimeZone ("Europe/London", new TimeSpan (0), "Europe/London", "British Standard Time", "British Summer Time", new TimeZoneInfo.AdjustmentRule [] {rule});
1049 MemoryStream stream = new MemoryStream ();
1050 BinaryFormatter formatter = new BinaryFormatter ();
1051 formatter.Serialize (stream, london);
1052 stream.Position = 0;
1053 TimeZoneInfo deserialized = (TimeZoneInfo) formatter.Deserialize (stream);
1056 Assert.IsTrue (london.Equals (deserialized));
1061 public class MultipleDaylightSavingTimeTests {
1062 private TimeZoneInfo cairo;
1063 private DateTime dst1Start;
1064 private DateTime dst1End;
1065 private DateTime dst2Start;
1066 private DateTime dst2End;
1068 private TimeSpan baseUtcOffset;
1069 private TimeSpan dstUtcOffset;
1070 private TimeSpan dstOffset;
1073 public void CreateTimeZones ()
1076 From 1/1/2014 12:00:00 AM to 6/30/2014 12:00:00 AM
1078 Begins at 12:00 AM on 16 May
1079 Ends at 1:00 AM on 29 June
1080 From 7/1/2014 12:00:00 AM to 12/31/2014 12:00:00 AM
1082 Begins at 12:00 AM on 29 July
1083 Ends at 12:00 AM on 26 September
1085 dst1Start = new DateTime (2014, 5, 16);
1086 dst1End = new DateTime (2014, 6, 29);
1087 dst2Start = new DateTime (2014, 7, 29);
1088 dst2End = new DateTime (2014, 9, 26);
1090 baseUtcOffset = new TimeSpan (2, 0, 0);
1091 dstUtcOffset = new TimeSpan (3, 0, 0);
1092 dstOffset = dstUtcOffset - baseUtcOffset;
1094 var rule1 = TimeZoneInfo.AdjustmentRule.CreateAdjustmentRule (
1095 new DateTime (2014, 1, 1), new DateTime (2014, 6, 30), dstOffset,
1096 CreateFixedDateRule (dst1Start), CreateFixedDateRule (dst1End));
1098 var rule2 = TimeZoneInfo.AdjustmentRule.CreateAdjustmentRule (
1099 new DateTime (2014, 7, 1), new DateTime (2014, 12, 31), dstOffset,
1100 CreateFixedDateRule (dst2Start), CreateFixedDateRule (dst2End));
1102 cairo = TimeZoneInfo.CreateCustomTimeZone ("Africa/Cairo", baseUtcOffset, "Africa/Cairo", "EET", "EEST",
1103 new [] {rule1, rule2});
1106 private static TimeZoneInfo.TransitionTime CreateFixedDateRule (DateTime dateTime)
1108 var time = new DateTime (dateTime.Ticks - dateTime.Date.Ticks);
1109 return TimeZoneInfo.TransitionTime.CreateFixedDateRule (time, dateTime.Month, dateTime.Day);
1113 public void GetUtcOffset_FromUTC ()
1115 var d = dst1Start.Add (-baseUtcOffset);
1116 d = DateTime.SpecifyKind (d, DateTimeKind.Utc);
1117 Assert.AreEqual(baseUtcOffset, cairo.GetUtcOffset (d.Add (new TimeSpan(0,0,0,-1))));
1118 Assert.AreEqual(dstUtcOffset, cairo.GetUtcOffset (d));
1119 Assert.AreEqual(dstUtcOffset, cairo.GetUtcOffset (d.Add (new TimeSpan(0,0,0, 1))));
1121 d = dst1End.Add (-baseUtcOffset-dstOffset);
1122 d = DateTime.SpecifyKind (d, DateTimeKind.Utc);
1123 Assert.AreEqual(dstUtcOffset, cairo.GetUtcOffset (d.Add (new TimeSpan(0,0,0,-1))));
1124 Assert.AreEqual(baseUtcOffset, cairo.GetUtcOffset (d));
1125 Assert.AreEqual(baseUtcOffset, cairo.GetUtcOffset (d.Add (new TimeSpan(0,0,0, 1))));
1127 d = dst2Start.Add (-baseUtcOffset);
1128 d = DateTime.SpecifyKind (d, DateTimeKind.Utc);
1129 Assert.AreEqual(baseUtcOffset, cairo.GetUtcOffset (d.Add (new TimeSpan(0,0,0,-1))));
1130 Assert.AreEqual(dstUtcOffset, cairo.GetUtcOffset (d));
1131 Assert.AreEqual(dstUtcOffset, cairo.GetUtcOffset (d.Add (new TimeSpan(0,0,0, 1))));
1133 d = dst2End.Add (-baseUtcOffset-dstOffset);
1134 d = DateTime.SpecifyKind (d, DateTimeKind.Utc);
1135 Assert.AreEqual(dstUtcOffset, cairo.GetUtcOffset (d.Add (new TimeSpan(0,0,0,-1))));
1136 Assert.AreEqual(baseUtcOffset, cairo.GetUtcOffset (d));
1137 Assert.AreEqual(baseUtcOffset, cairo.GetUtcOffset (d.Add (new TimeSpan(0,0,0, 1))));
1141 public void GetUtcOffset_FromLocal ()
1143 var d = dst1Start.Add (-baseUtcOffset);
1144 d = DateTime.SpecifyKind (d, DateTimeKind.Utc);
1145 d = d.ToLocalTime ();
1146 Assert.AreEqual(baseUtcOffset, cairo.GetUtcOffset (d.Add (new TimeSpan(0,0,0,-1))));
1147 Assert.AreEqual(dstUtcOffset, cairo.GetUtcOffset (d));
1148 Assert.AreEqual(dstUtcOffset, cairo.GetUtcOffset (d.Add (new TimeSpan(0,0,0, 1))));
1150 d = dst1End.Add (-baseUtcOffset-dstOffset);
1151 d = DateTime.SpecifyKind (d, DateTimeKind.Utc);
1152 d = d.ToLocalTime ();
1153 Assert.AreEqual(dstUtcOffset, cairo.GetUtcOffset (d.Add (new TimeSpan(0,0,0,-1))));
1154 Assert.AreEqual(baseUtcOffset, cairo.GetUtcOffset (d));
1155 Assert.AreEqual(baseUtcOffset, cairo.GetUtcOffset (d.Add (new TimeSpan(0,0,0, 1))));
1157 d = dst2Start.Add (-baseUtcOffset);
1158 d = DateTime.SpecifyKind (d, DateTimeKind.Utc);
1159 d = d.ToLocalTime ();
1160 Assert.AreEqual(baseUtcOffset, cairo.GetUtcOffset (d.Add (new TimeSpan(0,0,0,-1))));
1161 Assert.AreEqual(dstUtcOffset, cairo.GetUtcOffset (d));
1162 Assert.AreEqual(dstUtcOffset, cairo.GetUtcOffset (d.Add (new TimeSpan(0,0,0, 1))));
1164 d = dst2End.Add (-baseUtcOffset-dstOffset);
1165 d = DateTime.SpecifyKind (d, DateTimeKind.Utc);
1166 d = d.ToLocalTime ();
1167 Assert.AreEqual(dstUtcOffset, cairo.GetUtcOffset (d.Add (new TimeSpan(0,0,0,-1))));
1168 Assert.AreEqual(baseUtcOffset, cairo.GetUtcOffset (d));
1169 Assert.AreEqual(baseUtcOffset, cairo.GetUtcOffset (d.Add (new TimeSpan(0,0,0, 1))));
1173 public void GetUtcOffset_FromUnspecified ()
1175 var d = dst1Start.Add (dstOffset);
1176 Assert.AreEqual(baseUtcOffset, cairo.GetUtcOffset (d.Add (new TimeSpan(0,0,0,-1))));
1177 Assert.AreEqual(dstUtcOffset, cairo.GetUtcOffset (d));
1178 Assert.AreEqual(dstUtcOffset, cairo.GetUtcOffset (d.Add (new TimeSpan(0,0,0, 1))));
1180 d = dst1End.Add (-dstOffset);
1181 Assert.AreEqual(dstUtcOffset, cairo.GetUtcOffset (d.Add (new TimeSpan(0,0,0,-1))));
1182 Assert.AreEqual(dstUtcOffset, cairo.GetUtcOffset (d));
1183 Assert.AreEqual(baseUtcOffset, cairo.GetUtcOffset (d.Add (new TimeSpan(0,1,0, 1))));
1185 d = dst2Start.Add (dstOffset);
1186 Assert.AreEqual(baseUtcOffset, cairo.GetUtcOffset (d.Add (new TimeSpan(0,0,0,-1))));
1187 Assert.AreEqual(dstUtcOffset, cairo.GetUtcOffset (d));
1188 Assert.AreEqual(dstUtcOffset, cairo.GetUtcOffset (d.Add (new TimeSpan(0,0,0, 1))));
1190 d = dst2End.Add (-dstOffset);
1191 Assert.AreEqual(dstUtcOffset, cairo.GetUtcOffset (d.Add (new TimeSpan(0,0,0,-1))));
1192 Assert.AreEqual(dstUtcOffset, cairo.GetUtcOffset (d));
1193 Assert.AreEqual(baseUtcOffset, cairo.GetUtcOffset (d.Add (new TimeSpan(0,1,0, 1))));
1197 public void GetUtcOffset_FromDateTimeOffset ()
1199 DateTimeOffset offset;
1201 offset = new DateTimeOffset(dst1Start, baseUtcOffset);
1202 Assert.AreEqual(baseUtcOffset, cairo.GetUtcOffset(offset.Add(new TimeSpan(0, 0, 0, -1))), "dst1Start_with_baseUtcOffset#before");
1203 Assert.AreEqual(dstUtcOffset, cairo.GetUtcOffset(offset), "dst1Start_with_baseUtcOffset#exact");
1204 Assert.AreEqual(dstUtcOffset, cairo.GetUtcOffset(offset.Add(new TimeSpan(0, 0, 0, 1))), "dst1Start_with_baseUtcOffset#after");
1206 offset = new DateTimeOffset(dst1End, dstOffset + baseUtcOffset);
1207 Assert.AreEqual(dstUtcOffset, cairo.GetUtcOffset(offset.Add(new TimeSpan(0, 0, 0, -1))), "dst1End_with_dstOffset+baseUtcOffset#before");
1208 Assert.AreEqual(baseUtcOffset, cairo.GetUtcOffset(offset), "dst1End_with_dstOffset+baseUtcOffset#exact");
1209 Assert.AreEqual(baseUtcOffset, cairo.GetUtcOffset(offset.Add(new TimeSpan(0, 0, 0, 1))), "dst1End_with_dstOffset+baseUtcOffset#after");
1211 offset = new DateTimeOffset(dst2Start, baseUtcOffset);
1212 Assert.AreEqual(baseUtcOffset, cairo.GetUtcOffset(offset.Add(new TimeSpan(0, 0, 0, -1))), "dst2Start_with_baseUtcOffset#before");
1213 Assert.AreEqual(dstUtcOffset, cairo.GetUtcOffset(offset), "dst2Start_with_baseUtcOffset#exact");
1214 Assert.AreEqual(dstUtcOffset, cairo.GetUtcOffset(offset.Add(new TimeSpan(0, 0, 0, 1))), "dst2Start_with_baseUtcOffset#after");
1216 offset = new DateTimeOffset(dst2End, baseUtcOffset + dstOffset);
1217 Assert.AreEqual(dstUtcOffset, cairo.GetUtcOffset(offset.Add(new TimeSpan(0, 0, 0, -1))), "dst2End_with_dstOffset+baseUtcOffset#before");
1218 Assert.AreEqual(baseUtcOffset, cairo.GetUtcOffset(offset), "dst2End_with_dstOffset+baseUtcOffset#exact");
1219 Assert.AreEqual(baseUtcOffset, cairo.GetUtcOffset(offset.Add(new TimeSpan(0, 0, 0, 1))), "dst2End_with_dstOffset+baseUtcOffset#after");
1223 public void DTS_WithMinimalDate ()
1225 TimeZoneInfo.TransitionTime startTransition, endTransition;
1226 startTransition = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1, 1, 1, 4, 0, 0),
1227 10, 2, DayOfWeek.Sunday);
1228 endTransition = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1, 1, 1, 3, 0, 0),
1229 3, 2, DayOfWeek.Sunday);
1231 var ctz = TimeZoneInfo.CreateCustomTimeZone ("test", TimeSpan.FromHours (-5), "display", "sdisplay", "dst", new [] {
1232 TimeZoneInfo.AdjustmentRule.CreateAdjustmentRule (DateTime.MinValue, DateTime.MaxValue.Date, TimeSpan.FromHours (-1), startTransition, endTransition) });
1234 var offset = ctz.GetUtcOffset (DateTime.MinValue);
1235 Assert.AreEqual (TimeSpan.FromHours (-5), offset); // TODO: Wrong it should be -6
1240 public class GetDaylightChanges
1242 MethodInfo getChanges;
1245 public void Setup ()
1247 var flags = BindingFlags.Instance | BindingFlags.NonPublic;
1248 getChanges = typeof (TimeZoneInfo).GetMethod ("GetDaylightChanges", flags);
1252 public void TestSydneyDaylightChanges ()
1254 TimeZoneInfo tz = TimeZoneInfo.FindSystemTimeZoneById (MapTimeZoneId ("Australia/Sydney"));
1256 var changes = (DaylightTime) getChanges.Invoke (tz, new object [] {2014});
1258 Assert.AreEqual (new TimeSpan (1, 0, 0), changes.Delta);
1259 Assert.AreEqual (new DateTime (2014, 10, 5, 2, 0, 0), changes.Start);
1260 Assert.AreEqual (new DateTime (2014, 4, 6, 3, 0, 0), changes.End);
1264 public void TestAthensDaylightChanges ()
1266 TimeZoneInfo tz = TimeZoneInfo.FindSystemTimeZoneById (MapTimeZoneId ("Europe/Athens"));
1268 var changes = (DaylightTime) getChanges.Invoke (tz, new object [] {2014});
1270 Assert.AreEqual (new TimeSpan (1, 0, 0), changes.Delta);
1271 Assert.AreEqual (new DateTime (2014, 3, 30, 3, 0, 0), changes.Start);
1272 Assert.AreEqual (new DateTime (2014, 10, 26, 4, 0, 0), changes.End);
1276 public void AllTimeZonesDaylightChanges ()
1278 foreach (var tz in TimeZoneInfo.GetSystemTimeZones ()) {
1280 for (var year = 1950; year <= 2051; year++)
1281 getChanges.Invoke (tz, new object [] {year} );
1282 } catch (Exception e) {
1283 Assert.Fail ("TimeZone " + tz.Id + " exception: " + e.ToString ());
1290 public class ParseTZBuffer
1292 MethodInfo parseTZBuffer;
1297 var flags = BindingFlags.Static | BindingFlags.NonPublic;
1298 parseTZBuffer = typeof (TimeZoneInfo).GetMethod ("ParseTZBuffer", flags);
1302 public void Bug31432 ()
1304 // Europe/Moscow from failing device
1305 var base64Data = "VFppZjIAAAAAAAAAAAAAAAAAAAAAAAAPAAAADwAAAAAAAABNAAAADwAAACKbXx7HnT7yeZ4q7vme9zlpn4RX+aDYbOmhABYJoTymQKQQbcCkPTKwpRVosKU9A8CnHkVQtaQZYBUnp9AWGNxAFwjbUBf6D8AY6g7QGdtDQBrMk9AbvKDwHKyR8B2cgvAejHPwH3xk8CBsVfAhXEbwIkw38CM8KPAkLBnwJRwK8CYL+/AnBSdwJ/UYcCjlF4ApeL+AKdTQQCrEszArtNxwLKTNcC2UvnAuhK9wL3SgcDBkkXAxXbzwMnKX8DM9nvA0UnnwNR2A8DYyW/A2/WLwOBt4cDjdRPA5+1pwOr0m8DvbPHA8pkNwPbsecD6GJXA/mwBwQGYHcEGEHPBCRelwQ2P+8EQly3BFQ+DwRgWtcEcjwvBH7snwSQOk8EnOq/BK44bwS66N8EzMo3BNjm/wVEwdYAIBAgMBAwUEBQYFBwgHCQcJBwkHCQoLCgsKCwoLCgsKCwoMDQoJBwsKCwoLCgsKCwoLCgsKCwoLCgsKCwoLCgsKCwoLCgsKCwoLCg4KAAAjOQAAAAAxhwEEAAAjdwAAAAA/lwEIAAAqMAADAAA4QAENAABGUAEPAAAqMAARAAAcIAAVAAA4QAEZAAAqMAARAAA4QAEZAAAqMAEdAAAcIAAVAAA4QAARTU1UAE1TVABNRFNUAFMATQBNU0sARUVUAE1TRABFRVNUAAAAAAAAAAAAAAABAQEBAQAAAAAAAAAAAAAAAAAAAFRaaWYyAAAAAAAAAAAAAAAAAAAAAAAAEAAAABAAAAAAAAAATgAAABAAAAAm/////1a2wMf/////m18ex/////+dPvJ5/////54q7vn/////nvc5af////+fhFf5/////6DYbOn/////oQAWCf////+hPKZA/////6QQbcD/////pD0ysP////+lFWiw/////6U9A8D/////px5FUP////+1pBlgAAAAABUnp9AAAAAAFhjcQAAAAAAXCNtQAAAAABf6D8AAAAAAGOoO0AAAAAAZ20NAAAAAABrMk9AAAAAAG7yg8AAAAAAcrJHwAAAAAB2cgvAAAAAAHoxz8AAAAAAffGTwAAAAACBsVfAAAAAAIVxG8AAAAAAiTDfwAAAAACM8KPAAAAAAJCwZ8AAAAAAlHArwAAAAACYL+/AAAAAAJwUncAAAAAAn9RhwAAAAACjlF4AAAAAAKXi/gAAAAAAp1NBAAAAAACrEszAAAAAAK7TccAAAAAAspM1wAAAAAC2UvnAAAAAALoSvcAAAAAAvdKBwAAAAADBkkXAAAAAAMV288AAAAAAycpfwAAAAADM9nvAAAAAANFJ58AAAAAA1HYDwAAAAADYyW/AAAAAANv1i8AAAAAA4G3hwAAAAADjdRPAAAAAAOftacAAAAAA6vSbwAAAAADvbPHAAAAAAPKZDcAAAAAA9ux5wAAAAAD6GJXAAAAAAP5sAcAAAAABAZgdwAAAAAEGEHPAAAAAAQkXpcAAAAABDY/7wAAAAAEQly3AAAAAARUPg8AAAAABGBa1wAAAAAEcjwvAAAAAAR+7J8AAAAABJA6TwAAAAAEnOq/AAAAAASuOG8AAAAABLro3wAAAAAEzMo3AAAAAATY5v8AAAAABUTB1gAQMCAwQCBAYFBgcGCAkICggKCAoICgsMCwwLDAsMCwwLDAsNDgsKCAwLDAsMCwwLDAsMCwwLDAsMCwwLDAsMCwwLDAsMCwwLDAsMCw8LAAAjOQAAAAAjOQAEAAAxhwEIAAAjdwAEAAA/lwEMAAAqMAADAAA4QAERAABGUAETAAAqMAAVAAAcIAAZAAA4QAEdAAAqMAAVAAA4QAEdAAAqMAEhAAAcIAAZAAA4QAAVTE1UAE1NVABNU1QATURTVABTAE0ATVNLAEVFVABNU0QARUVTVAAAAAAAAAAAAAAAAAEBAQEBAAAAAAAAAAAAAAAAAAAAAApNU0stMwo=";
1307 var data = Convert.FromBase64String (base64Data);
1309 var tz = parseTZBuffer.Invoke (null, new object[] { "Test", data, data.Length});
1310 Assert.IsTrue (tz != null);