TimeZoneInfoTest.cs 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960
  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.IsFalse (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. [Test] //Covers #26008
  264. public void DSTWithFloatingDateRule ()
  265. {
  266. // Construct a custom time zone where daylight saving time starts on the
  267. // 2nd Sunday in March.
  268. var transitionToDaylight = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1, 1, 1, 2, 0, 0), 3, 2, DayOfWeek.Sunday);
  269. var transitionToStandard = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1, 1, 1, 2, 0, 0), 11, 1, DayOfWeek.Sunday);
  270. var adjustment = TimeZoneInfo.AdjustmentRule.CreateAdjustmentRule (DateTime.MinValue.Date, DateTime.MaxValue.Date, new TimeSpan (1, 0, 0), transitionToDaylight, transitionToStandard);
  271. var timeZone = TimeZoneInfo.CreateCustomTimeZone ("BugCheck", new TimeSpan (-8, 0, 0), "Testing", "Testing Standard", "Testing Daylight", new TimeZoneInfo.AdjustmentRule [] { adjustment });
  272. // See if March 7, 2014 is listed as being during daylight saving time.
  273. // If it is DST, then the runtime has the bug that we are looking for.
  274. Assert.IsFalse (timeZone.IsDaylightSavingTime (new DateTime (2014, 3, 7, 12, 0, 0, DateTimeKind.Unspecified)));
  275. }
  276. }
  277. [TestFixture]
  278. public class ConvertTimeTests
  279. {
  280. TimeZoneInfo london;
  281. [SetUp]
  282. public void CreateTimeZones ()
  283. {
  284. TimeZoneInfo.TransitionTime start = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,1,0,0), 3, 5, DayOfWeek.Sunday);
  285. TimeZoneInfo.TransitionTime end = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,2,0,0), 10, 5, DayOfWeek.Sunday);
  286. TimeZoneInfo.AdjustmentRule rule = TimeZoneInfo.AdjustmentRule.CreateAdjustmentRule (DateTime.MinValue.Date, DateTime.MaxValue.Date, new TimeSpan (1,0,0), start, end);
  287. london = TimeZoneInfo.CreateCustomTimeZone ("Europe/London", new TimeSpan (0), "Europe/London", "British Standard Time", "British Summer Time", new TimeZoneInfo.AdjustmentRule [] {rule});
  288. }
  289. [Test]
  290. [ExpectedException (typeof (ArgumentException))]
  291. public void ConvertFromUtc_KindIsLocalException ()
  292. {
  293. if (Environment.OSVersion.Platform != PlatformID.Unix)
  294. throw new ArgumentException ();
  295. TimeZoneInfo.ConvertTimeFromUtc (new DateTime (2007, 5, 3, 11, 8, 0, DateTimeKind.Local), TimeZoneInfo.Local);
  296. }
  297. [Test]
  298. [ExpectedException (typeof (ArgumentNullException))]
  299. public void ConvertFromUtc_DestinationTimeZoneIsNullException ()
  300. {
  301. TimeZoneInfo.ConvertTimeFromUtc (new DateTime (2007, 5, 3, 11, 8, 0), null);
  302. }
  303. [Test]
  304. public void ConvertFromUtc_DestinationIsUTC ()
  305. {
  306. DateTime now = DateTime.UtcNow;
  307. DateTime converted = TimeZoneInfo.ConvertTimeFromUtc (now, TimeZoneInfo.Utc);
  308. Assert.AreEqual (now, converted);
  309. }
  310. [Test]
  311. public void ConvertFromUTC_ConvertInWinter ()
  312. {
  313. if (Environment.OSVersion.Platform != PlatformID.Unix)
  314. Assert.Ignore ("Not running on Unix.");
  315. DateTime utc = new DateTime (2007, 12, 25, 12, 0, 0);
  316. DateTime converted = TimeZoneInfo.ConvertTimeFromUtc (utc, london);
  317. Assert.AreEqual (utc, converted);
  318. }
  319. [Test]
  320. public void ConvertFromUtc_ConvertInSummer ()
  321. {
  322. if (Environment.OSVersion.Platform != PlatformID.Unix)
  323. Assert.Ignore ("Not running on Unix.");
  324. DateTime utc = new DateTime (2007, 06, 01, 12, 0, 0);
  325. DateTime converted = TimeZoneInfo.ConvertTimeFromUtc (utc, london);
  326. Assert.AreEqual (utc + new TimeSpan (1,0,0), converted);
  327. }
  328. [Test]
  329. public void ConvertToUTC_KindIsUtc ()
  330. {
  331. DateTime now = DateTime.UtcNow;
  332. Assert.AreEqual (now.Kind, DateTimeKind.Utc);
  333. DateTime converted = TimeZoneInfo.ConvertTimeToUtc (now);
  334. Assert.AreEqual (now, converted);
  335. }
  336. [Test]
  337. [ExpectedException (typeof (ArgumentException))]
  338. public void ConvertToUTC_KindIsUTCButSourceIsNot ()
  339. {
  340. TimeZoneInfo.ConvertTimeToUtc (new DateTime (2007, 5, 3, 12, 8, 0, DateTimeKind.Utc), london);
  341. }
  342. [Test]
  343. [ExpectedException (typeof (ArgumentException))]
  344. public void ConvertToUTC_KindIsLocalButSourceIsNot ()
  345. {
  346. if (Environment.OSVersion.Platform != PlatformID.Unix)
  347. throw new ArgumentException ();
  348. TimeZoneInfo.ConvertTimeToUtc (new DateTime (2007, 5, 3, 12, 8, 0, DateTimeKind.Local), london);
  349. }
  350. [Test]
  351. [ExpectedException (typeof (ArgumentException))]
  352. public void ConvertToUTC_InvalidDate ()
  353. {
  354. TimeZoneInfo.ConvertTimeToUtc (new DateTime (2007, 3, 25, 1, 30, 0), london);
  355. }
  356. [Test]
  357. [ExpectedException (typeof (ArgumentNullException))]
  358. public void ConvertToUTC_SourceIsNull ()
  359. {
  360. TimeZoneInfo.ConvertTimeToUtc (new DateTime (2007, 5, 3, 12, 16, 0), null);
  361. }
  362. #if SLOW_TESTS
  363. [Test]
  364. public void ConvertToUtc_MatchDateTimeBehavior ()
  365. {
  366. 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)) {
  367. Assert.AreEqual (TimeZoneInfo.ConvertTimeToUtc (date), date.ToUniversalTime ());
  368. }
  369. }
  370. #endif
  371. [Test]
  372. public void ConvertFromToUtc ()
  373. {
  374. if (Environment.OSVersion.Platform != PlatformID.Unix)
  375. Assert.Ignore ("Not running on Unix.");
  376. DateTime utc = DateTime.UtcNow;
  377. Assert.AreEqual (utc.Kind, DateTimeKind.Utc);
  378. DateTime converted = TimeZoneInfo.ConvertTimeFromUtc (utc, london);
  379. Assert.AreEqual (converted.Kind, DateTimeKind.Unspecified);
  380. DateTime back = TimeZoneInfo.ConvertTimeToUtc (converted, london);
  381. Assert.AreEqual (back.Kind, DateTimeKind.Utc);
  382. Assert.AreEqual (utc, back);
  383. }
  384. [Test]
  385. public void ConvertFromToLocal ()
  386. {
  387. DateTime utc = DateTime.UtcNow;
  388. Assert.AreEqual(utc.Kind, DateTimeKind.Utc);
  389. DateTime converted = TimeZoneInfo.ConvertTimeFromUtc(utc, TimeZoneInfo.Local);
  390. #if NET_4_0
  391. Assert.AreEqual(DateTimeKind.Local, converted.Kind);
  392. #else
  393. Assert.AreEqual(DateTimeKind.Unspecified, converted.Kind);
  394. #endif
  395. DateTime back = TimeZoneInfo.ConvertTimeToUtc(converted, TimeZoneInfo.Local);
  396. Assert.AreEqual(back.Kind, DateTimeKind.Utc);
  397. Assert.AreEqual(utc, back);
  398. }
  399. [Test]
  400. public void ConvertToTimeZone ()
  401. {
  402. if (Environment.OSVersion.Platform != PlatformID.Unix)
  403. Assert.Ignore ("Not running on Unix.");
  404. TimeZoneInfo.ConvertTime (DateTime.Now, TimeZoneInfo.FindSystemTimeZoneById("Pacific/Auckland"));
  405. }
  406. [Test]
  407. [ExpectedException (typeof (ArgumentNullException))]
  408. public void ConvertTime_DateTime_TimeZoneInfo_DestinationTimeZoneIsNull ()
  409. {
  410. TimeZoneInfo.ConvertTime (DateTime.Now, null);
  411. }
  412. [Test]
  413. public void ConvertTime_DateTime_TimeZoneInfo_DateTimeKindMatch ()
  414. {
  415. var sdt = new DateTime (2014, 1, 9, 23, 0, 0, DateTimeKind.Utc);
  416. var ddt = TimeZoneInfo.ConvertTime (sdt, TimeZoneInfo.Utc);
  417. Assert.AreEqual (ddt.Kind, sdt.Kind, "#1.1");
  418. Assert.AreEqual (ddt.Kind, DateTimeKind.Utc, "#1.2");
  419. sdt = new DateTime (2014, 1, 9, 23, 0, 0, DateTimeKind.Local);
  420. ddt = TimeZoneInfo.ConvertTime (sdt, TimeZoneInfo.Local);
  421. Assert.AreEqual (ddt.Kind, sdt.Kind, "#2.1");
  422. Assert.AreEqual (ddt.Kind, DateTimeKind.Local, "#2.2");
  423. sdt = new DateTime (2014, 1, 9, 23, 0, 0);
  424. ddt = TimeZoneInfo.ConvertTime (sdt, TimeZoneInfo.Local);
  425. Assert.AreEqual (ddt.Kind, sdt.Kind, "#3.1");
  426. Assert.AreEqual (ddt.Kind, DateTimeKind.Unspecified, "#3.2");
  427. }
  428. [Test]
  429. [ExpectedException (typeof (ArgumentNullException))]
  430. public void ConverTime_DateTime_TimeZoneInfo_TimeZoneInfo_SourceTimeZoneIsNull ()
  431. {
  432. TimeZoneInfo.ConvertTime (DateTime.Now, null, TimeZoneInfo.Local);
  433. }
  434. [Test]
  435. [ExpectedException (typeof (ArgumentNullException))]
  436. public void ConverTime_DateTime_TimeZoneInfo_TimeZoneInfo_DestinationTimeZoneIsNull ()
  437. {
  438. TimeZoneInfo.ConvertTime (DateTime.Now, TimeZoneInfo.Utc, null);
  439. }
  440. [Test (Description="Fix for xambug https://bugzilla.xamarin.com/show_bug.cgi?id=17155")]
  441. public void ConvertTime_AdjustmentRuleAfterNewYears ()
  442. {
  443. TimeZoneInfo tz;
  444. if (Environment.OSVersion.Platform == PlatformID.Unix)
  445. tz = TimeZoneInfo.FindSystemTimeZoneById ("Pacific/Auckland"); // *nix
  446. else
  447. tz = TimeZoneInfo.FindSystemTimeZoneById ("New Zealand Standard Time"); // Windows
  448. // DST start: 9/29/2013 2:00:00 AM
  449. // DST end: 4/6/2014 3:00:00 AM
  450. DateTime sdt = new DateTime (2014, 1, 9, 23, 0, 0, DateTimeKind.Utc);
  451. DateTime ddt = TimeZoneInfo.ConvertTime (sdt, tz);
  452. Assert.AreEqual (10, ddt.Day, "#1.1");
  453. Assert.AreEqual (1, ddt.Month, "#1.2");
  454. Assert.AreEqual (2014, ddt.Year, "#1.3");
  455. Assert.AreEqual (12, ddt.Hour, "#1.4");
  456. Assert.AreEqual (0, ddt.Minute, "#1.5");
  457. Assert.AreEqual (0, ddt.Second, "#1.6");
  458. // DST start: 9/29/2014 2:00:00 AM
  459. // DST end: 4/6/2015 3:00:00 AM
  460. sdt = new DateTime (2014, 6, 9, 23, 0, 0, DateTimeKind.Utc);
  461. ddt = TimeZoneInfo.ConvertTime (sdt, tz);
  462. Assert.AreEqual (10, ddt.Day, "#2.1");
  463. Assert.AreEqual (6, ddt.Month, "#2.2");
  464. Assert.AreEqual (2014, ddt.Year, "#2.3");
  465. Assert.AreEqual (11, ddt.Hour, "#2.4");
  466. Assert.AreEqual (0, ddt.Minute, "#2.5");
  467. Assert.AreEqual (0, ddt.Second, "#2.6");
  468. // DST start: 9/29/2014 2:00:00 AM
  469. // DST end: 4/6/2015 3:00:00 AM
  470. sdt = new DateTime (2014, 10, 9, 23, 0, 0, DateTimeKind.Utc);
  471. ddt = TimeZoneInfo.ConvertTime (sdt, tz);
  472. Assert.AreEqual (10, ddt.Day, "#3.1");
  473. Assert.AreEqual (10, ddt.Month, "#3.2");
  474. Assert.AreEqual (2014, ddt.Year, "#3.3");
  475. Assert.AreEqual (12, ddt.Hour, "#3.4");
  476. Assert.AreEqual (0, ddt.Minute, "#3.5");
  477. Assert.AreEqual (0, ddt.Second, "#3.6");
  478. }
  479. }
  480. [TestFixture]
  481. public class IsInvalidTimeTests
  482. {
  483. TimeZoneInfo london;
  484. [SetUp]
  485. public void CreateTimeZones ()
  486. {
  487. TimeZoneInfo.TransitionTime start = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,1,0,0), 3, 5, DayOfWeek.Sunday);
  488. TimeZoneInfo.TransitionTime end = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,2,0,0), 10, 5, DayOfWeek.Sunday);
  489. TimeZoneInfo.AdjustmentRule rule = TimeZoneInfo.AdjustmentRule.CreateAdjustmentRule (DateTime.MinValue.Date, DateTime.MaxValue.Date, new TimeSpan (1,0,0), start, end);
  490. london = TimeZoneInfo.CreateCustomTimeZone ("Europe/London", new TimeSpan (0), "Europe/London", "British Standard Time", "British Summer Time", new TimeZoneInfo.AdjustmentRule [] {rule});
  491. }
  492. #if SLOW_TESTS
  493. [Test]
  494. public void UTCDate ()
  495. {
  496. 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)) {
  497. date = DateTime.SpecifyKind (date, DateTimeKind.Utc);
  498. Assert.IsFalse (london.IsInvalidTime (date));
  499. }
  500. }
  501. #endif
  502. [Test]
  503. public void InvalidDates ()
  504. {
  505. Assert.IsFalse (london.IsInvalidTime (new DateTime (2007, 03, 25, 0, 59, 59)));
  506. Assert.IsTrue (london.IsInvalidTime (new DateTime (2007, 03, 25, 1, 0, 0)));
  507. Assert.IsTrue (london.IsInvalidTime (new DateTime (2007, 03, 25, 1, 59, 59)));
  508. Assert.IsFalse (london.IsInvalidTime (new DateTime (2007, 03, 25, 2, 0, 0)));
  509. }
  510. }
  511. [TestFixture]
  512. public class IsAmbiguousTimeTests
  513. {
  514. TimeZoneInfo london;
  515. [SetUp]
  516. public void CreateTimeZones ()
  517. {
  518. TimeZoneInfo.TransitionTime start = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,1,0,0), 3, 5, DayOfWeek.Sunday);
  519. TimeZoneInfo.TransitionTime end = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,2,0,0), 10, 5, DayOfWeek.Sunday);
  520. TimeZoneInfo.AdjustmentRule rule = TimeZoneInfo.AdjustmentRule.CreateAdjustmentRule (DateTime.MinValue.Date, DateTime.MaxValue.Date, new TimeSpan (1,0,0), start, end);
  521. london = TimeZoneInfo.CreateCustomTimeZone ("Europe/London", new TimeSpan (0), "Europe/London", "British Standard Time", "British Summer Time", new TimeZoneInfo.AdjustmentRule [] {rule});
  522. }
  523. [Test]
  524. public void AmbiguousDates ()
  525. {
  526. if (Environment.OSVersion.Platform != PlatformID.Unix)
  527. Assert.Ignore ("Not running on Unix.");
  528. Assert.IsFalse (london.IsAmbiguousTime (new DateTime (2007, 10, 28, 1, 0, 0)));
  529. Assert.IsTrue (london.IsAmbiguousTime (new DateTime (2007, 10, 28, 1, 0, 1)));
  530. Assert.IsTrue (london.IsAmbiguousTime (new DateTime (2007, 10, 28, 2, 0, 0)));
  531. Assert.IsFalse (london.IsAmbiguousTime (new DateTime (2007, 10, 28, 2, 0, 1)));
  532. }
  533. [Test]
  534. public void AmbiguousUTCDates ()
  535. {
  536. if (Environment.OSVersion.Platform != PlatformID.Unix)
  537. Assert.Ignore ("Not running on Unix.");
  538. Assert.IsFalse (london.IsAmbiguousTime (new DateTime (2007, 10, 28, 0, 0, 0, DateTimeKind.Utc)));
  539. Assert.IsTrue (london.IsAmbiguousTime (new DateTime (2007, 10, 28, 0, 0, 1, DateTimeKind.Utc)));
  540. Assert.IsTrue (london.IsAmbiguousTime (new DateTime (2007, 10, 28, 0, 59, 59, DateTimeKind.Utc)));
  541. Assert.IsFalse (london.IsAmbiguousTime (new DateTime (2007, 10, 28, 1, 0, 0, DateTimeKind.Utc)));
  542. }
  543. #if SLOW_TESTS
  544. [Test]
  545. public void AmbiguousInUTC ()
  546. {
  547. 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)) {
  548. Assert.IsFalse (TimeZoneInfo.Utc.IsAmbiguousTime (date));
  549. }
  550. }
  551. #endif
  552. }
  553. [TestFixture]
  554. public class GetSystemTimeZonesTests
  555. {
  556. [Test]
  557. public void NotEmpty ()
  558. {
  559. if (Environment.OSVersion.Platform != PlatformID.Unix)
  560. Assert.Ignore ("Not running on Unix.");
  561. global::System.Collections.ObjectModel.ReadOnlyCollection<TimeZoneInfo> systemTZ = TimeZoneInfo.GetSystemTimeZones ();
  562. Assert.IsNotNull(systemTZ, "SystemTZ is null");
  563. Assert.IsFalse (systemTZ.Count == 0, "SystemTZ is empty");
  564. }
  565. [Test]
  566. public void ContainsBrussels ()
  567. {
  568. if (Environment.OSVersion.Platform != PlatformID.Unix)
  569. Assert.Ignore ("Not running on Unix.");
  570. global::System.Collections.ObjectModel.ReadOnlyCollection<TimeZoneInfo> systemTZ = TimeZoneInfo.GetSystemTimeZones ();
  571. foreach (TimeZoneInfo tz in systemTZ) {
  572. if (tz.Id == "Europe/Brussels")
  573. return;
  574. }
  575. Assert.Fail ("Europe/Brussels not found in SystemTZ");
  576. }
  577. }
  578. [TestFixture]
  579. public class FindSystemTimeZoneByIdTests
  580. {
  581. [Test]
  582. [ExpectedException (typeof (ArgumentNullException))]
  583. public void NullId ()
  584. {
  585. TimeZoneInfo.FindSystemTimeZoneById (null);
  586. }
  587. [Test]
  588. [ExpectedException (typeof (TimeZoneNotFoundException))]
  589. public void NonSystemTimezone ()
  590. {
  591. if (Environment.OSVersion.Platform != PlatformID.Unix)
  592. throw new TimeZoneNotFoundException ();
  593. TimeZoneInfo.FindSystemTimeZoneById ("Neverland/The_Lagoon");
  594. }
  595. [Test]
  596. public void FindBrusselsTZ ()
  597. {
  598. if (Environment.OSVersion.Platform != PlatformID.Unix)
  599. Assert.Ignore ("Not running on Unix.");
  600. TimeZoneInfo brussels = TimeZoneInfo.FindSystemTimeZoneById ("Europe/Brussels");
  601. Assert.IsNotNull (brussels);
  602. }
  603. [Test]
  604. public void OffsetIsCorrectInKinshasa ()
  605. {
  606. if (Environment.OSVersion.Platform != PlatformID.Unix)
  607. Assert.Ignore ("Not running on Unix.");
  608. TimeZoneInfo kin = TimeZoneInfo.FindSystemTimeZoneById ("Africa/Kinshasa");
  609. Assert.AreEqual (new TimeSpan (1,0,0), kin.BaseUtcOffset, "BaseUtcOffset in Kinshasa is not +1h");
  610. }
  611. [Test]
  612. public void OffsetIsCorrectInBrussels ()
  613. {
  614. if (Environment.OSVersion.Platform != PlatformID.Unix)
  615. Assert.Ignore ("Not running on Unix.");
  616. TimeZoneInfo brussels = TimeZoneInfo.FindSystemTimeZoneById ("Europe/Brussels");
  617. Assert.AreEqual (new TimeSpan (1,0,0), brussels.BaseUtcOffset, "BaseUtcOffset for Brussels is not +1h");
  618. }
  619. [Test]
  620. public void NoDSTInKinshasa ()
  621. {
  622. if (Environment.OSVersion.Platform != PlatformID.Unix)
  623. Assert.Ignore ("Not running on Unix.");
  624. TimeZoneInfo kin = TimeZoneInfo.FindSystemTimeZoneById ("Africa/Kinshasa");
  625. Assert.IsFalse (kin.SupportsDaylightSavingTime);
  626. }
  627. [Test]
  628. public void BrusselsSupportsDST ()
  629. {
  630. if (Environment.OSVersion.Platform != PlatformID.Unix)
  631. Assert.Ignore ("Not running on Unix.");
  632. TimeZoneInfo brussels = TimeZoneInfo.FindSystemTimeZoneById ("Europe/Brussels");
  633. Assert.IsTrue (brussels.SupportsDaylightSavingTime);
  634. }
  635. [Test]
  636. public void MelbourneSupportsDST ()
  637. {
  638. if (Environment.OSVersion.Platform != PlatformID.Unix)
  639. Assert.Ignore ("Not running on Unix.");
  640. TimeZoneInfo melbourne = TimeZoneInfo.FindSystemTimeZoneById ("Australia/Melbourne");
  641. Assert.IsTrue (melbourne.SupportsDaylightSavingTime);
  642. }
  643. [Test]
  644. public void RomeAndVaticanSharesTime ()
  645. {
  646. if (Environment.OSVersion.Platform != PlatformID.Unix)
  647. Assert.Ignore ("Not running on Unix.");
  648. TimeZoneInfo rome = TimeZoneInfo.FindSystemTimeZoneById ("Europe/Rome");
  649. TimeZoneInfo vatican = TimeZoneInfo.FindSystemTimeZoneById ("Europe/Vatican");
  650. Assert.IsTrue (rome.HasSameRules (vatican));
  651. }
  652. [Test]
  653. public void FindSystemTimeZoneById_Local_Roundtrip ()
  654. {
  655. Assert.AreEqual (TimeZoneInfo.Local.Id, TimeZoneInfo.FindSystemTimeZoneById (TimeZoneInfo.Local.Id).Id);
  656. }
  657. [Test]
  658. public void Test326 ()
  659. {
  660. DateTime utc = DateTime.UtcNow;
  661. DateTime local = TimeZoneInfo.ConvertTime (utc, TimeZoneInfo.Utc, TimeZoneInfo.FindSystemTimeZoneById (TimeZoneInfo.Local.Id));
  662. Assert.AreEqual (local, utc + TimeZoneInfo.Local.GetUtcOffset (utc), "ConvertTime/Local");
  663. }
  664. #if SLOW_TESTS
  665. [Test]
  666. public void BrusselsAdjustments ()
  667. {
  668. TimeZoneInfo.TransitionTime start = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,2,0,0), 3, 5, DayOfWeek.Sunday);
  669. TimeZoneInfo.TransitionTime end = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,3,0,0), 10, 5, DayOfWeek.Sunday);
  670. TimeZoneInfo.AdjustmentRule rule = TimeZoneInfo.AdjustmentRule.CreateAdjustmentRule (DateTime.MinValue.Date, DateTime.MaxValue.Date, new TimeSpan (1,0,0), start, end);
  671. TimeZoneInfo brussels = TimeZoneInfo.CreateCustomTimeZone ("Europe/Brussels", new TimeSpan (1, 0, 0), "Europe/Brussels", "", "", new TimeZoneInfo.AdjustmentRule [] {rule});
  672. TimeZoneInfo brussels_sys = TimeZoneInfo.FindSystemTimeZoneById ("Europe/Brussels");
  673. 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)) {
  674. Assert.AreEqual (brussels.GetUtcOffset (date), brussels_sys.GetUtcOffset (date));
  675. Assert.AreEqual (brussels.IsDaylightSavingTime (date), brussels_sys.IsDaylightSavingTime (date));
  676. }
  677. }
  678. #endif
  679. }
  680. [TestFixture]
  681. public class GetAmbiguousTimeOffsetsTests
  682. {
  683. [Test]
  684. [ExpectedException (typeof(ArgumentException))]
  685. public void DateIsNotAmbiguous ()
  686. {
  687. if (Environment.OSVersion.Platform != PlatformID.Unix)
  688. throw new ArgumentException ();
  689. TimeZoneInfo brussels = TimeZoneInfo.FindSystemTimeZoneById ("Europe/Brussels");
  690. DateTime date = new DateTime (2007, 05, 11, 11, 40, 00);
  691. brussels.GetAmbiguousTimeOffsets (date);
  692. }
  693. [Test]
  694. public void AmbiguousOffsets ()
  695. {
  696. if (Environment.OSVersion.Platform != PlatformID.Unix)
  697. Assert.Ignore ("Not running on Unix.");
  698. TimeZoneInfo brussels = TimeZoneInfo.FindSystemTimeZoneById ("Europe/Brussels");
  699. DateTime date = new DateTime (2007, 10, 28, 2, 30, 00);
  700. Assert.IsTrue (brussels.IsAmbiguousTime (date));
  701. Assert.AreEqual (2, brussels.GetAmbiguousTimeOffsets (date).Length);
  702. Assert.AreEqual (new TimeSpan[] {new TimeSpan (1, 0, 0), new TimeSpan (2, 0, 0)}, brussels.GetAmbiguousTimeOffsets (date));
  703. }
  704. }
  705. [TestFixture]
  706. public class HasSameRulesTests
  707. {
  708. [Test]
  709. public void NullAdjustments () //bnc #391011
  710. {
  711. TimeZoneInfo utc = TimeZoneInfo.Utc;
  712. TimeZoneInfo custom = TimeZoneInfo.CreateCustomTimeZone ("Custom", new TimeSpan (0), "Custom", "Custom");
  713. Assert.IsTrue (utc.HasSameRules (custom));
  714. }
  715. }
  716. [TestFixture]
  717. public class SerializationTests
  718. {
  719. [Test]
  720. public void Serialization_Deserialization ()
  721. {
  722. TimeZoneInfo.TransitionTime start = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,1,0,0), 3, 5, DayOfWeek.Sunday);
  723. TimeZoneInfo.TransitionTime end = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,2,0,0), 10, 5, DayOfWeek.Sunday);
  724. TimeZoneInfo.AdjustmentRule rule = TimeZoneInfo.AdjustmentRule.CreateAdjustmentRule (DateTime.MinValue.Date, DateTime.MaxValue.Date, new TimeSpan (1,0,0), start, end);
  725. TimeZoneInfo london = TimeZoneInfo.CreateCustomTimeZone ("Europe/London", new TimeSpan (0), "Europe/London", "British Standard Time", "British Summer Time", new TimeZoneInfo.AdjustmentRule [] {rule});
  726. MemoryStream stream = new MemoryStream ();
  727. BinaryFormatter formatter = new BinaryFormatter ();
  728. formatter.Serialize (stream, london);
  729. stream.Position = 0;
  730. TimeZoneInfo deserialized = (TimeZoneInfo) formatter.Deserialize (stream);
  731. stream.Close ();
  732. stream.Dispose ();
  733. Assert.IsTrue (london.Equals (deserialized));
  734. }
  735. }
  736. [TestFixture]
  737. public class MultipleDaylightSavingTimeTests {
  738. private TimeZoneInfo cairo;
  739. private DateTime dst1Start;
  740. private DateTime dst1End;
  741. private DateTime dst2Start;
  742. private DateTime dst2End;
  743. private TimeSpan baseUtcOffset;
  744. private TimeSpan dstUtcOffset;
  745. private TimeSpan dstOffset;
  746. [SetUp]
  747. public void CreateTimeZones ()
  748. {
  749. /*
  750. From 1/1/2014 12:00:00 AM to 6/30/2014 12:00:00 AM
  751. Delta: 01:00:00
  752. Begins at 12:00 AM on 16 May
  753. Ends at 1:00 AM on 29 June
  754. From 7/1/2014 12:00:00 AM to 12/31/2014 12:00:00 AM
  755. Delta: 01:00:00
  756. Begins at 12:00 AM on 29 July
  757. Ends at 12:00 AM on 26 September
  758. */
  759. dst1Start = new DateTime (2014, 5, 16);
  760. dst1End = new DateTime (2014, 6, 29);
  761. dst2Start = new DateTime (2014, 7, 29);
  762. dst2End = new DateTime (2014, 9, 26);
  763. baseUtcOffset = new TimeSpan (2, 0, 0);
  764. dstUtcOffset = new TimeSpan (3, 0, 0);
  765. dstOffset = dstUtcOffset - baseUtcOffset;
  766. var rule1 = TimeZoneInfo.AdjustmentRule.CreateAdjustmentRule (
  767. new DateTime (2014, 1, 1), new DateTime (2014, 6, 30), dstOffset,
  768. CreateFixedDateRule (dst1Start), CreateFixedDateRule (dst1End));
  769. var rule2 = TimeZoneInfo.AdjustmentRule.CreateAdjustmentRule (
  770. new DateTime (2014, 7, 1), new DateTime (2014, 12, 31), dstOffset,
  771. CreateFixedDateRule (dst2Start), CreateFixedDateRule (dst2End));
  772. cairo = TimeZoneInfo.CreateCustomTimeZone ("Africa/Cairo", baseUtcOffset, "Africa/Cairo", "EET", "EEST",
  773. new [] {rule1, rule2});
  774. }
  775. private static TimeZoneInfo.TransitionTime CreateFixedDateRule (DateTime dateTime)
  776. {
  777. var time = new DateTime (dateTime.Ticks - dateTime.Date.Ticks);
  778. return TimeZoneInfo.TransitionTime.CreateFixedDateRule (time, dateTime.Month, dateTime.Day);
  779. }
  780. [Test]
  781. public void GetUtcOffset_FromUTC ()
  782. {
  783. var d = dst1Start.Add (-baseUtcOffset);
  784. d = DateTime.SpecifyKind (d, DateTimeKind.Utc);
  785. Assert.AreEqual(baseUtcOffset, cairo.GetUtcOffset (d.Add (new TimeSpan(0,0,0,-1))));
  786. Assert.AreEqual(dstUtcOffset, cairo.GetUtcOffset (d));
  787. Assert.AreEqual(dstUtcOffset, cairo.GetUtcOffset (d.Add (new TimeSpan(0,0,0, 1))));
  788. d = dst1End.Add (-baseUtcOffset-dstOffset);
  789. d = DateTime.SpecifyKind (d, DateTimeKind.Utc);
  790. Assert.AreEqual(dstUtcOffset, cairo.GetUtcOffset (d.Add (new TimeSpan(0,0,0,-1))));
  791. Assert.AreEqual(baseUtcOffset, cairo.GetUtcOffset (d));
  792. Assert.AreEqual(baseUtcOffset, cairo.GetUtcOffset (d.Add (new TimeSpan(0,0,0, 1))));
  793. d = dst2Start.Add (-baseUtcOffset);
  794. d = DateTime.SpecifyKind (d, DateTimeKind.Utc);
  795. Assert.AreEqual(baseUtcOffset, cairo.GetUtcOffset (d.Add (new TimeSpan(0,0,0,-1))));
  796. Assert.AreEqual(dstUtcOffset, cairo.GetUtcOffset (d));
  797. Assert.AreEqual(dstUtcOffset, cairo.GetUtcOffset (d.Add (new TimeSpan(0,0,0, 1))));
  798. d = dst2End.Add (-baseUtcOffset-dstOffset);
  799. d = DateTime.SpecifyKind (d, DateTimeKind.Utc);
  800. Assert.AreEqual(dstUtcOffset, cairo.GetUtcOffset (d.Add (new TimeSpan(0,0,0,-1))));
  801. Assert.AreEqual(baseUtcOffset, cairo.GetUtcOffset (d));
  802. Assert.AreEqual(baseUtcOffset, cairo.GetUtcOffset (d.Add (new TimeSpan(0,0,0, 1))));
  803. }
  804. [Test]
  805. public void GetUtcOffset_FromLocal ()
  806. {
  807. var d = dst1Start.Add (-baseUtcOffset);
  808. d = DateTime.SpecifyKind (d, DateTimeKind.Utc);
  809. d = d.ToLocalTime ();
  810. Assert.AreEqual(baseUtcOffset, cairo.GetUtcOffset (d.Add (new TimeSpan(0,0,0,-1))));
  811. Assert.AreEqual(dstUtcOffset, cairo.GetUtcOffset (d));
  812. Assert.AreEqual(dstUtcOffset, cairo.GetUtcOffset (d.Add (new TimeSpan(0,0,0, 1))));
  813. d = dst1End.Add (-baseUtcOffset-dstOffset);
  814. d = DateTime.SpecifyKind (d, DateTimeKind.Utc);
  815. d = d.ToLocalTime ();
  816. Assert.AreEqual(dstUtcOffset, cairo.GetUtcOffset (d.Add (new TimeSpan(0,0,0,-1))));
  817. Assert.AreEqual(baseUtcOffset, cairo.GetUtcOffset (d));
  818. Assert.AreEqual(baseUtcOffset, cairo.GetUtcOffset (d.Add (new TimeSpan(0,0,0, 1))));
  819. d = dst2Start.Add (-baseUtcOffset);
  820. d = DateTime.SpecifyKind (d, DateTimeKind.Utc);
  821. d = d.ToLocalTime ();
  822. Assert.AreEqual(baseUtcOffset, cairo.GetUtcOffset (d.Add (new TimeSpan(0,0,0,-1))));
  823. Assert.AreEqual(dstUtcOffset, cairo.GetUtcOffset (d));
  824. Assert.AreEqual(dstUtcOffset, cairo.GetUtcOffset (d.Add (new TimeSpan(0,0,0, 1))));
  825. d = dst2End.Add (-baseUtcOffset-dstOffset);
  826. d = DateTime.SpecifyKind (d, DateTimeKind.Utc);
  827. d = d.ToLocalTime ();
  828. Assert.AreEqual(dstUtcOffset, cairo.GetUtcOffset (d.Add (new TimeSpan(0,0,0,-1))));
  829. Assert.AreEqual(baseUtcOffset, cairo.GetUtcOffset (d));
  830. Assert.AreEqual(baseUtcOffset, cairo.GetUtcOffset (d.Add (new TimeSpan(0,0,0, 1))));
  831. }
  832. [Test]
  833. public void GetUtcOffset_FromUnspecified ()
  834. {
  835. var d = dst1Start.Add (dstOffset);
  836. Assert.AreEqual(baseUtcOffset, cairo.GetUtcOffset (d.Add (new TimeSpan(0,0,0,-1))));
  837. Assert.AreEqual(dstUtcOffset, cairo.GetUtcOffset (d));
  838. Assert.AreEqual(dstUtcOffset, cairo.GetUtcOffset (d.Add (new TimeSpan(0,0,0, 1))));
  839. d = dst1End.Add (-dstOffset);
  840. Assert.AreEqual(dstUtcOffset, cairo.GetUtcOffset (d.Add (new TimeSpan(0,0,0,-1))));
  841. Assert.AreEqual(baseUtcOffset, cairo.GetUtcOffset (d));
  842. Assert.AreEqual(baseUtcOffset, cairo.GetUtcOffset (d.Add (new TimeSpan(0,0,0, 1))));
  843. d = dst2Start.Add (dstOffset);
  844. Assert.AreEqual(baseUtcOffset, cairo.GetUtcOffset (d.Add (new TimeSpan(0,0,0,-1))));
  845. Assert.AreEqual(dstUtcOffset, cairo.GetUtcOffset (d));
  846. Assert.AreEqual(dstUtcOffset, cairo.GetUtcOffset (d.Add (new TimeSpan(0,0,0, 1))));
  847. d = dst2End.Add (-dstOffset);
  848. Assert.AreEqual(dstUtcOffset, cairo.GetUtcOffset (d.Add (new TimeSpan(0,0,0,-1))));
  849. Assert.AreEqual(baseUtcOffset, cairo.GetUtcOffset (d));
  850. Assert.AreEqual(baseUtcOffset, cairo.GetUtcOffset (d.Add (new TimeSpan(0,0,0, 1))));
  851. }
  852. }
  853. }
  854. }
  855. #endif