TimeZoneInfoTest.cs 24 KB

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