5 * Stephane Delcroix <stephane@delcroix.org>
7 * Permission is hereby granted, free of charge, to any person obtaining
8 * a copy of this software and associated documentation files (the
9 * "Software"), to deal in the Software without restriction, including
10 * without limitation the rights to use, copy, modify, merge, publish,
11 * distribute, sublicense, and/or sell copies of the Software, and to
12 * permit persons to whom the Software is furnished to do so, subject to
13 * the following conditions:
15 * The above copyright notice and this permission notice shall be
16 * included in all copies or substantial portions of the Software.
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
22 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
23 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
24 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28 using System.Collections;
30 using NUnit.Framework;
32 namespace MonoTests.System
34 public class TimeZoneInfoTest
37 public class PropertiesTests
40 public void GetLocal ()
42 if (Environment.OSVersion.Platform != PlatformID.Unix)
44 TimeZoneInfo local = TimeZoneInfo.Local;
45 Assert.IsNotNull (local);
51 public class CreateCustomTimezoneTests
54 [ExpectedException (typeof (ArgumentNullException))]
55 public void IdIsNullException ()
57 TimeZoneInfo.CreateCustomTimeZone (null, new TimeSpan (0), null, null);
61 [ExpectedException (typeof (ArgumentException))]
62 public void IdIsEmptyString ()
64 TimeZoneInfo.CreateCustomTimeZone ("", new TimeSpan (0), null, null);
68 [ExpectedException (typeof (ArgumentException))]
69 public void OffsetIsNotMinutes ()
71 TimeZoneInfo.CreateCustomTimeZone ("mytimezone", new TimeSpan (0, 0, 55), null, null);
75 [ExpectedException (typeof (ArgumentOutOfRangeException))]
76 public void OffsetTooBig ()
78 TimeZoneInfo.CreateCustomTimeZone ("mytimezone", new TimeSpan (14, 1, 0), null, null);
82 [ExpectedException (typeof (ArgumentOutOfRangeException))]
83 public void OffsetTooSmall ()
85 TimeZoneInfo.CreateCustomTimeZone ("mytimezone", - new TimeSpan (14, 1, 0), null, null);
90 [ExpectedException (typeof (ArgumentException))]
91 public void IdLongerThan32 ()
93 TimeZoneInfo.CreateCustomTimeZone ("12345678901234567890123456789012345", new TimeSpan (0), null, null);
98 [ExpectedException (typeof (InvalidTimeZoneException))]
99 public void AdjustmentRulesOverlap ()
101 TimeZoneInfo.TransitionTime s1 = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,4,0,0), 3, 2, DayOfWeek.Sunday);
102 TimeZoneInfo.TransitionTime e1 = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,4,0,0), 10, 2, DayOfWeek.Sunday);
103 TimeZoneInfo.AdjustmentRule r1 = TimeZoneInfo.AdjustmentRule.CreateAdjustmentRule (new DateTime (2000,1,1), new DateTime (2005,1,1), new TimeSpan (1,0,0), s1, e1);
104 TimeZoneInfo.TransitionTime s2 = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,4,0,0), 2, 2, DayOfWeek.Sunday);
105 TimeZoneInfo.TransitionTime e2 = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,4,0,0), 11, 2, DayOfWeek.Sunday);
106 TimeZoneInfo.AdjustmentRule r2 = TimeZoneInfo.AdjustmentRule.CreateAdjustmentRule (new DateTime (2004,1,1), new DateTime (2007,1,1), new TimeSpan (1,0,0), s2, e2);
107 TimeZoneInfo.CreateCustomTimeZone ("mytimezone", new TimeSpan (6,0,0),null,null,null,new TimeZoneInfo.AdjustmentRule[] {r1, r2});
111 [ExpectedException (typeof (InvalidTimeZoneException))]
112 public void RulesNotOrdered ()
114 TimeZoneInfo.TransitionTime s1 = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,4,0,0), 3, 2, DayOfWeek.Sunday);
115 TimeZoneInfo.TransitionTime e1 = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,4,0,0), 10, 2, DayOfWeek.Sunday);
116 TimeZoneInfo.AdjustmentRule r1 = TimeZoneInfo.AdjustmentRule.CreateAdjustmentRule (new DateTime (2000,1,1), new DateTime (2005,1,1), new TimeSpan (1,0,0), s1, e1);
117 TimeZoneInfo.TransitionTime s2 = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,4,0,0), 2, 2, DayOfWeek.Sunday);
118 TimeZoneInfo.TransitionTime e2 = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,4,0,0), 11, 2, DayOfWeek.Sunday);
119 TimeZoneInfo.AdjustmentRule r2 = TimeZoneInfo.AdjustmentRule.CreateAdjustmentRule (new DateTime (2006,1,1), new DateTime (2007,1,1), new TimeSpan (1,0,0), s2, e2);
120 TimeZoneInfo.CreateCustomTimeZone ("mytimezone", new TimeSpan (6,0,0),null,null,null,new TimeZoneInfo.AdjustmentRule[] {r2, r1});
124 [ExpectedException (typeof (InvalidTimeZoneException))]
125 public void OffsetOutOfRange ()
127 TimeZoneInfo.TransitionTime startTransition = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,4,0,0), 3, 2, DayOfWeek.Sunday);
128 TimeZoneInfo.TransitionTime endTransition = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,4,0,0), 10, 2, DayOfWeek.Sunday);
129 TimeZoneInfo.AdjustmentRule rule = TimeZoneInfo.AdjustmentRule.CreateAdjustmentRule (new DateTime (2000,1,1), new DateTime (2005,1,1), new TimeSpan (3,0,0), startTransition, endTransition);
130 TimeZoneInfo.CreateCustomTimeZone ("mytimezone", new TimeSpan (12,0,0),null,null,null,new TimeZoneInfo.AdjustmentRule[] {rule});
134 [ExpectedException (typeof (InvalidTimeZoneException))]
135 public void NullRule ()
137 TimeZoneInfo.CreateCustomTimeZone ("mytimezone", new TimeSpan (12,0,0),null,null,null,new TimeZoneInfo.AdjustmentRule[] {null});
141 [ExpectedException (typeof (InvalidTimeZoneException))]
142 public void MultiplesRulesForDate ()
144 TimeZoneInfo.TransitionTime s1 = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,4,0,0), 3, 2, DayOfWeek.Sunday);
145 TimeZoneInfo.TransitionTime e1 = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,4,0,0), 10, 2, DayOfWeek.Sunday);
146 TimeZoneInfo.AdjustmentRule r1 = TimeZoneInfo.AdjustmentRule.CreateAdjustmentRule (new DateTime (2000,1,1), new DateTime (2005,1,1), new TimeSpan (1,0,0), s1, e1);
147 TimeZoneInfo.TransitionTime s2 = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,4,0,0), 2, 2, DayOfWeek.Sunday);
148 TimeZoneInfo.TransitionTime e2 = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,4,0,0), 11, 2, DayOfWeek.Sunday);
149 TimeZoneInfo.AdjustmentRule r2 = TimeZoneInfo.AdjustmentRule.CreateAdjustmentRule (new DateTime (2005,1,1), new DateTime (2007,1,1), new TimeSpan (1,0,0), s2, e2);
150 TimeZoneInfo.CreateCustomTimeZone ("mytimezone", new TimeSpan (6,0,0),null,null,null,new TimeZoneInfo.AdjustmentRule[] {r1, r2});
155 public class IsDaylightSavingTimeTests
160 public void CreateTimeZones ()
162 TimeZoneInfo.TransitionTime start = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,1,0,0), 3, 5, DayOfWeek.Sunday);
163 TimeZoneInfo.TransitionTime end = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,2,0,0), 10, 5, DayOfWeek.Sunday);
164 TimeZoneInfo.AdjustmentRule rule = TimeZoneInfo.AdjustmentRule.CreateAdjustmentRule (DateTime.MinValue.Date, DateTime.MaxValue.Date, new TimeSpan (1,0,0), start, end);
165 london = TimeZoneInfo.CreateCustomTimeZone ("Europe/London", new TimeSpan (0), "Europe/London", "British Standard Time", "British Summer Time", new TimeZoneInfo.AdjustmentRule [] {rule});
169 public void NoDSTInUTC ()
171 DateTime june01 = new DateTime (2007, 06, 01);
172 Assert.IsFalse (TimeZoneInfo.Utc.IsDaylightSavingTime (june01));
176 public void DSTInLondon ()
178 if (Environment.OSVersion.Platform != PlatformID.Unix)
180 DateTime june01 = new DateTime (2007, 06, 01);
181 DateTime xmas = new DateTime (2007, 12, 25);
182 Assert.IsTrue (london.IsDaylightSavingTime (june01), "June 01 is DST in London");
183 Assert.IsFalse (london.IsDaylightSavingTime (xmas), "Xmas is not DST in London");
187 public void DSTTransisions ()
189 if (Environment.OSVersion.Platform != PlatformID.Unix)
191 DateTime beforeDST = new DateTime (2007, 03, 25, 0, 59, 59, DateTimeKind.Unspecified);
192 DateTime startDST = new DateTime (2007, 03, 25, 2, 0, 0, DateTimeKind.Unspecified);
193 DateTime endDST = new DateTime (2007, 10, 28, 1, 59, 59, DateTimeKind.Unspecified);
194 DateTime afterDST = new DateTime (2007, 10, 28, 2, 0, 0, DateTimeKind.Unspecified);
195 Assert.IsFalse (london.IsDaylightSavingTime (beforeDST), "Just before DST");
196 Assert.IsTrue (london.IsDaylightSavingTime (startDST), "the first seconds of DST");
197 Assert.IsTrue (london.IsDaylightSavingTime (endDST), "The last seconds of DST");
198 Assert.IsFalse (london.IsDaylightSavingTime (afterDST), "Just after DST");
202 public void DSTTransisionsUTC ()
204 DateTime beforeDST = new DateTime (2007, 03, 25, 0, 59, 59, DateTimeKind.Utc);
205 DateTime startDST = new DateTime (2007, 03, 25, 1, 0, 0, DateTimeKind.Utc);
206 DateTime endDST = new DateTime (2007, 10, 28, 0, 59, 59, DateTimeKind.Utc);
207 DateTime afterDST = new DateTime (2007, 10, 28, 1, 0, 0, DateTimeKind.Utc);
208 Assert.IsFalse (london.IsDaylightSavingTime (beforeDST), "Just before DST");
209 Assert.IsTrue (london.IsDaylightSavingTime (startDST), "the first seconds of DST");
210 Assert.IsTrue (london.IsDaylightSavingTime (endDST), "The last seconds of DST");
211 Assert.IsFalse (london.IsDaylightSavingTime (afterDST), "Just after DST");
216 public void MatchTimeZoneBehavior ()
218 TimeZone tzone = TimeZone.CurrentTimeZone;
219 TimeZoneInfo local = TimeZoneInfo.Local;
220 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)) {
221 date = DateTime.SpecifyKind (date, DateTimeKind.Local);
222 if (local.IsInvalidTime (date))
224 Assert.IsTrue (tzone.IsDaylightSavingTime (date) == local.IsDaylightSavingTime (date));
231 public class ConvertTimeTests
236 public void CreateTimeZones ()
238 TimeZoneInfo.TransitionTime start = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,1,0,0), 3, 5, DayOfWeek.Sunday);
239 TimeZoneInfo.TransitionTime end = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,2,0,0), 10, 5, DayOfWeek.Sunday);
240 TimeZoneInfo.AdjustmentRule rule = TimeZoneInfo.AdjustmentRule.CreateAdjustmentRule (DateTime.MinValue.Date, DateTime.MaxValue.Date, new TimeSpan (1,0,0), start, end);
241 london = TimeZoneInfo.CreateCustomTimeZone ("Europe/London", new TimeSpan (0), "Europe/London", "British Standard Time", "British Summer Time", new TimeZoneInfo.AdjustmentRule [] {rule});
245 [ExpectedException (typeof (ArgumentException))]
246 public void ConvertFromUtc_KindIsLocalException ()
248 if (Environment.OSVersion.Platform != PlatformID.Unix)
249 throw new ArgumentException ();
250 TimeZoneInfo.ConvertTimeFromUtc (new DateTime (2007, 5, 3, 11, 8, 0, DateTimeKind.Local), TimeZoneInfo.Local);
254 [ExpectedException (typeof (ArgumentNullException))]
255 public void ConvertFromUtc_DestinationTimeZoneIsNullException ()
257 TimeZoneInfo.ConvertTimeFromUtc (new DateTime (2007, 5, 3, 11, 8, 0), null);
261 public void ConvertFromUtc_DestinationIsUTC ()
263 DateTime now = DateTime.UtcNow;
264 DateTime converted = TimeZoneInfo.ConvertTimeFromUtc (now, TimeZoneInfo.Utc);
265 Assert.AreEqual (now, converted);
269 public void ConvertFromUTC_ConvertInWinter ()
271 if (Environment.OSVersion.Platform != PlatformID.Unix)
273 DateTime utc = new DateTime (2007, 12, 25, 12, 0, 0);
274 DateTime converted = TimeZoneInfo.ConvertTimeFromUtc (utc, london);
275 Assert.AreEqual (utc, converted);
279 public void ConvertFromUtc_ConvertInSummer ()
281 if (Environment.OSVersion.Platform != PlatformID.Unix)
283 DateTime utc = new DateTime (2007, 06, 01, 12, 0, 0);
284 DateTime converted = TimeZoneInfo.ConvertTimeFromUtc (utc, london);
285 Assert.AreEqual (utc + new TimeSpan (1,0,0), converted);
289 public void ConvertToUTC_KindIsUtc ()
291 DateTime now = DateTime.UtcNow;
292 Assert.AreEqual (now.Kind, DateTimeKind.Utc);
293 DateTime converted = TimeZoneInfo.ConvertTimeToUtc (now);
294 Assert.AreEqual (now, converted);
298 [ExpectedException (typeof (ArgumentException))]
299 public void ConvertToUTC_KindIsUTCButSourceIsNot ()
301 TimeZoneInfo.ConvertTimeToUtc (new DateTime (2007, 5, 3, 12, 8, 0, DateTimeKind.Utc), london);
305 [ExpectedException (typeof (ArgumentException))]
306 public void ConvertToUTC_KindIsLocalButSourceIsNot ()
308 if (Environment.OSVersion.Platform != PlatformID.Unix)
309 throw new ArgumentException ();
310 TimeZoneInfo.ConvertTimeToUtc (new DateTime (2007, 5, 3, 12, 8, 0, DateTimeKind.Local), london);
314 [ExpectedException (typeof (ArgumentException))]
315 public void ConvertToUTC_InvalidDate ()
317 TimeZoneInfo.ConvertTimeToUtc (new DateTime (2007, 3, 25, 1, 30, 0), london);
321 [ExpectedException (typeof (ArgumentNullException))]
322 public void ConvertToUTC_SourceIsNull ()
324 TimeZoneInfo.ConvertTimeToUtc (new DateTime (2007, 5, 3, 12, 16, 0), null);
329 public void ConvertToUtc_MatchDateTimeBehavior ()
331 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)) {
332 Assert.AreEqual (TimeZoneInfo.ConvertTimeToUtc (date), date.ToUniversalTime ());
338 public void ConvertFromToUtc ()
340 if (Environment.OSVersion.Platform != PlatformID.Unix)
342 DateTime utc = DateTime.UtcNow;
343 Assert.AreEqual (utc.Kind, DateTimeKind.Utc);
344 DateTime converted = TimeZoneInfo.ConvertTimeFromUtc (utc, london);
345 Assert.AreEqual (converted.Kind, DateTimeKind.Unspecified);
346 DateTime back = TimeZoneInfo.ConvertTimeToUtc (converted, london);
347 Assert.AreEqual (back.Kind, DateTimeKind.Utc);
348 Assert.AreEqual (utc, back);
354 public class IsInvalidTimeTests
359 public void CreateTimeZones ()
361 TimeZoneInfo.TransitionTime start = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,1,0,0), 3, 5, DayOfWeek.Sunday);
362 TimeZoneInfo.TransitionTime end = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,2,0,0), 10, 5, DayOfWeek.Sunday);
363 TimeZoneInfo.AdjustmentRule rule = TimeZoneInfo.AdjustmentRule.CreateAdjustmentRule (DateTime.MinValue.Date, DateTime.MaxValue.Date, new TimeSpan (1,0,0), start, end);
364 london = TimeZoneInfo.CreateCustomTimeZone ("Europe/London", new TimeSpan (0), "Europe/London", "British Standard Time", "British Summer Time", new TimeZoneInfo.AdjustmentRule [] {rule});
369 public void UTCDate ()
371 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)) {
372 date = DateTime.SpecifyKind (date, DateTimeKind.Utc);
373 Assert.IsFalse (london.IsInvalidTime (date));
378 public void InvalidDates ()
380 Assert.IsFalse (london.IsInvalidTime (new DateTime (2007, 03, 25, 0, 59, 59)));
381 Assert.IsTrue (london.IsInvalidTime (new DateTime (2007, 03, 25, 1, 0, 0)));
382 Assert.IsTrue (london.IsInvalidTime (new DateTime (2007, 03, 25, 1, 59, 59)));
383 Assert.IsFalse (london.IsInvalidTime (new DateTime (2007, 03, 25, 2, 0, 0)));
388 public class IsAmbiguousTimeTests
393 public void CreateTimeZones ()
395 TimeZoneInfo.TransitionTime start = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,1,0,0), 3, 5, DayOfWeek.Sunday);
396 TimeZoneInfo.TransitionTime end = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,2,0,0), 10, 5, DayOfWeek.Sunday);
397 TimeZoneInfo.AdjustmentRule rule = TimeZoneInfo.AdjustmentRule.CreateAdjustmentRule (DateTime.MinValue.Date, DateTime.MaxValue.Date, new TimeSpan (1,0,0), start, end);
398 london = TimeZoneInfo.CreateCustomTimeZone ("Europe/London", new TimeSpan (0), "Europe/London", "British Standard Time", "British Summer Time", new TimeZoneInfo.AdjustmentRule [] {rule});
402 public void AmbiguousDates ()
404 if (Environment.OSVersion.Platform != PlatformID.Unix)
406 Assert.IsFalse (london.IsAmbiguousTime (new DateTime (2007, 10, 28, 1, 0, 0)));
407 Assert.IsTrue (london.IsAmbiguousTime (new DateTime (2007, 10, 28, 1, 0, 1)));
408 Assert.IsTrue (london.IsAmbiguousTime (new DateTime (2007, 10, 28, 2, 0, 0)));
409 Assert.IsFalse (london.IsAmbiguousTime (new DateTime (2007, 10, 28, 2, 0, 1)));
413 public void AmbiguousUTCDates ()
415 if (Environment.OSVersion.Platform != PlatformID.Unix)
417 Assert.IsFalse (london.IsAmbiguousTime (new DateTime (2007, 10, 28, 0, 0, 0, DateTimeKind.Utc)));
418 Assert.IsTrue (london.IsAmbiguousTime (new DateTime (2007, 10, 28, 0, 0, 1, DateTimeKind.Utc)));
419 Assert.IsTrue (london.IsAmbiguousTime (new DateTime (2007, 10, 28, 0, 59, 59, DateTimeKind.Utc)));
420 Assert.IsFalse (london.IsAmbiguousTime (new DateTime (2007, 10, 28, 1, 0, 0, DateTimeKind.Utc)));
425 public void AmbiguousInUTC ()
427 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)) {
428 Assert.IsFalse (TimeZoneInfo.Utc.IsAmbiguousTime (date));
435 public class GetSystemTimeZonesTests
438 public void NotEmpty ()
440 if (Environment.OSVersion.Platform != PlatformID.Unix)
442 global::System.Collections.ObjectModel.ReadOnlyCollection<TimeZoneInfo> systemTZ = TimeZoneInfo.GetSystemTimeZones ();
443 Assert.IsNotNull(systemTZ, "SystemTZ is null");
444 Assert.IsFalse (systemTZ.Count == 0, "SystemTZ is empty");
448 public void ContainsBrussels ()
450 if (Environment.OSVersion.Platform != PlatformID.Unix)
452 global::System.Collections.ObjectModel.ReadOnlyCollection<TimeZoneInfo> systemTZ = TimeZoneInfo.GetSystemTimeZones ();
453 foreach (TimeZoneInfo tz in systemTZ) {
454 if (tz.Id == "Europe/Brussels")
457 Assert.Fail ("Europe/Brussels not found in SystemTZ");
462 public class FindSystemTimeZoneByIdTests
465 [ExpectedException (typeof (ArgumentNullException))]
466 public void NullId ()
468 TimeZoneInfo.FindSystemTimeZoneById (null);
472 [ExpectedException (typeof (TimeZoneNotFoundException))]
473 public void NonSystemTimezone ()
475 if (Environment.OSVersion.Platform != PlatformID.Unix)
476 throw new TimeZoneNotFoundException ();
477 TimeZoneInfo.FindSystemTimeZoneById ("Neverland/The_Lagoon");
481 public void FindBrusselsTZ ()
483 if (Environment.OSVersion.Platform != PlatformID.Unix)
485 TimeZoneInfo brussels = TimeZoneInfo.FindSystemTimeZoneById ("Europe/Brussels");
486 Assert.IsNotNull (brussels);
490 public void OffsetIsCorrectInKinshasa ()
492 if (Environment.OSVersion.Platform != PlatformID.Unix)
494 TimeZoneInfo kin = TimeZoneInfo.FindSystemTimeZoneById ("Africa/Kinshasa");
495 Assert.AreEqual (new TimeSpan (1,0,0), kin.BaseUtcOffset, "BaseUtcOffset in Kinshasa is not +1h");
499 public void OffsetIsCorrectInBrussels ()
501 if (Environment.OSVersion.Platform != PlatformID.Unix)
503 TimeZoneInfo brussels = TimeZoneInfo.FindSystemTimeZoneById ("Europe/Brussels");
504 Assert.AreEqual (new TimeSpan (1,0,0), brussels.BaseUtcOffset, "BaseUtcOffset for Brussels is not +1h");
508 public void NoDSTInKinshasa ()
510 if (Environment.OSVersion.Platform != PlatformID.Unix)
512 TimeZoneInfo kin = TimeZoneInfo.FindSystemTimeZoneById ("Africa/Kinshasa");
513 Assert.IsFalse (kin.SupportsDaylightSavingTime);
517 public void BrusselsSupportsDST ()
519 if (Environment.OSVersion.Platform != PlatformID.Unix)
521 TimeZoneInfo brussels = TimeZoneInfo.FindSystemTimeZoneById ("Europe/Brussels");
522 Assert.IsTrue (brussels.SupportsDaylightSavingTime);
526 public void MelbourneSupportsDST ()
528 if (Environment.OSVersion.Platform != PlatformID.Unix)
530 TimeZoneInfo melbourne = TimeZoneInfo.FindSystemTimeZoneById ("Australia/Melbourne");
531 Assert.IsTrue (melbourne.SupportsDaylightSavingTime);
535 public void RomeAndVaticanSharesTime ()
537 if (Environment.OSVersion.Platform != PlatformID.Unix)
539 TimeZoneInfo rome = TimeZoneInfo.FindSystemTimeZoneById ("Europe/Rome");
540 TimeZoneInfo vatican = TimeZoneInfo.FindSystemTimeZoneById ("Europe/Vatican");
541 Assert.IsTrue (rome.HasSameRules (vatican));
545 public void FindSystemTimeZoneById_Local_Roundtrip ()
547 Assert.AreEqual (TimeZoneInfo.Local.Id, TimeZoneInfo.FindSystemTimeZoneById (TimeZoneInfo.Local.Id).Id);
551 public void Test326 ()
553 DateTime utc = DateTime.UtcNow;
554 DateTime local = TimeZoneInfo.ConvertTime (utc, TimeZoneInfo.Utc, TimeZoneInfo.FindSystemTimeZoneById (TimeZoneInfo.Local.Id));
555 Assert.AreEqual (local, utc + TimeZoneInfo.Local.GetUtcOffset (utc), "ConvertTime/Local");
560 public void BrusselsAdjustments ()
562 TimeZoneInfo.TransitionTime start = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,2,0,0), 3, 5, DayOfWeek.Sunday);
563 TimeZoneInfo.TransitionTime end = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,3,0,0), 10, 5, DayOfWeek.Sunday);
564 TimeZoneInfo.AdjustmentRule rule = TimeZoneInfo.AdjustmentRule.CreateAdjustmentRule (DateTime.MinValue.Date, DateTime.MaxValue.Date, new TimeSpan (1,0,0), start, end);
565 TimeZoneInfo brussels = TimeZoneInfo.CreateCustomTimeZone ("Europe/Brussels", new TimeSpan (1, 0, 0), "Europe/Brussels", "", "", new TimeZoneInfo.AdjustmentRule [] {rule});
567 TimeZoneInfo brussels_sys = TimeZoneInfo.FindSystemTimeZoneById ("Europe/Brussels");
569 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)) {
570 Assert.AreEqual (brussels.GetUtcOffset (date), brussels_sys.GetUtcOffset (date));
571 Assert.AreEqual (brussels.IsDaylightSavingTime (date), brussels_sys.IsDaylightSavingTime (date));
578 public class GetAmbiguousTimeOffsetsTests
581 [ExpectedException (typeof(ArgumentException))]
582 public void DateIsNotAmbiguous ()
584 if (Environment.OSVersion.Platform != PlatformID.Unix)
585 throw new ArgumentException ();
586 TimeZoneInfo brussels = TimeZoneInfo.FindSystemTimeZoneById ("Europe/Brussels");
587 DateTime date = new DateTime (2007, 05, 11, 11, 40, 00);
588 brussels.GetAmbiguousTimeOffsets (date);
592 public void AmbiguousOffsets ()
594 if (Environment.OSVersion.Platform != PlatformID.Unix)
596 TimeZoneInfo brussels = TimeZoneInfo.FindSystemTimeZoneById ("Europe/Brussels");
597 DateTime date = new DateTime (2007, 10, 28, 2, 30, 00);
598 Assert.IsTrue (brussels.IsAmbiguousTime (date));
599 Assert.AreEqual (2, brussels.GetAmbiguousTimeOffsets (date).Length);
600 Assert.AreEqual (new TimeSpan[] {new TimeSpan (1, 0, 0), new TimeSpan (2, 0, 0)}, brussels.GetAmbiguousTimeOffsets (date));
605 public class HasSameRulesTests
608 public void NullAdjustments () //bnc #391011
610 TimeZoneInfo utc = TimeZoneInfo.Utc;
611 TimeZoneInfo custom = TimeZoneInfo.CreateCustomTimeZone ("Custom", new TimeSpan (0), "Custom", "Custom");
612 Assert.IsTrue (utc.HasSameRules (custom));