Merge pull request #4248 from Unity-Technologies/boehm-gc-alloc-fixed
[mono.git] / mcs / class / corlib / System / TimeZoneInfo.cs
1
2 /*
3  * System.TimeZoneInfo
4  *
5  * Author(s)
6  *      Stephane Delcroix <stephane@delcroix.org>
7  *
8  * Copyright 2011 Xamarin Inc.
9  *
10  * Permission is hereby granted, free of charge, to any person obtaining
11  * a copy of this software and associated documentation files (the
12  * "Software"), to deal in the Software without restriction, including
13  * without limitation the rights to use, copy, modify, merge, publish,
14  * distribute, sublicense, and/or sell copies of the Software, and to
15  * permit persons to whom the Software is furnished to do so, subject to
16  * the following conditions:
17  * 
18  * The above copyright notice and this permission notice shall be
19  * included in all copies or substantial portions of the Software.
20  * 
21  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
25  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
26  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
27  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28  */
29
30 using System;
31 using System.Runtime.CompilerServices;
32 using System.Threading;
33 using System.Collections.Generic;
34 using System.Collections.ObjectModel;
35 using System.Runtime.Serialization;
36 using System.Runtime.InteropServices;
37 using System.Text;
38 using System.Globalization;
39 using System.IO;
40
41 using Microsoft.Win32;
42
43 namespace System
44 {
45         partial class TimeZoneInfo
46         {
47                 TimeSpan baseUtcOffset;
48                 public TimeSpan BaseUtcOffset {
49                         get { return baseUtcOffset; }
50                 }
51
52                 string daylightDisplayName;
53                 public string DaylightName {
54                         get { 
55                                 return supportsDaylightSavingTime
56                                         ? daylightDisplayName
57                                         : string.Empty;
58                         }
59                 }
60
61                 string displayName;
62                 public string DisplayName {
63                         get { return displayName; }
64                 }
65
66                 string id;
67                 public string Id {
68                         get { return id; }
69                 }
70
71                 static TimeZoneInfo local;
72                 public static TimeZoneInfo Local {
73                         get { 
74                                 var l = local;
75                                 if (l == null) {
76                                         l = CreateLocal ();
77                                         if (l == null)
78                                                 throw new TimeZoneNotFoundException ();
79
80                                         if (Interlocked.CompareExchange (ref local, l, null) != null)
81                                                 l = local;
82                                 }
83
84                                 return l;
85                         }
86                 }
87
88                 /*
89                         TimeZone transitions are stored when there is a change on the base offset.
90                 */
91                 private List<KeyValuePair<DateTime, TimeType>> transitions;
92
93                 private static bool readlinkNotFound;
94
95                 [DllImport ("libc")]
96                 private static extern int readlink (string path, byte[] buffer, int buflen);
97
98                 private static string readlink (string path)
99                 {
100                         if (readlinkNotFound)
101                                 return null;
102
103                         byte[] buf = new byte [512];
104                         int ret;
105
106                         try {
107                                 ret = readlink (path, buf, buf.Length);
108                         } catch (DllNotFoundException) {
109                                 readlinkNotFound = true;
110                                 return null;
111                         } catch (EntryPointNotFoundException) {
112                                 readlinkNotFound = true;
113                                 return null;
114                         }
115
116                         if (ret == -1) return null;
117                         char[] cbuf = new char [512];
118                         int chars = System.Text.Encoding.Default.GetChars (buf, 0, ret, cbuf, 0);
119                         return new String (cbuf, 0, chars);
120                 }
121
122                 private static bool TryGetNameFromPath (string path, out string name)
123                 {
124                         name = null;
125                         var linkPath = readlink (path);
126                         if (linkPath != null) {
127                                 if (Path.IsPathRooted(linkPath))
128                                         path = linkPath;
129                                 else
130                                         path = Path.Combine(Path.GetDirectoryName(path), linkPath);
131                         }
132
133                         path = Path.GetFullPath (path);
134
135                         if (string.IsNullOrEmpty (TimeZoneDirectory))
136                                 return false;
137
138                         var baseDir = TimeZoneDirectory;
139                         if (baseDir [baseDir.Length-1] != Path.DirectorySeparatorChar)
140                                 baseDir += Path.DirectorySeparatorChar;
141
142                         if (!path.StartsWith (baseDir, StringComparison.InvariantCulture))
143                                 return false;
144
145                         name = path.Substring (baseDir.Length);
146                         if (name == "localtime")
147                                 name = "Local";
148
149                         return true;
150                 }
151
152 #if !MONODROID && !MONOTOUCH && !XAMMAC
153                 static TimeZoneInfo CreateLocal ()
154                 {
155 #if !FULL_AOT_DESKTOP || WIN_PLATFORM
156                         if (IsWindows && LocalZoneKey != null) {
157                                 string name = (string)LocalZoneKey.GetValue ("TimeZoneKeyName");
158                                 if (name == null)
159                                         name = (string)LocalZoneKey.GetValue ("StandardName"); // windows xp
160                                 name = TrimSpecial (name);
161                                 if (name != null)
162                                         return TimeZoneInfo.FindSystemTimeZoneById (name);
163                         } else if (IsWindows) {
164                                 return GetLocalTimeZoneInfoWinRTFallback ();
165                         }
166 #endif
167
168                         var tz = Environment.GetEnvironmentVariable ("TZ");
169                         if (tz != null) {
170                                 if (tz == String.Empty)
171                                         return Utc;
172                                 try {
173                                         return FindSystemTimeZoneByFileName (tz, Path.Combine (TimeZoneDirectory, tz));
174                                 } catch {
175                                         return Utc;
176                                 }
177                         }
178
179                         var tzFilePaths = new string [] {
180                                 "/etc/localtime",
181                                 Path.Combine (TimeZoneDirectory, "localtime")};
182
183                         foreach (var tzFilePath in tzFilePaths) {
184                                 try {
185                                         string tzName = null;
186                                         if (!TryGetNameFromPath (tzFilePath, out tzName))
187                                                 tzName = "Local";
188                                         return FindSystemTimeZoneByFileName (tzName, tzFilePath);
189                                 } catch (TimeZoneNotFoundException) {
190                                         continue;
191                                 }
192                         }
193
194                         return Utc;
195                 }
196
197                 static TimeZoneInfo FindSystemTimeZoneByIdCore (string id)
198                 {
199 #if LIBC
200                         string filepath = Path.Combine (TimeZoneDirectory, id);
201                         return FindSystemTimeZoneByFileName (id, filepath);
202 #else
203                         throw new NotImplementedException ();
204 #endif
205                 }
206
207                 static void GetSystemTimeZonesCore (List<TimeZoneInfo> systemTimeZones)
208                 {
209 #if !FULL_AOT_DESKTOP || WIN_PLATFORM
210                         if (TimeZoneKey != null) {
211                                 foreach (string id in TimeZoneKey.GetSubKeyNames ()) {
212                                         try {
213                                                 systemTimeZones.Add (FindSystemTimeZoneById (id));
214                                         } catch {}
215                                 }
216
217                                 return;
218                         } else if (IsWindows) {
219                                 systemTimeZones.AddRange (GetSystemTimeZonesWinRTFallback ());
220                                 return;
221                         }
222 #endif
223
224 #if LIBC
225                         string[] continents = new string [] {"Africa", "America", "Antarctica", "Arctic", "Asia", "Atlantic", "Australia", "Brazil", "Canada", "Chile", "Europe", "Indian", "Mexico", "Mideast", "Pacific", "US"};
226                         foreach (string continent in continents) {
227                                 try {
228                                         foreach (string zonepath in Directory.GetFiles (Path.Combine (TimeZoneDirectory, continent))) {
229                                                 try {
230                                                         string id = String.Format ("{0}/{1}", continent, Path.GetFileName (zonepath));
231                                                         systemTimeZones.Add (FindSystemTimeZoneById (id));
232                                                 } catch (ArgumentNullException) {
233                                                 } catch (TimeZoneNotFoundException) {
234                                                 } catch (InvalidTimeZoneException) {
235                                                 } catch (Exception) {
236                                                         throw;
237                                                 }
238                                         }
239                                 } catch {}
240                         }
241 #else
242                         throw new NotImplementedException ("This method is not implemented for this platform");
243 #endif
244                 }
245 #endif // !MONODROID && !MONOTOUCH && !XAMMAC
246
247                 string standardDisplayName;
248                 public string StandardName {
249                         get { return standardDisplayName; }
250                 }
251
252                 bool supportsDaylightSavingTime;
253                 public bool SupportsDaylightSavingTime {
254                         get  { return supportsDaylightSavingTime; }
255                 }
256
257                 static TimeZoneInfo utc;
258                 public static TimeZoneInfo Utc {
259                         get {
260                                 if (utc == null)
261                                         utc = CreateCustomTimeZone ("UTC", new TimeSpan (0), "UTC", "UTC");
262                                 return utc;
263                         }
264                 }
265 #if LIBC
266                 static string timeZoneDirectory;
267                 static string TimeZoneDirectory {
268                         get {
269                                 if (timeZoneDirectory == null)
270                                         timeZoneDirectory = "/usr/share/zoneinfo";
271                                 return timeZoneDirectory;
272                         }
273                         set {
274                                 ClearCachedData ();
275                                 timeZoneDirectory = value;
276                         }
277                 }
278 #endif
279                 private AdjustmentRule [] adjustmentRules;
280
281 #if !MOBILE || !FULL_AOT_DESKTOP || WIN_PLATFORM
282                 /// <summary>
283                 /// Determine whether windows of not (taken Stephane Delcroix's code)
284                 /// </summary>
285                 private static bool IsWindows
286                 {
287                         get {
288                                 int platform = (int) Environment.OSVersion.Platform;
289                                 return ((platform != 4) && (platform != 6) && (platform != 128));
290                         }
291                 }
292                 
293                 /// <summary>
294                 /// Needed to trim misc garbage in MS registry keys
295                 /// </summary>
296                 private static string TrimSpecial (string str)
297                 {
298                         if (str == null)
299                                 return str;
300                         var Istart = 0;
301                         while (Istart < str.Length && !char.IsLetterOrDigit(str[Istart])) Istart++;
302                         var Iend = str.Length - 1;
303                         while (Iend > Istart && !char.IsLetterOrDigit(str[Iend]) && str[Iend] != ')') // zone name can include parentheses like "Central Standard Time (Mexico)"
304                                 Iend--;
305                         
306                         return str.Substring (Istart, Iend-Istart+1);
307                 }
308                 
309 #if !FULL_AOT_DESKTOP || WIN_PLATFORM
310                 static RegistryKey timeZoneKey;
311                 static RegistryKey TimeZoneKey {
312                         get {
313                                 if (timeZoneKey != null)
314                                         return timeZoneKey;
315                                 if (!IsWindows)
316                                         return null;
317                                 
318                                 try {
319                                         return timeZoneKey = Registry.LocalMachine.OpenSubKey (
320                                                 "SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Time Zones",
321                                                 false);
322                                 } catch {
323                                         return null;
324                                 }
325                         }
326                 }
327                 
328                 static RegistryKey localZoneKey;
329                 static RegistryKey LocalZoneKey {
330                         get {
331                                 if (localZoneKey != null)
332                                         return localZoneKey;
333                                 
334                                 if (!IsWindows)
335                                         return null;
336                                 
337                                 try {
338                                         return localZoneKey = Registry.LocalMachine.OpenSubKey (
339                                                 "SYSTEM\\CurrentControlSet\\Control\\TimeZoneInformation", false);
340                                 } catch {
341                                         return null;
342                                 }
343                         }
344                 }
345 #endif
346 #endif // !MOBILE || !FULL_AOT_DESKTOP || WIN_PLATFORM
347
348                 private static bool TryAddTicks (DateTime date, long ticks, out DateTime result, DateTimeKind kind = DateTimeKind.Unspecified)
349                 {
350                         var resultTicks = date.Ticks + ticks;
351                         if (resultTicks < DateTime.MinValue.Ticks) {
352                                 result = DateTime.SpecifyKind (DateTime.MinValue, kind);
353                                 return false;
354                         }
355
356                         if (resultTicks > DateTime.MaxValue.Ticks) {
357                                 result = DateTime.SpecifyKind (DateTime.MaxValue, kind);
358                                 return false;
359                         }
360
361                         result = new DateTime (resultTicks, kind);
362                         return true;
363                 }
364
365                 public static void ClearCachedData ()
366                 {
367                         local = null;
368                         utc = null;
369                         systemTimeZones = null;
370                 }
371
372                 public static DateTime ConvertTime (DateTime dateTime, TimeZoneInfo destinationTimeZone)
373                 {
374                         return ConvertTime (dateTime, dateTime.Kind == DateTimeKind.Utc ? TimeZoneInfo.Utc : TimeZoneInfo.Local, destinationTimeZone);
375                 }
376
377                 public static DateTime ConvertTime (DateTime dateTime, TimeZoneInfo sourceTimeZone, TimeZoneInfo destinationTimeZone)
378                 {
379                         if (sourceTimeZone == null)
380                                 throw new ArgumentNullException ("sourceTimeZone");
381
382                         if (destinationTimeZone == null)
383                                 throw new ArgumentNullException ("destinationTimeZone");
384                         
385                         if (dateTime.Kind == DateTimeKind.Local && sourceTimeZone != TimeZoneInfo.Local)
386                                 throw new ArgumentException ("Kind property of dateTime is Local but the sourceTimeZone does not equal TimeZoneInfo.Local");
387
388                         if (dateTime.Kind == DateTimeKind.Utc && sourceTimeZone != TimeZoneInfo.Utc)
389                                 throw new ArgumentException ("Kind property of dateTime is Utc but the sourceTimeZone does not equal TimeZoneInfo.Utc");
390                         
391                         if (sourceTimeZone.IsInvalidTime (dateTime))
392                                 throw new ArgumentException ("dateTime parameter is an invalid time");
393
394                         if (dateTime.Kind == DateTimeKind.Local && sourceTimeZone == TimeZoneInfo.Local && destinationTimeZone == TimeZoneInfo.Local)
395                                 return dateTime;
396
397                         DateTime utc = ConvertTimeToUtc (dateTime, sourceTimeZone);
398
399                         if (destinationTimeZone != TimeZoneInfo.Utc) {
400                                 utc = ConvertTimeFromUtc (utc, destinationTimeZone);
401                                 if (dateTime.Kind == DateTimeKind.Unspecified)
402                                         return DateTime.SpecifyKind (utc, DateTimeKind.Unspecified);
403                         }
404                         
405                         return utc;
406                 }
407
408                 public static DateTimeOffset ConvertTime(DateTimeOffset dateTimeOffset, TimeZoneInfo destinationTimeZone) 
409                 {
410                         if (destinationTimeZone == null) 
411                                 throw new ArgumentNullException("destinationTimeZone");
412
413                         var utcDateTime = dateTimeOffset.UtcDateTime;
414
415                         bool isDst;
416                         var utcOffset =  destinationTimeZone.GetUtcOffset(utcDateTime, out isDst);
417
418                         return new DateTimeOffset(DateTime.SpecifyKind(utcDateTime, DateTimeKind.Unspecified) + utcOffset, utcOffset);
419                 }
420
421                 public static DateTime ConvertTimeBySystemTimeZoneId (DateTime dateTime, string destinationTimeZoneId)
422                 {
423                         return ConvertTime (dateTime, FindSystemTimeZoneById (destinationTimeZoneId));
424                 }
425
426                 public static DateTime ConvertTimeBySystemTimeZoneId (DateTime dateTime, string sourceTimeZoneId, string destinationTimeZoneId)
427                 {
428                         TimeZoneInfo source_tz;
429                         if (dateTime.Kind == DateTimeKind.Utc && sourceTimeZoneId == TimeZoneInfo.Utc.Id) {
430                                 source_tz = Utc;
431                         } else {
432                                 source_tz = FindSystemTimeZoneById (sourceTimeZoneId);
433                         }
434
435                         return ConvertTime (dateTime, source_tz, FindSystemTimeZoneById (destinationTimeZoneId));
436                 }
437
438                 public static DateTimeOffset ConvertTimeBySystemTimeZoneId (DateTimeOffset dateTimeOffset, string destinationTimeZoneId)
439                 {
440                         return ConvertTime (dateTimeOffset, FindSystemTimeZoneById (destinationTimeZoneId));
441                 }
442
443                 private DateTime ConvertTimeFromUtc (DateTime dateTime)
444                 {
445                         if (dateTime.Kind == DateTimeKind.Local)
446                                 throw new ArgumentException ("Kind property of dateTime is Local");
447
448                         if (this == TimeZoneInfo.Utc)
449                                 return DateTime.SpecifyKind (dateTime, DateTimeKind.Utc);
450
451                         var utcOffset = GetUtcOffset (dateTime);
452
453                         var kind = (this == TimeZoneInfo.Local)? DateTimeKind.Local : DateTimeKind.Unspecified;
454
455                         DateTime result;
456                         if (!TryAddTicks (dateTime, utcOffset.Ticks, out result, kind))
457                                 return DateTime.SpecifyKind (DateTime.MaxValue, kind);
458
459                         return result;
460                 }
461
462                 public static DateTime ConvertTimeFromUtc (DateTime dateTime, TimeZoneInfo destinationTimeZone)
463                 {
464                         if (destinationTimeZone == null)
465                                 throw new ArgumentNullException ("destinationTimeZone");
466
467                         return destinationTimeZone.ConvertTimeFromUtc (dateTime);
468                 }
469
470                 public static DateTime ConvertTimeToUtc (DateTime dateTime)
471                 {
472                         if (dateTime.Kind == DateTimeKind.Utc)
473                                 return dateTime;
474
475                         return ConvertTimeToUtc (dateTime, TimeZoneInfo.Local);
476                 }
477
478                 static internal DateTime ConvertTimeToUtc(DateTime dateTime, TimeZoneInfoOptions flags)
479                 {
480                         return ConvertTimeToUtc (dateTime, TimeZoneInfo.Local, flags);
481                 }
482
483                 public static DateTime ConvertTimeToUtc (DateTime dateTime, TimeZoneInfo sourceTimeZone)
484                 {
485                         return ConvertTimeToUtc (dateTime, sourceTimeZone, TimeZoneInfoOptions.None);
486                 }
487
488                 static DateTime ConvertTimeToUtc (DateTime dateTime, TimeZoneInfo sourceTimeZone, TimeZoneInfoOptions flags)
489                 {
490                         if ((flags & TimeZoneInfoOptions.NoThrowOnInvalidTime) == 0) {
491                                 if (sourceTimeZone == null)
492                                         throw new ArgumentNullException ("sourceTimeZone");
493
494                                 if (dateTime.Kind == DateTimeKind.Utc && sourceTimeZone != TimeZoneInfo.Utc)
495                                         throw new ArgumentException ("Kind property of dateTime is Utc but the sourceTimeZone does not equal TimeZoneInfo.Utc");
496
497                                 if (dateTime.Kind == DateTimeKind.Local && sourceTimeZone != TimeZoneInfo.Local)
498                                         throw new ArgumentException ("Kind property of dateTime is Local but the sourceTimeZone does not equal TimeZoneInfo.Local");
499
500                                 if (sourceTimeZone.IsInvalidTime (dateTime))
501                                         throw new ArgumentException ("dateTime parameter is an invalid time");
502                         }
503
504                         if (dateTime.Kind == DateTimeKind.Utc)
505                                 return dateTime;
506
507                         bool isDst;
508                         var utcOffset = sourceTimeZone.GetUtcOffset (dateTime, out isDst);
509
510                         DateTime utcDateTime;
511                         TryAddTicks (dateTime, -utcOffset.Ticks, out utcDateTime, DateTimeKind.Utc);
512                         return utcDateTime;
513                 }
514
515                 static internal TimeSpan GetDateTimeNowUtcOffsetFromUtc(DateTime time, out Boolean isAmbiguousLocalDst)
516                 {
517                         bool isDaylightSavings;
518                         return GetUtcOffsetFromUtc(time, TimeZoneInfo.Local, out isDaylightSavings, out isAmbiguousLocalDst);
519                 }
520
521                 public static TimeZoneInfo CreateCustomTimeZone (string id, TimeSpan baseUtcOffset, string displayName, string standardDisplayName) 
522                 {
523                         return CreateCustomTimeZone (id, baseUtcOffset, displayName, standardDisplayName, null, null, true);
524                 }
525
526                 public static TimeZoneInfo CreateCustomTimeZone (string id, TimeSpan baseUtcOffset, string displayName, string standardDisplayName, string daylightDisplayName, TimeZoneInfo.AdjustmentRule [] adjustmentRules)
527                 {
528                         return CreateCustomTimeZone (id, baseUtcOffset, displayName, standardDisplayName, daylightDisplayName, adjustmentRules, false);
529                 }
530
531                 public static TimeZoneInfo CreateCustomTimeZone ( string id, TimeSpan baseUtcOffset, string displayName, string standardDisplayName, string daylightDisplayName, TimeZoneInfo.AdjustmentRule [] adjustmentRules, bool disableDaylightSavingTime)
532                 {
533                         return new TimeZoneInfo (id, baseUtcOffset, displayName, standardDisplayName, daylightDisplayName, adjustmentRules, disableDaylightSavingTime);
534                 }
535
536                 public override bool Equals (object obj)
537                 {
538                         return Equals (obj as TimeZoneInfo);
539                 }
540
541                 public bool Equals (TimeZoneInfo other)
542                 {
543                         if (other == null)
544                                 return false;
545
546                         return other.Id == this.Id && HasSameRules (other);
547                 }
548
549                 public static TimeZoneInfo FindSystemTimeZoneById (string id)
550                 {
551                         //FIXME: this method should check for cached values in systemTimeZones
552                         if (id == null)
553                                 throw new ArgumentNullException ("id");
554 #if WIN_PLATFORM
555                         if (TimeZoneKey != null)
556                         {
557                                 if (id == "Coordinated Universal Time")
558                                         id = "UTC"; //windows xp exception for "StandardName" property
559                                 RegistryKey key = TimeZoneKey.OpenSubKey (id, false);
560                                 if (key == null)
561                                         throw new TimeZoneNotFoundException ();
562                                 return FromRegistryKey(id, key);
563                         } else if (IsWindows) {
564                                 return FindSystemTimeZoneByIdWinRTFallback (id);
565                         }
566 #endif
567                         // Local requires special logic that already exists in the Local property (bug #326)
568                         if (id == "Local")
569                                 return Local;
570
571                         return FindSystemTimeZoneByIdCore (id);
572                 }
573
574 #if LIBC
575                 private static TimeZoneInfo FindSystemTimeZoneByFileName (string id, string filepath)
576                 {
577                         if (!File.Exists (filepath))
578                                 throw new TimeZoneNotFoundException ();
579
580                         using (FileStream stream = File.OpenRead (filepath)) {
581                                 return BuildFromStream (id, stream);
582                         }
583                 }
584 #endif
585
586 #if WIN_PLATFORM
587                 private static TimeZoneInfo FromRegistryKey (string id, RegistryKey key)
588                 {
589                         byte [] reg_tzi = (byte []) key.GetValue ("TZI");
590
591                         if (reg_tzi == null)
592                                 throw new InvalidTimeZoneException ();
593
594                         int bias = BitConverter.ToInt32 (reg_tzi, 0);
595                         TimeSpan baseUtcOffset = new TimeSpan (0, -bias, 0);
596
597                         string display_name = (string) key.GetValue ("Display");
598                         string standard_name = (string) key.GetValue ("Std");
599                         string daylight_name = (string) key.GetValue ("Dlt");
600
601                         List<AdjustmentRule> adjustmentRules = new List<AdjustmentRule> ();
602
603                         RegistryKey dst_key = key.OpenSubKey ("Dynamic DST", false);
604                         if (dst_key != null) {
605                                 int first_year = (int) dst_key.GetValue ("FirstEntry");
606                                 int last_year = (int) dst_key.GetValue ("LastEntry");
607                                 int year;
608
609                                 for (year=first_year; year<=last_year; year++) {
610                                         byte [] dst_tzi = (byte []) dst_key.GetValue (year.ToString ());
611                                         if (dst_tzi != null) {
612                                                 int start_year = year == first_year ? 1 : year;
613                                                 int end_year = year == last_year ? 9999 : year;
614                                                 ParseRegTzi(adjustmentRules, start_year, end_year, dst_tzi);
615                                         }
616                                 }
617                         }
618                         else
619                                 ParseRegTzi(adjustmentRules, 1, 9999, reg_tzi);
620
621                         return CreateCustomTimeZone (id, baseUtcOffset, display_name, standard_name, daylight_name, ValidateRules (adjustmentRules).ToArray ());
622                 }
623
624                 private static void ParseRegTzi (List<AdjustmentRule> adjustmentRules, int start_year, int end_year, byte [] buffer)
625                 {
626                         //int standard_bias = BitConverter.ToInt32 (buffer, 4); /* not sure how to handle this */
627                         int daylight_bias = BitConverter.ToInt32 (buffer, 8);
628
629                         int standard_year = BitConverter.ToInt16 (buffer, 12);
630                         int standard_month = BitConverter.ToInt16 (buffer, 14);
631                         int standard_dayofweek = BitConverter.ToInt16 (buffer, 16);
632                         int standard_day = BitConverter.ToInt16 (buffer, 18);
633                         int standard_hour = BitConverter.ToInt16 (buffer, 20);
634                         int standard_minute = BitConverter.ToInt16 (buffer, 22);
635                         int standard_second = BitConverter.ToInt16 (buffer, 24);
636                         int standard_millisecond = BitConverter.ToInt16 (buffer, 26);
637
638                         int daylight_year = BitConverter.ToInt16 (buffer, 28);
639                         int daylight_month = BitConverter.ToInt16 (buffer, 30);
640                         int daylight_dayofweek = BitConverter.ToInt16 (buffer, 32);
641                         int daylight_day = BitConverter.ToInt16 (buffer, 34);
642                         int daylight_hour = BitConverter.ToInt16 (buffer, 36);
643                         int daylight_minute = BitConverter.ToInt16 (buffer, 38);
644                         int daylight_second = BitConverter.ToInt16 (buffer, 40);
645                         int daylight_millisecond = BitConverter.ToInt16 (buffer, 42);
646
647                         if (standard_month == 0 || daylight_month == 0)
648                                 return;
649
650                         DateTime start_date;
651                         DateTime start_timeofday = new DateTime (1, 1, 1, daylight_hour, daylight_minute, daylight_second, daylight_millisecond);
652                         TransitionTime start_transition_time;
653
654                         if (daylight_year == 0) {
655                                 start_date = new DateTime (start_year, 1, 1);
656                                 start_transition_time = TransitionTime.CreateFloatingDateRule (
657                                         start_timeofday, daylight_month, daylight_day,
658                                         (DayOfWeek) daylight_dayofweek);
659                         }
660                         else {
661                                 start_date = new DateTime (daylight_year, daylight_month, daylight_day,
662                                         daylight_hour, daylight_minute, daylight_second, daylight_millisecond);
663                                 start_transition_time = TransitionTime.CreateFixedDateRule (
664                                         start_timeofday, daylight_month, daylight_day);
665                         }
666
667                         DateTime end_date;
668                         DateTime end_timeofday = new DateTime (1, 1, 1, standard_hour, standard_minute, standard_second, standard_millisecond);
669                         TransitionTime end_transition_time;
670
671                         if (standard_year == 0) {
672                                 end_date = new DateTime (end_year, 12, 31);
673                                 end_transition_time = TransitionTime.CreateFloatingDateRule (
674                                         end_timeofday, standard_month, standard_day,
675                                         (DayOfWeek) standard_dayofweek);
676                         }
677                         else {
678                                 end_date = new DateTime (standard_year, standard_month, standard_day,
679                                         standard_hour, standard_minute, standard_second, standard_millisecond);
680                                 end_transition_time = TransitionTime.CreateFixedDateRule (
681                                         end_timeofday, standard_month, standard_day);
682                         }
683
684                         TimeSpan daylight_delta = new TimeSpan(0, -daylight_bias, 0);
685
686                         adjustmentRules.Add (AdjustmentRule.CreateAdjustmentRule (
687                                 start_date, end_date, daylight_delta,
688                                 start_transition_time, end_transition_time));
689                 }
690 #endif
691
692                 public AdjustmentRule [] GetAdjustmentRules ()
693                 {
694                         if (!supportsDaylightSavingTime || adjustmentRules == null)
695                                 return new AdjustmentRule [0];
696                         else
697                                 return (AdjustmentRule []) adjustmentRules.Clone ();
698                 }
699
700                 public TimeSpan [] GetAmbiguousTimeOffsets (DateTime dateTime)
701                 {
702                         if (!IsAmbiguousTime (dateTime))
703                                 throw new ArgumentException ("dateTime is not an ambiguous time");
704
705                         AdjustmentRule rule = GetApplicableRule (dateTime);
706                         if (rule != null)
707                                 return new TimeSpan[] {baseUtcOffset, baseUtcOffset + rule.DaylightDelta};
708                         else
709                                 return new TimeSpan[] {baseUtcOffset, baseUtcOffset};
710                 }
711
712                 public TimeSpan [] GetAmbiguousTimeOffsets (DateTimeOffset dateTimeOffset)
713                 {
714                         if (!IsAmbiguousTime (dateTimeOffset))
715                                 throw new ArgumentException ("dateTimeOffset is not an ambiguous time");
716
717                         throw new NotImplementedException ();
718                 }
719
720                 public override int GetHashCode ()
721                 {
722                         int hash_code = Id.GetHashCode ();
723                         foreach (AdjustmentRule rule in GetAdjustmentRules ())
724                                 hash_code ^= rule.GetHashCode ();
725                         return hash_code;
726                 }
727
728                 void ISerializable.GetObjectData (SerializationInfo info, StreamingContext context)
729                 {
730                         if (info == null)
731                                 throw new ArgumentNullException ("info");
732                         info.AddValue ("Id", id);
733                         info.AddValue ("DisplayName", displayName);
734                         info.AddValue ("StandardName", standardDisplayName);
735                         info.AddValue ("DaylightName", daylightDisplayName);
736                         info.AddValue ("BaseUtcOffset", baseUtcOffset);
737                         info.AddValue ("AdjustmentRules", adjustmentRules);
738                         info.AddValue ("SupportsDaylightSavingTime", SupportsDaylightSavingTime);
739                 }
740
741                 static ReadOnlyCollection<TimeZoneInfo> systemTimeZones;
742
743                 public static ReadOnlyCollection<TimeZoneInfo> GetSystemTimeZones ()
744                 {
745                         if (systemTimeZones == null) {
746                                 var tz = new List<TimeZoneInfo> ();
747                                 GetSystemTimeZonesCore (tz);
748                                 Interlocked.CompareExchange (ref systemTimeZones, new ReadOnlyCollection<TimeZoneInfo> (tz), null);
749                         }
750
751                         return systemTimeZones;
752                 }
753
754                 public TimeSpan GetUtcOffset (DateTime dateTime)
755                 {
756                         bool isDST;
757                         return GetUtcOffset (dateTime, out isDST);
758                 }
759
760                 public TimeSpan GetUtcOffset (DateTimeOffset dateTimeOffset)
761                 {
762                         bool isDST;
763                         return GetUtcOffset (dateTimeOffset.UtcDateTime, out isDST);
764                 }
765
766                 private TimeSpan GetUtcOffset (DateTime dateTime, out bool isDST)
767                 {
768                         isDST = false;
769
770                         TimeZoneInfo tz = this;
771                         if (dateTime.Kind == DateTimeKind.Utc)
772                                 tz = TimeZoneInfo.Utc;
773
774                         if (dateTime.Kind == DateTimeKind.Local)
775                                 tz = TimeZoneInfo.Local;
776
777                         bool isTzDst;
778                         var tzOffset = GetUtcOffsetHelper (dateTime, tz, out isTzDst);
779
780                         if (tz == this) {
781                                 isDST = isTzDst;
782                                 return tzOffset;
783                         }
784
785                         DateTime utcDateTime;
786                         if (!TryAddTicks (dateTime, -tzOffset.Ticks, out utcDateTime, DateTimeKind.Utc))
787                                 return BaseUtcOffset;
788
789                         return GetUtcOffsetHelper (utcDateTime, this, out isDST);
790                 }
791
792                 // This is an helper method used by the method above, do not use this on its own.
793                 private static TimeSpan GetUtcOffsetHelper (DateTime dateTime, TimeZoneInfo tz, out bool isDST)
794                 {
795                         if (dateTime.Kind == DateTimeKind.Local && tz != TimeZoneInfo.Local)
796                                 throw new Exception ();
797
798                         isDST = false;
799
800                         if (tz == TimeZoneInfo.Utc)
801                                 return TimeSpan.Zero;
802
803                         TimeSpan offset;
804                         if (tz.TryGetTransitionOffset(dateTime, out offset, out isDST))
805                                 return offset;
806
807                         if (dateTime.Kind == DateTimeKind.Utc) {
808                                 var utcRule = tz.GetApplicableRule (dateTime);
809                                 if (utcRule != null && tz.IsInDST (utcRule, dateTime)) {
810                                         isDST = true;
811                                         return tz.BaseUtcOffset + utcRule.DaylightDelta;
812                                 }
813
814                                 return tz.BaseUtcOffset;
815                         }
816
817                         DateTime stdUtcDateTime;
818                         if (!TryAddTicks (dateTime, -tz.BaseUtcOffset.Ticks, out stdUtcDateTime, DateTimeKind.Utc))
819                                 return tz.BaseUtcOffset;
820
821                         var tzRule = tz.GetApplicableRule (stdUtcDateTime);
822
823                         DateTime dstUtcDateTime = DateTime.MinValue;
824                         if (tzRule != null) {
825                                 if (!TryAddTicks (stdUtcDateTime, -tzRule.DaylightDelta.Ticks, out dstUtcDateTime, DateTimeKind.Utc))
826                                         return tz.BaseUtcOffset;
827                         }
828
829                         if (tzRule != null && tz.IsInDST (tzRule, stdUtcDateTime)) {
830                                 // Replicate what .NET does when given a time which falls into the hour which is lost when
831                                 // DST starts. isDST should always be true but the offset should be BaseUtcOffset without the
832                                 // DST delta while in that hour.
833                                 isDST = true;
834                                 if (tz.IsInDST (tzRule, dstUtcDateTime)) {
835                                         return tz.BaseUtcOffset + tzRule.DaylightDelta;
836                                 } else {
837                                         return tz.BaseUtcOffset;
838                                 }
839                         }
840
841                         return tz.BaseUtcOffset;
842                 }
843
844                 public bool HasSameRules (TimeZoneInfo other)
845                 {
846                         if (other == null)
847                                 throw new ArgumentNullException ("other");
848
849                         if ((this.adjustmentRules == null) != (other.adjustmentRules == null))
850                                 return false;
851
852                         if (this.adjustmentRules == null)
853                                 return true;
854
855                         if (this.BaseUtcOffset != other.BaseUtcOffset)
856                                 return false;
857
858                         if (this.adjustmentRules.Length != other.adjustmentRules.Length)
859                                 return false;
860
861                         for (int i = 0; i < adjustmentRules.Length; i++) {
862                                 if (! (this.adjustmentRules [i]).Equals (other.adjustmentRules [i]))
863                                         return false;
864                         }
865                         
866                         return true;
867                 }
868
869                 public bool IsAmbiguousTime (DateTime dateTime)
870                 {
871                         if (dateTime.Kind == DateTimeKind.Local && IsInvalidTime (dateTime))
872                                 throw new ArgumentException ("Kind is Local and time is Invalid");
873
874                         if (this == TimeZoneInfo.Utc)
875                                 return false;
876                         
877                         if (dateTime.Kind == DateTimeKind.Utc)
878                                 dateTime = ConvertTimeFromUtc (dateTime);
879
880                         if (dateTime.Kind == DateTimeKind.Local && this != TimeZoneInfo.Local)
881                                 dateTime = ConvertTime (dateTime, TimeZoneInfo.Local, this);
882
883                         AdjustmentRule rule = GetApplicableRule (dateTime);
884                         if (rule != null) {
885                                 DateTime tpoint = TransitionPoint (rule.DaylightTransitionEnd, dateTime.Year);
886                                 if (dateTime > tpoint - rule.DaylightDelta  && dateTime <= tpoint)
887                                         return true;
888                         }
889                                 
890                         return false;
891                 }
892
893                 public bool IsAmbiguousTime (DateTimeOffset dateTimeOffset)
894                 {
895                         throw new NotImplementedException ();
896                 }
897
898                 private bool IsInDST (AdjustmentRule rule, DateTime dateTime)
899                 {
900                         // Check whether we're in the dateTime year's DST period
901                         if (IsInDSTForYear (rule, dateTime, dateTime.Year))
902                                 return true;
903
904                         // We might be in the dateTime previous year's DST period
905                         return dateTime.Year > 1 && IsInDSTForYear (rule, dateTime, dateTime.Year - 1);
906                 }
907
908                 bool IsInDSTForYear (AdjustmentRule rule, DateTime dateTime, int year)
909                 {
910                         DateTime DST_start = TransitionPoint (rule.DaylightTransitionStart, year);
911                         DateTime DST_end = TransitionPoint (rule.DaylightTransitionEnd, year + ((rule.DaylightTransitionStart.Month < rule.DaylightTransitionEnd.Month) ? 0 : 1));
912                         if (dateTime.Kind == DateTimeKind.Utc) {
913                                 DST_start -= BaseUtcOffset;
914                                 DST_end -= (BaseUtcOffset + rule.DaylightDelta);
915                         }
916
917                         return (dateTime >= DST_start && dateTime < DST_end);
918                 }
919                 
920                 public bool IsDaylightSavingTime (DateTime dateTime)
921                 {
922                         if (dateTime.Kind == DateTimeKind.Local && IsInvalidTime (dateTime))
923                                 throw new ArgumentException ("dateTime is invalid and Kind is Local");
924
925                         if (this == TimeZoneInfo.Utc)
926                                 return false;
927                         
928                         if (!SupportsDaylightSavingTime)
929                                 return false;
930
931                         bool isDst;
932                         GetUtcOffset (dateTime, out isDst);
933
934                         return isDst;
935                 }
936
937                 internal bool IsDaylightSavingTime (DateTime dateTime, TimeZoneInfoOptions flags)
938                 {
939                         return IsDaylightSavingTime (dateTime);
940                 }
941
942                 public bool IsDaylightSavingTime (DateTimeOffset dateTimeOffset)
943                 {
944                         return IsDaylightSavingTime (dateTimeOffset.DateTime);
945                 }
946
947                 internal DaylightTime GetDaylightChanges (int year)
948                 {
949                         DateTime start = DateTime.MinValue, end = DateTime.MinValue;
950                         TimeSpan delta = new TimeSpan ();
951
952                         if (transitions != null) {
953                                 end = DateTime.MaxValue;
954                                 for (var i =  transitions.Count - 1; i >= 0; i--) {
955                                         var pair = transitions [i];
956                                         DateTime ttime = pair.Key;
957                                         TimeType ttype = pair.Value;
958
959                                         if (ttime.Year > year)
960                                                 continue;
961                                         if (ttime.Year < year)
962                                                 break;
963
964                                         if (ttype.IsDst) {
965                                                 // DaylightTime.Delta is relative to the current BaseUtcOffset.
966                                                 delta =  new TimeSpan (0, 0, ttype.Offset) - BaseUtcOffset;
967                                                 start = ttime;
968                                         } else {
969                                                 end = ttime;
970                                         }
971                                 }
972
973                                 // DaylightTime.Start is relative to the Standard time.
974                                 if (!TryAddTicks (start, BaseUtcOffset.Ticks, out start))
975                                         start = DateTime.MinValue;
976
977                                 // DaylightTime.End is relative to the DST time.
978                                 if (!TryAddTicks (end, BaseUtcOffset.Ticks + delta.Ticks, out end))
979                                         end = DateTime.MinValue;
980                         } else {
981                                 AdjustmentRule first = null, last = null;
982
983                                 // Rule start/end dates are either very specific or very broad depending on the platform
984                                 //   2015-10-04..2016-04-03 - Rule for a time zone in southern hemisphere on non-Windows platforms
985                                 //   2016-03-27..2016-10-03 - Rule for a time zone in northern hemisphere on non-Windows platforms
986                                 //   0001-01-01..9999-12-31 - Rule for a time zone on Windows
987
988                                 foreach (var rule in GetAdjustmentRules ()) {
989                                         if (rule.DateStart.Year > year || rule.DateEnd.Year < year)
990                                                 continue;
991                                         if (rule.DateStart.Year <= year && (first == null || rule.DateStart.Year > first.DateStart.Year))
992                                                 first = rule;
993                                         if (rule.DateEnd.Year >= year && (last == null || rule.DateEnd.Year < last.DateEnd.Year))
994                                                 last = rule;
995                                 }
996
997                                 if (first == null || last == null)
998                                         return new DaylightTime (new DateTime (), new DateTime (), new TimeSpan ());
999
1000                                 start = TransitionPoint (first.DaylightTransitionStart, year);
1001                                 end = TransitionPoint (last.DaylightTransitionEnd, year);
1002                                 delta = first.DaylightDelta;
1003                         }
1004
1005                         if (start == DateTime.MinValue || end == DateTime.MinValue)
1006                                 return new DaylightTime (new DateTime (), new DateTime (), new TimeSpan ());
1007
1008                         return new DaylightTime (start, end, delta);
1009                 }
1010
1011                 public bool IsInvalidTime (DateTime dateTime)
1012                 {
1013                         if (dateTime.Kind == DateTimeKind.Utc)
1014                                 return false;
1015                         if (dateTime.Kind == DateTimeKind.Local && this != Local)
1016                                 return false;
1017
1018                         AdjustmentRule rule = GetApplicableRule (dateTime);
1019                         if (rule != null) {
1020                                 DateTime tpoint = TransitionPoint (rule.DaylightTransitionStart, dateTime.Year);
1021                                 if (dateTime >= tpoint && dateTime < tpoint + rule.DaylightDelta)
1022                                         return true;
1023                         }
1024
1025                         return false;
1026                 }
1027
1028                 void IDeserializationCallback.OnDeserialization (object sender)
1029                 {
1030                         try {
1031                                         TimeZoneInfo.Validate (id, baseUtcOffset, adjustmentRules);
1032                                 } catch (ArgumentException ex) {
1033                                         throw new SerializationException ("invalid serialization data", ex);
1034                                 }
1035                 }
1036
1037                 private static void Validate (string id, TimeSpan baseUtcOffset, AdjustmentRule [] adjustmentRules)
1038                 {
1039                         if (id == null)
1040                                 throw new ArgumentNullException ("id");
1041
1042                         if (id == String.Empty)
1043                                 throw new ArgumentException ("id parameter is an empty string");
1044
1045                         if (baseUtcOffset.Ticks % TimeSpan.TicksPerMinute != 0)
1046                                 throw new ArgumentException ("baseUtcOffset parameter does not represent a whole number of minutes");
1047
1048                         if (baseUtcOffset > new TimeSpan (14, 0, 0) || baseUtcOffset < new TimeSpan (-14, 0, 0))
1049                                 throw new ArgumentOutOfRangeException ("baseUtcOffset parameter is greater than 14 hours or less than -14 hours");
1050
1051 #if STRICT
1052                         if (id.Length > 32)
1053                                 throw new ArgumentException ("id parameter shouldn't be longer than 32 characters");
1054 #endif
1055
1056                         if (adjustmentRules != null && adjustmentRules.Length != 0) {
1057                                 AdjustmentRule prev = null;
1058                                 foreach (AdjustmentRule current in adjustmentRules) {
1059                                         if (current == null)
1060                                                 throw new InvalidTimeZoneException ("one or more elements in adjustmentRules are null");
1061
1062                                         if ((baseUtcOffset + current.DaylightDelta < new TimeSpan (-14, 0, 0)) ||
1063                                                         (baseUtcOffset + current.DaylightDelta > new TimeSpan (14, 0, 0)))
1064                                                 throw new InvalidTimeZoneException ("Sum of baseUtcOffset and DaylightDelta of one or more object in adjustmentRules array is greater than 14 or less than -14 hours;");
1065
1066                                         if (prev != null && prev.DateStart > current.DateStart)
1067                                                 throw new InvalidTimeZoneException ("adjustment rules specified in adjustmentRules parameter are not in chronological order");
1068                                         
1069                                         if (prev != null && prev.DateEnd > current.DateStart)
1070                                                 throw new InvalidTimeZoneException ("some adjustment rules in the adjustmentRules parameter overlap");
1071
1072                                         if (prev != null && prev.DateEnd == current.DateStart)
1073                                                 throw new InvalidTimeZoneException ("a date can have multiple adjustment rules applied to it");
1074
1075                                         prev = current;
1076                                 }
1077                         }
1078                 }
1079                 
1080                 public override string ToString ()
1081                 {
1082                         return DisplayName;
1083                 }
1084
1085                 private TimeZoneInfo (SerializationInfo info, StreamingContext context)
1086                 {
1087                         if (info == null)
1088                                 throw new ArgumentNullException ("info");
1089                         id = (string) info.GetValue ("Id", typeof (string));
1090                         displayName = (string) info.GetValue ("DisplayName", typeof (string));
1091                         standardDisplayName = (string) info.GetValue ("StandardName", typeof (string));
1092                         daylightDisplayName = (string) info.GetValue ("DaylightName", typeof (string));
1093                         baseUtcOffset = (TimeSpan) info.GetValue ("BaseUtcOffset", typeof (TimeSpan));
1094                         adjustmentRules = (TimeZoneInfo.AdjustmentRule []) info.GetValue ("AdjustmentRules", typeof (TimeZoneInfo.AdjustmentRule []));
1095                         supportsDaylightSavingTime = (bool) info.GetValue ("SupportsDaylightSavingTime", typeof (bool));
1096                 }
1097
1098                 private TimeZoneInfo (string id, TimeSpan baseUtcOffset, string displayName, string standardDisplayName, string daylightDisplayName, TimeZoneInfo.AdjustmentRule [] adjustmentRules, bool disableDaylightSavingTime)
1099                 {
1100                         if (id == null)
1101                                 throw new ArgumentNullException ("id");
1102
1103                         if (id == String.Empty)
1104                                 throw new ArgumentException ("id parameter is an empty string");
1105
1106                         if (baseUtcOffset.Ticks % TimeSpan.TicksPerMinute != 0)
1107                                 throw new ArgumentException ("baseUtcOffset parameter does not represent a whole number of minutes");
1108
1109                         if (baseUtcOffset > new TimeSpan (14, 0, 0) || baseUtcOffset < new TimeSpan (-14, 0, 0))
1110                                 throw new ArgumentOutOfRangeException ("baseUtcOffset parameter is greater than 14 hours or less than -14 hours");
1111
1112 #if STRICT
1113                         if (id.Length > 32)
1114                                 throw new ArgumentException ("id parameter shouldn't be longer than 32 characters");
1115 #endif
1116
1117                         bool supportsDaylightSavingTime = !disableDaylightSavingTime;
1118
1119                         if (adjustmentRules != null && adjustmentRules.Length != 0) {
1120                                 AdjustmentRule prev = null;
1121                                 foreach (AdjustmentRule current in adjustmentRules) {
1122                                         if (current == null)
1123                                                 throw new InvalidTimeZoneException ("one or more elements in adjustmentRules are null");
1124
1125                                         if ((baseUtcOffset + current.DaylightDelta < new TimeSpan (-14, 0, 0)) ||
1126                                                         (baseUtcOffset + current.DaylightDelta > new TimeSpan (14, 0, 0)))
1127                                                 throw new InvalidTimeZoneException ("Sum of baseUtcOffset and DaylightDelta of one or more object in adjustmentRules array is greater than 14 or less than -14 hours;");
1128
1129                                         if (prev != null && prev.DateStart > current.DateStart)
1130                                                 throw new InvalidTimeZoneException ("adjustment rules specified in adjustmentRules parameter are not in chronological order");
1131                                         
1132                                         if (prev != null && prev.DateEnd > current.DateStart)
1133                                                 throw new InvalidTimeZoneException ("some adjustment rules in the adjustmentRules parameter overlap");
1134
1135                                         if (prev != null && prev.DateEnd == current.DateStart)
1136                                                 throw new InvalidTimeZoneException ("a date can have multiple adjustment rules applied to it");
1137
1138                                         prev = current;
1139                                 }
1140                         } else {
1141                                 supportsDaylightSavingTime = false;
1142                         }
1143                         
1144                         this.id = id;
1145                         this.baseUtcOffset = baseUtcOffset;
1146                         this.displayName = displayName ?? id;
1147                         this.standardDisplayName = standardDisplayName ?? id;
1148                         this.daylightDisplayName = daylightDisplayName;
1149                         this.supportsDaylightSavingTime = supportsDaylightSavingTime;
1150                         this.adjustmentRules = adjustmentRules;
1151                 }
1152
1153                 private AdjustmentRule GetApplicableRule (DateTime dateTime)
1154                 {
1155                         //Applicable rules are in standard time
1156                         DateTime date = dateTime;
1157
1158                         if (dateTime.Kind == DateTimeKind.Local && this != TimeZoneInfo.Local) {
1159                                 if (!TryAddTicks (date.ToUniversalTime (), BaseUtcOffset.Ticks, out date))
1160                                         return null;
1161                         } else if (dateTime.Kind == DateTimeKind.Utc && this != TimeZoneInfo.Utc) {
1162                                 if (!TryAddTicks (date, BaseUtcOffset.Ticks, out date))
1163                                         return null;
1164                         }
1165
1166                         // get the date component of the datetime
1167                         date = date.Date;
1168
1169                         if (adjustmentRules != null) {
1170                                 foreach (AdjustmentRule rule in adjustmentRules) {
1171                                         if (rule.DateStart > date)
1172                                                 return null;
1173                                         if (rule.DateEnd < date)
1174                                                 continue;
1175                                         return rule;
1176                                 }
1177                         }
1178                         return null;
1179                 }
1180
1181                 private bool TryGetTransitionOffset (DateTime dateTime, out TimeSpan offset,out bool isDst)
1182                 {
1183                         offset = BaseUtcOffset;
1184                         isDst = false;
1185
1186                         if (transitions == null)
1187                                 return false;
1188
1189                         //Transitions are in UTC
1190                         DateTime date = dateTime;
1191
1192                         if (dateTime.Kind == DateTimeKind.Local && this != TimeZoneInfo.Local) {
1193                                 if (!TryAddTicks (date.ToUniversalTime (), BaseUtcOffset.Ticks, out date, DateTimeKind.Utc))
1194                                         return false;
1195                         }
1196
1197                         if (dateTime.Kind != DateTimeKind.Utc) {
1198                                 if (!TryAddTicks (date, -BaseUtcOffset.Ticks, out date, DateTimeKind.Utc))
1199                                         return false;
1200                         }
1201
1202                         var inDelta = false;
1203                         for (var i =  transitions.Count - 1; i >= 0; i--) {
1204                                 var pair = transitions [i];
1205                                 DateTime ttime = pair.Key;
1206                                 TimeType ttype = pair.Value;
1207
1208                                 var delta =  new TimeSpan (0, 0, ttype.Offset) - BaseUtcOffset;
1209
1210                                 if ((ttime + delta) > date) {
1211                                         inDelta = ttime <= date;
1212                                         continue;
1213                                 }
1214
1215                                 offset =  new TimeSpan (0, 0, ttype.Offset);
1216                                 if (inDelta) {
1217                                         // Replicate what .NET does when given a time which falls into the hour which is lost when
1218                                         // DST starts. isDST should be true but the offset should be the non-DST offset.
1219                                         isDst = transitions [i - 1].Value.IsDst;
1220                                 } else {
1221                                         isDst = ttype.IsDst;
1222                                 }
1223
1224                                 return true;
1225                         }
1226
1227                         return false;
1228                 }
1229
1230                 private static DateTime TransitionPoint (TransitionTime transition, int year)
1231                 {
1232                         if (transition.IsFixedDateRule)
1233                                 return new DateTime (year, transition.Month, transition.Day) + transition.TimeOfDay.TimeOfDay;
1234
1235                         DayOfWeek first = (new DateTime (year, transition.Month, 1)).DayOfWeek;
1236                         int day = 1 + (transition.Week - 1) * 7 + (transition.DayOfWeek - first + 7) % 7;
1237                         if (day >  DateTime.DaysInMonth (year, transition.Month))
1238                                 day -= 7;
1239                         if (day < 1)
1240                                 day += 7;
1241                         return new DateTime (year, transition.Month, day) + transition.TimeOfDay.TimeOfDay;
1242                 }
1243
1244                 static List<AdjustmentRule> ValidateRules (List<AdjustmentRule> adjustmentRules)
1245                 {
1246                         AdjustmentRule prev = null;
1247                         foreach (AdjustmentRule current in adjustmentRules.ToArray ()) {
1248                                 if (prev != null && prev.DateEnd > current.DateStart) {
1249                                         adjustmentRules.Remove (current);
1250                                 }
1251                                 prev = current;
1252                         }
1253                         return adjustmentRules;
1254                 }
1255
1256 #if LIBC || MONOTOUCH
1257                 const int BUFFER_SIZE = 16384; //Big enough for any tz file (on Oct 2008, all tz files are under 10k)
1258                 
1259                 private static TimeZoneInfo BuildFromStream (string id, Stream stream)
1260                 {
1261                         byte [] buffer = new byte [BUFFER_SIZE];
1262                         int length = stream.Read (buffer, 0, BUFFER_SIZE);
1263                         
1264                         if (!ValidTZFile (buffer, length))
1265                                 throw new InvalidTimeZoneException ("TZ file too big for the buffer");
1266
1267                         try {
1268                                 return ParseTZBuffer (id, buffer, length);
1269                         } catch (InvalidTimeZoneException) {
1270                                 throw;
1271                         } catch (Exception e) {
1272                                 throw new InvalidTimeZoneException ("Time zone information file contains invalid data", e);
1273                         }
1274                 }
1275
1276                 private static bool ValidTZFile (byte [] buffer, int length)
1277                 {
1278                         StringBuilder magic = new StringBuilder ();
1279
1280                         for (int i = 0; i < 4; i++)
1281                                 magic.Append ((char)buffer [i]);
1282                         
1283                         if (magic.ToString () != "TZif")
1284                                 return false;
1285
1286                         if (length >= BUFFER_SIZE)
1287                                 return false;
1288
1289                         return true;
1290                 }
1291
1292                 static int SwapInt32 (int i)
1293                 {
1294                         return (((i >> 24) & 0xff)
1295                                 | ((i >> 8) & 0xff00)
1296                                 | ((i << 8) & 0xff0000)
1297                                 | (((i & 0xff) << 24)));
1298                 }
1299
1300                 static int ReadBigEndianInt32 (byte [] buffer, int start)
1301                 {
1302                         int i = BitConverter.ToInt32 (buffer, start);
1303                         if (!BitConverter.IsLittleEndian)
1304                                 return i;
1305
1306                         return SwapInt32 (i);
1307                 }
1308
1309                 private static TimeZoneInfo ParseTZBuffer (string id, byte [] buffer, int length)
1310                 {
1311                         //Reading the header. 4 bytes for magic, 16 are reserved
1312                         int ttisgmtcnt = ReadBigEndianInt32 (buffer, 20);
1313                         int ttisstdcnt = ReadBigEndianInt32 (buffer, 24);
1314                         int leapcnt = ReadBigEndianInt32 (buffer, 28);
1315                         int timecnt = ReadBigEndianInt32 (buffer, 32);
1316                         int typecnt = ReadBigEndianInt32 (buffer, 36);
1317                         int charcnt = ReadBigEndianInt32 (buffer, 40);
1318
1319                         if (length < 44 + timecnt * 5 + typecnt * 6 + charcnt + leapcnt * 8 + ttisstdcnt + ttisgmtcnt)
1320                                 throw new InvalidTimeZoneException ();
1321
1322                         Dictionary<int, string> abbreviations = ParseAbbreviations (buffer, 44 + 4 * timecnt + timecnt + 6 * typecnt, charcnt);
1323                         Dictionary<int, TimeType> time_types = ParseTimesTypes (buffer, 44 + 4 * timecnt + timecnt, typecnt, abbreviations);
1324                         List<KeyValuePair<DateTime, TimeType>> transitions = ParseTransitions (buffer, 44, timecnt, time_types);
1325
1326                         if (time_types.Count == 0)
1327                                 throw new InvalidTimeZoneException ();
1328
1329                         if (time_types.Count == 1 && time_types[0].IsDst)
1330                                 throw new InvalidTimeZoneException ();
1331
1332                         TimeSpan baseUtcOffset = new TimeSpan (0);
1333                         TimeSpan dstDelta = new TimeSpan (0);
1334                         string standardDisplayName = null;
1335                         string daylightDisplayName = null;
1336                         bool dst_observed = false;
1337                         DateTime dst_start = DateTime.MinValue;
1338                         List<AdjustmentRule> adjustmentRules = new List<AdjustmentRule> ();
1339                         bool storeTransition = false;
1340
1341                         for (int i = 0; i < transitions.Count; i++) {
1342                                 var pair = transitions [i];
1343                                 DateTime ttime = pair.Key;
1344                                 TimeType ttype = pair.Value;
1345                                 if (!ttype.IsDst) {
1346                                         if (standardDisplayName != ttype.Name)
1347                                                 standardDisplayName = ttype.Name;
1348                                         if (baseUtcOffset.TotalSeconds != ttype.Offset) {
1349                                                 baseUtcOffset = new TimeSpan (0, 0, ttype.Offset);
1350                                                 if (adjustmentRules.Count > 0) // We ignore AdjustmentRules but store transitions.
1351                                                         storeTransition = true;
1352                                                 adjustmentRules = new List<AdjustmentRule> ();
1353                                                 dst_observed = false;
1354                                         }
1355                                         if (dst_observed) {
1356                                                 //FIXME: check additional fields for this:
1357                                                 //most of the transitions are expressed in GMT 
1358                                                 dst_start += baseUtcOffset;
1359                                                 DateTime dst_end = ttime + baseUtcOffset + dstDelta;
1360
1361                                                 //some weird timezone (America/Phoenix) have end dates on Jan 1st
1362                                                 if (dst_end.Date == new DateTime (dst_end.Year, 1, 1) && dst_end.Year > dst_start.Year)
1363                                                         dst_end -= new TimeSpan (24, 0, 0);
1364
1365                                                 /*
1366                                                  * AdjustmentRule specifies a DST period that starts and ends within a year.
1367                                                  * When we have a DST period longer than a year, the generated AdjustmentRule may not be usable.
1368                                                  * Thus we fallback to the transitions.
1369                                                  */
1370                                                 if (dst_start.AddYears (1) < dst_end)
1371                                                         storeTransition = true;
1372
1373                                                 DateTime dateStart, dateEnd;
1374                                                 if (dst_start.Month < 7)
1375                                                         dateStart = new DateTime (dst_start.Year, 1, 1);
1376                                                 else
1377                                                         dateStart = new DateTime (dst_start.Year, 7, 1);
1378
1379                                                 if (dst_end.Month >= 7)
1380                                                         dateEnd = new DateTime (dst_end.Year, 12, 31);
1381                                                 else
1382                                                         dateEnd = new DateTime (dst_end.Year, 6, 30);
1383
1384                                                 
1385                                                 TransitionTime transition_start = TransitionTime.CreateFixedDateRule (new DateTime (1, 1, 1) + dst_start.TimeOfDay, dst_start.Month, dst_start.Day);
1386                                                 TransitionTime transition_end = TransitionTime.CreateFixedDateRule (new DateTime (1, 1, 1) + dst_end.TimeOfDay, dst_end.Month, dst_end.Day);
1387                                                 if  (transition_start != transition_end) //y, that happened in Argentina in 1943-1946
1388                                                         adjustmentRules.Add (AdjustmentRule.CreateAdjustmentRule (dateStart, dateEnd, dstDelta, transition_start, transition_end));
1389                                         }
1390                                         dst_observed = false;
1391                                 } else {
1392                                         if (daylightDisplayName != ttype.Name)
1393                                                 daylightDisplayName = ttype.Name;
1394                                         if (dstDelta.TotalSeconds != ttype.Offset - baseUtcOffset.TotalSeconds) {
1395                                                 // Round to nearest minute, since it's not possible to create an adjustment rule
1396                                                 // with sub-minute precision ("The TimeSpan parameter cannot be specified more precisely than whole minutes.")
1397                                                 // This happens for instance with Europe/Dublin, which had an offset of 34 minutes and 39 seconds in 1916.
1398                                                 dstDelta = new TimeSpan (0, 0, ttype.Offset) - baseUtcOffset;
1399                                                 if (dstDelta.Ticks % TimeSpan.TicksPerMinute != 0)
1400                                                         dstDelta = TimeSpan.FromMinutes ((long) (dstDelta.TotalMinutes + 0.5f));
1401                                         }
1402
1403                                         dst_start = ttime;
1404                                         dst_observed = true;
1405                                 }
1406                         }
1407
1408                         TimeZoneInfo tz;
1409                         if (adjustmentRules.Count == 0 && !storeTransition) {
1410                                 if (standardDisplayName == null) {
1411                                         var t = time_types [0];
1412                                         standardDisplayName = t.Name;
1413                                         baseUtcOffset = new TimeSpan (0, 0, t.Offset);
1414                                 }
1415                                 tz = CreateCustomTimeZone (id, baseUtcOffset, id, standardDisplayName);
1416                         } else {
1417                                 tz = CreateCustomTimeZone (id, baseUtcOffset, id, standardDisplayName, daylightDisplayName, ValidateRules (adjustmentRules).ToArray ());
1418                         }
1419
1420                         if (storeTransition && transitions.Count > 0) {
1421                                 tz.transitions = transitions;
1422                                 tz.supportsDaylightSavingTime = true;
1423                         }
1424
1425                         return tz;
1426                 }
1427
1428                 static Dictionary<int, string> ParseAbbreviations (byte [] buffer, int index, int count)
1429                 {
1430                         var abbrevs = new Dictionary<int, string> ();
1431                         int abbrev_index = 0;
1432                         var sb = new StringBuilder ();
1433                         for (int i = 0; i < count; i++) {
1434                                 char c = (char) buffer [index + i];
1435                                 if (c != '\0')
1436                                         sb.Append (c);
1437                                 else {
1438                                         abbrevs.Add (abbrev_index, sb.ToString ());
1439                                         //Adding all the substrings too, as it seems to be used, at least for Africa/Windhoek
1440                                         //j == sb.Length empty substring also needs to be added #31432
1441                                         for (int j = 1; j <= sb.Length; j++)
1442                                                 abbrevs.Add (abbrev_index + j, sb.ToString (j, sb.Length - j));
1443                                         abbrev_index = i + 1;
1444                                         sb = new StringBuilder ();
1445                                 }
1446                         }
1447                         return abbrevs;
1448                 }
1449
1450                 static Dictionary<int, TimeType> ParseTimesTypes (byte [] buffer, int index, int count, Dictionary<int, string> abbreviations)
1451                 {
1452                         var types = new Dictionary<int, TimeType> (count);
1453                         for (int i = 0; i < count; i++) {
1454                                 int offset = ReadBigEndianInt32 (buffer, index + 6 * i);
1455
1456                                 //
1457                                 // The official tz database contains timezone with GMT offsets
1458                                 // not only in whole hours/minutes but in seconds. This happens for years
1459                                 // before 1901. For example
1460                                 //
1461                                 // NAME                 GMTOFF   RULES  FORMAT  UNTIL
1462                                 // Europe/Madrid        -0:14:44 -      LMT     1901 Jan  1  0:00s
1463                                 //
1464                                 // .NET as of 4.6.2 cannot handle that and uses hours/minutes only, so
1465                                 // we remove seconds to not crash later
1466                                 //
1467                                 offset = (offset / 60) * 60;
1468
1469                                 byte is_dst = buffer [index + 6 * i + 4];
1470                                 byte abbrev = buffer [index + 6 * i + 5];
1471                                 types.Add (i, new TimeType (offset, (is_dst != 0), abbreviations [(int)abbrev]));
1472                         }
1473                         return types;
1474                 }
1475
1476                 static List<KeyValuePair<DateTime, TimeType>> ParseTransitions (byte [] buffer, int index, int count, Dictionary<int, TimeType> time_types)
1477                 {
1478                         var list = new List<KeyValuePair<DateTime, TimeType>> (count);
1479                         for (int i = 0; i < count; i++) {
1480                                 int unixtime = ReadBigEndianInt32 (buffer, index + 4 * i);
1481                                 DateTime ttime = DateTimeFromUnixTime (unixtime);
1482                                 byte ttype = buffer [index + 4 * count + i];
1483                                 list.Add (new KeyValuePair<DateTime, TimeType> (ttime, time_types [(int)ttype]));
1484                         }
1485                         return list;
1486                 }
1487
1488                 static DateTime DateTimeFromUnixTime (long unix_time)
1489                 {
1490                         DateTime date_time = new DateTime (1970, 1, 1);
1491                         return date_time.AddSeconds (unix_time);
1492                 }
1493
1494 #region reference sources
1495                 // Shortcut for TimeZoneInfo.Local.GetUtcOffset
1496                 internal static TimeSpan GetLocalUtcOffset(DateTime dateTime, TimeZoneInfoOptions flags)
1497                 {
1498                         bool dst;
1499                         return Local.GetUtcOffset (dateTime, out dst);
1500                 }
1501
1502                 internal TimeSpan GetUtcOffset(DateTime dateTime, TimeZoneInfoOptions flags)
1503                 {
1504                         bool dst;
1505                         return GetUtcOffset (dateTime, out dst);
1506                 }
1507
1508                 static internal TimeSpan GetUtcOffsetFromUtc (DateTime time, TimeZoneInfo zone, out Boolean isDaylightSavings, out Boolean isAmbiguousLocalDst)
1509                 {
1510                         isDaylightSavings = false;
1511                         isAmbiguousLocalDst = false;
1512                         TimeSpan baseOffset = zone.BaseUtcOffset;
1513
1514                         if (zone.IsAmbiguousTime (time)) {
1515                                 isAmbiguousLocalDst = true;
1516                                 return baseOffset;
1517                         }
1518
1519                         return zone.GetUtcOffset (time, out isDaylightSavings);
1520                 }
1521 #endregion
1522         }
1523
1524         class TimeType {
1525                 public readonly int Offset;
1526                 public readonly bool IsDst;
1527                 public string Name;
1528
1529                 public TimeType (int offset, bool is_dst, string abbrev)
1530                 {
1531                         this.Offset = offset;
1532                         this.IsDst = is_dst;
1533                         this.Name = abbrev;
1534                 }
1535
1536                 public override string ToString ()
1537                 {
1538                         return "offset: " + Offset + "s, is_dst: " + IsDst + ", zone name: " + Name;
1539                 }
1540 #else
1541         }
1542 #endif
1543         }
1544 }