TimeZoneInfoTest.cs 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662
  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. [Test]
  142. public void SupportsDaylightSavingTime_NonEmptyAdjustmentRule ()
  143. {
  144. TimeZoneInfo.TransitionTime s1 = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,4,0,0), 3, 2, DayOfWeek.Sunday);
  145. TimeZoneInfo.TransitionTime e1 = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,4,0,0), 10, 2, DayOfWeek.Sunday);
  146. TimeZoneInfo.AdjustmentRule r1 = TimeZoneInfo.AdjustmentRule.CreateAdjustmentRule (new DateTime (2000,1,1), new DateTime (2005,1,1), new TimeSpan (1,0,0), s1, e1);
  147. TimeZoneInfo tz = TimeZoneInfo.CreateCustomTimeZone ("mytimezone", new TimeSpan (6,0,0),null,null,null,new TimeZoneInfo.AdjustmentRule[] {r1});
  148. Assert.IsTrue (tz.SupportsDaylightSavingTime);
  149. }
  150. [Test]
  151. public void SupportsDaylightSavingTime_EmptyAdjustmentRule ()
  152. {
  153. TimeZoneInfo tz = TimeZoneInfo.CreateCustomTimeZone ("mytimezone", new TimeSpan (6,0,0),null,null,null,null);
  154. Assert.IsFalse (tz.SupportsDaylightSavingTime);
  155. }
  156. [Test]
  157. public void SupportsDaylightSavingTime_NonEmptyAdjustmentRule_DisableDaylightSavingTime ()
  158. {
  159. TimeZoneInfo.TransitionTime s1 = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,4,0,0), 3, 2, DayOfWeek.Sunday);
  160. TimeZoneInfo.TransitionTime e1 = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,4,0,0), 10, 2, DayOfWeek.Sunday);
  161. TimeZoneInfo.AdjustmentRule r1 = TimeZoneInfo.AdjustmentRule.CreateAdjustmentRule (new DateTime (2000,1,1), new DateTime (2005,1,1), new TimeSpan (1,0,0), s1, e1);
  162. TimeZoneInfo tz = TimeZoneInfo.CreateCustomTimeZone ("mytimezone", new TimeSpan (6,0,0),null,null,null,new TimeZoneInfo.AdjustmentRule[] {r1}, true);
  163. Assert.IsFalse (tz.SupportsDaylightSavingTime);
  164. }
  165. [Test]
  166. public void SupportsDaylightSavingTime_EmptyAdjustmentRule_DisableDaylightSavingTime ()
  167. {
  168. TimeZoneInfo tz = TimeZoneInfo.CreateCustomTimeZone ("mytimezone", new TimeSpan (6,0,0),null,null,null,null,true);
  169. Assert.IsFalse (tz.SupportsDaylightSavingTime);
  170. }
  171. }
  172. [TestFixture]
  173. public class IsDaylightSavingTimeTests
  174. {
  175. TimeZoneInfo london;
  176. [SetUp]
  177. public void CreateTimeZones ()
  178. {
  179. TimeZoneInfo.TransitionTime start = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,1,0,0), 3, 5, DayOfWeek.Sunday);
  180. TimeZoneInfo.TransitionTime end = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,2,0,0), 10, 5, DayOfWeek.Sunday);
  181. TimeZoneInfo.AdjustmentRule rule = TimeZoneInfo.AdjustmentRule.CreateAdjustmentRule (DateTime.MinValue.Date, DateTime.MaxValue.Date, new TimeSpan (1,0,0), start, end);
  182. london = TimeZoneInfo.CreateCustomTimeZone ("Europe/London", new TimeSpan (0), "Europe/London", "British Standard Time", "British Summer Time", new TimeZoneInfo.AdjustmentRule [] {rule});
  183. }
  184. [Test]
  185. public void NoDSTInUTC ()
  186. {
  187. DateTime june01 = new DateTime (2007, 06, 01);
  188. Assert.IsFalse (TimeZoneInfo.Utc.IsDaylightSavingTime (june01));
  189. }
  190. [Test]
  191. public void DSTInLondon ()
  192. {
  193. if (Environment.OSVersion.Platform != PlatformID.Unix)
  194. return;
  195. DateTime june01 = new DateTime (2007, 06, 01);
  196. DateTime xmas = new DateTime (2007, 12, 25);
  197. Assert.IsTrue (london.IsDaylightSavingTime (june01), "June 01 is DST in London");
  198. Assert.IsFalse (london.IsDaylightSavingTime (xmas), "Xmas is not DST in London");
  199. }
  200. [Test]
  201. public void DSTTransisions ()
  202. {
  203. if (Environment.OSVersion.Platform != PlatformID.Unix)
  204. return;
  205. DateTime beforeDST = new DateTime (2007, 03, 25, 0, 59, 59, DateTimeKind.Unspecified);
  206. DateTime startDST = new DateTime (2007, 03, 25, 2, 0, 0, DateTimeKind.Unspecified);
  207. DateTime endDST = new DateTime (2007, 10, 28, 1, 59, 59, DateTimeKind.Unspecified);
  208. DateTime afterDST = new DateTime (2007, 10, 28, 2, 0, 0, DateTimeKind.Unspecified);
  209. Assert.IsFalse (london.IsDaylightSavingTime (beforeDST), "Just before DST");
  210. Assert.IsTrue (london.IsDaylightSavingTime (startDST), "the first seconds of DST");
  211. Assert.IsTrue (london.IsDaylightSavingTime (endDST), "The last seconds of DST");
  212. Assert.IsFalse (london.IsDaylightSavingTime (afterDST), "Just after DST");
  213. }
  214. [Test]
  215. public void DSTTransisionsUTC ()
  216. {
  217. DateTime beforeDST = new DateTime (2007, 03, 25, 0, 59, 59, DateTimeKind.Utc);
  218. DateTime startDST = new DateTime (2007, 03, 25, 1, 0, 0, DateTimeKind.Utc);
  219. DateTime endDST = new DateTime (2007, 10, 28, 0, 59, 59, DateTimeKind.Utc);
  220. DateTime afterDST = new DateTime (2007, 10, 28, 1, 0, 0, DateTimeKind.Utc);
  221. Assert.IsFalse (london.IsDaylightSavingTime (beforeDST), "Just before DST");
  222. Assert.IsTrue (london.IsDaylightSavingTime (startDST), "the first seconds of DST");
  223. Assert.IsTrue (london.IsDaylightSavingTime (endDST), "The last seconds of DST");
  224. Assert.IsFalse (london.IsDaylightSavingTime (afterDST), "Just after DST");
  225. }
  226. #if SLOW_TESTS
  227. [Test]
  228. public void MatchTimeZoneBehavior ()
  229. {
  230. TimeZone tzone = TimeZone.CurrentTimeZone;
  231. TimeZoneInfo local = TimeZoneInfo.Local;
  232. 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)) {
  233. date = DateTime.SpecifyKind (date, DateTimeKind.Local);
  234. if (local.IsInvalidTime (date))
  235. continue;
  236. Assert.IsTrue (tzone.IsDaylightSavingTime (date) == local.IsDaylightSavingTime (date));
  237. }
  238. }
  239. #endif
  240. }
  241. [TestFixture]
  242. public class ConvertTimeTests
  243. {
  244. TimeZoneInfo london;
  245. [SetUp]
  246. public void CreateTimeZones ()
  247. {
  248. TimeZoneInfo.TransitionTime start = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,1,0,0), 3, 5, DayOfWeek.Sunday);
  249. TimeZoneInfo.TransitionTime end = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,2,0,0), 10, 5, DayOfWeek.Sunday);
  250. TimeZoneInfo.AdjustmentRule rule = TimeZoneInfo.AdjustmentRule.CreateAdjustmentRule (DateTime.MinValue.Date, DateTime.MaxValue.Date, new TimeSpan (1,0,0), start, end);
  251. london = TimeZoneInfo.CreateCustomTimeZone ("Europe/London", new TimeSpan (0), "Europe/London", "British Standard Time", "British Summer Time", new TimeZoneInfo.AdjustmentRule [] {rule});
  252. }
  253. [Test]
  254. [ExpectedException (typeof (ArgumentException))]
  255. public void ConvertFromUtc_KindIsLocalException ()
  256. {
  257. if (Environment.OSVersion.Platform != PlatformID.Unix)
  258. throw new ArgumentException ();
  259. TimeZoneInfo.ConvertTimeFromUtc (new DateTime (2007, 5, 3, 11, 8, 0, DateTimeKind.Local), TimeZoneInfo.Local);
  260. }
  261. [Test]
  262. [ExpectedException (typeof (ArgumentNullException))]
  263. public void ConvertFromUtc_DestinationTimeZoneIsNullException ()
  264. {
  265. TimeZoneInfo.ConvertTimeFromUtc (new DateTime (2007, 5, 3, 11, 8, 0), null);
  266. }
  267. [Test]
  268. public void ConvertFromUtc_DestinationIsUTC ()
  269. {
  270. DateTime now = DateTime.UtcNow;
  271. DateTime converted = TimeZoneInfo.ConvertTimeFromUtc (now, TimeZoneInfo.Utc);
  272. Assert.AreEqual (now, converted);
  273. }
  274. [Test]
  275. public void ConvertFromUTC_ConvertInWinter ()
  276. {
  277. if (Environment.OSVersion.Platform != PlatformID.Unix)
  278. return;
  279. DateTime utc = new DateTime (2007, 12, 25, 12, 0, 0);
  280. DateTime converted = TimeZoneInfo.ConvertTimeFromUtc (utc, london);
  281. Assert.AreEqual (utc, converted);
  282. }
  283. [Test]
  284. public void ConvertFromUtc_ConvertInSummer ()
  285. {
  286. if (Environment.OSVersion.Platform != PlatformID.Unix)
  287. return;
  288. DateTime utc = new DateTime (2007, 06, 01, 12, 0, 0);
  289. DateTime converted = TimeZoneInfo.ConvertTimeFromUtc (utc, london);
  290. Assert.AreEqual (utc + new TimeSpan (1,0,0), converted);
  291. }
  292. [Test]
  293. public void ConvertToUTC_KindIsUtc ()
  294. {
  295. DateTime now = DateTime.UtcNow;
  296. Assert.AreEqual (now.Kind, DateTimeKind.Utc);
  297. DateTime converted = TimeZoneInfo.ConvertTimeToUtc (now);
  298. Assert.AreEqual (now, converted);
  299. }
  300. [Test]
  301. [ExpectedException (typeof (ArgumentException))]
  302. public void ConvertToUTC_KindIsUTCButSourceIsNot ()
  303. {
  304. TimeZoneInfo.ConvertTimeToUtc (new DateTime (2007, 5, 3, 12, 8, 0, DateTimeKind.Utc), london);
  305. }
  306. [Test]
  307. [ExpectedException (typeof (ArgumentException))]
  308. public void ConvertToUTC_KindIsLocalButSourceIsNot ()
  309. {
  310. if (Environment.OSVersion.Platform != PlatformID.Unix)
  311. throw new ArgumentException ();
  312. TimeZoneInfo.ConvertTimeToUtc (new DateTime (2007, 5, 3, 12, 8, 0, DateTimeKind.Local), london);
  313. }
  314. [Test]
  315. [ExpectedException (typeof (ArgumentException))]
  316. public void ConvertToUTC_InvalidDate ()
  317. {
  318. TimeZoneInfo.ConvertTimeToUtc (new DateTime (2007, 3, 25, 1, 30, 0), london);
  319. }
  320. [Test]
  321. [ExpectedException (typeof (ArgumentNullException))]
  322. public void ConvertToUTC_SourceIsNull ()
  323. {
  324. TimeZoneInfo.ConvertTimeToUtc (new DateTime (2007, 5, 3, 12, 16, 0), null);
  325. }
  326. #if SLOW_TESTS
  327. [Test]
  328. public void ConvertToUtc_MatchDateTimeBehavior ()
  329. {
  330. 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)) {
  331. Assert.AreEqual (TimeZoneInfo.ConvertTimeToUtc (date), date.ToUniversalTime ());
  332. }
  333. }
  334. #endif
  335. [Test]
  336. public void ConvertFromToUtc ()
  337. {
  338. if (Environment.OSVersion.Platform != PlatformID.Unix)
  339. return;
  340. DateTime utc = DateTime.UtcNow;
  341. Assert.AreEqual (utc.Kind, DateTimeKind.Utc);
  342. DateTime converted = TimeZoneInfo.ConvertTimeFromUtc (utc, london);
  343. Assert.AreEqual (converted.Kind, DateTimeKind.Unspecified);
  344. DateTime back = TimeZoneInfo.ConvertTimeToUtc (converted, london);
  345. Assert.AreEqual (back.Kind, DateTimeKind.Utc);
  346. Assert.AreEqual (utc, back);
  347. }
  348. [Test]
  349. public void ConvertToTimeZone ()
  350. {
  351. if (Environment.OSVersion.Platform != PlatformID.Unix)
  352. return;
  353. TimeZoneInfo.ConvertTime (DateTime.Now, TimeZoneInfo.FindSystemTimeZoneById("Pacific/Auckland"));
  354. }
  355. }
  356. [TestFixture]
  357. public class IsInvalidTimeTests
  358. {
  359. TimeZoneInfo london;
  360. [SetUp]
  361. public void CreateTimeZones ()
  362. {
  363. TimeZoneInfo.TransitionTime start = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,1,0,0), 3, 5, DayOfWeek.Sunday);
  364. TimeZoneInfo.TransitionTime end = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,2,0,0), 10, 5, DayOfWeek.Sunday);
  365. TimeZoneInfo.AdjustmentRule rule = TimeZoneInfo.AdjustmentRule.CreateAdjustmentRule (DateTime.MinValue.Date, DateTime.MaxValue.Date, new TimeSpan (1,0,0), start, end);
  366. london = TimeZoneInfo.CreateCustomTimeZone ("Europe/London", new TimeSpan (0), "Europe/London", "British Standard Time", "British Summer Time", new TimeZoneInfo.AdjustmentRule [] {rule});
  367. }
  368. #if SLOW_TESTS
  369. [Test]
  370. public void UTCDate ()
  371. {
  372. 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)) {
  373. date = DateTime.SpecifyKind (date, DateTimeKind.Utc);
  374. Assert.IsFalse (london.IsInvalidTime (date));
  375. }
  376. }
  377. #endif
  378. [Test]
  379. public void InvalidDates ()
  380. {
  381. Assert.IsFalse (london.IsInvalidTime (new DateTime (2007, 03, 25, 0, 59, 59)));
  382. Assert.IsTrue (london.IsInvalidTime (new DateTime (2007, 03, 25, 1, 0, 0)));
  383. Assert.IsTrue (london.IsInvalidTime (new DateTime (2007, 03, 25, 1, 59, 59)));
  384. Assert.IsFalse (london.IsInvalidTime (new DateTime (2007, 03, 25, 2, 0, 0)));
  385. }
  386. }
  387. [TestFixture]
  388. public class IsAmbiguousTimeTests
  389. {
  390. TimeZoneInfo london;
  391. [SetUp]
  392. public void CreateTimeZones ()
  393. {
  394. TimeZoneInfo.TransitionTime start = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,1,0,0), 3, 5, DayOfWeek.Sunday);
  395. TimeZoneInfo.TransitionTime end = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,2,0,0), 10, 5, DayOfWeek.Sunday);
  396. TimeZoneInfo.AdjustmentRule rule = TimeZoneInfo.AdjustmentRule.CreateAdjustmentRule (DateTime.MinValue.Date, DateTime.MaxValue.Date, new TimeSpan (1,0,0), start, end);
  397. london = TimeZoneInfo.CreateCustomTimeZone ("Europe/London", new TimeSpan (0), "Europe/London", "British Standard Time", "British Summer Time", new TimeZoneInfo.AdjustmentRule [] {rule});
  398. }
  399. [Test]
  400. public void AmbiguousDates ()
  401. {
  402. if (Environment.OSVersion.Platform != PlatformID.Unix)
  403. return;
  404. Assert.IsFalse (london.IsAmbiguousTime (new DateTime (2007, 10, 28, 1, 0, 0)));
  405. Assert.IsTrue (london.IsAmbiguousTime (new DateTime (2007, 10, 28, 1, 0, 1)));
  406. Assert.IsTrue (london.IsAmbiguousTime (new DateTime (2007, 10, 28, 2, 0, 0)));
  407. Assert.IsFalse (london.IsAmbiguousTime (new DateTime (2007, 10, 28, 2, 0, 1)));
  408. }
  409. [Test]
  410. public void AmbiguousUTCDates ()
  411. {
  412. if (Environment.OSVersion.Platform != PlatformID.Unix)
  413. return;
  414. Assert.IsFalse (london.IsAmbiguousTime (new DateTime (2007, 10, 28, 0, 0, 0, DateTimeKind.Utc)));
  415. Assert.IsTrue (london.IsAmbiguousTime (new DateTime (2007, 10, 28, 0, 0, 1, DateTimeKind.Utc)));
  416. Assert.IsTrue (london.IsAmbiguousTime (new DateTime (2007, 10, 28, 0, 59, 59, DateTimeKind.Utc)));
  417. Assert.IsFalse (london.IsAmbiguousTime (new DateTime (2007, 10, 28, 1, 0, 0, DateTimeKind.Utc)));
  418. }
  419. #if SLOW_TESTS
  420. [Test]
  421. public void AmbiguousInUTC ()
  422. {
  423. 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)) {
  424. Assert.IsFalse (TimeZoneInfo.Utc.IsAmbiguousTime (date));
  425. }
  426. }
  427. #endif
  428. }
  429. [TestFixture]
  430. public class GetSystemTimeZonesTests
  431. {
  432. [Test]
  433. public void NotEmpty ()
  434. {
  435. if (Environment.OSVersion.Platform != PlatformID.Unix)
  436. return;
  437. global::System.Collections.ObjectModel.ReadOnlyCollection<TimeZoneInfo> systemTZ = TimeZoneInfo.GetSystemTimeZones ();
  438. Assert.IsNotNull(systemTZ, "SystemTZ is null");
  439. Assert.IsFalse (systemTZ.Count == 0, "SystemTZ is empty");
  440. }
  441. [Test]
  442. public void ContainsBrussels ()
  443. {
  444. if (Environment.OSVersion.Platform != PlatformID.Unix)
  445. return;
  446. global::System.Collections.ObjectModel.ReadOnlyCollection<TimeZoneInfo> systemTZ = TimeZoneInfo.GetSystemTimeZones ();
  447. foreach (TimeZoneInfo tz in systemTZ) {
  448. if (tz.Id == "Europe/Brussels")
  449. return;
  450. }
  451. Assert.Fail ("Europe/Brussels not found in SystemTZ");
  452. }
  453. }
  454. [TestFixture]
  455. public class FindSystemTimeZoneByIdTests
  456. {
  457. [Test]
  458. [ExpectedException (typeof (ArgumentNullException))]
  459. public void NullId ()
  460. {
  461. TimeZoneInfo.FindSystemTimeZoneById (null);
  462. }
  463. [Test]
  464. [ExpectedException (typeof (TimeZoneNotFoundException))]
  465. public void NonSystemTimezone ()
  466. {
  467. if (Environment.OSVersion.Platform != PlatformID.Unix)
  468. throw new TimeZoneNotFoundException ();
  469. TimeZoneInfo.FindSystemTimeZoneById ("Neverland/The_Lagoon");
  470. }
  471. [Test]
  472. public void FindBrusselsTZ ()
  473. {
  474. if (Environment.OSVersion.Platform != PlatformID.Unix)
  475. return;
  476. TimeZoneInfo brussels = TimeZoneInfo.FindSystemTimeZoneById ("Europe/Brussels");
  477. Assert.IsNotNull (brussels);
  478. }
  479. [Test]
  480. public void OffsetIsCorrectInKinshasa ()
  481. {
  482. if (Environment.OSVersion.Platform != PlatformID.Unix)
  483. return;
  484. TimeZoneInfo kin = TimeZoneInfo.FindSystemTimeZoneById ("Africa/Kinshasa");
  485. Assert.AreEqual (new TimeSpan (1,0,0), kin.BaseUtcOffset, "BaseUtcOffset in Kinshasa is not +1h");
  486. }
  487. [Test]
  488. public void OffsetIsCorrectInBrussels ()
  489. {
  490. if (Environment.OSVersion.Platform != PlatformID.Unix)
  491. return;
  492. TimeZoneInfo brussels = TimeZoneInfo.FindSystemTimeZoneById ("Europe/Brussels");
  493. Assert.AreEqual (new TimeSpan (1,0,0), brussels.BaseUtcOffset, "BaseUtcOffset for Brussels is not +1h");
  494. }
  495. [Test]
  496. public void NoDSTInKinshasa ()
  497. {
  498. if (Environment.OSVersion.Platform != PlatformID.Unix)
  499. return;
  500. TimeZoneInfo kin = TimeZoneInfo.FindSystemTimeZoneById ("Africa/Kinshasa");
  501. Assert.IsFalse (kin.SupportsDaylightSavingTime);
  502. }
  503. [Test]
  504. public void BrusselsSupportsDST ()
  505. {
  506. if (Environment.OSVersion.Platform != PlatformID.Unix)
  507. return;
  508. TimeZoneInfo brussels = TimeZoneInfo.FindSystemTimeZoneById ("Europe/Brussels");
  509. Assert.IsTrue (brussels.SupportsDaylightSavingTime);
  510. }
  511. [Test]
  512. public void MelbourneSupportsDST ()
  513. {
  514. if (Environment.OSVersion.Platform != PlatformID.Unix)
  515. return;
  516. TimeZoneInfo melbourne = TimeZoneInfo.FindSystemTimeZoneById ("Australia/Melbourne");
  517. Assert.IsTrue (melbourne.SupportsDaylightSavingTime);
  518. }
  519. [Test]
  520. public void RomeAndVaticanSharesTime ()
  521. {
  522. if (Environment.OSVersion.Platform != PlatformID.Unix)
  523. return;
  524. TimeZoneInfo rome = TimeZoneInfo.FindSystemTimeZoneById ("Europe/Rome");
  525. TimeZoneInfo vatican = TimeZoneInfo.FindSystemTimeZoneById ("Europe/Vatican");
  526. Assert.IsTrue (rome.HasSameRules (vatican));
  527. }
  528. [Test]
  529. public void FindSystemTimeZoneById_Local_Roundtrip ()
  530. {
  531. Assert.AreEqual (TimeZoneInfo.Local.Id, TimeZoneInfo.FindSystemTimeZoneById (TimeZoneInfo.Local.Id).Id);
  532. }
  533. [Test]
  534. public void Test326 ()
  535. {
  536. DateTime utc = DateTime.UtcNow;
  537. DateTime local = TimeZoneInfo.ConvertTime (utc, TimeZoneInfo.Utc, TimeZoneInfo.FindSystemTimeZoneById (TimeZoneInfo.Local.Id));
  538. Assert.AreEqual (local, utc + TimeZoneInfo.Local.GetUtcOffset (utc), "ConvertTime/Local");
  539. }
  540. #if SLOW_TESTS
  541. [Test]
  542. public void BrusselsAdjustments ()
  543. {
  544. TimeZoneInfo.TransitionTime start = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,2,0,0), 3, 5, DayOfWeek.Sunday);
  545. TimeZoneInfo.TransitionTime end = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,3,0,0), 10, 5, DayOfWeek.Sunday);
  546. TimeZoneInfo.AdjustmentRule rule = TimeZoneInfo.AdjustmentRule.CreateAdjustmentRule (DateTime.MinValue.Date, DateTime.MaxValue.Date, new TimeSpan (1,0,0), start, end);
  547. TimeZoneInfo brussels = TimeZoneInfo.CreateCustomTimeZone ("Europe/Brussels", new TimeSpan (1, 0, 0), "Europe/Brussels", "", "", new TimeZoneInfo.AdjustmentRule [] {rule});
  548. TimeZoneInfo brussels_sys = TimeZoneInfo.FindSystemTimeZoneById ("Europe/Brussels");
  549. 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)) {
  550. Assert.AreEqual (brussels.GetUtcOffset (date), brussels_sys.GetUtcOffset (date));
  551. Assert.AreEqual (brussels.IsDaylightSavingTime (date), brussels_sys.IsDaylightSavingTime (date));
  552. }
  553. }
  554. #endif
  555. }
  556. [TestFixture]
  557. public class GetAmbiguousTimeOffsetsTests
  558. {
  559. [Test]
  560. [ExpectedException (typeof(ArgumentException))]
  561. public void DateIsNotAmbiguous ()
  562. {
  563. if (Environment.OSVersion.Platform != PlatformID.Unix)
  564. throw new ArgumentException ();
  565. TimeZoneInfo brussels = TimeZoneInfo.FindSystemTimeZoneById ("Europe/Brussels");
  566. DateTime date = new DateTime (2007, 05, 11, 11, 40, 00);
  567. brussels.GetAmbiguousTimeOffsets (date);
  568. }
  569. [Test]
  570. public void AmbiguousOffsets ()
  571. {
  572. if (Environment.OSVersion.Platform != PlatformID.Unix)
  573. return;
  574. TimeZoneInfo brussels = TimeZoneInfo.FindSystemTimeZoneById ("Europe/Brussels");
  575. DateTime date = new DateTime (2007, 10, 28, 2, 30, 00);
  576. Assert.IsTrue (brussels.IsAmbiguousTime (date));
  577. Assert.AreEqual (2, brussels.GetAmbiguousTimeOffsets (date).Length);
  578. Assert.AreEqual (new TimeSpan[] {new TimeSpan (1, 0, 0), new TimeSpan (2, 0, 0)}, brussels.GetAmbiguousTimeOffsets (date));
  579. }
  580. }
  581. [TestFixture]
  582. public class HasSameRulesTests
  583. {
  584. [Test]
  585. public void NullAdjustments () //bnc #391011
  586. {
  587. TimeZoneInfo utc = TimeZoneInfo.Utc;
  588. TimeZoneInfo custom = TimeZoneInfo.CreateCustomTimeZone ("Custom", new TimeSpan (0), "Custom", "Custom");
  589. Assert.IsTrue (utc.HasSameRules (custom));
  590. }
  591. }
  592. }
  593. }
  594. #endif