TimeZoneInfoTest.cs 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809
  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. [Test (Description="Description xambug #17155")]
  243. public void AdjustmentRuleAfterNewYears ()
  244. {
  245. TimeZoneInfo tz;
  246. if (Environment.OSVersion.Platform == PlatformID.Unix)
  247. tz = TimeZoneInfo.FindSystemTimeZoneById ("Pacific/Auckland"); // *nix
  248. else
  249. tz = TimeZoneInfo.FindSystemTimeZoneById ("New Zealand Standard Time"); // Windows
  250. // DST start: 9/29/2013 2:00:00 AM
  251. // DST end: 4/6/2014 3:00:00 AM
  252. DateTime dt = new DateTime (2014, 1, 9, 23, 0, 0, DateTimeKind.Utc);
  253. Assert.IsTrue (tz.IsDaylightSavingTime (dt), "#1.1");
  254. // DST start: 9/29/2014 2:00:00 AM
  255. // DST end: 4/6/2015 3:00:00 AM
  256. dt = new DateTime (2014, 6, 9, 23, 0, 0, DateTimeKind.Utc);
  257. Assert.IsFalse (tz.IsDaylightSavingTime (dt), "#2.1");
  258. // DST start: 9/29/2014 2:00:00 AM
  259. // DST end: 4/6/2015 3:00:00 AM
  260. dt = new DateTime (2014, 10, 9, 23, 0, 0, DateTimeKind.Utc);
  261. Assert.IsTrue (tz.IsDaylightSavingTime (dt), "#3.1");
  262. }
  263. }
  264. [TestFixture]
  265. public class ConvertTimeTests
  266. {
  267. TimeZoneInfo london;
  268. [SetUp]
  269. public void CreateTimeZones ()
  270. {
  271. TimeZoneInfo.TransitionTime start = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,1,0,0), 3, 5, DayOfWeek.Sunday);
  272. TimeZoneInfo.TransitionTime end = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,2,0,0), 10, 5, DayOfWeek.Sunday);
  273. TimeZoneInfo.AdjustmentRule rule = TimeZoneInfo.AdjustmentRule.CreateAdjustmentRule (DateTime.MinValue.Date, DateTime.MaxValue.Date, new TimeSpan (1,0,0), start, end);
  274. london = TimeZoneInfo.CreateCustomTimeZone ("Europe/London", new TimeSpan (0), "Europe/London", "British Standard Time", "British Summer Time", new TimeZoneInfo.AdjustmentRule [] {rule});
  275. }
  276. [Test]
  277. [ExpectedException (typeof (ArgumentException))]
  278. public void ConvertFromUtc_KindIsLocalException ()
  279. {
  280. if (Environment.OSVersion.Platform != PlatformID.Unix)
  281. throw new ArgumentException ();
  282. TimeZoneInfo.ConvertTimeFromUtc (new DateTime (2007, 5, 3, 11, 8, 0, DateTimeKind.Local), TimeZoneInfo.Local);
  283. }
  284. [Test]
  285. [ExpectedException (typeof (ArgumentNullException))]
  286. public void ConvertFromUtc_DestinationTimeZoneIsNullException ()
  287. {
  288. TimeZoneInfo.ConvertTimeFromUtc (new DateTime (2007, 5, 3, 11, 8, 0), null);
  289. }
  290. [Test]
  291. public void ConvertFromUtc_DestinationIsUTC ()
  292. {
  293. DateTime now = DateTime.UtcNow;
  294. DateTime converted = TimeZoneInfo.ConvertTimeFromUtc (now, TimeZoneInfo.Utc);
  295. Assert.AreEqual (now, converted);
  296. }
  297. [Test]
  298. public void ConvertFromUTC_ConvertInWinter ()
  299. {
  300. if (Environment.OSVersion.Platform != PlatformID.Unix)
  301. Assert.Ignore ("Not running on Unix.");
  302. DateTime utc = new DateTime (2007, 12, 25, 12, 0, 0);
  303. DateTime converted = TimeZoneInfo.ConvertTimeFromUtc (utc, london);
  304. Assert.AreEqual (utc, converted);
  305. }
  306. [Test]
  307. public void ConvertFromUtc_ConvertInSummer ()
  308. {
  309. if (Environment.OSVersion.Platform != PlatformID.Unix)
  310. Assert.Ignore ("Not running on Unix.");
  311. DateTime utc = new DateTime (2007, 06, 01, 12, 0, 0);
  312. DateTime converted = TimeZoneInfo.ConvertTimeFromUtc (utc, london);
  313. Assert.AreEqual (utc + new TimeSpan (1,0,0), converted);
  314. }
  315. [Test]
  316. public void ConvertToUTC_KindIsUtc ()
  317. {
  318. DateTime now = DateTime.UtcNow;
  319. Assert.AreEqual (now.Kind, DateTimeKind.Utc);
  320. DateTime converted = TimeZoneInfo.ConvertTimeToUtc (now);
  321. Assert.AreEqual (now, converted);
  322. }
  323. [Test]
  324. [ExpectedException (typeof (ArgumentException))]
  325. public void ConvertToUTC_KindIsUTCButSourceIsNot ()
  326. {
  327. TimeZoneInfo.ConvertTimeToUtc (new DateTime (2007, 5, 3, 12, 8, 0, DateTimeKind.Utc), london);
  328. }
  329. [Test]
  330. [ExpectedException (typeof (ArgumentException))]
  331. public void ConvertToUTC_KindIsLocalButSourceIsNot ()
  332. {
  333. if (Environment.OSVersion.Platform != PlatformID.Unix)
  334. throw new ArgumentException ();
  335. TimeZoneInfo.ConvertTimeToUtc (new DateTime (2007, 5, 3, 12, 8, 0, DateTimeKind.Local), london);
  336. }
  337. [Test]
  338. [ExpectedException (typeof (ArgumentException))]
  339. public void ConvertToUTC_InvalidDate ()
  340. {
  341. TimeZoneInfo.ConvertTimeToUtc (new DateTime (2007, 3, 25, 1, 30, 0), london);
  342. }
  343. [Test]
  344. [ExpectedException (typeof (ArgumentNullException))]
  345. public void ConvertToUTC_SourceIsNull ()
  346. {
  347. TimeZoneInfo.ConvertTimeToUtc (new DateTime (2007, 5, 3, 12, 16, 0), null);
  348. }
  349. #if SLOW_TESTS
  350. [Test]
  351. public void ConvertToUtc_MatchDateTimeBehavior ()
  352. {
  353. 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)) {
  354. Assert.AreEqual (TimeZoneInfo.ConvertTimeToUtc (date), date.ToUniversalTime ());
  355. }
  356. }
  357. #endif
  358. [Test]
  359. public void ConvertFromToUtc ()
  360. {
  361. if (Environment.OSVersion.Platform != PlatformID.Unix)
  362. Assert.Ignore ("Not running on Unix.");
  363. DateTime utc = DateTime.UtcNow;
  364. Assert.AreEqual (utc.Kind, DateTimeKind.Utc);
  365. DateTime converted = TimeZoneInfo.ConvertTimeFromUtc (utc, london);
  366. Assert.AreEqual (converted.Kind, DateTimeKind.Unspecified);
  367. DateTime back = TimeZoneInfo.ConvertTimeToUtc (converted, london);
  368. Assert.AreEqual (back.Kind, DateTimeKind.Utc);
  369. Assert.AreEqual (utc, back);
  370. }
  371. [Test]
  372. public void ConvertFromToLocal ()
  373. {
  374. DateTime utc = DateTime.UtcNow;
  375. Assert.AreEqual(utc.Kind, DateTimeKind.Utc);
  376. DateTime converted = TimeZoneInfo.ConvertTimeFromUtc(utc, TimeZoneInfo.Local);
  377. #if NET_4_0
  378. Assert.AreEqual(DateTimeKind.Local, converted.Kind);
  379. #else
  380. Assert.AreEqual(DateTimeKind.Unspecified, converted.Kind);
  381. #endif
  382. DateTime back = TimeZoneInfo.ConvertTimeToUtc(converted, TimeZoneInfo.Local);
  383. Assert.AreEqual(back.Kind, DateTimeKind.Utc);
  384. Assert.AreEqual(utc, back);
  385. }
  386. [Test]
  387. public void ConvertToTimeZone ()
  388. {
  389. if (Environment.OSVersion.Platform != PlatformID.Unix)
  390. Assert.Ignore ("Not running on Unix.");
  391. TimeZoneInfo.ConvertTime (DateTime.Now, TimeZoneInfo.FindSystemTimeZoneById("Pacific/Auckland"));
  392. }
  393. [Test]
  394. [ExpectedException (typeof (ArgumentNullException))]
  395. public void ConvertTime_DateTime_TimeZoneInfo_DestinationTimeZoneIsNull ()
  396. {
  397. TimeZoneInfo.ConvertTime (DateTime.Now, null);
  398. }
  399. [Test]
  400. public void ConvertTime_DateTime_TimeZoneInfo_DateTimeKindMatch ()
  401. {
  402. var sdt = new DateTime (2014, 1, 9, 23, 0, 0, DateTimeKind.Utc);
  403. var ddt = TimeZoneInfo.ConvertTime (sdt, TimeZoneInfo.Utc);
  404. Assert.AreEqual (ddt.Kind, sdt.Kind, "#1.1");
  405. Assert.AreEqual (ddt.Kind, DateTimeKind.Utc, "#1.2");
  406. sdt = new DateTime (2014, 1, 9, 23, 0, 0, DateTimeKind.Local);
  407. ddt = TimeZoneInfo.ConvertTime (sdt, TimeZoneInfo.Local);
  408. Assert.AreEqual (ddt.Kind, sdt.Kind, "#2.1");
  409. Assert.AreEqual (ddt.Kind, DateTimeKind.Local, "#2.2");
  410. sdt = new DateTime (2014, 1, 9, 23, 0, 0);
  411. ddt = TimeZoneInfo.ConvertTime (sdt, TimeZoneInfo.Local);
  412. Assert.AreEqual (ddt.Kind, sdt.Kind, "#3.1");
  413. Assert.AreEqual (ddt.Kind, DateTimeKind.Unspecified, "#3.2");
  414. }
  415. [Test]
  416. [ExpectedException (typeof (ArgumentNullException))]
  417. public void ConverTime_DateTime_TimeZoneInfo_TimeZoneInfo_SourceTimeZoneIsNull ()
  418. {
  419. TimeZoneInfo.ConvertTime (DateTime.Now, null, TimeZoneInfo.Local);
  420. }
  421. [Test]
  422. [ExpectedException (typeof (ArgumentNullException))]
  423. public void ConverTime_DateTime_TimeZoneInfo_TimeZoneInfo_DestinationTimeZoneIsNull ()
  424. {
  425. TimeZoneInfo.ConvertTime (DateTime.Now, TimeZoneInfo.Utc, null);
  426. }
  427. [Test (Description="Fix for xambug https://bugzilla.xamarin.com/show_bug.cgi?id=17155")]
  428. public void ConvertTime_AdjustmentRuleAfterNewYears ()
  429. {
  430. TimeZoneInfo tz;
  431. if (Environment.OSVersion.Platform == PlatformID.Unix)
  432. tz = TimeZoneInfo.FindSystemTimeZoneById ("Pacific/Auckland"); // *nix
  433. else
  434. tz = TimeZoneInfo.FindSystemTimeZoneById ("New Zealand Standard Time"); // Windows
  435. // DST start: 9/29/2013 2:00:00 AM
  436. // DST end: 4/6/2014 3:00:00 AM
  437. DateTime sdt = new DateTime (2014, 1, 9, 23, 0, 0, DateTimeKind.Utc);
  438. DateTime ddt = TimeZoneInfo.ConvertTime (sdt, tz);
  439. Assert.AreEqual (10, ddt.Day, "#1.1");
  440. Assert.AreEqual (1, ddt.Month, "#1.2");
  441. Assert.AreEqual (2014, ddt.Year, "#1.3");
  442. Assert.AreEqual (12, ddt.Hour, "#1.4");
  443. Assert.AreEqual (0, ddt.Minute, "#1.5");
  444. Assert.AreEqual (0, ddt.Second, "#1.6");
  445. // DST start: 9/29/2014 2:00:00 AM
  446. // DST end: 4/6/2015 3:00:00 AM
  447. sdt = new DateTime (2014, 6, 9, 23, 0, 0, DateTimeKind.Utc);
  448. ddt = TimeZoneInfo.ConvertTime (sdt, tz);
  449. Assert.AreEqual (10, ddt.Day, "#2.1");
  450. Assert.AreEqual (6, ddt.Month, "#2.2");
  451. Assert.AreEqual (2014, ddt.Year, "#2.3");
  452. Assert.AreEqual (11, ddt.Hour, "#2.4");
  453. Assert.AreEqual (0, ddt.Minute, "#2.5");
  454. Assert.AreEqual (0, ddt.Second, "#2.6");
  455. // DST start: 9/29/2014 2:00:00 AM
  456. // DST end: 4/6/2015 3:00:00 AM
  457. sdt = new DateTime (2014, 10, 9, 23, 0, 0, DateTimeKind.Utc);
  458. ddt = TimeZoneInfo.ConvertTime (sdt, tz);
  459. Assert.AreEqual (10, ddt.Day, "#3.1");
  460. Assert.AreEqual (10, ddt.Month, "#3.2");
  461. Assert.AreEqual (2014, ddt.Year, "#3.3");
  462. Assert.AreEqual (12, ddt.Hour, "#3.4");
  463. Assert.AreEqual (0, ddt.Minute, "#3.5");
  464. Assert.AreEqual (0, ddt.Second, "#3.6");
  465. }
  466. }
  467. [TestFixture]
  468. public class IsInvalidTimeTests
  469. {
  470. TimeZoneInfo london;
  471. [SetUp]
  472. public void CreateTimeZones ()
  473. {
  474. TimeZoneInfo.TransitionTime start = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,1,0,0), 3, 5, DayOfWeek.Sunday);
  475. TimeZoneInfo.TransitionTime end = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,2,0,0), 10, 5, DayOfWeek.Sunday);
  476. TimeZoneInfo.AdjustmentRule rule = TimeZoneInfo.AdjustmentRule.CreateAdjustmentRule (DateTime.MinValue.Date, DateTime.MaxValue.Date, new TimeSpan (1,0,0), start, end);
  477. london = TimeZoneInfo.CreateCustomTimeZone ("Europe/London", new TimeSpan (0), "Europe/London", "British Standard Time", "British Summer Time", new TimeZoneInfo.AdjustmentRule [] {rule});
  478. }
  479. #if SLOW_TESTS
  480. [Test]
  481. public void UTCDate ()
  482. {
  483. 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)) {
  484. date = DateTime.SpecifyKind (date, DateTimeKind.Utc);
  485. Assert.IsFalse (london.IsInvalidTime (date));
  486. }
  487. }
  488. #endif
  489. [Test]
  490. public void InvalidDates ()
  491. {
  492. Assert.IsFalse (london.IsInvalidTime (new DateTime (2007, 03, 25, 0, 59, 59)));
  493. Assert.IsTrue (london.IsInvalidTime (new DateTime (2007, 03, 25, 1, 0, 0)));
  494. Assert.IsTrue (london.IsInvalidTime (new DateTime (2007, 03, 25, 1, 59, 59)));
  495. Assert.IsFalse (london.IsInvalidTime (new DateTime (2007, 03, 25, 2, 0, 0)));
  496. }
  497. }
  498. [TestFixture]
  499. public class IsAmbiguousTimeTests
  500. {
  501. TimeZoneInfo london;
  502. [SetUp]
  503. public void CreateTimeZones ()
  504. {
  505. TimeZoneInfo.TransitionTime start = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,1,0,0), 3, 5, DayOfWeek.Sunday);
  506. TimeZoneInfo.TransitionTime end = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,2,0,0), 10, 5, DayOfWeek.Sunday);
  507. TimeZoneInfo.AdjustmentRule rule = TimeZoneInfo.AdjustmentRule.CreateAdjustmentRule (DateTime.MinValue.Date, DateTime.MaxValue.Date, new TimeSpan (1,0,0), start, end);
  508. london = TimeZoneInfo.CreateCustomTimeZone ("Europe/London", new TimeSpan (0), "Europe/London", "British Standard Time", "British Summer Time", new TimeZoneInfo.AdjustmentRule [] {rule});
  509. }
  510. [Test]
  511. public void AmbiguousDates ()
  512. {
  513. if (Environment.OSVersion.Platform != PlatformID.Unix)
  514. Assert.Ignore ("Not running on Unix.");
  515. Assert.IsFalse (london.IsAmbiguousTime (new DateTime (2007, 10, 28, 1, 0, 0)));
  516. Assert.IsTrue (london.IsAmbiguousTime (new DateTime (2007, 10, 28, 1, 0, 1)));
  517. Assert.IsTrue (london.IsAmbiguousTime (new DateTime (2007, 10, 28, 2, 0, 0)));
  518. Assert.IsFalse (london.IsAmbiguousTime (new DateTime (2007, 10, 28, 2, 0, 1)));
  519. }
  520. [Test]
  521. public void AmbiguousUTCDates ()
  522. {
  523. if (Environment.OSVersion.Platform != PlatformID.Unix)
  524. Assert.Ignore ("Not running on Unix.");
  525. Assert.IsFalse (london.IsAmbiguousTime (new DateTime (2007, 10, 28, 0, 0, 0, DateTimeKind.Utc)));
  526. Assert.IsTrue (london.IsAmbiguousTime (new DateTime (2007, 10, 28, 0, 0, 1, DateTimeKind.Utc)));
  527. Assert.IsTrue (london.IsAmbiguousTime (new DateTime (2007, 10, 28, 0, 59, 59, DateTimeKind.Utc)));
  528. Assert.IsFalse (london.IsAmbiguousTime (new DateTime (2007, 10, 28, 1, 0, 0, DateTimeKind.Utc)));
  529. }
  530. #if SLOW_TESTS
  531. [Test]
  532. public void AmbiguousInUTC ()
  533. {
  534. 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)) {
  535. Assert.IsFalse (TimeZoneInfo.Utc.IsAmbiguousTime (date));
  536. }
  537. }
  538. #endif
  539. }
  540. [TestFixture]
  541. public class GetSystemTimeZonesTests
  542. {
  543. [Test]
  544. public void NotEmpty ()
  545. {
  546. if (Environment.OSVersion.Platform != PlatformID.Unix)
  547. Assert.Ignore ("Not running on Unix.");
  548. global::System.Collections.ObjectModel.ReadOnlyCollection<TimeZoneInfo> systemTZ = TimeZoneInfo.GetSystemTimeZones ();
  549. Assert.IsNotNull(systemTZ, "SystemTZ is null");
  550. Assert.IsFalse (systemTZ.Count == 0, "SystemTZ is empty");
  551. }
  552. [Test]
  553. public void ContainsBrussels ()
  554. {
  555. if (Environment.OSVersion.Platform != PlatformID.Unix)
  556. Assert.Ignore ("Not running on Unix.");
  557. global::System.Collections.ObjectModel.ReadOnlyCollection<TimeZoneInfo> systemTZ = TimeZoneInfo.GetSystemTimeZones ();
  558. foreach (TimeZoneInfo tz in systemTZ) {
  559. if (tz.Id == "Europe/Brussels")
  560. return;
  561. }
  562. Assert.Fail ("Europe/Brussels not found in SystemTZ");
  563. }
  564. }
  565. [TestFixture]
  566. public class FindSystemTimeZoneByIdTests
  567. {
  568. [Test]
  569. [ExpectedException (typeof (ArgumentNullException))]
  570. public void NullId ()
  571. {
  572. TimeZoneInfo.FindSystemTimeZoneById (null);
  573. }
  574. [Test]
  575. [ExpectedException (typeof (TimeZoneNotFoundException))]
  576. public void NonSystemTimezone ()
  577. {
  578. if (Environment.OSVersion.Platform != PlatformID.Unix)
  579. throw new TimeZoneNotFoundException ();
  580. TimeZoneInfo.FindSystemTimeZoneById ("Neverland/The_Lagoon");
  581. }
  582. [Test]
  583. public void FindBrusselsTZ ()
  584. {
  585. if (Environment.OSVersion.Platform != PlatformID.Unix)
  586. Assert.Ignore ("Not running on Unix.");
  587. TimeZoneInfo brussels = TimeZoneInfo.FindSystemTimeZoneById ("Europe/Brussels");
  588. Assert.IsNotNull (brussels);
  589. }
  590. [Test]
  591. public void OffsetIsCorrectInKinshasa ()
  592. {
  593. if (Environment.OSVersion.Platform != PlatformID.Unix)
  594. Assert.Ignore ("Not running on Unix.");
  595. TimeZoneInfo kin = TimeZoneInfo.FindSystemTimeZoneById ("Africa/Kinshasa");
  596. Assert.AreEqual (new TimeSpan (1,0,0), kin.BaseUtcOffset, "BaseUtcOffset in Kinshasa is not +1h");
  597. }
  598. [Test]
  599. public void OffsetIsCorrectInBrussels ()
  600. {
  601. if (Environment.OSVersion.Platform != PlatformID.Unix)
  602. Assert.Ignore ("Not running on Unix.");
  603. TimeZoneInfo brussels = TimeZoneInfo.FindSystemTimeZoneById ("Europe/Brussels");
  604. Assert.AreEqual (new TimeSpan (1,0,0), brussels.BaseUtcOffset, "BaseUtcOffset for Brussels is not +1h");
  605. }
  606. [Test]
  607. public void NoDSTInKinshasa ()
  608. {
  609. if (Environment.OSVersion.Platform != PlatformID.Unix)
  610. Assert.Ignore ("Not running on Unix.");
  611. TimeZoneInfo kin = TimeZoneInfo.FindSystemTimeZoneById ("Africa/Kinshasa");
  612. Assert.IsFalse (kin.SupportsDaylightSavingTime);
  613. }
  614. [Test]
  615. public void BrusselsSupportsDST ()
  616. {
  617. if (Environment.OSVersion.Platform != PlatformID.Unix)
  618. Assert.Ignore ("Not running on Unix.");
  619. TimeZoneInfo brussels = TimeZoneInfo.FindSystemTimeZoneById ("Europe/Brussels");
  620. Assert.IsTrue (brussels.SupportsDaylightSavingTime);
  621. }
  622. [Test]
  623. public void MelbourneSupportsDST ()
  624. {
  625. if (Environment.OSVersion.Platform != PlatformID.Unix)
  626. Assert.Ignore ("Not running on Unix.");
  627. TimeZoneInfo melbourne = TimeZoneInfo.FindSystemTimeZoneById ("Australia/Melbourne");
  628. Assert.IsTrue (melbourne.SupportsDaylightSavingTime);
  629. }
  630. [Test]
  631. public void RomeAndVaticanSharesTime ()
  632. {
  633. if (Environment.OSVersion.Platform != PlatformID.Unix)
  634. Assert.Ignore ("Not running on Unix.");
  635. TimeZoneInfo rome = TimeZoneInfo.FindSystemTimeZoneById ("Europe/Rome");
  636. TimeZoneInfo vatican = TimeZoneInfo.FindSystemTimeZoneById ("Europe/Vatican");
  637. Assert.IsTrue (rome.HasSameRules (vatican));
  638. }
  639. [Test]
  640. public void FindSystemTimeZoneById_Local_Roundtrip ()
  641. {
  642. Assert.AreEqual (TimeZoneInfo.Local.Id, TimeZoneInfo.FindSystemTimeZoneById (TimeZoneInfo.Local.Id).Id);
  643. }
  644. [Test]
  645. public void Test326 ()
  646. {
  647. DateTime utc = DateTime.UtcNow;
  648. DateTime local = TimeZoneInfo.ConvertTime (utc, TimeZoneInfo.Utc, TimeZoneInfo.FindSystemTimeZoneById (TimeZoneInfo.Local.Id));
  649. Assert.AreEqual (local, utc + TimeZoneInfo.Local.GetUtcOffset (utc), "ConvertTime/Local");
  650. }
  651. #if SLOW_TESTS
  652. [Test]
  653. public void BrusselsAdjustments ()
  654. {
  655. TimeZoneInfo.TransitionTime start = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,2,0,0), 3, 5, DayOfWeek.Sunday);
  656. TimeZoneInfo.TransitionTime end = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,3,0,0), 10, 5, DayOfWeek.Sunday);
  657. TimeZoneInfo.AdjustmentRule rule = TimeZoneInfo.AdjustmentRule.CreateAdjustmentRule (DateTime.MinValue.Date, DateTime.MaxValue.Date, new TimeSpan (1,0,0), start, end);
  658. TimeZoneInfo brussels = TimeZoneInfo.CreateCustomTimeZone ("Europe/Brussels", new TimeSpan (1, 0, 0), "Europe/Brussels", "", "", new TimeZoneInfo.AdjustmentRule [] {rule});
  659. TimeZoneInfo brussels_sys = TimeZoneInfo.FindSystemTimeZoneById ("Europe/Brussels");
  660. 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)) {
  661. Assert.AreEqual (brussels.GetUtcOffset (date), brussels_sys.GetUtcOffset (date));
  662. Assert.AreEqual (brussels.IsDaylightSavingTime (date), brussels_sys.IsDaylightSavingTime (date));
  663. }
  664. }
  665. #endif
  666. }
  667. [TestFixture]
  668. public class GetAmbiguousTimeOffsetsTests
  669. {
  670. [Test]
  671. [ExpectedException (typeof(ArgumentException))]
  672. public void DateIsNotAmbiguous ()
  673. {
  674. if (Environment.OSVersion.Platform != PlatformID.Unix)
  675. throw new ArgumentException ();
  676. TimeZoneInfo brussels = TimeZoneInfo.FindSystemTimeZoneById ("Europe/Brussels");
  677. DateTime date = new DateTime (2007, 05, 11, 11, 40, 00);
  678. brussels.GetAmbiguousTimeOffsets (date);
  679. }
  680. [Test]
  681. public void AmbiguousOffsets ()
  682. {
  683. if (Environment.OSVersion.Platform != PlatformID.Unix)
  684. Assert.Ignore ("Not running on Unix.");
  685. TimeZoneInfo brussels = TimeZoneInfo.FindSystemTimeZoneById ("Europe/Brussels");
  686. DateTime date = new DateTime (2007, 10, 28, 2, 30, 00);
  687. Assert.IsTrue (brussels.IsAmbiguousTime (date));
  688. Assert.AreEqual (2, brussels.GetAmbiguousTimeOffsets (date).Length);
  689. Assert.AreEqual (new TimeSpan[] {new TimeSpan (1, 0, 0), new TimeSpan (2, 0, 0)}, brussels.GetAmbiguousTimeOffsets (date));
  690. }
  691. }
  692. [TestFixture]
  693. public class HasSameRulesTests
  694. {
  695. [Test]
  696. public void NullAdjustments () //bnc #391011
  697. {
  698. TimeZoneInfo utc = TimeZoneInfo.Utc;
  699. TimeZoneInfo custom = TimeZoneInfo.CreateCustomTimeZone ("Custom", new TimeSpan (0), "Custom", "Custom");
  700. Assert.IsTrue (utc.HasSameRules (custom));
  701. }
  702. }
  703. [TestFixture]
  704. public class SerializationTests
  705. {
  706. [Test]
  707. public void Serialization_Deserialization ()
  708. {
  709. TimeZoneInfo.TransitionTime start = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,1,0,0), 3, 5, DayOfWeek.Sunday);
  710. TimeZoneInfo.TransitionTime end = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,2,0,0), 10, 5, DayOfWeek.Sunday);
  711. TimeZoneInfo.AdjustmentRule rule = TimeZoneInfo.AdjustmentRule.CreateAdjustmentRule (DateTime.MinValue.Date, DateTime.MaxValue.Date, new TimeSpan (1,0,0), start, end);
  712. TimeZoneInfo london = TimeZoneInfo.CreateCustomTimeZone ("Europe/London", new TimeSpan (0), "Europe/London", "British Standard Time", "British Summer Time", new TimeZoneInfo.AdjustmentRule [] {rule});
  713. MemoryStream stream = new MemoryStream ();
  714. BinaryFormatter formatter = new BinaryFormatter ();
  715. formatter.Serialize (stream, london);
  716. stream.Position = 0;
  717. TimeZoneInfo deserialized = (TimeZoneInfo) formatter.Deserialize (stream);
  718. stream.Close ();
  719. stream.Dispose ();
  720. Assert.IsTrue (london.Equals (deserialized));
  721. }
  722. }
  723. }
  724. }
  725. #endif