TimeZoneInfoTest.cs 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986
  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. namespace MonoTests.System
  34. {
  35. public class TimeZoneInfoTest
  36. {
  37. [TestFixture]
  38. public class PropertiesTests
  39. {
  40. [Test]
  41. public void GetLocal ()
  42. {
  43. if (Environment.OSVersion.Platform != PlatformID.Unix)
  44. Assert.Ignore ("Not running on Unix.");
  45. TimeZoneInfo local = TimeZoneInfo.Local;
  46. Assert.IsNotNull (local);
  47. Assert.IsTrue (true);
  48. }
  49. }
  50. [TestFixture]
  51. public class CreateCustomTimezoneTests
  52. {
  53. [Test]
  54. [ExpectedException (typeof (ArgumentNullException))]
  55. public void IdIsNullException ()
  56. {
  57. TimeZoneInfo.CreateCustomTimeZone (null, new TimeSpan (0), null, null);
  58. }
  59. [Test]
  60. [ExpectedException (typeof (ArgumentException))]
  61. public void IdIsEmptyString ()
  62. {
  63. TimeZoneInfo.CreateCustomTimeZone ("", new TimeSpan (0), null, null);
  64. }
  65. [Test]
  66. [ExpectedException (typeof (ArgumentException))]
  67. public void OffsetIsNotMinutes ()
  68. {
  69. TimeZoneInfo.CreateCustomTimeZone ("mytimezone", new TimeSpan (0, 0, 55), null, null);
  70. }
  71. [Test]
  72. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  73. public void OffsetTooBig ()
  74. {
  75. TimeZoneInfo.CreateCustomTimeZone ("mytimezone", new TimeSpan (14, 1, 0), null, null);
  76. }
  77. [Test]
  78. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  79. public void OffsetTooSmall ()
  80. {
  81. TimeZoneInfo.CreateCustomTimeZone ("mytimezone", - new TimeSpan (14, 1, 0), null, null);
  82. }
  83. #if STRICT
  84. [Test]
  85. [ExpectedException (typeof (ArgumentException))]
  86. public void IdLongerThan32 ()
  87. {
  88. TimeZoneInfo.CreateCustomTimeZone ("12345678901234567890123456789012345", new TimeSpan (0), null, null);
  89. }
  90. #endif
  91. [Test]
  92. [ExpectedException (typeof (InvalidTimeZoneException))]
  93. public void AdjustmentRulesOverlap ()
  94. {
  95. TimeZoneInfo.TransitionTime s1 = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,4,0,0), 3, 2, DayOfWeek.Sunday);
  96. TimeZoneInfo.TransitionTime e1 = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,4,0,0), 10, 2, DayOfWeek.Sunday);
  97. TimeZoneInfo.AdjustmentRule r1 = TimeZoneInfo.AdjustmentRule.CreateAdjustmentRule (new DateTime (2000,1,1), new DateTime (2005,1,1), new TimeSpan (1,0,0), s1, e1);
  98. TimeZoneInfo.TransitionTime s2 = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,4,0,0), 2, 2, DayOfWeek.Sunday);
  99. TimeZoneInfo.TransitionTime e2 = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,4,0,0), 11, 2, DayOfWeek.Sunday);
  100. TimeZoneInfo.AdjustmentRule r2 = TimeZoneInfo.AdjustmentRule.CreateAdjustmentRule (new DateTime (2004,1,1), new DateTime (2007,1,1), new TimeSpan (1,0,0), s2, e2);
  101. TimeZoneInfo.CreateCustomTimeZone ("mytimezone", new TimeSpan (6,0,0),null,null,null,new TimeZoneInfo.AdjustmentRule[] {r1, r2});
  102. }
  103. [Test]
  104. [ExpectedException (typeof (InvalidTimeZoneException))]
  105. public void RulesNotOrdered ()
  106. {
  107. TimeZoneInfo.TransitionTime s1 = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,4,0,0), 3, 2, DayOfWeek.Sunday);
  108. TimeZoneInfo.TransitionTime e1 = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,4,0,0), 10, 2, DayOfWeek.Sunday);
  109. TimeZoneInfo.AdjustmentRule r1 = TimeZoneInfo.AdjustmentRule.CreateAdjustmentRule (new DateTime (2000,1,1), new DateTime (2005,1,1), new TimeSpan (1,0,0), s1, e1);
  110. TimeZoneInfo.TransitionTime s2 = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,4,0,0), 2, 2, DayOfWeek.Sunday);
  111. TimeZoneInfo.TransitionTime e2 = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,4,0,0), 11, 2, DayOfWeek.Sunday);
  112. TimeZoneInfo.AdjustmentRule r2 = TimeZoneInfo.AdjustmentRule.CreateAdjustmentRule (new DateTime (2006,1,1), new DateTime (2007,1,1), new TimeSpan (1,0,0), s2, e2);
  113. TimeZoneInfo.CreateCustomTimeZone ("mytimezone", new TimeSpan (6,0,0),null,null,null,new TimeZoneInfo.AdjustmentRule[] {r2, r1});
  114. }
  115. [Test]
  116. [ExpectedException (typeof (InvalidTimeZoneException))]
  117. public void OffsetOutOfRange ()
  118. {
  119. TimeZoneInfo.TransitionTime startTransition = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,4,0,0), 3, 2, DayOfWeek.Sunday);
  120. TimeZoneInfo.TransitionTime endTransition = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,4,0,0), 10, 2, DayOfWeek.Sunday);
  121. TimeZoneInfo.AdjustmentRule rule = TimeZoneInfo.AdjustmentRule.CreateAdjustmentRule (new DateTime (2000,1,1), new DateTime (2005,1,1), new TimeSpan (3,0,0), startTransition, endTransition);
  122. TimeZoneInfo.CreateCustomTimeZone ("mytimezone", new TimeSpan (12,0,0),null,null,null,new TimeZoneInfo.AdjustmentRule[] {rule});
  123. }
  124. [Test]
  125. [ExpectedException (typeof (InvalidTimeZoneException))]
  126. public void NullRule ()
  127. {
  128. TimeZoneInfo.CreateCustomTimeZone ("mytimezone", new TimeSpan (12,0,0),null,null,null,new TimeZoneInfo.AdjustmentRule[] {null});
  129. }
  130. [Test]
  131. [ExpectedException (typeof (InvalidTimeZoneException))]
  132. public void MultiplesRulesForDate ()
  133. {
  134. TimeZoneInfo.TransitionTime s1 = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,4,0,0), 3, 2, DayOfWeek.Sunday);
  135. TimeZoneInfo.TransitionTime e1 = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,4,0,0), 10, 2, DayOfWeek.Sunday);
  136. TimeZoneInfo.AdjustmentRule r1 = TimeZoneInfo.AdjustmentRule.CreateAdjustmentRule (new DateTime (2000,1,1), new DateTime (2005,1,1), new TimeSpan (1,0,0), s1, e1);
  137. TimeZoneInfo.TransitionTime s2 = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,4,0,0), 2, 2, DayOfWeek.Sunday);
  138. TimeZoneInfo.TransitionTime e2 = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,4,0,0), 11, 2, DayOfWeek.Sunday);
  139. TimeZoneInfo.AdjustmentRule r2 = TimeZoneInfo.AdjustmentRule.CreateAdjustmentRule (new DateTime (2005,1,1), new DateTime (2007,1,1), new TimeSpan (1,0,0), s2, e2);
  140. TimeZoneInfo.CreateCustomTimeZone ("mytimezone", new TimeSpan (6,0,0),null,null,null,new TimeZoneInfo.AdjustmentRule[] {r1, r2});
  141. }
  142. [Test]
  143. public void SupportsDaylightSavingTime_NonEmptyAdjustmentRule ()
  144. {
  145. TimeZoneInfo.TransitionTime s1 = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,4,0,0), 3, 2, DayOfWeek.Sunday);
  146. TimeZoneInfo.TransitionTime e1 = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,4,0,0), 10, 2, DayOfWeek.Sunday);
  147. TimeZoneInfo.AdjustmentRule r1 = TimeZoneInfo.AdjustmentRule.CreateAdjustmentRule (new DateTime (2000,1,1), new DateTime (2005,1,1), new TimeSpan (1,0,0), s1, e1);
  148. TimeZoneInfo tz = TimeZoneInfo.CreateCustomTimeZone ("mytimezone", new TimeSpan (6,0,0),null,null,null,new TimeZoneInfo.AdjustmentRule[] {r1});
  149. Assert.IsTrue (tz.SupportsDaylightSavingTime);
  150. }
  151. [Test]
  152. public void SupportsDaylightSavingTime_EmptyAdjustmentRule ()
  153. {
  154. TimeZoneInfo tz = TimeZoneInfo.CreateCustomTimeZone ("mytimezone", new TimeSpan (6,0,0),null,null,null,null);
  155. Assert.IsFalse (tz.SupportsDaylightSavingTime);
  156. }
  157. [Test]
  158. public void SupportsDaylightSavingTime_NonEmptyAdjustmentRule_DisableDaylightSavingTime ()
  159. {
  160. TimeZoneInfo.TransitionTime s1 = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,4,0,0), 3, 2, DayOfWeek.Sunday);
  161. TimeZoneInfo.TransitionTime e1 = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,4,0,0), 10, 2, DayOfWeek.Sunday);
  162. TimeZoneInfo.AdjustmentRule r1 = TimeZoneInfo.AdjustmentRule.CreateAdjustmentRule (new DateTime (2000,1,1), new DateTime (2005,1,1), new TimeSpan (1,0,0), s1, e1);
  163. TimeZoneInfo tz = TimeZoneInfo.CreateCustomTimeZone ("mytimezone", new TimeSpan (6,0,0),null,null,null,new TimeZoneInfo.AdjustmentRule[] {r1}, true);
  164. Assert.IsFalse (tz.SupportsDaylightSavingTime);
  165. }
  166. [Test]
  167. public void SupportsDaylightSavingTime_EmptyAdjustmentRule_DisableDaylightSavingTime ()
  168. {
  169. TimeZoneInfo tz = TimeZoneInfo.CreateCustomTimeZone ("mytimezone", new TimeSpan (6,0,0),null,null,null,null,true);
  170. Assert.IsFalse (tz.SupportsDaylightSavingTime);
  171. }
  172. }
  173. [TestFixture]
  174. public class IsDaylightSavingTimeTests
  175. {
  176. TimeZoneInfo london;
  177. [SetUp]
  178. public void CreateTimeZones ()
  179. {
  180. TimeZoneInfo.TransitionTime start = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,1,0,0), 3, 5, DayOfWeek.Sunday);
  181. TimeZoneInfo.TransitionTime end = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,2,0,0), 10, 5, DayOfWeek.Sunday);
  182. TimeZoneInfo.AdjustmentRule rule = TimeZoneInfo.AdjustmentRule.CreateAdjustmentRule (DateTime.MinValue.Date, DateTime.MaxValue.Date, new TimeSpan (1,0,0), start, end);
  183. london = TimeZoneInfo.CreateCustomTimeZone ("Europe/London", new TimeSpan (0), "Europe/London", "British Standard Time", "British Summer Time", new TimeZoneInfo.AdjustmentRule [] {rule});
  184. }
  185. [Test]
  186. public void NoDSTInUTC ()
  187. {
  188. DateTime june01 = new DateTime (2007, 06, 01);
  189. Assert.IsFalse (TimeZoneInfo.Utc.IsDaylightSavingTime (june01));
  190. }
  191. [Test]
  192. public void DSTInLondon ()
  193. {
  194. if (Environment.OSVersion.Platform != PlatformID.Unix)
  195. Assert.Ignore ("Not running on Unix.");
  196. DateTime june01 = new DateTime (2007, 06, 01);
  197. DateTime xmas = new DateTime (2007, 12, 25);
  198. Assert.IsTrue (london.IsDaylightSavingTime (june01), "June 01 is DST in London");
  199. Assert.IsFalse (london.IsDaylightSavingTime (xmas), "Xmas is not DST in London");
  200. }
  201. [Test]
  202. public void DSTTransisions ()
  203. {
  204. if (Environment.OSVersion.Platform != PlatformID.Unix)
  205. Assert.Ignore ("Not running on Unix.");
  206. DateTime beforeDST = new DateTime (2007, 03, 25, 0, 59, 59, DateTimeKind.Unspecified);
  207. DateTime startDST = new DateTime (2007, 03, 25, 2, 0, 0, DateTimeKind.Unspecified);
  208. DateTime endDST = new DateTime (2007, 10, 28, 1, 59, 59, DateTimeKind.Unspecified);
  209. DateTime afterDST = new DateTime (2007, 10, 28, 2, 0, 0, DateTimeKind.Unspecified);
  210. Assert.IsFalse (london.IsDaylightSavingTime (beforeDST), "Just before DST");
  211. Assert.IsTrue (london.IsDaylightSavingTime (startDST), "the first seconds of DST");
  212. Assert.IsFalse (london.IsDaylightSavingTime (endDST), "The last seconds of DST");
  213. Assert.IsFalse (london.IsDaylightSavingTime (afterDST), "Just after DST");
  214. }
  215. [Test]
  216. public void DSTTransisionsUTC ()
  217. {
  218. DateTime beforeDST = new DateTime (2007, 03, 25, 0, 59, 59, DateTimeKind.Utc);
  219. DateTime startDST = new DateTime (2007, 03, 25, 1, 0, 0, DateTimeKind.Utc);
  220. DateTime endDST = new DateTime (2007, 10, 28, 0, 59, 59, DateTimeKind.Utc);
  221. DateTime afterDST = new DateTime (2007, 10, 28, 1, 0, 0, DateTimeKind.Utc);
  222. Assert.IsFalse (london.IsDaylightSavingTime (beforeDST), "Just before DST");
  223. Assert.IsTrue (london.IsDaylightSavingTime (startDST), "the first seconds of DST");
  224. Assert.IsTrue (london.IsDaylightSavingTime (endDST), "The last seconds of DST");
  225. Assert.IsFalse (london.IsDaylightSavingTime (afterDST), "Just after DST");
  226. }
  227. #if SLOW_TESTS
  228. [Test]
  229. public void MatchTimeZoneBehavior ()
  230. {
  231. TimeZone tzone = TimeZone.CurrentTimeZone;
  232. TimeZoneInfo local = TimeZoneInfo.Local;
  233. 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)) {
  234. date = DateTime.SpecifyKind (date, DateTimeKind.Local);
  235. if (local.IsInvalidTime (date))
  236. continue;
  237. Assert.IsTrue (tzone.IsDaylightSavingTime (date) == local.IsDaylightSavingTime (date));
  238. }
  239. }
  240. #endif
  241. [Test (Description="Description xambug #17155")]
  242. public void AdjustmentRuleAfterNewYears ()
  243. {
  244. TimeZoneInfo tz;
  245. if (Environment.OSVersion.Platform == PlatformID.Unix)
  246. tz = TimeZoneInfo.FindSystemTimeZoneById ("Pacific/Auckland"); // *nix
  247. else
  248. tz = TimeZoneInfo.FindSystemTimeZoneById ("New Zealand Standard Time"); // Windows
  249. // DST start: 9/29/2013 2:00:00 AM
  250. // DST end: 4/6/2014 3:00:00 AM
  251. DateTime dt = new DateTime (2014, 1, 9, 23, 0, 0, DateTimeKind.Utc);
  252. Assert.IsTrue (tz.IsDaylightSavingTime (dt), "#1.1");
  253. // DST start: 9/29/2014 2:00:00 AM
  254. // DST end: 4/6/2015 3:00:00 AM
  255. dt = new DateTime (2014, 6, 9, 23, 0, 0, DateTimeKind.Utc);
  256. Assert.IsFalse (tz.IsDaylightSavingTime (dt), "#2.1");
  257. // DST start: 9/29/2014 2:00:00 AM
  258. // DST end: 4/6/2015 3:00:00 AM
  259. dt = new DateTime (2014, 10, 9, 23, 0, 0, DateTimeKind.Utc);
  260. Assert.IsTrue (tz.IsDaylightSavingTime (dt), "#3.1");
  261. }
  262. [Test] //Covers #26008
  263. public void DSTWithFloatingDateRule ()
  264. {
  265. // Construct a custom time zone where daylight saving time starts on the
  266. // 2nd Sunday in March.
  267. var transitionToDaylight = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1, 1, 1, 2, 0, 0), 3, 2, DayOfWeek.Sunday);
  268. var transitionToStandard = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1, 1, 1, 2, 0, 0), 11, 1, DayOfWeek.Sunday);
  269. var adjustment = TimeZoneInfo.AdjustmentRule.CreateAdjustmentRule (DateTime.MinValue.Date, DateTime.MaxValue.Date, new TimeSpan (1, 0, 0), transitionToDaylight, transitionToStandard);
  270. var timeZone = TimeZoneInfo.CreateCustomTimeZone ("BugCheck", new TimeSpan (-8, 0, 0), "Testing", "Testing Standard", "Testing Daylight", new TimeZoneInfo.AdjustmentRule [] { adjustment });
  271. // See if March 7, 2014 is listed as being during daylight saving time.
  272. // If it is DST, then the runtime has the bug that we are looking for.
  273. Assert.IsFalse (timeZone.IsDaylightSavingTime (new DateTime (2014, 3, 7, 12, 0, 0, DateTimeKind.Unspecified)));
  274. }
  275. }
  276. [TestFixture]
  277. public class ConvertTimeTests
  278. {
  279. TimeZoneInfo london;
  280. [SetUp]
  281. public void CreateTimeZones ()
  282. {
  283. TimeZoneInfo.TransitionTime start = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,1,0,0), 3, 5, DayOfWeek.Sunday);
  284. TimeZoneInfo.TransitionTime end = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,2,0,0), 10, 5, DayOfWeek.Sunday);
  285. TimeZoneInfo.AdjustmentRule rule = TimeZoneInfo.AdjustmentRule.CreateAdjustmentRule (DateTime.MinValue.Date, DateTime.MaxValue.Date, new TimeSpan (1,0,0), start, end);
  286. london = TimeZoneInfo.CreateCustomTimeZone ("Europe/London", new TimeSpan (0), "Europe/London", "British Standard Time", "British Summer Time", new TimeZoneInfo.AdjustmentRule [] {rule});
  287. }
  288. [Test]
  289. [ExpectedException (typeof (ArgumentException))]
  290. public void ConvertFromUtc_KindIsLocalException ()
  291. {
  292. if (Environment.OSVersion.Platform != PlatformID.Unix)
  293. throw new ArgumentException ();
  294. TimeZoneInfo.ConvertTimeFromUtc (new DateTime (2007, 5, 3, 11, 8, 0, DateTimeKind.Local), TimeZoneInfo.Local);
  295. }
  296. [Test]
  297. [ExpectedException (typeof (ArgumentNullException))]
  298. public void ConvertFromUtc_DestinationTimeZoneIsNullException ()
  299. {
  300. TimeZoneInfo.ConvertTimeFromUtc (new DateTime (2007, 5, 3, 11, 8, 0), null);
  301. }
  302. [Test]
  303. public void ConvertFromUtc_DestinationIsUTC ()
  304. {
  305. DateTime now = DateTime.UtcNow;
  306. DateTime converted = TimeZoneInfo.ConvertTimeFromUtc (now, TimeZoneInfo.Utc);
  307. Assert.AreEqual (now, converted);
  308. }
  309. [Test]
  310. public void ConvertFromUTC_ConvertInWinter ()
  311. {
  312. if (Environment.OSVersion.Platform != PlatformID.Unix)
  313. Assert.Ignore ("Not running on Unix.");
  314. DateTime utc = new DateTime (2007, 12, 25, 12, 0, 0);
  315. DateTime converted = TimeZoneInfo.ConvertTimeFromUtc (utc, london);
  316. Assert.AreEqual (utc, converted);
  317. }
  318. [Test]
  319. public void ConvertFromUtc_ConvertInSummer ()
  320. {
  321. if (Environment.OSVersion.Platform != PlatformID.Unix)
  322. Assert.Ignore ("Not running on Unix.");
  323. DateTime utc = new DateTime (2007, 06, 01, 12, 0, 0);
  324. DateTime converted = TimeZoneInfo.ConvertTimeFromUtc (utc, london);
  325. Assert.AreEqual (utc + new TimeSpan (1,0,0), converted);
  326. }
  327. [Test]
  328. public void ConvertToUTC_KindIsUtc ()
  329. {
  330. DateTime now = DateTime.UtcNow;
  331. Assert.AreEqual (now.Kind, DateTimeKind.Utc);
  332. DateTime converted = TimeZoneInfo.ConvertTimeToUtc (now);
  333. Assert.AreEqual (now, converted);
  334. }
  335. [Test]
  336. [ExpectedException (typeof (ArgumentException))]
  337. public void ConvertToUTC_KindIsUTCButSourceIsNot ()
  338. {
  339. TimeZoneInfo.ConvertTimeToUtc (new DateTime (2007, 5, 3, 12, 8, 0, DateTimeKind.Utc), london);
  340. }
  341. [Test]
  342. [ExpectedException (typeof (ArgumentException))]
  343. public void ConvertToUTC_KindIsLocalButSourceIsNot ()
  344. {
  345. if (Environment.OSVersion.Platform != PlatformID.Unix)
  346. throw new ArgumentException ();
  347. TimeZoneInfo.ConvertTimeToUtc (new DateTime (2007, 5, 3, 12, 8, 0, DateTimeKind.Local), london);
  348. }
  349. [Test]
  350. [ExpectedException (typeof (ArgumentException))]
  351. public void ConvertToUTC_InvalidDate ()
  352. {
  353. TimeZoneInfo.ConvertTimeToUtc (new DateTime (2007, 3, 25, 1, 30, 0), london);
  354. }
  355. [Test]
  356. [ExpectedException (typeof (ArgumentNullException))]
  357. public void ConvertToUTC_SourceIsNull ()
  358. {
  359. TimeZoneInfo.ConvertTimeToUtc (new DateTime (2007, 5, 3, 12, 16, 0), null);
  360. }
  361. #if SLOW_TESTS
  362. [Test]
  363. public void ConvertToUtc_MatchDateTimeBehavior ()
  364. {
  365. for (DateTime date = new DateTime (2007, 01, 01, 0, 0, 0); date < new DateTime (2007, 12, 31, 23, 59, 59); date += new TimeSpan (0,1,0)) {
  366. Assert.AreEqual (TimeZoneInfo.ConvertTimeToUtc (date), date.ToUniversalTime ());
  367. }
  368. }
  369. #endif
  370. [Test]
  371. public void ConvertFromToUtc ()
  372. {
  373. if (Environment.OSVersion.Platform != PlatformID.Unix)
  374. Assert.Ignore ("Not running on Unix.");
  375. DateTime utc = DateTime.UtcNow;
  376. Assert.AreEqual (utc.Kind, DateTimeKind.Utc);
  377. DateTime converted = TimeZoneInfo.ConvertTimeFromUtc (utc, london);
  378. Assert.AreEqual (converted.Kind, DateTimeKind.Unspecified);
  379. DateTime back = TimeZoneInfo.ConvertTimeToUtc (converted, london);
  380. Assert.AreEqual (back.Kind, DateTimeKind.Utc);
  381. Assert.AreEqual (utc, back);
  382. }
  383. [Test]
  384. public void ConvertFromToLocal ()
  385. {
  386. DateTime utc = DateTime.UtcNow;
  387. Assert.AreEqual(utc.Kind, DateTimeKind.Utc);
  388. DateTime converted = TimeZoneInfo.ConvertTimeFromUtc(utc, TimeZoneInfo.Local);
  389. #if NET_4_0
  390. Assert.AreEqual(DateTimeKind.Local, converted.Kind);
  391. #else
  392. Assert.AreEqual(DateTimeKind.Unspecified, converted.Kind);
  393. #endif
  394. DateTime back = TimeZoneInfo.ConvertTimeToUtc(converted, TimeZoneInfo.Local);
  395. Assert.AreEqual(back.Kind, DateTimeKind.Utc);
  396. Assert.AreEqual(utc, back);
  397. }
  398. [Test]
  399. public void ConvertToTimeZone ()
  400. {
  401. if (Environment.OSVersion.Platform != PlatformID.Unix)
  402. Assert.Ignore ("Not running on Unix.");
  403. TimeZoneInfo.ConvertTime (DateTime.Now, TimeZoneInfo.FindSystemTimeZoneById("Pacific/Auckland"));
  404. }
  405. [Test]
  406. [ExpectedException (typeof (ArgumentNullException))]
  407. public void ConvertTime_DateTime_TimeZoneInfo_DestinationTimeZoneIsNull ()
  408. {
  409. TimeZoneInfo.ConvertTime (DateTime.Now, null);
  410. }
  411. [Test]
  412. public void ConvertTime_DateTime_TimeZoneInfo_DateTimeKindMatch ()
  413. {
  414. var sdt = new DateTime (2014, 1, 9, 23, 0, 0, DateTimeKind.Utc);
  415. var ddt = TimeZoneInfo.ConvertTime (sdt, TimeZoneInfo.Utc);
  416. Assert.AreEqual (ddt.Kind, sdt.Kind, "#1.1");
  417. Assert.AreEqual (ddt.Kind, DateTimeKind.Utc, "#1.2");
  418. sdt = new DateTime (2014, 1, 9, 23, 0, 0, DateTimeKind.Local);
  419. ddt = TimeZoneInfo.ConvertTime (sdt, TimeZoneInfo.Local);
  420. Assert.AreEqual (ddt.Kind, sdt.Kind, "#2.1");
  421. Assert.AreEqual (ddt.Kind, DateTimeKind.Local, "#2.2");
  422. sdt = new DateTime (2014, 1, 9, 23, 0, 0);
  423. ddt = TimeZoneInfo.ConvertTime (sdt, TimeZoneInfo.Local);
  424. Assert.AreEqual (ddt.Kind, sdt.Kind, "#3.1");
  425. Assert.AreEqual (ddt.Kind, DateTimeKind.Unspecified, "#3.2");
  426. }
  427. [Test]
  428. [ExpectedException (typeof (ArgumentNullException))]
  429. public void ConverTime_DateTime_TimeZoneInfo_TimeZoneInfo_SourceTimeZoneIsNull ()
  430. {
  431. TimeZoneInfo.ConvertTime (DateTime.Now, null, TimeZoneInfo.Local);
  432. }
  433. [Test]
  434. [ExpectedException (typeof (ArgumentNullException))]
  435. public void ConverTime_DateTime_TimeZoneInfo_TimeZoneInfo_DestinationTimeZoneIsNull ()
  436. {
  437. TimeZoneInfo.ConvertTime (DateTime.Now, TimeZoneInfo.Utc, null);
  438. }
  439. [Test (Description="Fix for xambug https://bugzilla.xamarin.com/show_bug.cgi?id=17155")]
  440. public void ConvertTime_AdjustmentRuleAfterNewYears ()
  441. {
  442. TimeZoneInfo tz;
  443. if (Environment.OSVersion.Platform == PlatformID.Unix)
  444. tz = TimeZoneInfo.FindSystemTimeZoneById ("Pacific/Auckland"); // *nix
  445. else
  446. tz = TimeZoneInfo.FindSystemTimeZoneById ("New Zealand Standard Time"); // Windows
  447. // DST start: 9/29/2013 2:00:00 AM
  448. // DST end: 4/6/2014 3:00:00 AM
  449. DateTime sdt = new DateTime (2014, 1, 9, 23, 0, 0, DateTimeKind.Utc);
  450. DateTime ddt = TimeZoneInfo.ConvertTime (sdt, tz);
  451. Assert.AreEqual (10, ddt.Day, "#1.1");
  452. Assert.AreEqual (1, ddt.Month, "#1.2");
  453. Assert.AreEqual (2014, ddt.Year, "#1.3");
  454. Assert.AreEqual (12, ddt.Hour, "#1.4");
  455. Assert.AreEqual (0, ddt.Minute, "#1.5");
  456. Assert.AreEqual (0, ddt.Second, "#1.6");
  457. // DST start: 9/29/2014 2:00:00 AM
  458. // DST end: 4/6/2015 3:00:00 AM
  459. sdt = new DateTime (2014, 6, 9, 23, 0, 0, DateTimeKind.Utc);
  460. ddt = TimeZoneInfo.ConvertTime (sdt, tz);
  461. Assert.AreEqual (10, ddt.Day, "#2.1");
  462. Assert.AreEqual (6, ddt.Month, "#2.2");
  463. Assert.AreEqual (2014, ddt.Year, "#2.3");
  464. Assert.AreEqual (11, ddt.Hour, "#2.4");
  465. Assert.AreEqual (0, ddt.Minute, "#2.5");
  466. Assert.AreEqual (0, ddt.Second, "#2.6");
  467. // DST start: 9/29/2014 2:00:00 AM
  468. // DST end: 4/6/2015 3:00:00 AM
  469. sdt = new DateTime (2014, 10, 9, 23, 0, 0, DateTimeKind.Utc);
  470. ddt = TimeZoneInfo.ConvertTime (sdt, tz);
  471. Assert.AreEqual (10, ddt.Day, "#3.1");
  472. Assert.AreEqual (10, ddt.Month, "#3.2");
  473. Assert.AreEqual (2014, ddt.Year, "#3.3");
  474. Assert.AreEqual (12, ddt.Hour, "#3.4");
  475. Assert.AreEqual (0, ddt.Minute, "#3.5");
  476. Assert.AreEqual (0, ddt.Second, "#3.6");
  477. }
  478. [Test (Description="Fix the bug https://bugzilla.xamarin.com/show_bug.cgi?id=1849")]
  479. public void ConvertTime_AjustmentConvertTimeWithSourceTimeZone () {
  480. TimeZoneInfo easternTimeZone;
  481. TimeZoneInfo pacificTimeZone;
  482. if (Environment.OSVersion.Platform == PlatformID.Unix) {
  483. // *nix
  484. easternTimeZone = TimeZoneInfo.FindSystemTimeZoneById ("US/Eastern");
  485. pacificTimeZone = TimeZoneInfo.FindSystemTimeZoneById ("US/Pacific");
  486. }
  487. else {
  488. // Windows
  489. easternTimeZone = TimeZoneInfo.FindSystemTimeZoneById ("Eastern Standard Time");
  490. pacificTimeZone = TimeZoneInfo.FindSystemTimeZoneById ("Pacific Standard Time");
  491. }
  492. DateTime lastMidnight = new DateTime (new DateTime (2012, 06, 13).Ticks, DateTimeKind.Unspecified);
  493. DateTime lastMidnightAsEST = TimeZoneInfo.ConvertTime (lastMidnight, pacificTimeZone, easternTimeZone);
  494. DateTime lastMidnightAsPST = TimeZoneInfo.ConvertTime (lastMidnightAsEST, easternTimeZone, pacificTimeZone);
  495. // Last midnight in PST as EST should be 3AM
  496. DateTime expectedDate = new DateTime (2012, 06, 13, 3, 0, 0);
  497. Assert.AreEqual (expectedDate, lastMidnightAsEST);
  498. Assert.AreEqual (lastMidnight, lastMidnightAsPST);
  499. }
  500. }
  501. [TestFixture]
  502. public class IsInvalidTimeTests
  503. {
  504. TimeZoneInfo london;
  505. [SetUp]
  506. public void CreateTimeZones ()
  507. {
  508. TimeZoneInfo.TransitionTime start = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,1,0,0), 3, 5, DayOfWeek.Sunday);
  509. TimeZoneInfo.TransitionTime end = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,2,0,0), 10, 5, DayOfWeek.Sunday);
  510. TimeZoneInfo.AdjustmentRule rule = TimeZoneInfo.AdjustmentRule.CreateAdjustmentRule (DateTime.MinValue.Date, DateTime.MaxValue.Date, new TimeSpan (1,0,0), start, end);
  511. london = TimeZoneInfo.CreateCustomTimeZone ("Europe/London", new TimeSpan (0), "Europe/London", "British Standard Time", "British Summer Time", new TimeZoneInfo.AdjustmentRule [] {rule});
  512. }
  513. #if SLOW_TESTS
  514. [Test]
  515. public void UTCDate ()
  516. {
  517. 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)) {
  518. date = DateTime.SpecifyKind (date, DateTimeKind.Utc);
  519. Assert.IsFalse (london.IsInvalidTime (date));
  520. }
  521. }
  522. #endif
  523. [Test]
  524. public void InvalidDates ()
  525. {
  526. Assert.IsFalse (london.IsInvalidTime (new DateTime (2007, 03, 25, 0, 59, 59)));
  527. Assert.IsTrue (london.IsInvalidTime (new DateTime (2007, 03, 25, 1, 0, 0)));
  528. Assert.IsTrue (london.IsInvalidTime (new DateTime (2007, 03, 25, 1, 59, 59)));
  529. Assert.IsFalse (london.IsInvalidTime (new DateTime (2007, 03, 25, 2, 0, 0)));
  530. }
  531. }
  532. [TestFixture]
  533. public class IsAmbiguousTimeTests
  534. {
  535. TimeZoneInfo london;
  536. [SetUp]
  537. public void CreateTimeZones ()
  538. {
  539. TimeZoneInfo.TransitionTime start = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,1,0,0), 3, 5, DayOfWeek.Sunday);
  540. TimeZoneInfo.TransitionTime end = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,2,0,0), 10, 5, DayOfWeek.Sunday);
  541. TimeZoneInfo.AdjustmentRule rule = TimeZoneInfo.AdjustmentRule.CreateAdjustmentRule (DateTime.MinValue.Date, DateTime.MaxValue.Date, new TimeSpan (1,0,0), start, end);
  542. london = TimeZoneInfo.CreateCustomTimeZone ("Europe/London", new TimeSpan (0), "Europe/London", "British Standard Time", "British Summer Time", new TimeZoneInfo.AdjustmentRule [] {rule});
  543. }
  544. [Test]
  545. public void AmbiguousDates ()
  546. {
  547. if (Environment.OSVersion.Platform != PlatformID.Unix)
  548. Assert.Ignore ("Not running on Unix.");
  549. Assert.IsFalse (london.IsAmbiguousTime (new DateTime (2007, 10, 28, 1, 0, 0)));
  550. Assert.IsTrue (london.IsAmbiguousTime (new DateTime (2007, 10, 28, 1, 0, 1)));
  551. Assert.IsTrue (london.IsAmbiguousTime (new DateTime (2007, 10, 28, 2, 0, 0)));
  552. Assert.IsFalse (london.IsAmbiguousTime (new DateTime (2007, 10, 28, 2, 0, 1)));
  553. }
  554. [Test]
  555. public void AmbiguousUTCDates ()
  556. {
  557. if (Environment.OSVersion.Platform != PlatformID.Unix)
  558. Assert.Ignore ("Not running on Unix.");
  559. Assert.IsFalse (london.IsAmbiguousTime (new DateTime (2007, 10, 28, 0, 0, 0, DateTimeKind.Utc)));
  560. Assert.IsTrue (london.IsAmbiguousTime (new DateTime (2007, 10, 28, 0, 0, 1, DateTimeKind.Utc)));
  561. Assert.IsTrue (london.IsAmbiguousTime (new DateTime (2007, 10, 28, 0, 59, 59, DateTimeKind.Utc)));
  562. Assert.IsFalse (london.IsAmbiguousTime (new DateTime (2007, 10, 28, 1, 0, 0, DateTimeKind.Utc)));
  563. }
  564. #if SLOW_TESTS
  565. [Test]
  566. public void AmbiguousInUTC ()
  567. {
  568. 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)) {
  569. Assert.IsFalse (TimeZoneInfo.Utc.IsAmbiguousTime (date));
  570. }
  571. }
  572. #endif
  573. }
  574. [TestFixture]
  575. public class GetSystemTimeZonesTests
  576. {
  577. [Test]
  578. public void NotEmpty ()
  579. {
  580. if (Environment.OSVersion.Platform != PlatformID.Unix)
  581. Assert.Ignore ("Not running on Unix.");
  582. global::System.Collections.ObjectModel.ReadOnlyCollection<TimeZoneInfo> systemTZ = TimeZoneInfo.GetSystemTimeZones ();
  583. Assert.IsNotNull(systemTZ, "SystemTZ is null");
  584. Assert.IsFalse (systemTZ.Count == 0, "SystemTZ is empty");
  585. }
  586. [Test]
  587. public void ContainsBrussels ()
  588. {
  589. if (Environment.OSVersion.Platform != PlatformID.Unix)
  590. Assert.Ignore ("Not running on Unix.");
  591. global::System.Collections.ObjectModel.ReadOnlyCollection<TimeZoneInfo> systemTZ = TimeZoneInfo.GetSystemTimeZones ();
  592. foreach (TimeZoneInfo tz in systemTZ) {
  593. if (tz.Id == "Europe/Brussels")
  594. return;
  595. }
  596. Assert.Fail ("Europe/Brussels not found in SystemTZ");
  597. }
  598. }
  599. [TestFixture]
  600. public class FindSystemTimeZoneByIdTests
  601. {
  602. [Test]
  603. [ExpectedException (typeof (ArgumentNullException))]
  604. public void NullId ()
  605. {
  606. TimeZoneInfo.FindSystemTimeZoneById (null);
  607. }
  608. [Test]
  609. [ExpectedException (typeof (TimeZoneNotFoundException))]
  610. public void NonSystemTimezone ()
  611. {
  612. if (Environment.OSVersion.Platform != PlatformID.Unix)
  613. throw new TimeZoneNotFoundException ();
  614. TimeZoneInfo.FindSystemTimeZoneById ("Neverland/The_Lagoon");
  615. }
  616. [Test]
  617. public void FindBrusselsTZ ()
  618. {
  619. if (Environment.OSVersion.Platform != PlatformID.Unix)
  620. Assert.Ignore ("Not running on Unix.");
  621. TimeZoneInfo brussels = TimeZoneInfo.FindSystemTimeZoneById ("Europe/Brussels");
  622. Assert.IsNotNull (brussels);
  623. }
  624. [Test]
  625. public void OffsetIsCorrectInKinshasa ()
  626. {
  627. if (Environment.OSVersion.Platform != PlatformID.Unix)
  628. Assert.Ignore ("Not running on Unix.");
  629. TimeZoneInfo kin = TimeZoneInfo.FindSystemTimeZoneById ("Africa/Kinshasa");
  630. Assert.AreEqual (new TimeSpan (1,0,0), kin.BaseUtcOffset, "BaseUtcOffset in Kinshasa is not +1h");
  631. }
  632. [Test]
  633. public void OffsetIsCorrectInBrussels ()
  634. {
  635. if (Environment.OSVersion.Platform != PlatformID.Unix)
  636. Assert.Ignore ("Not running on Unix.");
  637. TimeZoneInfo brussels = TimeZoneInfo.FindSystemTimeZoneById ("Europe/Brussels");
  638. Assert.AreEqual (new TimeSpan (1,0,0), brussels.BaseUtcOffset, "BaseUtcOffset for Brussels is not +1h");
  639. }
  640. [Test]
  641. public void NoDSTInKinshasa ()
  642. {
  643. if (Environment.OSVersion.Platform != PlatformID.Unix)
  644. Assert.Ignore ("Not running on Unix.");
  645. TimeZoneInfo kin = TimeZoneInfo.FindSystemTimeZoneById ("Africa/Kinshasa");
  646. Assert.IsFalse (kin.SupportsDaylightSavingTime);
  647. }
  648. [Test]
  649. public void BrusselsSupportsDST ()
  650. {
  651. if (Environment.OSVersion.Platform != PlatformID.Unix)
  652. Assert.Ignore ("Not running on Unix.");
  653. TimeZoneInfo brussels = TimeZoneInfo.FindSystemTimeZoneById ("Europe/Brussels");
  654. Assert.IsTrue (brussels.SupportsDaylightSavingTime);
  655. }
  656. [Test]
  657. public void MelbourneSupportsDST ()
  658. {
  659. if (Environment.OSVersion.Platform != PlatformID.Unix)
  660. Assert.Ignore ("Not running on Unix.");
  661. TimeZoneInfo melbourne = TimeZoneInfo.FindSystemTimeZoneById ("Australia/Melbourne");
  662. Assert.IsTrue (melbourne.SupportsDaylightSavingTime);
  663. }
  664. [Test]
  665. public void RomeAndVaticanSharesTime ()
  666. {
  667. if (Environment.OSVersion.Platform != PlatformID.Unix)
  668. Assert.Ignore ("Not running on Unix.");
  669. TimeZoneInfo rome = TimeZoneInfo.FindSystemTimeZoneById ("Europe/Rome");
  670. TimeZoneInfo vatican = TimeZoneInfo.FindSystemTimeZoneById ("Europe/Vatican");
  671. Assert.IsTrue (rome.HasSameRules (vatican));
  672. }
  673. [Test]
  674. public void FindSystemTimeZoneById_Local_Roundtrip ()
  675. {
  676. Assert.AreEqual (TimeZoneInfo.Local.Id, TimeZoneInfo.FindSystemTimeZoneById (TimeZoneInfo.Local.Id).Id);
  677. }
  678. [Test]
  679. public void Test326 ()
  680. {
  681. DateTime utc = DateTime.UtcNow;
  682. DateTime local = TimeZoneInfo.ConvertTime (utc, TimeZoneInfo.Utc, TimeZoneInfo.FindSystemTimeZoneById (TimeZoneInfo.Local.Id));
  683. Assert.AreEqual (local, utc + TimeZoneInfo.Local.GetUtcOffset (utc), "ConvertTime/Local");
  684. }
  685. #if SLOW_TESTS
  686. [Test]
  687. public void BrusselsAdjustments ()
  688. {
  689. TimeZoneInfo.TransitionTime start = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,2,0,0), 3, 5, DayOfWeek.Sunday);
  690. TimeZoneInfo.TransitionTime end = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,3,0,0), 10, 5, DayOfWeek.Sunday);
  691. TimeZoneInfo.AdjustmentRule rule = TimeZoneInfo.AdjustmentRule.CreateAdjustmentRule (DateTime.MinValue.Date, DateTime.MaxValue.Date, new TimeSpan (1,0,0), start, end);
  692. TimeZoneInfo brussels = TimeZoneInfo.CreateCustomTimeZone ("Europe/Brussels", new TimeSpan (1, 0, 0), "Europe/Brussels", "", "", new TimeZoneInfo.AdjustmentRule [] {rule});
  693. TimeZoneInfo brussels_sys = TimeZoneInfo.FindSystemTimeZoneById ("Europe/Brussels");
  694. 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)) {
  695. Assert.AreEqual (brussels.GetUtcOffset (date), brussels_sys.GetUtcOffset (date));
  696. Assert.AreEqual (brussels.IsDaylightSavingTime (date), brussels_sys.IsDaylightSavingTime (date));
  697. }
  698. }
  699. #endif
  700. }
  701. [TestFixture]
  702. public class GetAmbiguousTimeOffsetsTests
  703. {
  704. [Test]
  705. [ExpectedException (typeof(ArgumentException))]
  706. public void DateIsNotAmbiguous ()
  707. {
  708. if (Environment.OSVersion.Platform != PlatformID.Unix)
  709. throw new ArgumentException ();
  710. TimeZoneInfo brussels = TimeZoneInfo.FindSystemTimeZoneById ("Europe/Brussels");
  711. DateTime date = new DateTime (2007, 05, 11, 11, 40, 00);
  712. brussels.GetAmbiguousTimeOffsets (date);
  713. }
  714. [Test]
  715. public void AmbiguousOffsets ()
  716. {
  717. if (Environment.OSVersion.Platform != PlatformID.Unix)
  718. Assert.Ignore ("Not running on Unix.");
  719. TimeZoneInfo brussels = TimeZoneInfo.FindSystemTimeZoneById ("Europe/Brussels");
  720. DateTime date = new DateTime (2007, 10, 28, 2, 30, 00);
  721. Assert.IsTrue (brussels.IsAmbiguousTime (date));
  722. Assert.AreEqual (2, brussels.GetAmbiguousTimeOffsets (date).Length);
  723. Assert.AreEqual (new TimeSpan[] {new TimeSpan (1, 0, 0), new TimeSpan (2, 0, 0)}, brussels.GetAmbiguousTimeOffsets (date));
  724. }
  725. }
  726. [TestFixture]
  727. public class HasSameRulesTests
  728. {
  729. [Test]
  730. public void NullAdjustments () //bnc #391011
  731. {
  732. TimeZoneInfo utc = TimeZoneInfo.Utc;
  733. TimeZoneInfo custom = TimeZoneInfo.CreateCustomTimeZone ("Custom", new TimeSpan (0), "Custom", "Custom");
  734. Assert.IsTrue (utc.HasSameRules (custom));
  735. }
  736. }
  737. [TestFixture]
  738. public class SerializationTests
  739. {
  740. [Test]
  741. public void Serialization_Deserialization ()
  742. {
  743. TimeZoneInfo.TransitionTime start = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,1,0,0), 3, 5, DayOfWeek.Sunday);
  744. TimeZoneInfo.TransitionTime end = TimeZoneInfo.TransitionTime.CreateFloatingDateRule (new DateTime (1,1,1,2,0,0), 10, 5, DayOfWeek.Sunday);
  745. TimeZoneInfo.AdjustmentRule rule = TimeZoneInfo.AdjustmentRule.CreateAdjustmentRule (DateTime.MinValue.Date, DateTime.MaxValue.Date, new TimeSpan (1,0,0), start, end);
  746. TimeZoneInfo london = TimeZoneInfo.CreateCustomTimeZone ("Europe/London", new TimeSpan (0), "Europe/London", "British Standard Time", "British Summer Time", new TimeZoneInfo.AdjustmentRule [] {rule});
  747. MemoryStream stream = new MemoryStream ();
  748. BinaryFormatter formatter = new BinaryFormatter ();
  749. formatter.Serialize (stream, london);
  750. stream.Position = 0;
  751. TimeZoneInfo deserialized = (TimeZoneInfo) formatter.Deserialize (stream);
  752. stream.Close ();
  753. stream.Dispose ();
  754. Assert.IsTrue (london.Equals (deserialized));
  755. }
  756. }
  757. [TestFixture]
  758. public class MultipleDaylightSavingTimeTests {
  759. private TimeZoneInfo cairo;
  760. private DateTime dst1Start;
  761. private DateTime dst1End;
  762. private DateTime dst2Start;
  763. private DateTime dst2End;
  764. private TimeSpan baseUtcOffset;
  765. private TimeSpan dstUtcOffset;
  766. private TimeSpan dstOffset;
  767. [SetUp]
  768. public void CreateTimeZones ()
  769. {
  770. /*
  771. From 1/1/2014 12:00:00 AM to 6/30/2014 12:00:00 AM
  772. Delta: 01:00:00
  773. Begins at 12:00 AM on 16 May
  774. Ends at 1:00 AM on 29 June
  775. From 7/1/2014 12:00:00 AM to 12/31/2014 12:00:00 AM
  776. Delta: 01:00:00
  777. Begins at 12:00 AM on 29 July
  778. Ends at 12:00 AM on 26 September
  779. */
  780. dst1Start = new DateTime (2014, 5, 16);
  781. dst1End = new DateTime (2014, 6, 29);
  782. dst2Start = new DateTime (2014, 7, 29);
  783. dst2End = new DateTime (2014, 9, 26);
  784. baseUtcOffset = new TimeSpan (2, 0, 0);
  785. dstUtcOffset = new TimeSpan (3, 0, 0);
  786. dstOffset = dstUtcOffset - baseUtcOffset;
  787. var rule1 = TimeZoneInfo.AdjustmentRule.CreateAdjustmentRule (
  788. new DateTime (2014, 1, 1), new DateTime (2014, 6, 30), dstOffset,
  789. CreateFixedDateRule (dst1Start), CreateFixedDateRule (dst1End));
  790. var rule2 = TimeZoneInfo.AdjustmentRule.CreateAdjustmentRule (
  791. new DateTime (2014, 7, 1), new DateTime (2014, 12, 31), dstOffset,
  792. CreateFixedDateRule (dst2Start), CreateFixedDateRule (dst2End));
  793. cairo = TimeZoneInfo.CreateCustomTimeZone ("Africa/Cairo", baseUtcOffset, "Africa/Cairo", "EET", "EEST",
  794. new [] {rule1, rule2});
  795. }
  796. private static TimeZoneInfo.TransitionTime CreateFixedDateRule (DateTime dateTime)
  797. {
  798. var time = new DateTime (dateTime.Ticks - dateTime.Date.Ticks);
  799. return TimeZoneInfo.TransitionTime.CreateFixedDateRule (time, dateTime.Month, dateTime.Day);
  800. }
  801. [Test]
  802. public void GetUtcOffset_FromUTC ()
  803. {
  804. var d = dst1Start.Add (-baseUtcOffset);
  805. d = DateTime.SpecifyKind (d, DateTimeKind.Utc);
  806. Assert.AreEqual(baseUtcOffset, cairo.GetUtcOffset (d.Add (new TimeSpan(0,0,0,-1))));
  807. Assert.AreEqual(dstUtcOffset, cairo.GetUtcOffset (d));
  808. Assert.AreEqual(dstUtcOffset, cairo.GetUtcOffset (d.Add (new TimeSpan(0,0,0, 1))));
  809. d = dst1End.Add (-baseUtcOffset-dstOffset);
  810. d = DateTime.SpecifyKind (d, DateTimeKind.Utc);
  811. Assert.AreEqual(dstUtcOffset, cairo.GetUtcOffset (d.Add (new TimeSpan(0,0,0,-1))));
  812. Assert.AreEqual(baseUtcOffset, cairo.GetUtcOffset (d));
  813. Assert.AreEqual(baseUtcOffset, cairo.GetUtcOffset (d.Add (new TimeSpan(0,0,0, 1))));
  814. d = dst2Start.Add (-baseUtcOffset);
  815. d = DateTime.SpecifyKind (d, DateTimeKind.Utc);
  816. Assert.AreEqual(baseUtcOffset, cairo.GetUtcOffset (d.Add (new TimeSpan(0,0,0,-1))));
  817. Assert.AreEqual(dstUtcOffset, cairo.GetUtcOffset (d));
  818. Assert.AreEqual(dstUtcOffset, cairo.GetUtcOffset (d.Add (new TimeSpan(0,0,0, 1))));
  819. d = dst2End.Add (-baseUtcOffset-dstOffset);
  820. d = DateTime.SpecifyKind (d, DateTimeKind.Utc);
  821. Assert.AreEqual(dstUtcOffset, cairo.GetUtcOffset (d.Add (new TimeSpan(0,0,0,-1))));
  822. Assert.AreEqual(baseUtcOffset, cairo.GetUtcOffset (d));
  823. Assert.AreEqual(baseUtcOffset, cairo.GetUtcOffset (d.Add (new TimeSpan(0,0,0, 1))));
  824. }
  825. [Test]
  826. public void GetUtcOffset_FromLocal ()
  827. {
  828. var d = dst1Start.Add (-baseUtcOffset);
  829. d = DateTime.SpecifyKind (d, DateTimeKind.Utc);
  830. d = d.ToLocalTime ();
  831. Assert.AreEqual(baseUtcOffset, cairo.GetUtcOffset (d.Add (new TimeSpan(0,0,0,-1))));
  832. Assert.AreEqual(dstUtcOffset, cairo.GetUtcOffset (d));
  833. Assert.AreEqual(dstUtcOffset, cairo.GetUtcOffset (d.Add (new TimeSpan(0,0,0, 1))));
  834. d = dst1End.Add (-baseUtcOffset-dstOffset);
  835. d = DateTime.SpecifyKind (d, DateTimeKind.Utc);
  836. d = d.ToLocalTime ();
  837. Assert.AreEqual(dstUtcOffset, cairo.GetUtcOffset (d.Add (new TimeSpan(0,0,0,-1))));
  838. Assert.AreEqual(baseUtcOffset, cairo.GetUtcOffset (d));
  839. Assert.AreEqual(baseUtcOffset, cairo.GetUtcOffset (d.Add (new TimeSpan(0,0,0, 1))));
  840. d = dst2Start.Add (-baseUtcOffset);
  841. d = DateTime.SpecifyKind (d, DateTimeKind.Utc);
  842. d = d.ToLocalTime ();
  843. Assert.AreEqual(baseUtcOffset, cairo.GetUtcOffset (d.Add (new TimeSpan(0,0,0,-1))));
  844. Assert.AreEqual(dstUtcOffset, cairo.GetUtcOffset (d));
  845. Assert.AreEqual(dstUtcOffset, cairo.GetUtcOffset (d.Add (new TimeSpan(0,0,0, 1))));
  846. d = dst2End.Add (-baseUtcOffset-dstOffset);
  847. d = DateTime.SpecifyKind (d, DateTimeKind.Utc);
  848. d = d.ToLocalTime ();
  849. Assert.AreEqual(dstUtcOffset, cairo.GetUtcOffset (d.Add (new TimeSpan(0,0,0,-1))));
  850. Assert.AreEqual(baseUtcOffset, cairo.GetUtcOffset (d));
  851. Assert.AreEqual(baseUtcOffset, cairo.GetUtcOffset (d.Add (new TimeSpan(0,0,0, 1))));
  852. }
  853. [Test]
  854. public void GetUtcOffset_FromUnspecified ()
  855. {
  856. var d = dst1Start.Add (dstOffset);
  857. Assert.AreEqual(baseUtcOffset, cairo.GetUtcOffset (d.Add (new TimeSpan(0,0,0,-1))));
  858. Assert.AreEqual(dstUtcOffset, cairo.GetUtcOffset (d));
  859. Assert.AreEqual(dstUtcOffset, cairo.GetUtcOffset (d.Add (new TimeSpan(0,0,0, 1))));
  860. d = dst1End.Add (-dstOffset);
  861. Assert.AreEqual(dstUtcOffset, cairo.GetUtcOffset (d.Add (new TimeSpan(0,0,0,-1))));
  862. Assert.AreEqual(baseUtcOffset, cairo.GetUtcOffset (d));
  863. Assert.AreEqual(baseUtcOffset, cairo.GetUtcOffset (d.Add (new TimeSpan(0,0,0, 1))));
  864. d = dst2Start.Add (dstOffset);
  865. Assert.AreEqual(baseUtcOffset, cairo.GetUtcOffset (d.Add (new TimeSpan(0,0,0,-1))));
  866. Assert.AreEqual(dstUtcOffset, cairo.GetUtcOffset (d));
  867. Assert.AreEqual(dstUtcOffset, cairo.GetUtcOffset (d.Add (new TimeSpan(0,0,0, 1))));
  868. d = dst2End.Add (-dstOffset);
  869. Assert.AreEqual(dstUtcOffset, cairo.GetUtcOffset (d.Add (new TimeSpan(0,0,0,-1))));
  870. Assert.AreEqual(baseUtcOffset, cairo.GetUtcOffset (d));
  871. Assert.AreEqual(baseUtcOffset, cairo.GetUtcOffset (d.Add (new TimeSpan(0,0,0, 1))));
  872. }
  873. }
  874. }
  875. }