TimeZoneInfoTest.cs 25 KB

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