TimeZoneInfoTest.cs 29 KB

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