TimeZoneInfoTest.cs 24 KB

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