TimeZoneInfoTest.cs 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653
  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. }
  349. [TestFixture]
  350. public class IsInvalidTimeTests
  351. {
  352. TimeZoneInfo london;
  353. [SetUp]
  354. public void CreateTimeZones ()
  355. {
  356. TimeZoneInfo.TransitionTime start = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,1,0,0), 3, 5, DayOfWeek.Sunday);
  357. TimeZoneInfo.TransitionTime end = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,2,0,0), 10, 5, DayOfWeek.Sunday);
  358. TimeZoneInfo.AdjustmentRule rule = TimeZoneInfo.AdjustmentRule.CreateAdjustmentRule (DateTime.MinValue.Date, DateTime.MaxValue.Date, new TimeSpan (1,0,0), start, end);
  359. london = TimeZoneInfo.CreateCustomTimeZone ("Europe/London", new TimeSpan (0), "Europe/London", "British Standard Time", "British Summer Time", new TimeZoneInfo.AdjustmentRule [] {rule});
  360. }
  361. #if SLOW_TESTS
  362. [Test]
  363. public void UTCDate ()
  364. {
  365. 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)) {
  366. date = DateTime.SpecifyKind (date, DateTimeKind.Utc);
  367. Assert.IsFalse (london.IsInvalidTime (date));
  368. }
  369. }
  370. #endif
  371. [Test]
  372. public void InvalidDates ()
  373. {
  374. Assert.IsFalse (london.IsInvalidTime (new DateTime (2007, 03, 25, 0, 59, 59)));
  375. Assert.IsTrue (london.IsInvalidTime (new DateTime (2007, 03, 25, 1, 0, 0)));
  376. Assert.IsTrue (london.IsInvalidTime (new DateTime (2007, 03, 25, 1, 59, 59)));
  377. Assert.IsFalse (london.IsInvalidTime (new DateTime (2007, 03, 25, 2, 0, 0)));
  378. }
  379. }
  380. [TestFixture]
  381. public class IsAmbiguousTimeTests
  382. {
  383. TimeZoneInfo london;
  384. [SetUp]
  385. public void CreateTimeZones ()
  386. {
  387. TimeZoneInfo.TransitionTime start = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,1,0,0), 3, 5, DayOfWeek.Sunday);
  388. TimeZoneInfo.TransitionTime end = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,2,0,0), 10, 5, DayOfWeek.Sunday);
  389. TimeZoneInfo.AdjustmentRule rule = TimeZoneInfo.AdjustmentRule.CreateAdjustmentRule (DateTime.MinValue.Date, DateTime.MaxValue.Date, new TimeSpan (1,0,0), start, end);
  390. london = TimeZoneInfo.CreateCustomTimeZone ("Europe/London", new TimeSpan (0), "Europe/London", "British Standard Time", "British Summer Time", new TimeZoneInfo.AdjustmentRule [] {rule});
  391. }
  392. [Test]
  393. public void AmbiguousDates ()
  394. {
  395. if (Environment.OSVersion.Platform != PlatformID.Unix)
  396. return;
  397. Assert.IsFalse (london.IsAmbiguousTime (new DateTime (2007, 10, 28, 1, 0, 0)));
  398. Assert.IsTrue (london.IsAmbiguousTime (new DateTime (2007, 10, 28, 1, 0, 1)));
  399. Assert.IsTrue (london.IsAmbiguousTime (new DateTime (2007, 10, 28, 2, 0, 0)));
  400. Assert.IsFalse (london.IsAmbiguousTime (new DateTime (2007, 10, 28, 2, 0, 1)));
  401. }
  402. [Test]
  403. public void AmbiguousUTCDates ()
  404. {
  405. if (Environment.OSVersion.Platform != PlatformID.Unix)
  406. return;
  407. Assert.IsFalse (london.IsAmbiguousTime (new DateTime (2007, 10, 28, 0, 0, 0, DateTimeKind.Utc)));
  408. Assert.IsTrue (london.IsAmbiguousTime (new DateTime (2007, 10, 28, 0, 0, 1, DateTimeKind.Utc)));
  409. Assert.IsTrue (london.IsAmbiguousTime (new DateTime (2007, 10, 28, 0, 59, 59, DateTimeKind.Utc)));
  410. Assert.IsFalse (london.IsAmbiguousTime (new DateTime (2007, 10, 28, 1, 0, 0, DateTimeKind.Utc)));
  411. }
  412. #if SLOW_TESTS
  413. [Test]
  414. public void AmbiguousInUTC ()
  415. {
  416. 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)) {
  417. Assert.IsFalse (TimeZoneInfo.Utc.IsAmbiguousTime (date));
  418. }
  419. }
  420. #endif
  421. }
  422. [TestFixture]
  423. public class GetSystemTimeZonesTests
  424. {
  425. [Test]
  426. public void NotEmpty ()
  427. {
  428. if (Environment.OSVersion.Platform != PlatformID.Unix)
  429. return;
  430. global::System.Collections.ObjectModel.ReadOnlyCollection<TimeZoneInfo> systemTZ = TimeZoneInfo.GetSystemTimeZones ();
  431. Assert.IsNotNull(systemTZ, "SystemTZ is null");
  432. Assert.IsFalse (systemTZ.Count == 0, "SystemTZ is empty");
  433. }
  434. [Test]
  435. public void ContainsBrussels ()
  436. {
  437. if (Environment.OSVersion.Platform != PlatformID.Unix)
  438. return;
  439. global::System.Collections.ObjectModel.ReadOnlyCollection<TimeZoneInfo> systemTZ = TimeZoneInfo.GetSystemTimeZones ();
  440. foreach (TimeZoneInfo tz in systemTZ) {
  441. if (tz.Id == "Europe/Brussels")
  442. return;
  443. }
  444. Assert.Fail ("Europe/Brussels not found in SystemTZ");
  445. }
  446. }
  447. [TestFixture]
  448. public class FindSystemTimeZoneByIdTests
  449. {
  450. [Test]
  451. [ExpectedException (typeof (ArgumentNullException))]
  452. public void NullId ()
  453. {
  454. TimeZoneInfo.FindSystemTimeZoneById (null);
  455. }
  456. [Test]
  457. [ExpectedException (typeof (TimeZoneNotFoundException))]
  458. public void NonSystemTimezone ()
  459. {
  460. if (Environment.OSVersion.Platform != PlatformID.Unix)
  461. throw new TimeZoneNotFoundException ();
  462. TimeZoneInfo.FindSystemTimeZoneById ("Neverland/The_Lagoon");
  463. }
  464. [Test]
  465. public void FindBrusselsTZ ()
  466. {
  467. if (Environment.OSVersion.Platform != PlatformID.Unix)
  468. return;
  469. TimeZoneInfo brussels = TimeZoneInfo.FindSystemTimeZoneById ("Europe/Brussels");
  470. Assert.IsNotNull (brussels);
  471. }
  472. [Test]
  473. public void OffsetIsCorrectInKinshasa ()
  474. {
  475. if (Environment.OSVersion.Platform != PlatformID.Unix)
  476. return;
  477. TimeZoneInfo kin = TimeZoneInfo.FindSystemTimeZoneById ("Africa/Kinshasa");
  478. Assert.AreEqual (new TimeSpan (1,0,0), kin.BaseUtcOffset, "BaseUtcOffset in Kinshasa is not +1h");
  479. }
  480. [Test]
  481. public void OffsetIsCorrectInBrussels ()
  482. {
  483. if (Environment.OSVersion.Platform != PlatformID.Unix)
  484. return;
  485. TimeZoneInfo brussels = TimeZoneInfo.FindSystemTimeZoneById ("Europe/Brussels");
  486. Assert.AreEqual (new TimeSpan (1,0,0), brussels.BaseUtcOffset, "BaseUtcOffset for Brussels is not +1h");
  487. }
  488. [Test]
  489. public void NoDSTInKinshasa ()
  490. {
  491. if (Environment.OSVersion.Platform != PlatformID.Unix)
  492. return;
  493. TimeZoneInfo kin = TimeZoneInfo.FindSystemTimeZoneById ("Africa/Kinshasa");
  494. Assert.IsFalse (kin.SupportsDaylightSavingTime);
  495. }
  496. [Test]
  497. public void BrusselsSupportsDST ()
  498. {
  499. if (Environment.OSVersion.Platform != PlatformID.Unix)
  500. return;
  501. TimeZoneInfo brussels = TimeZoneInfo.FindSystemTimeZoneById ("Europe/Brussels");
  502. Assert.IsTrue (brussels.SupportsDaylightSavingTime);
  503. }
  504. [Test]
  505. public void MelbourneSupportsDST ()
  506. {
  507. if (Environment.OSVersion.Platform != PlatformID.Unix)
  508. return;
  509. TimeZoneInfo melbourne = TimeZoneInfo.FindSystemTimeZoneById ("Australia/Melbourne");
  510. Assert.IsTrue (melbourne.SupportsDaylightSavingTime);
  511. }
  512. [Test]
  513. public void RomeAndVaticanSharesTime ()
  514. {
  515. if (Environment.OSVersion.Platform != PlatformID.Unix)
  516. return;
  517. TimeZoneInfo rome = TimeZoneInfo.FindSystemTimeZoneById ("Europe/Rome");
  518. TimeZoneInfo vatican = TimeZoneInfo.FindSystemTimeZoneById ("Europe/Vatican");
  519. Assert.IsTrue (rome.HasSameRules (vatican));
  520. }
  521. [Test]
  522. public void FindSystemTimeZoneById_Local_Roundtrip ()
  523. {
  524. Assert.AreEqual (TimeZoneInfo.Local.Id, TimeZoneInfo.FindSystemTimeZoneById (TimeZoneInfo.Local.Id).Id);
  525. }
  526. [Test]
  527. public void Test326 ()
  528. {
  529. DateTime utc = DateTime.UtcNow;
  530. DateTime local = TimeZoneInfo.ConvertTime (utc, TimeZoneInfo.Utc, TimeZoneInfo.FindSystemTimeZoneById (TimeZoneInfo.Local.Id));
  531. Assert.AreEqual (local, utc + TimeZoneInfo.Local.GetUtcOffset (utc), "ConvertTime/Local");
  532. }
  533. #if SLOW_TESTS
  534. [Test]
  535. public void BrusselsAdjustments ()
  536. {
  537. TimeZoneInfo.TransitionTime start = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,2,0,0), 3, 5, DayOfWeek.Sunday);
  538. TimeZoneInfo.TransitionTime end = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,3,0,0), 10, 5, DayOfWeek.Sunday);
  539. TimeZoneInfo.AdjustmentRule rule = TimeZoneInfo.AdjustmentRule.CreateAdjustmentRule (DateTime.MinValue.Date, DateTime.MaxValue.Date, new TimeSpan (1,0,0), start, end);
  540. TimeZoneInfo brussels = TimeZoneInfo.CreateCustomTimeZone ("Europe/Brussels", new TimeSpan (1, 0, 0), "Europe/Brussels", "", "", new TimeZoneInfo.AdjustmentRule [] {rule});
  541. TimeZoneInfo brussels_sys = TimeZoneInfo.FindSystemTimeZoneById ("Europe/Brussels");
  542. 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)) {
  543. Assert.AreEqual (brussels.GetUtcOffset (date), brussels_sys.GetUtcOffset (date));
  544. Assert.AreEqual (brussels.IsDaylightSavingTime (date), brussels_sys.IsDaylightSavingTime (date));
  545. }
  546. }
  547. #endif
  548. }
  549. [TestFixture]
  550. public class GetAmbiguousTimeOffsetsTests
  551. {
  552. [Test]
  553. [ExpectedException (typeof(ArgumentException))]
  554. public void DateIsNotAmbiguous ()
  555. {
  556. if (Environment.OSVersion.Platform != PlatformID.Unix)
  557. throw new ArgumentException ();
  558. TimeZoneInfo brussels = TimeZoneInfo.FindSystemTimeZoneById ("Europe/Brussels");
  559. DateTime date = new DateTime (2007, 05, 11, 11, 40, 00);
  560. brussels.GetAmbiguousTimeOffsets (date);
  561. }
  562. [Test]
  563. public void AmbiguousOffsets ()
  564. {
  565. if (Environment.OSVersion.Platform != PlatformID.Unix)
  566. return;
  567. TimeZoneInfo brussels = TimeZoneInfo.FindSystemTimeZoneById ("Europe/Brussels");
  568. DateTime date = new DateTime (2007, 10, 28, 2, 30, 00);
  569. Assert.IsTrue (brussels.IsAmbiguousTime (date));
  570. Assert.AreEqual (2, brussels.GetAmbiguousTimeOffsets (date).Length);
  571. Assert.AreEqual (new TimeSpan[] {new TimeSpan (1, 0, 0), new TimeSpan (2, 0, 0)}, brussels.GetAmbiguousTimeOffsets (date));
  572. }
  573. }
  574. [TestFixture]
  575. public class HasSameRulesTests
  576. {
  577. [Test]
  578. public void NullAdjustments () //bnc #391011
  579. {
  580. TimeZoneInfo utc = TimeZoneInfo.Utc;
  581. TimeZoneInfo custom = TimeZoneInfo.CreateCustomTimeZone ("Custom", new TimeSpan (0), "Custom", "Custom");
  582. Assert.IsTrue (utc.HasSameRules (custom));
  583. }
  584. }
  585. }
  586. }
  587. #endif