5 * Stephane Delcroix <stephane@delcroix.org>
7 * Copyright 2011 Xamarin Inc.
9 * Permission is hereby granted, free of charge, to any person obtaining
10 * a copy of this software and associated documentation files (the
11 * "Software"), to deal in the Software without restriction, including
12 * without limitation the rights to use, copy, modify, merge, publish,
13 * distribute, sublicense, and/or sell copies of the Software, and to
14 * permit persons to whom the Software is furnished to do so, subject to
15 * the following conditions:
17 * The above copyright notice and this permission notice shall be
18 * included in all copies or substantial portions of the Software.
20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 using System.Runtime.Serialization.Formatters.Binary;
32 using System.Collections;
34 using NUnit.Framework;
36 namespace MonoTests.System
38 public class TimeZoneInfoTest
41 public class PropertiesTests
44 public void GetLocal ()
46 if (Environment.OSVersion.Platform != PlatformID.Unix)
47 Assert.Ignore ("Not running on Unix.");
48 TimeZoneInfo local = TimeZoneInfo.Local;
49 Assert.IsNotNull (local);
55 public class CreateCustomTimezoneTests
58 [ExpectedException (typeof (ArgumentNullException))]
59 public void IdIsNullException ()
61 TimeZoneInfo.CreateCustomTimeZone (null, new TimeSpan (0), null, null);
65 [ExpectedException (typeof (ArgumentException))]
66 public void IdIsEmptyString ()
68 TimeZoneInfo.CreateCustomTimeZone ("", new TimeSpan (0), null, null);
72 [ExpectedException (typeof (ArgumentException))]
73 public void OffsetIsNotMinutes ()
75 TimeZoneInfo.CreateCustomTimeZone ("mytimezone", new TimeSpan (0, 0, 55), null, null);
79 [ExpectedException (typeof (ArgumentOutOfRangeException))]
80 public void OffsetTooBig ()
82 TimeZoneInfo.CreateCustomTimeZone ("mytimezone", new TimeSpan (14, 1, 0), null, null);
86 [ExpectedException (typeof (ArgumentOutOfRangeException))]
87 public void OffsetTooSmall ()
89 TimeZoneInfo.CreateCustomTimeZone ("mytimezone", - new TimeSpan (14, 1, 0), null, null);
94 [ExpectedException (typeof (ArgumentException))]
95 public void IdLongerThan32 ()
97 TimeZoneInfo.CreateCustomTimeZone ("12345678901234567890123456789012345", new TimeSpan (0), null, null);
102 [ExpectedException (typeof (InvalidTimeZoneException))]
103 public void AdjustmentRulesOverlap ()
105 TimeZoneInfo.TransitionTime s1 = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,4,0,0), 3, 2, DayOfWeek.Sunday);
106 TimeZoneInfo.TransitionTime e1 = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,4,0,0), 10, 2, DayOfWeek.Sunday);
107 TimeZoneInfo.AdjustmentRule r1 = TimeZoneInfo.AdjustmentRule.CreateAdjustmentRule (new DateTime (2000,1,1), new DateTime (2005,1,1), new TimeSpan (1,0,0), s1, e1);
108 TimeZoneInfo.TransitionTime s2 = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,4,0,0), 2, 2, DayOfWeek.Sunday);
109 TimeZoneInfo.TransitionTime e2 = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,4,0,0), 11, 2, DayOfWeek.Sunday);
110 TimeZoneInfo.AdjustmentRule r2 = TimeZoneInfo.AdjustmentRule.CreateAdjustmentRule (new DateTime (2004,1,1), new DateTime (2007,1,1), new TimeSpan (1,0,0), s2, e2);
111 TimeZoneInfo.CreateCustomTimeZone ("mytimezone", new TimeSpan (6,0,0),null,null,null,new TimeZoneInfo.AdjustmentRule[] {r1, r2});
115 [ExpectedException (typeof (InvalidTimeZoneException))]
116 public void RulesNotOrdered ()
118 TimeZoneInfo.TransitionTime s1 = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,4,0,0), 3, 2, DayOfWeek.Sunday);
119 TimeZoneInfo.TransitionTime e1 = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,4,0,0), 10, 2, DayOfWeek.Sunday);
120 TimeZoneInfo.AdjustmentRule r1 = TimeZoneInfo.AdjustmentRule.CreateAdjustmentRule (new DateTime (2000,1,1), new DateTime (2005,1,1), new TimeSpan (1,0,0), s1, e1);
121 TimeZoneInfo.TransitionTime s2 = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,4,0,0), 2, 2, DayOfWeek.Sunday);
122 TimeZoneInfo.TransitionTime e2 = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,4,0,0), 11, 2, DayOfWeek.Sunday);
123 TimeZoneInfo.AdjustmentRule r2 = TimeZoneInfo.AdjustmentRule.CreateAdjustmentRule (new DateTime (2006,1,1), new DateTime (2007,1,1), new TimeSpan (1,0,0), s2, e2);
124 TimeZoneInfo.CreateCustomTimeZone ("mytimezone", new TimeSpan (6,0,0),null,null,null,new TimeZoneInfo.AdjustmentRule[] {r2, r1});
128 [ExpectedException (typeof (InvalidTimeZoneException))]
129 public void OffsetOutOfRange ()
131 TimeZoneInfo.TransitionTime startTransition = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,4,0,0), 3, 2, DayOfWeek.Sunday);
132 TimeZoneInfo.TransitionTime endTransition = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,4,0,0), 10, 2, DayOfWeek.Sunday);
133 TimeZoneInfo.AdjustmentRule rule = TimeZoneInfo.AdjustmentRule.CreateAdjustmentRule (new DateTime (2000,1,1), new DateTime (2005,1,1), new TimeSpan (3,0,0), startTransition, endTransition);
134 TimeZoneInfo.CreateCustomTimeZone ("mytimezone", new TimeSpan (12,0,0),null,null,null,new TimeZoneInfo.AdjustmentRule[] {rule});
138 [ExpectedException (typeof (InvalidTimeZoneException))]
139 public void NullRule ()
141 TimeZoneInfo.CreateCustomTimeZone ("mytimezone", new TimeSpan (12,0,0),null,null,null,new TimeZoneInfo.AdjustmentRule[] {null});
145 [ExpectedException (typeof (InvalidTimeZoneException))]
146 public void MultiplesRulesForDate ()
148 TimeZoneInfo.TransitionTime s1 = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,4,0,0), 3, 2, DayOfWeek.Sunday);
149 TimeZoneInfo.TransitionTime e1 = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,4,0,0), 10, 2, DayOfWeek.Sunday);
150 TimeZoneInfo.AdjustmentRule r1 = TimeZoneInfo.AdjustmentRule.CreateAdjustmentRule (new DateTime (2000,1,1), new DateTime (2005,1,1), new TimeSpan (1,0,0), s1, e1);
151 TimeZoneInfo.TransitionTime s2 = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,4,0,0), 2, 2, DayOfWeek.Sunday);
152 TimeZoneInfo.TransitionTime e2 = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,4,0,0), 11, 2, DayOfWeek.Sunday);
153 TimeZoneInfo.AdjustmentRule r2 = TimeZoneInfo.AdjustmentRule.CreateAdjustmentRule (new DateTime (2005,1,1), new DateTime (2007,1,1), new TimeSpan (1,0,0), s2, e2);
154 TimeZoneInfo.CreateCustomTimeZone ("mytimezone", new TimeSpan (6,0,0),null,null,null,new TimeZoneInfo.AdjustmentRule[] {r1, r2});
158 public void SupportsDaylightSavingTime_NonEmptyAdjustmentRule ()
160 TimeZoneInfo.TransitionTime s1 = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,4,0,0), 3, 2, DayOfWeek.Sunday);
161 TimeZoneInfo.TransitionTime e1 = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,4,0,0), 10, 2, DayOfWeek.Sunday);
162 TimeZoneInfo.AdjustmentRule r1 = TimeZoneInfo.AdjustmentRule.CreateAdjustmentRule (new DateTime (2000,1,1), new DateTime (2005,1,1), new TimeSpan (1,0,0), s1, e1);
163 TimeZoneInfo tz = TimeZoneInfo.CreateCustomTimeZone ("mytimezone", new TimeSpan (6,0,0),null,null,null,new TimeZoneInfo.AdjustmentRule[] {r1});
164 Assert.IsTrue (tz.SupportsDaylightSavingTime);
168 public void SupportsDaylightSavingTime_EmptyAdjustmentRule ()
170 TimeZoneInfo tz = TimeZoneInfo.CreateCustomTimeZone ("mytimezone", new TimeSpan (6,0,0),null,null,null,null);
171 Assert.IsFalse (tz.SupportsDaylightSavingTime);
175 public void SupportsDaylightSavingTime_NonEmptyAdjustmentRule_DisableDaylightSavingTime ()
177 TimeZoneInfo.TransitionTime s1 = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,4,0,0), 3, 2, DayOfWeek.Sunday);
178 TimeZoneInfo.TransitionTime e1 = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,4,0,0), 10, 2, DayOfWeek.Sunday);
179 TimeZoneInfo.AdjustmentRule r1 = TimeZoneInfo.AdjustmentRule.CreateAdjustmentRule (new DateTime (2000,1,1), new DateTime (2005,1,1), new TimeSpan (1,0,0), s1, e1);
180 TimeZoneInfo tz = TimeZoneInfo.CreateCustomTimeZone ("mytimezone", new TimeSpan (6,0,0),null,null,null,new TimeZoneInfo.AdjustmentRule[] {r1}, true);
181 Assert.IsFalse (tz.SupportsDaylightSavingTime);
185 public void SupportsDaylightSavingTime_EmptyAdjustmentRule_DisableDaylightSavingTime ()
187 TimeZoneInfo tz = TimeZoneInfo.CreateCustomTimeZone ("mytimezone", new TimeSpan (6,0,0),null,null,null,null,true);
188 Assert.IsFalse (tz.SupportsDaylightSavingTime);
193 public class IsDaylightSavingTimeTests
198 public void CreateTimeZones ()
200 TimeZoneInfo.TransitionTime start = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,1,0,0), 3, 5, DayOfWeek.Sunday);
201 TimeZoneInfo.TransitionTime end = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,2,0,0), 10, 5, DayOfWeek.Sunday);
202 TimeZoneInfo.AdjustmentRule rule = TimeZoneInfo.AdjustmentRule.CreateAdjustmentRule (DateTime.MinValue.Date, DateTime.MaxValue.Date, new TimeSpan (1,0,0), start, end);
203 london = TimeZoneInfo.CreateCustomTimeZone ("Europe/London", new TimeSpan (0), "Europe/London", "British Standard Time", "British Summer Time", new TimeZoneInfo.AdjustmentRule [] {rule});
207 public void NoDSTInUTC ()
209 DateTime june01 = new DateTime (2007, 06, 01);
210 Assert.IsFalse (TimeZoneInfo.Utc.IsDaylightSavingTime (june01));
214 public void DSTInLondon ()
216 if (Environment.OSVersion.Platform != PlatformID.Unix)
217 Assert.Ignore ("Not running on Unix.");
218 DateTime june01 = new DateTime (2007, 06, 01);
219 DateTime xmas = new DateTime (2007, 12, 25);
220 Assert.IsTrue (london.IsDaylightSavingTime (june01), "June 01 is DST in London");
221 Assert.IsFalse (london.IsDaylightSavingTime (xmas), "Xmas is not DST in London");
225 public void DSTTransisions ()
227 if (Environment.OSVersion.Platform != PlatformID.Unix)
228 Assert.Ignore ("Not running on Unix.");
229 DateTime beforeDST = new DateTime (2007, 03, 25, 0, 59, 59, DateTimeKind.Unspecified);
230 DateTime startDST = new DateTime (2007, 03, 25, 2, 0, 0, DateTimeKind.Unspecified);
231 DateTime endDST = new DateTime (2007, 10, 28, 1, 59, 59, DateTimeKind.Unspecified);
232 DateTime afterDST = new DateTime (2007, 10, 28, 2, 0, 0, DateTimeKind.Unspecified);
233 Assert.IsFalse (london.IsDaylightSavingTime (beforeDST), "Just before DST");
234 Assert.IsTrue (london.IsDaylightSavingTime (startDST), "the first seconds of DST");
235 Assert.IsTrue (london.IsDaylightSavingTime (endDST), "The last seconds of DST");
236 Assert.IsFalse (london.IsDaylightSavingTime (afterDST), "Just after DST");
240 public void DSTTransisionsUTC ()
242 DateTime beforeDST = new DateTime (2007, 03, 25, 0, 59, 59, DateTimeKind.Utc);
243 DateTime startDST = new DateTime (2007, 03, 25, 1, 0, 0, DateTimeKind.Utc);
244 DateTime endDST = new DateTime (2007, 10, 28, 0, 59, 59, DateTimeKind.Utc);
245 DateTime afterDST = new DateTime (2007, 10, 28, 1, 0, 0, DateTimeKind.Utc);
246 Assert.IsFalse (london.IsDaylightSavingTime (beforeDST), "Just before DST");
247 Assert.IsTrue (london.IsDaylightSavingTime (startDST), "the first seconds of DST");
248 Assert.IsTrue (london.IsDaylightSavingTime (endDST), "The last seconds of DST");
249 Assert.IsFalse (london.IsDaylightSavingTime (afterDST), "Just after DST");
254 public void MatchTimeZoneBehavior ()
256 TimeZone tzone = TimeZone.CurrentTimeZone;
257 TimeZoneInfo local = TimeZoneInfo.Local;
258 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)) {
259 date = DateTime.SpecifyKind (date, DateTimeKind.Local);
260 if (local.IsInvalidTime (date))
262 Assert.IsTrue (tzone.IsDaylightSavingTime (date) == local.IsDaylightSavingTime (date));
269 public class ConvertTimeTests
274 public void CreateTimeZones ()
276 TimeZoneInfo.TransitionTime start = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,1,0,0), 3, 5, DayOfWeek.Sunday);
277 TimeZoneInfo.TransitionTime end = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,2,0,0), 10, 5, DayOfWeek.Sunday);
278 TimeZoneInfo.AdjustmentRule rule = TimeZoneInfo.AdjustmentRule.CreateAdjustmentRule (DateTime.MinValue.Date, DateTime.MaxValue.Date, new TimeSpan (1,0,0), start, end);
279 london = TimeZoneInfo.CreateCustomTimeZone ("Europe/London", new TimeSpan (0), "Europe/London", "British Standard Time", "British Summer Time", new TimeZoneInfo.AdjustmentRule [] {rule});
283 [ExpectedException (typeof (ArgumentException))]
284 public void ConvertFromUtc_KindIsLocalException ()
286 if (Environment.OSVersion.Platform != PlatformID.Unix)
287 throw new ArgumentException ();
288 TimeZoneInfo.ConvertTimeFromUtc (new DateTime (2007, 5, 3, 11, 8, 0, DateTimeKind.Local), TimeZoneInfo.Local);
292 [ExpectedException (typeof (ArgumentNullException))]
293 public void ConvertFromUtc_DestinationTimeZoneIsNullException ()
295 TimeZoneInfo.ConvertTimeFromUtc (new DateTime (2007, 5, 3, 11, 8, 0), null);
299 public void ConvertFromUtc_DestinationIsUTC ()
301 DateTime now = DateTime.UtcNow;
302 DateTime converted = TimeZoneInfo.ConvertTimeFromUtc (now, TimeZoneInfo.Utc);
303 Assert.AreEqual (now, converted);
307 public void ConvertFromUTC_ConvertInWinter ()
309 if (Environment.OSVersion.Platform != PlatformID.Unix)
310 Assert.Ignore ("Not running on Unix.");
311 DateTime utc = new DateTime (2007, 12, 25, 12, 0, 0);
312 DateTime converted = TimeZoneInfo.ConvertTimeFromUtc (utc, london);
313 Assert.AreEqual (utc, converted);
317 public void ConvertFromUtc_ConvertInSummer ()
319 if (Environment.OSVersion.Platform != PlatformID.Unix)
320 Assert.Ignore ("Not running on Unix.");
321 DateTime utc = new DateTime (2007, 06, 01, 12, 0, 0);
322 DateTime converted = TimeZoneInfo.ConvertTimeFromUtc (utc, london);
323 Assert.AreEqual (utc + new TimeSpan (1,0,0), converted);
327 public void ConvertToUTC_KindIsUtc ()
329 DateTime now = DateTime.UtcNow;
330 Assert.AreEqual (now.Kind, DateTimeKind.Utc);
331 DateTime converted = TimeZoneInfo.ConvertTimeToUtc (now);
332 Assert.AreEqual (now, converted);
336 [ExpectedException (typeof (ArgumentException))]
337 public void ConvertToUTC_KindIsUTCButSourceIsNot ()
339 TimeZoneInfo.ConvertTimeToUtc (new DateTime (2007, 5, 3, 12, 8, 0, DateTimeKind.Utc), london);
343 [ExpectedException (typeof (ArgumentException))]
344 public void ConvertToUTC_KindIsLocalButSourceIsNot ()
346 if (Environment.OSVersion.Platform != PlatformID.Unix)
347 throw new ArgumentException ();
348 TimeZoneInfo.ConvertTimeToUtc (new DateTime (2007, 5, 3, 12, 8, 0, DateTimeKind.Local), london);
352 [ExpectedException (typeof (ArgumentException))]
353 public void ConvertToUTC_InvalidDate ()
355 TimeZoneInfo.ConvertTimeToUtc (new DateTime (2007, 3, 25, 1, 30, 0), london);
359 [ExpectedException (typeof (ArgumentNullException))]
360 public void ConvertToUTC_SourceIsNull ()
362 TimeZoneInfo.ConvertTimeToUtc (new DateTime (2007, 5, 3, 12, 16, 0), null);
367 public void ConvertToUtc_MatchDateTimeBehavior ()
369 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)) {
370 Assert.AreEqual (TimeZoneInfo.ConvertTimeToUtc (date), date.ToUniversalTime ());
376 public void ConvertFromToUtc ()
378 if (Environment.OSVersion.Platform != PlatformID.Unix)
379 Assert.Ignore ("Not running on Unix.");
380 DateTime utc = DateTime.UtcNow;
381 Assert.AreEqual (utc.Kind, DateTimeKind.Utc);
382 DateTime converted = TimeZoneInfo.ConvertTimeFromUtc (utc, london);
383 Assert.AreEqual (converted.Kind, DateTimeKind.Unspecified);
384 DateTime back = TimeZoneInfo.ConvertTimeToUtc (converted, london);
385 Assert.AreEqual (back.Kind, DateTimeKind.Utc);
386 Assert.AreEqual (utc, back);
392 public void ConvertFromToLocal ()
394 DateTime utc = DateTime.UtcNow;
395 Assert.AreEqual(utc.Kind, DateTimeKind.Utc);
396 DateTime converted = TimeZoneInfo.ConvertTimeFromUtc(utc, TimeZoneInfo.Local);
398 Assert.AreEqual(DateTimeKind.Local, converted.Kind);
400 Assert.AreEqual(DateTimeKind.Unspecified, converted.Kind);
402 DateTime back = TimeZoneInfo.ConvertTimeToUtc(converted, TimeZoneInfo.Local);
403 Assert.AreEqual(back.Kind, DateTimeKind.Utc);
404 Assert.AreEqual(utc, back);
408 public void ConvertToTimeZone ()
410 if (Environment.OSVersion.Platform != PlatformID.Unix)
411 Assert.Ignore ("Not running on Unix.");
413 TimeZoneInfo.ConvertTime (DateTime.Now, TimeZoneInfo.FindSystemTimeZoneById("Pacific/Auckland"));
418 public class IsInvalidTimeTests
423 public void CreateTimeZones ()
425 TimeZoneInfo.TransitionTime start = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,1,0,0), 3, 5, DayOfWeek.Sunday);
426 TimeZoneInfo.TransitionTime end = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,2,0,0), 10, 5, DayOfWeek.Sunday);
427 TimeZoneInfo.AdjustmentRule rule = TimeZoneInfo.AdjustmentRule.CreateAdjustmentRule (DateTime.MinValue.Date, DateTime.MaxValue.Date, new TimeSpan (1,0,0), start, end);
428 london = TimeZoneInfo.CreateCustomTimeZone ("Europe/London", new TimeSpan (0), "Europe/London", "British Standard Time", "British Summer Time", new TimeZoneInfo.AdjustmentRule [] {rule});
433 public void UTCDate ()
435 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)) {
436 date = DateTime.SpecifyKind (date, DateTimeKind.Utc);
437 Assert.IsFalse (london.IsInvalidTime (date));
442 public void InvalidDates ()
444 Assert.IsFalse (london.IsInvalidTime (new DateTime (2007, 03, 25, 0, 59, 59)));
445 Assert.IsTrue (london.IsInvalidTime (new DateTime (2007, 03, 25, 1, 0, 0)));
446 Assert.IsTrue (london.IsInvalidTime (new DateTime (2007, 03, 25, 1, 59, 59)));
447 Assert.IsFalse (london.IsInvalidTime (new DateTime (2007, 03, 25, 2, 0, 0)));
452 public class IsAmbiguousTimeTests
457 public void CreateTimeZones ()
459 TimeZoneInfo.TransitionTime start = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,1,0,0), 3, 5, DayOfWeek.Sunday);
460 TimeZoneInfo.TransitionTime end = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,2,0,0), 10, 5, DayOfWeek.Sunday);
461 TimeZoneInfo.AdjustmentRule rule = TimeZoneInfo.AdjustmentRule.CreateAdjustmentRule (DateTime.MinValue.Date, DateTime.MaxValue.Date, new TimeSpan (1,0,0), start, end);
462 london = TimeZoneInfo.CreateCustomTimeZone ("Europe/London", new TimeSpan (0), "Europe/London", "British Standard Time", "British Summer Time", new TimeZoneInfo.AdjustmentRule [] {rule});
466 public void AmbiguousDates ()
468 if (Environment.OSVersion.Platform != PlatformID.Unix)
469 Assert.Ignore ("Not running on Unix.");
470 Assert.IsFalse (london.IsAmbiguousTime (new DateTime (2007, 10, 28, 1, 0, 0)));
471 Assert.IsTrue (london.IsAmbiguousTime (new DateTime (2007, 10, 28, 1, 0, 1)));
472 Assert.IsTrue (london.IsAmbiguousTime (new DateTime (2007, 10, 28, 2, 0, 0)));
473 Assert.IsFalse (london.IsAmbiguousTime (new DateTime (2007, 10, 28, 2, 0, 1)));
477 public void AmbiguousUTCDates ()
479 if (Environment.OSVersion.Platform != PlatformID.Unix)
480 Assert.Ignore ("Not running on Unix.");
481 Assert.IsFalse (london.IsAmbiguousTime (new DateTime (2007, 10, 28, 0, 0, 0, DateTimeKind.Utc)));
482 Assert.IsTrue (london.IsAmbiguousTime (new DateTime (2007, 10, 28, 0, 0, 1, DateTimeKind.Utc)));
483 Assert.IsTrue (london.IsAmbiguousTime (new DateTime (2007, 10, 28, 0, 59, 59, DateTimeKind.Utc)));
484 Assert.IsFalse (london.IsAmbiguousTime (new DateTime (2007, 10, 28, 1, 0, 0, DateTimeKind.Utc)));
489 public void AmbiguousInUTC ()
491 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)) {
492 Assert.IsFalse (TimeZoneInfo.Utc.IsAmbiguousTime (date));
499 public class GetSystemTimeZonesTests
502 public void NotEmpty ()
504 if (Environment.OSVersion.Platform != PlatformID.Unix)
505 Assert.Ignore ("Not running on Unix.");
506 global::System.Collections.ObjectModel.ReadOnlyCollection<TimeZoneInfo> systemTZ = TimeZoneInfo.GetSystemTimeZones ();
507 Assert.IsNotNull(systemTZ, "SystemTZ is null");
508 Assert.IsFalse (systemTZ.Count == 0, "SystemTZ is empty");
512 public void ContainsBrussels ()
514 if (Environment.OSVersion.Platform != PlatformID.Unix)
515 Assert.Ignore ("Not running on Unix.");
516 global::System.Collections.ObjectModel.ReadOnlyCollection<TimeZoneInfo> systemTZ = TimeZoneInfo.GetSystemTimeZones ();
517 foreach (TimeZoneInfo tz in systemTZ) {
518 if (tz.Id == "Europe/Brussels")
521 Assert.Fail ("Europe/Brussels not found in SystemTZ");
526 public class FindSystemTimeZoneByIdTests
529 [ExpectedException (typeof (ArgumentNullException))]
530 public void NullId ()
532 TimeZoneInfo.FindSystemTimeZoneById (null);
536 [ExpectedException (typeof (TimeZoneNotFoundException))]
537 public void NonSystemTimezone ()
539 if (Environment.OSVersion.Platform != PlatformID.Unix)
540 throw new TimeZoneNotFoundException ();
541 TimeZoneInfo.FindSystemTimeZoneById ("Neverland/The_Lagoon");
545 public void FindBrusselsTZ ()
547 if (Environment.OSVersion.Platform != PlatformID.Unix)
548 Assert.Ignore ("Not running on Unix.");
549 TimeZoneInfo brussels = TimeZoneInfo.FindSystemTimeZoneById ("Europe/Brussels");
550 Assert.IsNotNull (brussels);
554 public void OffsetIsCorrectInKinshasa ()
556 if (Environment.OSVersion.Platform != PlatformID.Unix)
557 Assert.Ignore ("Not running on Unix.");
558 TimeZoneInfo kin = TimeZoneInfo.FindSystemTimeZoneById ("Africa/Kinshasa");
559 Assert.AreEqual (new TimeSpan (1,0,0), kin.BaseUtcOffset, "BaseUtcOffset in Kinshasa is not +1h");
563 public void OffsetIsCorrectInBrussels ()
565 if (Environment.OSVersion.Platform != PlatformID.Unix)
566 Assert.Ignore ("Not running on Unix.");
567 TimeZoneInfo brussels = TimeZoneInfo.FindSystemTimeZoneById ("Europe/Brussels");
568 Assert.AreEqual (new TimeSpan (1,0,0), brussels.BaseUtcOffset, "BaseUtcOffset for Brussels is not +1h");
572 public void NoDSTInKinshasa ()
574 if (Environment.OSVersion.Platform != PlatformID.Unix)
575 Assert.Ignore ("Not running on Unix.");
576 TimeZoneInfo kin = TimeZoneInfo.FindSystemTimeZoneById ("Africa/Kinshasa");
577 Assert.IsFalse (kin.SupportsDaylightSavingTime);
581 public void BrusselsSupportsDST ()
583 if (Environment.OSVersion.Platform != PlatformID.Unix)
584 Assert.Ignore ("Not running on Unix.");
585 TimeZoneInfo brussels = TimeZoneInfo.FindSystemTimeZoneById ("Europe/Brussels");
586 Assert.IsTrue (brussels.SupportsDaylightSavingTime);
590 public void MelbourneSupportsDST ()
592 if (Environment.OSVersion.Platform != PlatformID.Unix)
593 Assert.Ignore ("Not running on Unix.");
594 TimeZoneInfo melbourne = TimeZoneInfo.FindSystemTimeZoneById ("Australia/Melbourne");
595 Assert.IsTrue (melbourne.SupportsDaylightSavingTime);
599 public void RomeAndVaticanSharesTime ()
601 if (Environment.OSVersion.Platform != PlatformID.Unix)
602 Assert.Ignore ("Not running on Unix.");
603 TimeZoneInfo rome = TimeZoneInfo.FindSystemTimeZoneById ("Europe/Rome");
604 TimeZoneInfo vatican = TimeZoneInfo.FindSystemTimeZoneById ("Europe/Vatican");
605 Assert.IsTrue (rome.HasSameRules (vatican));
609 public void FindSystemTimeZoneById_Local_Roundtrip ()
611 Assert.AreEqual (TimeZoneInfo.Local.Id, TimeZoneInfo.FindSystemTimeZoneById (TimeZoneInfo.Local.Id).Id);
615 public void Test326 ()
617 DateTime utc = DateTime.UtcNow;
618 DateTime local = TimeZoneInfo.ConvertTime (utc, TimeZoneInfo.Utc, TimeZoneInfo.FindSystemTimeZoneById (TimeZoneInfo.Local.Id));
619 Assert.AreEqual (local, utc + TimeZoneInfo.Local.GetUtcOffset (utc), "ConvertTime/Local");
624 public void BrusselsAdjustments ()
626 TimeZoneInfo.TransitionTime start = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,2,0,0), 3, 5, DayOfWeek.Sunday);
627 TimeZoneInfo.TransitionTime end = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,3,0,0), 10, 5, DayOfWeek.Sunday);
628 TimeZoneInfo.AdjustmentRule rule = TimeZoneInfo.AdjustmentRule.CreateAdjustmentRule (DateTime.MinValue.Date, DateTime.MaxValue.Date, new TimeSpan (1,0,0), start, end);
629 TimeZoneInfo brussels = TimeZoneInfo.CreateCustomTimeZone ("Europe/Brussels", new TimeSpan (1, 0, 0), "Europe/Brussels", "", "", new TimeZoneInfo.AdjustmentRule [] {rule});
631 TimeZoneInfo brussels_sys = TimeZoneInfo.FindSystemTimeZoneById ("Europe/Brussels");
633 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)) {
634 Assert.AreEqual (brussels.GetUtcOffset (date), brussels_sys.GetUtcOffset (date));
635 Assert.AreEqual (brussels.IsDaylightSavingTime (date), brussels_sys.IsDaylightSavingTime (date));
642 public class GetAmbiguousTimeOffsetsTests
645 [ExpectedException (typeof(ArgumentException))]
646 public void DateIsNotAmbiguous ()
648 if (Environment.OSVersion.Platform != PlatformID.Unix)
649 throw new ArgumentException ();
650 TimeZoneInfo brussels = TimeZoneInfo.FindSystemTimeZoneById ("Europe/Brussels");
651 DateTime date = new DateTime (2007, 05, 11, 11, 40, 00);
652 brussels.GetAmbiguousTimeOffsets (date);
656 public void AmbiguousOffsets ()
658 if (Environment.OSVersion.Platform != PlatformID.Unix)
659 Assert.Ignore ("Not running on Unix.");
660 TimeZoneInfo brussels = TimeZoneInfo.FindSystemTimeZoneById ("Europe/Brussels");
661 DateTime date = new DateTime (2007, 10, 28, 2, 30, 00);
662 Assert.IsTrue (brussels.IsAmbiguousTime (date));
663 Assert.AreEqual (2, brussels.GetAmbiguousTimeOffsets (date).Length);
664 Assert.AreEqual (new TimeSpan[] {new TimeSpan (1, 0, 0), new TimeSpan (2, 0, 0)}, brussels.GetAmbiguousTimeOffsets (date));
669 public class HasSameRulesTests
672 public void NullAdjustments () //bnc #391011
674 TimeZoneInfo utc = TimeZoneInfo.Utc;
675 TimeZoneInfo custom = TimeZoneInfo.CreateCustomTimeZone ("Custom", new TimeSpan (0), "Custom", "Custom");
676 Assert.IsTrue (utc.HasSameRules (custom));
681 public class SerializationTests
684 public void Serialization_Deserialization ()
686 TimeZoneInfo.TransitionTime start = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,1,0,0), 3, 5, DayOfWeek.Sunday);
687 TimeZoneInfo.TransitionTime end = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,2,0,0), 10, 5, DayOfWeek.Sunday);
688 TimeZoneInfo.AdjustmentRule rule = TimeZoneInfo.AdjustmentRule.CreateAdjustmentRule (DateTime.MinValue.Date, DateTime.MaxValue.Date, new TimeSpan (1,0,0), start, end);
689 TimeZoneInfo london = TimeZoneInfo.CreateCustomTimeZone ("Europe/London", new TimeSpan (0), "Europe/London", "British Standard Time", "British Summer Time", new TimeZoneInfo.AdjustmentRule [] {rule});
690 MemoryStream stream = new MemoryStream ();
691 BinaryFormatter formatter = new BinaryFormatter ();
692 formatter.Serialize (stream, london);
694 TimeZoneInfo deserialized = (TimeZoneInfo) formatter.Deserialize (stream);
697 Assert.IsTrue (london.Equals (deserialized));