TimeZoneInfoTest.cs 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546
  1. /*
  2. * TimeZoneInfo.Tests
  3. *
  4. * Author(s)
  5. * Stephane Delcroix <[email protected]>
  6. *
  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:
  14. *
  15. * The above copyright notice and this permission notice shall be
  16. * included in all copies or substantial portions of the Software.
  17. *
  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.
  25. */
  26. using System;
  27. using System.Collections;
  28. using NUnit.Framework;
  29. #if NET_2_0
  30. namespace MonoTests.System
  31. {
  32. public class TimeZoneInfoTest
  33. {
  34. [TestFixture]
  35. public class PropertiesTests
  36. {
  37. [Test]
  38. public void GetLocal ()
  39. {
  40. TimeZoneInfo local = TimeZoneInfo.Local;
  41. Assert.IsTrue (true);
  42. }
  43. }
  44. [TestFixture]
  45. public class CreateCustomTimezoneTests
  46. {
  47. [Test]
  48. [ExpectedException (typeof (ArgumentNullException))]
  49. public void IdIsNullException ()
  50. {
  51. TimeZoneInfo.CreateCustomTimeZone (null, new TimeSpan (0), null, null);
  52. }
  53. [Test]
  54. [ExpectedException (typeof (ArgumentException))]
  55. public void IdIsEmptyString ()
  56. {
  57. TimeZoneInfo.CreateCustomTimeZone ("", new TimeSpan (0), null, null);
  58. }
  59. [Test]
  60. [ExpectedException (typeof (ArgumentException))]
  61. public void OffsetIsNotMinutes ()
  62. {
  63. TimeZoneInfo.CreateCustomTimeZone ("mytimezone", new TimeSpan (0, 0, 55), null, null);
  64. }
  65. [Test]
  66. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  67. public void OffsetTooBig ()
  68. {
  69. TimeZoneInfo.CreateCustomTimeZone ("mytimezone", new TimeSpan (14, 1, 0), null, null);
  70. }
  71. [Test]
  72. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  73. public void OffsetTooSmall ()
  74. {
  75. TimeZoneInfo.CreateCustomTimeZone ("mytimezone", - new TimeSpan (14, 1, 0), null, null);
  76. }
  77. #if STRICT
  78. [Test]
  79. [ExpectedException (typeof (ArgumentException))]
  80. public void IdLongerThan32 ()
  81. {
  82. TimeZoneInfo.CreateCustomTimeZone ("12345678901234567890123456789012345", new TimeSpan (0), null, null);
  83. }
  84. #endif
  85. [Test]
  86. [ExpectedException (typeof (InvalidTimeZoneException))]
  87. public void AdjustmentRulesOverlap ()
  88. {
  89. TimeZoneInfo.TransitionTime s1 = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,4,0,0), 3, 2, DayOfWeek.Sunday);
  90. TimeZoneInfo.TransitionTime e1 = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,4,0,0), 10, 2, DayOfWeek.Sunday);
  91. TimeZoneInfo.AdjustmentRule r1 = TimeZoneInfo.AdjustmentRule.CreateAdjustmentRule (new DateTime (2000,1,1), new DateTime (2005,1,1), new TimeSpan (1,0,0), s1, e1);
  92. TimeZoneInfo.TransitionTime s2 = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,4,0,0), 2, 2, DayOfWeek.Sunday);
  93. TimeZoneInfo.TransitionTime e2 = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,4,0,0), 11, 2, DayOfWeek.Sunday);
  94. TimeZoneInfo.AdjustmentRule r2 = TimeZoneInfo.AdjustmentRule.CreateAdjustmentRule (new DateTime (2004,1,1), new DateTime (2007,1,1), new TimeSpan (1,0,0), s2, e2);
  95. TimeZoneInfo.CreateCustomTimeZone ("mytimezone", new TimeSpan (6,0,0),null,null,null,new TimeZoneInfo.AdjustmentRule[] {r1, r2});
  96. }
  97. [Test]
  98. [ExpectedException (typeof (InvalidTimeZoneException))]
  99. public void RulesNotOrdered ()
  100. {
  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 (2006,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[] {r2, r1});
  108. }
  109. [Test]
  110. [ExpectedException (typeof (InvalidTimeZoneException))]
  111. public void OffsetOutOfRange ()
  112. {
  113. TimeZoneInfo.TransitionTime startTransition = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,4,0,0), 3, 2, DayOfWeek.Sunday);
  114. TimeZoneInfo.TransitionTime endTransition = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,4,0,0), 10, 2, DayOfWeek.Sunday);
  115. TimeZoneInfo.AdjustmentRule rule = TimeZoneInfo.AdjustmentRule.CreateAdjustmentRule (new DateTime (2000,1,1), new DateTime (2005,1,1), new TimeSpan (3,0,0), startTransition, endTransition);
  116. TimeZoneInfo.CreateCustomTimeZone ("mytimezone", new TimeSpan (12,0,0),null,null,null,new TimeZoneInfo.AdjustmentRule[] {rule});
  117. }
  118. [Test]
  119. [ExpectedException (typeof (InvalidTimeZoneException))]
  120. public void NullRule ()
  121. {
  122. TimeZoneInfo.CreateCustomTimeZone ("mytimezone", new TimeSpan (12,0,0),null,null,null,new TimeZoneInfo.AdjustmentRule[] {null});
  123. }
  124. [Test]
  125. [ExpectedException (typeof (InvalidTimeZoneException))]
  126. public void MultiplesRulesForDate ()
  127. {
  128. TimeZoneInfo.TransitionTime s1 = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,4,0,0), 3, 2, DayOfWeek.Sunday);
  129. TimeZoneInfo.TransitionTime e1 = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,4,0,0), 10, 2, DayOfWeek.Sunday);
  130. TimeZoneInfo.AdjustmentRule r1 = TimeZoneInfo.AdjustmentRule.CreateAdjustmentRule (new DateTime (2000,1,1), new DateTime (2005,1,1), new TimeSpan (1,0,0), s1, e1);
  131. TimeZoneInfo.TransitionTime s2 = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,4,0,0), 2, 2, DayOfWeek.Sunday);
  132. TimeZoneInfo.TransitionTime e2 = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,4,0,0), 11, 2, DayOfWeek.Sunday);
  133. TimeZoneInfo.AdjustmentRule r2 = TimeZoneInfo.AdjustmentRule.CreateAdjustmentRule (new DateTime (2005,1,1), new DateTime (2007,1,1), new TimeSpan (1,0,0), s2, e2);
  134. TimeZoneInfo.CreateCustomTimeZone ("mytimezone", new TimeSpan (6,0,0),null,null,null,new TimeZoneInfo.AdjustmentRule[] {r1, r2});
  135. }
  136. }
  137. [TestFixture]
  138. public class IsDaylightSavingTimeTests
  139. {
  140. TimeZoneInfo london;
  141. [SetUp]
  142. public void CreateTimeZones ()
  143. {
  144. TimeZoneInfo.TransitionTime start = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,1,0,0), 3, 5, DayOfWeek.Sunday);
  145. TimeZoneInfo.TransitionTime end = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,2,0,0), 10, 5, DayOfWeek.Sunday);
  146. TimeZoneInfo.AdjustmentRule rule = TimeZoneInfo.AdjustmentRule.CreateAdjustmentRule (DateTime.MinValue.Date, DateTime.MaxValue.Date, new TimeSpan (1,0,0), start, end);
  147. london = TimeZoneInfo.CreateCustomTimeZone ("Europe/London", new TimeSpan (0), "Europe/London", "British Standard Time", "British Summer Time", new TimeZoneInfo.AdjustmentRule [] {rule});
  148. }
  149. [Test]
  150. public void NoDSTInUTC ()
  151. {
  152. DateTime june01 = new DateTime (2007, 06, 01);
  153. Assert.IsFalse (TimeZoneInfo.Utc.IsDaylightSavingTime (june01));
  154. }
  155. [Test]
  156. public void DSTInLondon ()
  157. {
  158. DateTime june01 = new DateTime (2007, 06, 01);
  159. DateTime xmas = new DateTime (2007, 12, 25);
  160. Assert.IsTrue (london.IsDaylightSavingTime (june01), "June 01 is DST in London");
  161. Assert.IsFalse (london.IsDaylightSavingTime (xmas), "Xmas is not DST in London");
  162. }
  163. [Test]
  164. public void DSTTransisions ()
  165. {
  166. DateTime beforeDST = new DateTime (2007, 03, 25, 0, 59, 59, DateTimeKind.Unspecified);
  167. DateTime startDST = new DateTime (2007, 03, 25, 2, 0, 0, DateTimeKind.Unspecified);
  168. DateTime endDST = new DateTime (2007, 10, 28, 1, 59, 59, DateTimeKind.Unspecified);
  169. DateTime afterDST = new DateTime (2007, 10, 28, 2, 0, 0, DateTimeKind.Unspecified);
  170. Assert.IsFalse (london.IsDaylightSavingTime (beforeDST), "Just before DST");
  171. Assert.IsTrue (london.IsDaylightSavingTime (startDST), "the first seconds of DST");
  172. Assert.IsTrue (london.IsDaylightSavingTime (endDST), "The last seconds of DST");
  173. Assert.IsFalse (london.IsDaylightSavingTime (afterDST), "Just after DST");
  174. }
  175. [Test]
  176. public void DSTTransisionsUTC ()
  177. {
  178. DateTime beforeDST = new DateTime (2007, 03, 25, 0, 59, 59, DateTimeKind.Utc);
  179. DateTime startDST = new DateTime (2007, 03, 25, 1, 0, 0, DateTimeKind.Utc);
  180. DateTime endDST = new DateTime (2007, 10, 28, 0, 59, 59, DateTimeKind.Utc);
  181. DateTime afterDST = new DateTime (2007, 10, 28, 1, 0, 0, DateTimeKind.Utc);
  182. Assert.IsFalse (london.IsDaylightSavingTime (beforeDST), "Just before DST");
  183. Assert.IsTrue (london.IsDaylightSavingTime (startDST), "the first seconds of DST");
  184. Assert.IsTrue (london.IsDaylightSavingTime (endDST), "The last seconds of DST");
  185. Assert.IsFalse (london.IsDaylightSavingTime (afterDST), "Just after DST");
  186. }
  187. #if SLOW_TESTS
  188. [Test]
  189. public void MatchTimeZoneBehavior ()
  190. {
  191. TimeZone tzone = TimeZone.CurrentTimeZone;
  192. TimeZoneInfo local = TimeZoneInfo.Local;
  193. 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)) {
  194. date = DateTime.SpecifyKind (date, DateTimeKind.Local);
  195. if (local.IsInvalidTime (date))
  196. continue;
  197. Assert.IsTrue (tzone.IsDaylightSavingTime (date) == local.IsDaylightSavingTime (date));
  198. }
  199. }
  200. #endif
  201. }
  202. [TestFixture]
  203. public class ConvertTimeTests
  204. {
  205. TimeZoneInfo london;
  206. [SetUp]
  207. public void CreateTimeZones ()
  208. {
  209. TimeZoneInfo.TransitionTime start = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,1,0,0), 3, 5, DayOfWeek.Sunday);
  210. TimeZoneInfo.TransitionTime end = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,2,0,0), 10, 5, DayOfWeek.Sunday);
  211. TimeZoneInfo.AdjustmentRule rule = TimeZoneInfo.AdjustmentRule.CreateAdjustmentRule (DateTime.MinValue.Date, DateTime.MaxValue.Date, new TimeSpan (1,0,0), start, end);
  212. london = TimeZoneInfo.CreateCustomTimeZone ("Europe/London", new TimeSpan (0), "Europe/London", "British Standard Time", "British Summer Time", new TimeZoneInfo.AdjustmentRule [] {rule});
  213. }
  214. [Test]
  215. [ExpectedException (typeof (ArgumentException))]
  216. public void ConvertFromUtc_KindIsLocalException ()
  217. {
  218. TimeZoneInfo.ConvertTimeFromUtc (new DateTime (2007, 5, 3, 11, 8, 0, DateTimeKind.Local), TimeZoneInfo.Local);
  219. }
  220. [Test]
  221. [ExpectedException (typeof (ArgumentNullException))]
  222. public void ConvertFromUtc_DestinationTimeZoneIsNullException ()
  223. {
  224. TimeZoneInfo.ConvertTimeFromUtc (new DateTime (2007, 5, 3, 11, 8, 0), null);
  225. }
  226. [Test]
  227. public void ConvertFromUtc_DestinationIsUTC ()
  228. {
  229. DateTime now = DateTime.UtcNow;
  230. DateTime converted = TimeZoneInfo.ConvertTimeFromUtc (now, TimeZoneInfo.Utc);
  231. Assert.AreEqual (now, converted);
  232. }
  233. [Test]
  234. public void ConvertFromUTC_ConvertInWinter ()
  235. {
  236. DateTime utc = new DateTime (2007, 12, 25, 12, 0, 0);
  237. DateTime converted = TimeZoneInfo.ConvertTimeFromUtc (utc, london);
  238. Assert.AreEqual (utc, converted);
  239. }
  240. [Test]
  241. public void ConvertFromUtc_ConvertInSummer ()
  242. {
  243. DateTime utc = new DateTime (2007, 06, 01, 12, 0, 0);
  244. DateTime converted = TimeZoneInfo.ConvertTimeFromUtc (utc, london);
  245. Assert.AreEqual (utc + new TimeSpan (1,0,0), converted);
  246. }
  247. [Test]
  248. public void ConvertToUTC_KindIsUtc ()
  249. {
  250. DateTime now = DateTime.UtcNow;
  251. Assert.AreEqual (now.Kind, DateTimeKind.Utc);
  252. DateTime converted = TimeZoneInfo.ConvertTimeToUtc (now);
  253. Assert.AreEqual (now, converted);
  254. }
  255. [Test]
  256. [ExpectedException (typeof (ArgumentException))]
  257. public void ConvertToUTC_KindIsUTCButSourceIsNot ()
  258. {
  259. TimeZoneInfo.ConvertTimeToUtc (new DateTime (2007, 5, 3, 12, 8, 0, DateTimeKind.Utc), london);
  260. }
  261. [Test]
  262. [ExpectedException (typeof (ArgumentException))]
  263. public void ConvertToUTC_KindIsLocalButSourceIsNot ()
  264. {
  265. TimeZoneInfo.ConvertTimeToUtc (new DateTime (2007, 5, 3, 12, 8, 0, DateTimeKind.Local), london);
  266. }
  267. [Test]
  268. [ExpectedException (typeof (ArgumentException))]
  269. public void ConvertToUTC_InvalidDate ()
  270. {
  271. TimeZoneInfo.ConvertTimeToUtc (new DateTime (2007, 3, 25, 1, 30, 0), london);
  272. }
  273. [Test]
  274. [ExpectedException (typeof (ArgumentNullException))]
  275. public void ConvertToUTC_SourceIsNull ()
  276. {
  277. TimeZoneInfo.ConvertTimeToUtc (new DateTime (2007, 5, 3, 12, 16, 0), null);
  278. }
  279. #if SLOW_TESTS
  280. [Test]
  281. public void ConvertToUtc_MatchDateTimeBehavior ()
  282. {
  283. 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)) {
  284. Assert.AreEqual (TimeZoneInfo.ConvertTimeToUtc (date), date.ToUniversalTime ());
  285. }
  286. }
  287. #endif
  288. [Test]
  289. public void ConvertFromToUtc ()
  290. {
  291. DateTime utc = DateTime.UtcNow;
  292. Assert.AreEqual (utc.Kind, DateTimeKind.Utc);
  293. DateTime converted = TimeZoneInfo.ConvertTimeFromUtc (utc, london);
  294. Assert.AreEqual (converted.Kind, DateTimeKind.Unspecified);
  295. DateTime back = TimeZoneInfo.ConvertTimeToUtc (converted, london);
  296. Assert.AreEqual (back.Kind, DateTimeKind.Utc);
  297. Assert.AreEqual (utc, back);
  298. }
  299. }
  300. [TestFixture]
  301. public class IsInvalidTimeTests
  302. {
  303. TimeZoneInfo london;
  304. [SetUp]
  305. public void CreateTimeZones ()
  306. {
  307. TimeZoneInfo.TransitionTime start = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,1,0,0), 3, 5, DayOfWeek.Sunday);
  308. TimeZoneInfo.TransitionTime end = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,2,0,0), 10, 5, DayOfWeek.Sunday);
  309. TimeZoneInfo.AdjustmentRule rule = TimeZoneInfo.AdjustmentRule.CreateAdjustmentRule (DateTime.MinValue.Date, DateTime.MaxValue.Date, new TimeSpan (1,0,0), start, end);
  310. london = TimeZoneInfo.CreateCustomTimeZone ("Europe/London", new TimeSpan (0), "Europe/London", "British Standard Time", "British Summer Time", new TimeZoneInfo.AdjustmentRule [] {rule});
  311. }
  312. #if SLOW_TESTS
  313. [Test]
  314. public void UTCDate ()
  315. {
  316. 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)) {
  317. date = DateTime.SpecifyKind (date, DateTimeKind.Utc);
  318. Assert.IsFalse (london.IsInvalidTime (date));
  319. }
  320. }
  321. #endif
  322. [Test]
  323. public void InvalidDates ()
  324. {
  325. Assert.IsFalse (london.IsInvalidTime (new DateTime (2007, 03, 25, 0, 59, 59)));
  326. Assert.IsTrue (london.IsInvalidTime (new DateTime (2007, 03, 25, 1, 0, 0)));
  327. Assert.IsTrue (london.IsInvalidTime (new DateTime (2007, 03, 25, 1, 59, 59)));
  328. Assert.IsFalse (london.IsInvalidTime (new DateTime (2007, 03, 25, 2, 0, 0)));
  329. }
  330. }
  331. [TestFixture]
  332. public class IsAmbiguousTimeTests
  333. {
  334. TimeZoneInfo london;
  335. [SetUp]
  336. public void CreateTimeZones ()
  337. {
  338. TimeZoneInfo.TransitionTime start = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,1,0,0), 3, 5, DayOfWeek.Sunday);
  339. TimeZoneInfo.TransitionTime end = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,2,0,0), 10, 5, DayOfWeek.Sunday);
  340. TimeZoneInfo.AdjustmentRule rule = TimeZoneInfo.AdjustmentRule.CreateAdjustmentRule (DateTime.MinValue.Date, DateTime.MaxValue.Date, new TimeSpan (1,0,0), start, end);
  341. london = TimeZoneInfo.CreateCustomTimeZone ("Europe/London", new TimeSpan (0), "Europe/London", "British Standard Time", "British Summer Time", new TimeZoneInfo.AdjustmentRule [] {rule});
  342. }
  343. [Test]
  344. public void AmbiguousDates ()
  345. {
  346. Assert.IsFalse (london.IsAmbiguousTime (new DateTime (2007, 10, 28, 1, 0, 0)));
  347. Assert.IsTrue (london.IsAmbiguousTime (new DateTime (2007, 10, 28, 1, 0, 1)));
  348. Assert.IsTrue (london.IsAmbiguousTime (new DateTime (2007, 10, 28, 2, 0, 0)));
  349. Assert.IsFalse (london.IsAmbiguousTime (new DateTime (2007, 10, 28, 2, 0, 1)));
  350. }
  351. [Test]
  352. public void AmbiguousUTCDates ()
  353. {
  354. Assert.IsFalse (london.IsAmbiguousTime (new DateTime (2007, 10, 28, 0, 0, 0, DateTimeKind.Utc)));
  355. Assert.IsTrue (london.IsAmbiguousTime (new DateTime (2007, 10, 28, 0, 0, 1, DateTimeKind.Utc)));
  356. Assert.IsTrue (london.IsAmbiguousTime (new DateTime (2007, 10, 28, 0, 59, 59, DateTimeKind.Utc)));
  357. Assert.IsFalse (london.IsAmbiguousTime (new DateTime (2007, 10, 28, 1, 0, 0, DateTimeKind.Utc)));
  358. }
  359. #if SLOW_TESTS
  360. [Test]
  361. public void AmbiguousInUTC ()
  362. {
  363. 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)) {
  364. Assert.IsFalse (TimeZoneInfo.Utc.IsAmbiguousTime (date));
  365. }
  366. }
  367. #endif
  368. }
  369. [TestFixture]
  370. public class GetSystemTimeZonesTests
  371. {
  372. [Test]
  373. public void NotEmpty ()
  374. {
  375. global::System.Collections.ObjectModel.ReadOnlyCollection<TimeZoneInfo> systemTZ = TimeZoneInfo.GetSystemTimeZones ();
  376. Assert.IsNotNull(systemTZ, "SystemTZ is null");
  377. Assert.IsFalse (systemTZ.Count == 0, "SystemTZ is empty");
  378. }
  379. [Test]
  380. public void ContainsBrussels ()
  381. {
  382. global::System.Collections.ObjectModel.ReadOnlyCollection<TimeZoneInfo> systemTZ = TimeZoneInfo.GetSystemTimeZones ();
  383. foreach (TimeZoneInfo tz in systemTZ) {
  384. if (tz.Id == "Europe/Brussels")
  385. return;
  386. }
  387. Assert.Fail ("Europe/Brussels not found in SystemTZ");
  388. }
  389. }
  390. [TestFixture]
  391. public class FindSystemTimeZoneByIdTests
  392. {
  393. [Test]
  394. [ExpectedException (typeof (ArgumentNullException))]
  395. public void NullId ()
  396. {
  397. TimeZoneInfo.FindSystemTimeZoneById (null);
  398. }
  399. [Test]
  400. [ExpectedException (typeof (TimeZoneNotFoundException))]
  401. public void NonSystemTimezone ()
  402. {
  403. TimeZoneInfo.FindSystemTimeZoneById ("Neverland/The_Lagoon");
  404. }
  405. [Test]
  406. public void FindBrusselsTZ ()
  407. {
  408. TimeZoneInfo brussels = TimeZoneInfo.FindSystemTimeZoneById ("Europe/Brussels");
  409. Assert.IsNotNull (brussels);
  410. }
  411. [Test]
  412. public void OffsetIsCorrectInKinshasa ()
  413. {
  414. TimeZoneInfo kin = TimeZoneInfo.FindSystemTimeZoneById ("Africa/Kinshasa");
  415. Assert.AreEqual (new TimeSpan (1,0,0), kin.BaseUtcOffset, "BaseUtcOffset in Kinshasa is not +1h");
  416. }
  417. [Test]
  418. public void OffsetIsCorrectInBrussels ()
  419. {
  420. TimeZoneInfo brussels = TimeZoneInfo.FindSystemTimeZoneById ("Europe/Brussels");
  421. Assert.AreEqual (new TimeSpan (1,0,0), brussels.BaseUtcOffset, "BaseUtcOffset for Brussels is not +1h");
  422. }
  423. [Test]
  424. public void NoDSTInKinshasa ()
  425. {
  426. TimeZoneInfo kin = TimeZoneInfo.FindSystemTimeZoneById ("Africa/Kinshasa");
  427. Assert.IsFalse (kin.SupportsDaylightSavingTime);
  428. }
  429. [Test]
  430. public void BrusselsSupportsDST ()
  431. {
  432. TimeZoneInfo brussels = TimeZoneInfo.FindSystemTimeZoneById ("Europe/Brussels");
  433. Assert.IsTrue (brussels.SupportsDaylightSavingTime);
  434. }
  435. [Test]
  436. public void MelbourneSupportsDST ()
  437. {
  438. TimeZoneInfo melbourne = TimeZoneInfo.FindSystemTimeZoneById ("Australia/Melbourne");
  439. Assert.IsTrue (melbourne.SupportsDaylightSavingTime);
  440. }
  441. [Test]
  442. public void RomeAndVaticanSharesTime ()
  443. {
  444. TimeZoneInfo rome = TimeZoneInfo.FindSystemTimeZoneById ("Europe/Rome");
  445. TimeZoneInfo vatican = TimeZoneInfo.FindSystemTimeZoneById ("Europe/Vatican");
  446. Assert.IsTrue (rome.HasSameRules (vatican));
  447. }
  448. #if SLOW_TESTS
  449. [Test]
  450. public void BrusselsAdjustments ()
  451. {
  452. TimeZoneInfo.TransitionTime start = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,2,0,0), 3, 5, DayOfWeek.Sunday);
  453. TimeZoneInfo.TransitionTime end = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,3,0,0), 10, 5, DayOfWeek.Sunday);
  454. TimeZoneInfo.AdjustmentRule rule = TimeZoneInfo.AdjustmentRule.CreateAdjustmentRule (DateTime.MinValue.Date, DateTime.MaxValue.Date, new TimeSpan (1,0,0), start, end);
  455. TimeZoneInfo brussels = TimeZoneInfo.CreateCustomTimeZone ("Europe/Brussels", new TimeSpan (1, 0, 0), "Europe/Brussels", "", "", new TimeZoneInfo.AdjustmentRule [] {rule});
  456. TimeZoneInfo brussels_sys = TimeZoneInfo.FindSystemTimeZoneById ("Europe/Brussels");
  457. 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)) {
  458. Assert.AreEqual (brussels.GetUtcOffset (date), brussels_sys.GetUtcOffset (date));
  459. Assert.AreEqual (brussels.IsDaylightSavingTime (date), brussels_sys.IsDaylightSavingTime (date));
  460. }
  461. }
  462. #endif
  463. }
  464. [TestFixture]
  465. public class GetAmbiguousTimeOffsetsTests
  466. {
  467. [Test]
  468. [ExpectedException (typeof(ArgumentException))]
  469. public void DateIsNotAmbiguous ()
  470. {
  471. TimeZoneInfo brussels = TimeZoneInfo.FindSystemTimeZoneById ("Europe/Brussels");
  472. DateTime date = new DateTime (2007, 05, 11, 11, 40, 00);
  473. brussels.GetAmbiguousTimeOffsets (date);
  474. }
  475. [Test]
  476. public void AmbiguousOffsets ()
  477. {
  478. TimeZoneInfo brussels = TimeZoneInfo.FindSystemTimeZoneById ("Europe/Brussels");
  479. DateTime date = new DateTime (2007, 10, 28, 2, 30, 00);
  480. Assert.IsTrue (brussels.IsAmbiguousTime (date));
  481. Assert.AreEqual (2, brussels.GetAmbiguousTimeOffsets (date).Length);
  482. Assert.AreEqual (new TimeSpan[] {new TimeSpan (1, 0, 0), new TimeSpan (2, 0, 0)}, brussels.GetAmbiguousTimeOffsets (date));
  483. }
  484. }
  485. }
  486. }
  487. #endif