TimeZoneInfoTest.cs 28 KB

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