[mono-api-html] Print string fields with no (or null) value without an NRE.
[mono.git] / mcs / class / System.Core / 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
34 #if !INSIDE_CORLIB && NET_4_0
35
36 [assembly:TypeForwardedTo (typeof(TimeZoneInfo))]
37
38 #elif (INSIDE_CORLIB && NET_4_0) || (!INSIDE_CORLIB && (NET_3_5 && !NET_4_0 && !MOBILE))
39
40 using System.Collections.Generic;
41 using System.Collections.ObjectModel;
42 using System.Runtime.Serialization;
43 using System.Text;
44
45 #if LIBC || MONODROID
46 using System.IO;
47 using Mono;
48 #endif
49
50 using Microsoft.Win32;
51
52 namespace System
53 {
54 #if MOBILE
55         [TypeForwardedFrom (Consts.AssemblySystem_Core)]
56 #elif NET_4_0
57         [TypeForwardedFrom (Consts.AssemblySystemCore_3_5)]
58 #endif
59         [SerializableAttribute]
60         public sealed partial class TimeZoneInfo : IEquatable<TimeZoneInfo>, ISerializable, IDeserializationCallback
61         {
62                 TimeSpan baseUtcOffset;
63                 public TimeSpan BaseUtcOffset {
64                         get { return baseUtcOffset; }
65                 }
66
67                 string daylightDisplayName;
68                 public string DaylightName {
69                         get { 
70                                 return supportsDaylightSavingTime
71                                         ? daylightDisplayName
72                                         : string.Empty;
73                         }
74                 }
75
76                 string displayName;
77                 public string DisplayName {
78                         get { return displayName; }
79                 }
80
81                 string id;
82                 public string Id {
83                         get { return id; }
84                 }
85
86                 static TimeZoneInfo local;
87                 public static TimeZoneInfo Local {
88                         get { 
89                                 var l = local;
90                                 if (l == null) {
91                                         l = CreateLocal ();
92                                         if (l == null)
93                                                 throw new TimeZoneNotFoundException ();
94
95                                         if (Interlocked.CompareExchange (ref local, l, null) != null)
96                                                 l = local;
97                                 }
98
99                                 return l;
100                         }
101                 }
102
103                 static TimeZoneInfo CreateLocal ()
104                 {
105 #if MONODROID
106                         return AndroidTimeZones.Default;
107 #elif MONOTOUCH
108                         using (Stream stream = GetMonoTouchData (null)) {
109                                 return BuildFromStream ("Local", stream);
110                         }
111 #elif LIBC
112                         try {
113                                 return FindSystemTimeZoneByFileName ("Local", "/etc/localtime");        
114                         } catch {
115                                 try {
116                                         return FindSystemTimeZoneByFileName ("Local", Path.Combine (TimeZoneDirectory, "localtime"));   
117                                 } catch {
118                                         return null;
119                                 }
120                         }
121 #else
122                         if (IsWindows && LocalZoneKey != null) {
123                                 string name = (string)LocalZoneKey.GetValue ("TimeZoneKeyName");
124                                 name = TrimSpecial (name);
125                                 if (name != null)
126                                         return TimeZoneInfo.FindSystemTimeZoneById (name);
127                         }
128
129                         return null;
130 #endif
131                 }
132
133                 string standardDisplayName;
134                 public string StandardName {
135                         get { return standardDisplayName; }
136                 }
137
138                 bool supportsDaylightSavingTime;
139                 public bool SupportsDaylightSavingTime {
140                         get  { return supportsDaylightSavingTime; }
141                 }
142
143                 static TimeZoneInfo utc;
144                 public static TimeZoneInfo Utc {
145                         get {
146                                 if (utc == null)
147                                         utc = CreateCustomTimeZone ("UTC", new TimeSpan (0), "UTC", "UTC");
148                                 return utc;
149                         }
150                 }
151 #if LIBC
152                 static string timeZoneDirectory;
153                 static string TimeZoneDirectory {
154                         get {
155                                 if (timeZoneDirectory == null)
156                                         timeZoneDirectory = "/usr/share/zoneinfo";
157                                 return timeZoneDirectory;
158                         }
159                         set {
160                                 ClearCachedData ();
161                                 timeZoneDirectory = value;
162                         }
163                 }
164 #endif
165                 private AdjustmentRule [] adjustmentRules;
166
167 #if !NET_2_1
168                 /// <summary>
169                 /// Determine whether windows of not (taken Stephane Delcroix's code)
170                 /// </summary>
171                 private static bool IsWindows
172                 {
173                         get {
174                                 int platform = (int) Environment.OSVersion.Platform;
175                                 return ((platform != 4) && (platform != 6) && (platform != 128));
176                         }
177                 }
178                 
179                 /// <summary>
180                 /// Needed to trim misc garbage in MS registry keys
181                 /// </summary>
182                 private static string TrimSpecial (string str)
183                 {
184                         var Istart = 0;
185                         while (Istart < str.Length && !char.IsLetterOrDigit(str[Istart])) Istart++;
186                         var Iend = str.Length - 1;
187                         while (Iend > Istart && !char.IsLetterOrDigit(str[Iend])) Iend--;
188                         
189                         return str.Substring (Istart, Iend-Istart+1);
190                 }
191                 
192                 static RegistryKey timeZoneKey;
193                 static RegistryKey TimeZoneKey {
194                         get {
195                                 if (timeZoneKey != null)
196                                         return timeZoneKey;
197                                 if (!IsWindows)
198                                         return null;
199                                 
200                                 return timeZoneKey = Registry.LocalMachine.OpenSubKey (
201                                         "SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Time Zones",
202                                         false);
203                         }
204                 }
205                 
206                 static RegistryKey localZoneKey;
207                 static RegistryKey LocalZoneKey {
208                         get {
209                                 if (localZoneKey != null)
210                                         return localZoneKey;
211                                 
212                                 if (!IsWindows)
213                                         return null;
214                                 
215                                 return localZoneKey = Registry.LocalMachine.OpenSubKey (
216                                         "SYSTEM\\CurrentControlSet\\Control\\TimeZoneInformation", false);
217                         }
218                 }
219 #endif
220
221                 public static void ClearCachedData ()
222                 {
223                         local = null;
224                         utc = null;
225                         systemTimeZones = null;
226                 }
227
228                 public static DateTime ConvertTime (DateTime dateTime, TimeZoneInfo destinationTimeZone)
229                 {
230                         return ConvertTime (dateTime, TimeZoneInfo.Local, destinationTimeZone);
231                 }
232
233                 public static DateTime ConvertTime (DateTime dateTime, TimeZoneInfo sourceTimeZone, TimeZoneInfo destinationTimeZone)
234                 {
235                         if (dateTime.Kind == DateTimeKind.Local && sourceTimeZone != TimeZoneInfo.Local)
236                                 throw new ArgumentException ("Kind property of dateTime is Local but the sourceTimeZone does not equal TimeZoneInfo.Local");
237
238                         if (dateTime.Kind == DateTimeKind.Utc && sourceTimeZone != TimeZoneInfo.Utc)
239                                 throw new ArgumentException ("Kind property of dateTime is Utc but the sourceTimeZone does not equal TimeZoneInfo.Utc");
240
241                         if (sourceTimeZone.IsInvalidTime (dateTime))
242                                 throw new ArgumentException ("dateTime parameter is an invalid time");
243
244                         if (sourceTimeZone == null)
245                                 throw new ArgumentNullException ("sourceTimeZone");
246
247                         if (destinationTimeZone == null)
248                                 throw new ArgumentNullException ("destinationTimeZone");
249
250                         if (dateTime.Kind == DateTimeKind.Local && sourceTimeZone == TimeZoneInfo.Local && destinationTimeZone == TimeZoneInfo.Local)
251                                 return dateTime;
252
253                         DateTime utc = ConvertTimeToUtc (dateTime);
254
255                         if (destinationTimeZone == TimeZoneInfo.Utc)
256                                 return utc;
257
258                         return ConvertTimeFromUtc (utc, destinationTimeZone);   
259
260                 }
261
262                 public static DateTimeOffset ConvertTime(DateTimeOffset dateTimeOffset, TimeZoneInfo destinationTimeZone) 
263                 {
264                         if (destinationTimeZone == null) 
265                                 throw new ArgumentNullException("destinationTimeZone");
266                 
267                         var utcDateTime = dateTimeOffset.UtcDateTime;
268                         AdjustmentRule rule = destinationTimeZone.GetApplicableRule (utcDateTime);
269                 
270                         if (rule != null && destinationTimeZone.IsDaylightSavingTime(utcDateTime)) {
271                                 var offset = destinationTimeZone.BaseUtcOffset + rule.DaylightDelta;
272                                 return new DateTimeOffset(DateTime.SpecifyKind(utcDateTime, DateTimeKind.Unspecified) + offset, offset);
273                         }
274                         else {
275                                 return new DateTimeOffset(DateTime.SpecifyKind(utcDateTime, DateTimeKind.Unspecified) + destinationTimeZone.BaseUtcOffset, destinationTimeZone.BaseUtcOffset);
276                         }
277                 }
278
279                 public static DateTime ConvertTimeBySystemTimeZoneId (DateTime dateTime, string destinationTimeZoneId)
280                 {
281                         return ConvertTime (dateTime, FindSystemTimeZoneById (destinationTimeZoneId));
282                 }
283
284                 public static DateTime ConvertTimeBySystemTimeZoneId (DateTime dateTime, string sourceTimeZoneId, string destinationTimeZoneId)
285                 {
286                         return ConvertTime (dateTime, FindSystemTimeZoneById (sourceTimeZoneId), FindSystemTimeZoneById (destinationTimeZoneId));
287                 }
288
289                 public static DateTimeOffset ConvertTimeBySystemTimeZoneId (DateTimeOffset dateTimeOffset, string destinationTimeZoneId)
290                 {
291                         return ConvertTime (dateTimeOffset, FindSystemTimeZoneById (destinationTimeZoneId));
292                 }
293
294                 private DateTime ConvertTimeFromUtc (DateTime dateTime)
295                 {
296                         if (dateTime.Kind == DateTimeKind.Local)
297                                 throw new ArgumentException ("Kind property of dateTime is Local");
298
299                         if (this == TimeZoneInfo.Utc)
300                                 return DateTime.SpecifyKind (dateTime, DateTimeKind.Utc);
301                         
302                         //FIXME: do not rely on DateTime implementation !
303                         if (this == TimeZoneInfo.Local) 
304                         {
305 #if NET_4_0
306                                 return dateTime.ToLocalTime ();
307 #else
308                                 return DateTime.SpecifyKind (dateTime.ToLocalTime (), DateTimeKind.Unspecified);
309 #endif
310                         }
311
312
313                         AdjustmentRule rule = GetApplicableRule (dateTime);
314                 
315                         if (rule != null && IsDaylightSavingTime (DateTime.SpecifyKind (dateTime, DateTimeKind.Utc)))
316                                 return DateTime.SpecifyKind (dateTime + BaseUtcOffset + rule.DaylightDelta , DateTimeKind.Unspecified);
317                         else
318                                 return DateTime.SpecifyKind (dateTime + BaseUtcOffset, DateTimeKind.Unspecified);
319                 }
320
321                 public static DateTime ConvertTimeFromUtc (DateTime dateTime, TimeZoneInfo destinationTimeZone)
322                 {
323                         if (destinationTimeZone == null)
324                                 throw new ArgumentNullException ("destinationTimeZone");
325
326                         return destinationTimeZone.ConvertTimeFromUtc (dateTime);
327                 }
328
329                 public static DateTime ConvertTimeToUtc (DateTime dateTime)
330                 {
331                         if (dateTime.Kind == DateTimeKind.Utc)
332                                 return dateTime;
333
334                         //FIXME: do not rely on DateTime implementation !
335                         return DateTime.SpecifyKind (dateTime.ToUniversalTime (), DateTimeKind.Utc);
336                 }
337
338                 public static DateTime ConvertTimeToUtc (DateTime dateTime, TimeZoneInfo sourceTimeZone)
339                 {
340                         if (sourceTimeZone == null)
341                                 throw new ArgumentNullException ("sourceTimeZone");
342
343                         if (dateTime.Kind == DateTimeKind.Utc && sourceTimeZone != TimeZoneInfo.Utc)
344                                 throw new ArgumentException ("Kind property of dateTime is Utc but the sourceTimeZone does not equal TimeZoneInfo.Utc");
345
346                         if (dateTime.Kind == DateTimeKind.Local && sourceTimeZone != TimeZoneInfo.Local)
347                                 throw new ArgumentException ("Kind property of dateTime is Local but the sourceTimeZone does not equal TimeZoneInfo.Local");
348
349                         if (sourceTimeZone.IsInvalidTime (dateTime))
350                                 throw new ArgumentException ("dateTime parameter is an invalid time");
351
352                         if (dateTime.Kind == DateTimeKind.Utc && sourceTimeZone == TimeZoneInfo.Utc)
353                                 return dateTime;
354
355                         if (dateTime.Kind == DateTimeKind.Utc)
356                                 return dateTime;
357
358                         if (dateTime.Kind == DateTimeKind.Local)
359                                 return ConvertTimeToUtc (dateTime);
360
361                         if (sourceTimeZone.IsAmbiguousTime (dateTime) || !sourceTimeZone.IsDaylightSavingTime (dateTime))
362                                 return DateTime.SpecifyKind (dateTime - sourceTimeZone.BaseUtcOffset, DateTimeKind.Utc);
363                         else {
364                                 AdjustmentRule rule = sourceTimeZone.GetApplicableRule (dateTime);
365                                 if (rule != null)
366                                         return DateTime.SpecifyKind (dateTime - sourceTimeZone.BaseUtcOffset - rule.DaylightDelta, DateTimeKind.Utc);
367                                 else
368                                         return DateTime.SpecifyKind (dateTime - sourceTimeZone.BaseUtcOffset, DateTimeKind.Utc);
369                         }
370                 }
371
372                 public static TimeZoneInfo CreateCustomTimeZone (string id, TimeSpan baseUtcOffset, string displayName, string standardDisplayName) 
373                 {
374                         return CreateCustomTimeZone (id, baseUtcOffset, displayName, standardDisplayName, null, null, true);
375                 }
376
377                 public static TimeZoneInfo CreateCustomTimeZone (string id, TimeSpan baseUtcOffset, string displayName, string standardDisplayName, string daylightDisplayName, TimeZoneInfo.AdjustmentRule [] adjustmentRules)
378                 {
379                         return CreateCustomTimeZone (id, baseUtcOffset, displayName, standardDisplayName, daylightDisplayName, adjustmentRules, false);
380                 }
381
382                 public static TimeZoneInfo CreateCustomTimeZone ( string id, TimeSpan baseUtcOffset, string displayName, string standardDisplayName, string daylightDisplayName, TimeZoneInfo.AdjustmentRule [] adjustmentRules, bool disableDaylightSavingTime)
383                 {
384                         return new TimeZoneInfo (id, baseUtcOffset, displayName, standardDisplayName, daylightDisplayName, adjustmentRules, disableDaylightSavingTime);
385                 }
386
387 #if NET_4_5
388                 public override bool Equals (object obj)
389                 {
390                         return Equals (obj as TimeZoneInfo);
391                 }
392 #endif
393
394                 public bool Equals (TimeZoneInfo other)
395                 {
396                         if (other == null)
397                                 return false;
398
399                         return other.Id == this.Id && HasSameRules (other);
400                 }
401
402                 public static TimeZoneInfo FindSystemTimeZoneById (string id)
403                 {
404                         //FIXME: this method should check for cached values in systemTimeZones
405                         if (id == null)
406                                 throw new ArgumentNullException ("id");
407 #if !NET_2_1
408                         if (TimeZoneKey != null)
409                         {
410                                 RegistryKey key = TimeZoneKey.OpenSubKey (id, false);
411                                 if (key == null)
412                                         throw new TimeZoneNotFoundException ();
413                                 return FromRegistryKey(id, key);
414                         }
415 #endif
416 #if MONODROID
417                         var timeZoneInfo = AndroidTimeZones.GetTimeZone (id);
418                         if (timeZoneInfo == null)
419                                 throw new TimeZoneNotFoundException ();
420                         return timeZoneInfo;
421 #else
422                         // Local requires special logic that already exists in the Local property (bug #326)
423                         if (id == "Local")
424                                 return Local;
425 #if MONOTOUCH
426                         using (Stream stream = GetMonoTouchData (id)) {
427                                 return BuildFromStream (id, stream);
428                         }
429 #elif LIBC
430                         string filepath = Path.Combine (TimeZoneDirectory, id);
431                         return FindSystemTimeZoneByFileName (id, filepath);
432 #else
433                         throw new NotImplementedException ();
434 #endif
435 #endif
436                 }
437
438 #if LIBC
439                 private static TimeZoneInfo FindSystemTimeZoneByFileName (string id, string filepath)
440                 {
441                         if (!File.Exists (filepath))
442                                 throw new TimeZoneNotFoundException ();
443
444                         using (FileStream stream = File.OpenRead (filepath)) {
445                                 return BuildFromStream (id, stream);
446                         }
447                 }
448 #endif
449 #if LIBC || MONOTOUCH
450                 const int BUFFER_SIZE = 16384; //Big enough for any tz file (on Oct 2008, all tz files are under 10k)
451                 
452                 private static TimeZoneInfo BuildFromStream (string id, Stream stream) 
453                 {
454                         byte [] buffer = new byte [BUFFER_SIZE];
455                         int length = stream.Read (buffer, 0, BUFFER_SIZE);
456                         
457                         if (!ValidTZFile (buffer, length))
458                                 throw new InvalidTimeZoneException ("TZ file too big for the buffer");
459
460                         try {
461                                 return ParseTZBuffer (id, buffer, length);
462                         } catch (Exception e) {
463                                 throw new InvalidTimeZoneException (e.Message);
464                         }
465                 }
466 #endif
467
468 #if !NET_2_1
469                 private static TimeZoneInfo FromRegistryKey (string id, RegistryKey key)
470                 {
471                         byte [] reg_tzi = (byte []) key.GetValue ("TZI");
472
473                         if (reg_tzi == null)
474                                 throw new InvalidTimeZoneException ();
475
476                         int bias = BitConverter.ToInt32 (reg_tzi, 0);
477                         TimeSpan baseUtcOffset = new TimeSpan (0, -bias, 0);
478
479                         string display_name = (string) key.GetValue ("Display");
480                         string standard_name = (string) key.GetValue ("Std");
481                         string daylight_name = (string) key.GetValue ("Dlt");
482
483                         List<AdjustmentRule> adjustmentRules = new List<AdjustmentRule> ();
484
485                         RegistryKey dst_key = key.OpenSubKey ("Dynamic DST", false);
486                         if (dst_key != null) {
487                                 int first_year = (int) dst_key.GetValue ("FirstEntry");
488                                 int last_year = (int) dst_key.GetValue ("LastEntry");
489                                 int year;
490
491                                 for (year=first_year; year<=last_year; year++) {
492                                         byte [] dst_tzi = (byte []) dst_key.GetValue (year.ToString ());
493                                         if (dst_tzi != null) {
494                                                 int start_year = year == first_year ? 1 : year;
495                                                 int end_year = year == last_year ? 9999 : year;
496                                                 ParseRegTzi(adjustmentRules, start_year, end_year, dst_tzi);
497                                         }
498                                 }
499                         }
500                         else
501                                 ParseRegTzi(adjustmentRules, 1, 9999, reg_tzi);
502
503                         return CreateCustomTimeZone (id, baseUtcOffset, display_name, standard_name, daylight_name, ValidateRules (adjustmentRules).ToArray ());
504                 }
505
506                 private static void ParseRegTzi (List<AdjustmentRule> adjustmentRules, int start_year, int end_year, byte [] buffer)
507                 {
508                         //int standard_bias = BitConverter.ToInt32 (buffer, 4); /* not sure how to handle this */
509                         int daylight_bias = BitConverter.ToInt32 (buffer, 8);
510
511                         int standard_year = BitConverter.ToInt16 (buffer, 12);
512                         int standard_month = BitConverter.ToInt16 (buffer, 14);
513                         int standard_dayofweek = BitConverter.ToInt16 (buffer, 16);
514                         int standard_day = BitConverter.ToInt16 (buffer, 18);
515                         int standard_hour = BitConverter.ToInt16 (buffer, 20);
516                         int standard_minute = BitConverter.ToInt16 (buffer, 22);
517                         int standard_second = BitConverter.ToInt16 (buffer, 24);
518                         int standard_millisecond = BitConverter.ToInt16 (buffer, 26);
519
520                         int daylight_year = BitConverter.ToInt16 (buffer, 28);
521                         int daylight_month = BitConverter.ToInt16 (buffer, 30);
522                         int daylight_dayofweek = BitConverter.ToInt16 (buffer, 32);
523                         int daylight_day = BitConverter.ToInt16 (buffer, 34);
524                         int daylight_hour = BitConverter.ToInt16 (buffer, 36);
525                         int daylight_minute = BitConverter.ToInt16 (buffer, 38);
526                         int daylight_second = BitConverter.ToInt16 (buffer, 40);
527                         int daylight_millisecond = BitConverter.ToInt16 (buffer, 42);
528
529                         if (standard_month == 0 || daylight_month == 0)
530                                 return;
531
532                         DateTime start_date;
533                         DateTime start_timeofday = new DateTime (1, 1, 1, daylight_hour, daylight_minute, daylight_second, daylight_millisecond);
534                         TransitionTime start_transition_time;
535
536                         if (daylight_year == 0) {
537                                 start_date = new DateTime (start_year, 1, 1);
538                                 start_transition_time = TransitionTime.CreateFloatingDateRule (
539                                         start_timeofday, daylight_month, daylight_day,
540                                         (DayOfWeek) daylight_dayofweek);
541                         }
542                         else {
543                                 start_date = new DateTime (daylight_year, daylight_month, daylight_day,
544                                         daylight_hour, daylight_minute, daylight_second, daylight_millisecond);
545                                 start_transition_time = TransitionTime.CreateFixedDateRule (
546                                         start_timeofday, daylight_month, daylight_day);
547                         }
548
549                         DateTime end_date;
550                         DateTime end_timeofday = new DateTime (1, 1, 1, standard_hour, standard_minute, standard_second, standard_millisecond);
551                         TransitionTime end_transition_time;
552
553                         if (standard_year == 0) {
554                                 end_date = new DateTime (end_year, 12, 31);
555                                 end_transition_time = TransitionTime.CreateFloatingDateRule (
556                                         end_timeofday, standard_month, standard_day,
557                                         (DayOfWeek) standard_dayofweek);
558                         }
559                         else {
560                                 end_date = new DateTime (standard_year, standard_month, standard_day,
561                                         standard_hour, standard_minute, standard_second, standard_millisecond);
562                                 end_transition_time = TransitionTime.CreateFixedDateRule (
563                                         end_timeofday, standard_month, standard_day);
564                         }
565
566                         TimeSpan daylight_delta = new TimeSpan(0, -daylight_bias, 0);
567
568                         adjustmentRules.Add (AdjustmentRule.CreateAdjustmentRule (
569                                 start_date, end_date, daylight_delta,
570                                 start_transition_time, end_transition_time));
571                 }
572 #endif
573
574                 public static TimeZoneInfo FromSerializedString (string source)
575                 {
576                         throw new NotImplementedException ();
577                 }
578
579                 public AdjustmentRule [] GetAdjustmentRules ()
580                 {
581                         if (!supportsDaylightSavingTime)
582                                 return new AdjustmentRule [0];
583                         else
584                                 return (AdjustmentRule []) adjustmentRules.Clone ();
585                 }
586
587                 public TimeSpan [] GetAmbiguousTimeOffsets (DateTime dateTime)
588                 {
589                         if (!IsAmbiguousTime (dateTime))
590                                 throw new ArgumentException ("dateTime is not an ambiguous time");
591
592                         AdjustmentRule rule = GetApplicableRule (dateTime);
593                         if (rule != null)
594                                 return new TimeSpan[] {baseUtcOffset, baseUtcOffset + rule.DaylightDelta};
595                         else
596                                 return new TimeSpan[] {baseUtcOffset, baseUtcOffset};
597                 }
598
599                 public TimeSpan [] GetAmbiguousTimeOffsets (DateTimeOffset dateTimeOffset)
600                 {
601                         if (!IsAmbiguousTime (dateTimeOffset))
602                                 throw new ArgumentException ("dateTimeOffset is not an ambiguous time");
603
604                         throw new NotImplementedException ();
605                 }
606
607                 public override int GetHashCode ()
608                 {
609                         int hash_code = Id.GetHashCode ();
610                         foreach (AdjustmentRule rule in GetAdjustmentRules ())
611                                 hash_code ^= rule.GetHashCode ();
612                         return hash_code;
613                 }
614
615 #if NET_4_0
616                 void ISerializable.GetObjectData (SerializationInfo info, StreamingContext context)
617 #else
618                 public void GetObjectData (SerializationInfo info, StreamingContext context)
619 #endif
620                 {
621                         if (info == null)
622                                 throw new ArgumentNullException ("info");
623                         info.AddValue ("Id", id);
624                         info.AddValue ("DisplayName", displayName);
625                         info.AddValue ("StandardName", standardDisplayName);
626                         info.AddValue ("DaylightName", daylightDisplayName);
627                         info.AddValue ("BaseUtcOffset", baseUtcOffset);
628                         info.AddValue ("AdjustmentRules", adjustmentRules);
629                         info.AddValue ("SupportsDaylightSavingTime", SupportsDaylightSavingTime);
630                 }
631
632                 //FIXME: change this to a generic Dictionary and allow caching for FindSystemTimeZoneById
633                 private static List<TimeZoneInfo> systemTimeZones;
634                 public static ReadOnlyCollection<TimeZoneInfo> GetSystemTimeZones ()
635                 {
636                         if (systemTimeZones == null) {
637                                 systemTimeZones = new List<TimeZoneInfo> ();
638 #if !NET_2_1
639                                 if (TimeZoneKey != null) {
640                                         foreach (string id in TimeZoneKey.GetSubKeyNames ()) {
641                                                 try {
642                                                         systemTimeZones.Add (FindSystemTimeZoneById (id));
643                                                 } catch {}
644                                         }
645
646                                         return new ReadOnlyCollection<TimeZoneInfo> (systemTimeZones);
647                                 }
648 #endif
649 #if MONODROID
650                         foreach (string id in AndroidTimeZones.GetAvailableIds ()) {
651                                 var tz = AndroidTimeZones.GetTimeZone (id);
652                                 if (tz != null)
653                                         systemTimeZones.Add (tz);
654                         }
655 #elif MONOTOUCH
656                                 if (systemTimeZones.Count == 0) {
657                                         foreach (string name in GetMonoTouchNames ()) {
658                                                 using (Stream stream = GetMonoTouchData (name)) {
659                                                         systemTimeZones.Add (BuildFromStream (name, stream));
660                                                 }
661                                         }
662                                 }
663 #elif LIBC
664                                 string[] continents = new string [] {"Africa", "America", "Antarctica", "Arctic", "Asia", "Atlantic", "Brazil", "Canada", "Chile", "Europe", "Indian", "Mexico", "Mideast", "Pacific", "US"};
665                                 foreach (string continent in continents) {
666                                         try {
667                                                 foreach (string zonepath in Directory.GetFiles (Path.Combine (TimeZoneDirectory, continent))) {
668                                                         try {
669                                                                 string id = String.Format ("{0}/{1}", continent, Path.GetFileName (zonepath));
670                                                                 systemTimeZones.Add (FindSystemTimeZoneById (id));
671                                                         } catch (ArgumentNullException) {
672                                                         } catch (TimeZoneNotFoundException) {
673                                                         } catch (InvalidTimeZoneException) {
674                                                         } catch (Exception) {
675                                                                 throw;
676                                                         }
677                                                 }
678                                         } catch {}
679                                 }
680 #else
681                                 throw new NotImplementedException ("This method is not implemented for this platform");
682 #endif
683                         }
684                         return new ReadOnlyCollection<TimeZoneInfo> (systemTimeZones);
685                 }
686
687                 public TimeSpan GetUtcOffset (DateTime dateTime)
688                 {
689                         if (IsDaylightSavingTime (dateTime)) {
690                                 AdjustmentRule rule = GetApplicableRule (dateTime);
691                                 if (rule != null)
692                                         return BaseUtcOffset + rule.DaylightDelta;
693                         }
694                         
695                         return BaseUtcOffset;
696                 }
697
698                 public TimeSpan GetUtcOffset (DateTimeOffset dateTimeOffset)
699                 {
700                         throw new NotImplementedException ();
701                 }
702
703                 public bool HasSameRules (TimeZoneInfo other)
704                 {
705                         if (other == null)
706                                 throw new ArgumentNullException ("other");
707
708                         if ((this.adjustmentRules == null) != (other.adjustmentRules == null))
709                                 return false;
710
711                         if (this.adjustmentRules == null)
712                                 return true;
713
714                         if (this.BaseUtcOffset != other.BaseUtcOffset)
715                                 return false;
716
717                         if (this.adjustmentRules.Length != other.adjustmentRules.Length)
718                                 return false;
719
720                         for (int i = 0; i < adjustmentRules.Length; i++) {
721                                 if (! (this.adjustmentRules [i]).Equals (other.adjustmentRules [i]))
722                                         return false;
723                         }
724                         
725                         return true;
726                 }
727
728                 public bool IsAmbiguousTime (DateTime dateTime)
729                 {
730                         if (dateTime.Kind == DateTimeKind.Local && IsInvalidTime (dateTime))
731                                 throw new ArgumentException ("Kind is Local and time is Invalid");
732
733                         if (this == TimeZoneInfo.Utc)
734                                 return false;
735                         
736                         if (dateTime.Kind == DateTimeKind.Utc)
737                                 dateTime = ConvertTimeFromUtc (dateTime);
738
739                         if (dateTime.Kind == DateTimeKind.Local && this != TimeZoneInfo.Local)
740                                 dateTime = ConvertTime (dateTime, TimeZoneInfo.Local, this);
741
742                         AdjustmentRule rule = GetApplicableRule (dateTime);
743                         if (rule != null) {
744                                 DateTime tpoint = TransitionPoint (rule.DaylightTransitionEnd, dateTime.Year);
745                                 if (dateTime > tpoint - rule.DaylightDelta  && dateTime <= tpoint)
746                                         return true;
747                         }
748                                 
749                         return false;
750                 }
751
752                 public bool IsAmbiguousTime (DateTimeOffset dateTimeOffset)
753                 {
754                         throw new NotImplementedException ();
755                 }
756
757                 public bool IsDaylightSavingTime (DateTime dateTime)
758                 {
759                         if (dateTime.Kind == DateTimeKind.Local && IsInvalidTime (dateTime))
760                                 throw new ArgumentException ("dateTime is invalid and Kind is Local");
761
762                         if (this == TimeZoneInfo.Utc)
763                                 return false;
764
765                         if (!SupportsDaylightSavingTime)
766                                 return false;
767                         //FIXME: do not rely on DateTime implementation !
768                         if ((dateTime.Kind == DateTimeKind.Local || dateTime.Kind == DateTimeKind.Unspecified) && this == TimeZoneInfo.Local)
769                                 return dateTime.IsDaylightSavingTime ();
770
771                         //FIXME: do not rely on DateTime implementation !
772                         if (dateTime.Kind == DateTimeKind.Local && this != TimeZoneInfo.Utc)
773                                 return IsDaylightSavingTime (DateTime.SpecifyKind (dateTime.ToUniversalTime (), DateTimeKind.Utc));
774                                 
775                         AdjustmentRule rule = GetApplicableRule (dateTime.Date);
776                         if (rule == null)
777                                 return false;
778
779                         DateTime DST_start = TransitionPoint (rule.DaylightTransitionStart, dateTime.Year);
780                         DateTime DST_end = TransitionPoint (rule.DaylightTransitionEnd, dateTime.Year + ((rule.DaylightTransitionStart.Month < rule.DaylightTransitionEnd.Month) ? 0 : 1));
781                         if (dateTime.Kind == DateTimeKind.Utc) {
782                                 DST_start -= BaseUtcOffset;
783                                 DST_end -= (BaseUtcOffset + rule.DaylightDelta);
784                         }
785
786                         return (dateTime >= DST_start && dateTime < DST_end);
787                 }
788
789                 public bool IsDaylightSavingTime (DateTimeOffset dateTimeOffset)
790                 {
791                         throw new NotImplementedException ();
792                 }
793
794                 public bool IsInvalidTime (DateTime dateTime)
795                 {
796                         if (dateTime.Kind == DateTimeKind.Utc)
797                                 return false;
798                         if (dateTime.Kind == DateTimeKind.Local && this != Local)
799                                 return false;
800
801                         AdjustmentRule rule = GetApplicableRule (dateTime);
802                         if (rule != null) {
803                                 DateTime tpoint = TransitionPoint (rule.DaylightTransitionStart, dateTime.Year);
804                                 if (dateTime >= tpoint && dateTime < tpoint + rule.DaylightDelta)
805                                         return true;
806                         }
807                                 
808                         return false;
809                 }
810
811 #if NET_4_0
812                 void IDeserializationCallback.OnDeserialization (object sender)
813 #else
814                 public void OnDeserialization (object sender)
815 #endif
816                 {
817                         try {
818                                         TimeZoneInfo.Validate (id, baseUtcOffset, adjustmentRules);
819                                 } catch (ArgumentException ex) {
820                                         throw new SerializationException ("invalid serialization data", ex);
821                                 }
822                 }
823
824                 private static void Validate (string id, TimeSpan baseUtcOffset, AdjustmentRule [] adjustmentRules)
825                 {
826                         if (id == null)
827                                 throw new ArgumentNullException ("id");
828
829                         if (id == String.Empty)
830                                 throw new ArgumentException ("id parameter is an empty string");
831
832                         if (baseUtcOffset.Ticks % TimeSpan.TicksPerMinute != 0)
833                                 throw new ArgumentException ("baseUtcOffset parameter does not represent a whole number of minutes");
834
835                         if (baseUtcOffset > new TimeSpan (14, 0, 0) || baseUtcOffset < new TimeSpan (-14, 0, 0))
836                                 throw new ArgumentOutOfRangeException ("baseUtcOffset parameter is greater than 14 hours or less than -14 hours");
837
838 #if STRICT
839                         if (id.Length > 32)
840                                 throw new ArgumentException ("id parameter shouldn't be longer than 32 characters");
841 #endif
842
843                         if (adjustmentRules != null && adjustmentRules.Length != 0) {
844                                 AdjustmentRule prev = null;
845                                 foreach (AdjustmentRule current in adjustmentRules) {
846                                         if (current == null)
847                                                 throw new InvalidTimeZoneException ("one or more elements in adjustmentRules are null");
848
849                                         if ((baseUtcOffset + current.DaylightDelta < new TimeSpan (-14, 0, 0)) ||
850                                                         (baseUtcOffset + current.DaylightDelta > new TimeSpan (14, 0, 0)))
851                                                 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;");
852
853                                         if (prev != null && prev.DateStart > current.DateStart)
854                                                 throw new InvalidTimeZoneException ("adjustment rules specified in adjustmentRules parameter are not in chronological order");
855                                         
856                                         if (prev != null && prev.DateEnd > current.DateStart)
857                                                 throw new InvalidTimeZoneException ("some adjustment rules in the adjustmentRules parameter overlap");
858
859                                         if (prev != null && prev.DateEnd == current.DateStart)
860                                                 throw new InvalidTimeZoneException ("a date can have multiple adjustment rules applied to it");
861
862                                         prev = current;
863                                 }
864                         }
865                 }
866                 
867                 public string ToSerializedString ()
868                 {
869                         throw new NotImplementedException ();
870                 }
871
872                 public override string ToString ()
873                 {
874                         return DisplayName;
875                 }
876
877                 private TimeZoneInfo (SerializationInfo info, StreamingContext context)
878                 {
879                         if (info == null)
880                                 throw new ArgumentNullException ("info");
881                         id = (string) info.GetValue ("Id", typeof (string));
882                         displayName = (string) info.GetValue ("DisplayName", typeof (string));
883                         standardDisplayName = (string) info.GetValue ("StandardName", typeof (string));
884                         daylightDisplayName = (string) info.GetValue ("DaylightName", typeof (string));
885                         baseUtcOffset = (TimeSpan) info.GetValue ("BaseUtcOffset", typeof (TimeSpan));
886                         adjustmentRules = (TimeZoneInfo.AdjustmentRule []) info.GetValue ("AdjustmentRules", typeof (TimeZoneInfo.AdjustmentRule []));
887                         supportsDaylightSavingTime = (bool) info.GetValue ("SupportsDaylightSavingTime", typeof (bool));
888                 }
889
890                 private TimeZoneInfo (string id, TimeSpan baseUtcOffset, string displayName, string standardDisplayName, string daylightDisplayName, TimeZoneInfo.AdjustmentRule [] adjustmentRules, bool disableDaylightSavingTime)
891                 {
892                         if (id == null)
893                                 throw new ArgumentNullException ("id");
894
895                         if (id == String.Empty)
896                                 throw new ArgumentException ("id parameter is an empty string");
897
898                         if (baseUtcOffset.Ticks % TimeSpan.TicksPerMinute != 0)
899                                 throw new ArgumentException ("baseUtcOffset parameter does not represent a whole number of minutes");
900
901                         if (baseUtcOffset > new TimeSpan (14, 0, 0) || baseUtcOffset < new TimeSpan (-14, 0, 0))
902                                 throw new ArgumentOutOfRangeException ("baseUtcOffset parameter is greater than 14 hours or less than -14 hours");
903
904 #if STRICT
905                         if (id.Length > 32)
906                                 throw new ArgumentException ("id parameter shouldn't be longer than 32 characters");
907 #endif
908
909                         bool supportsDaylightSavingTime = !disableDaylightSavingTime;
910
911                         if (adjustmentRules != null && adjustmentRules.Length != 0) {
912                                 AdjustmentRule prev = null;
913                                 foreach (AdjustmentRule current in adjustmentRules) {
914                                         if (current == null)
915                                                 throw new InvalidTimeZoneException ("one or more elements in adjustmentRules are null");
916
917                                         if ((baseUtcOffset + current.DaylightDelta < new TimeSpan (-14, 0, 0)) ||
918                                                         (baseUtcOffset + current.DaylightDelta > new TimeSpan (14, 0, 0)))
919                                                 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;");
920
921                                         if (prev != null && prev.DateStart > current.DateStart)
922                                                 throw new InvalidTimeZoneException ("adjustment rules specified in adjustmentRules parameter are not in chronological order");
923                                         
924                                         if (prev != null && prev.DateEnd > current.DateStart)
925                                                 throw new InvalidTimeZoneException ("some adjustment rules in the adjustmentRules parameter overlap");
926
927                                         if (prev != null && prev.DateEnd == current.DateStart)
928                                                 throw new InvalidTimeZoneException ("a date can have multiple adjustment rules applied to it");
929
930                                         prev = current;
931                                 }
932                         } else {
933                                 supportsDaylightSavingTime = false;
934                         }
935                         
936                         this.id = id;
937                         this.baseUtcOffset = baseUtcOffset;
938                         this.displayName = displayName ?? id;
939                         this.standardDisplayName = standardDisplayName ?? id;
940                         this.daylightDisplayName = daylightDisplayName;
941                         this.supportsDaylightSavingTime = supportsDaylightSavingTime;
942                         this.adjustmentRules = adjustmentRules;
943                 }
944
945                 private AdjustmentRule GetApplicableRule (DateTime dateTime)
946                 {
947                         //Transitions are always in standard time
948                         DateTime date = dateTime;
949
950                         if (dateTime.Kind == DateTimeKind.Local && this != TimeZoneInfo.Local)
951                                 date = date.ToUniversalTime () + BaseUtcOffset;
952
953                         if (dateTime.Kind == DateTimeKind.Utc && this != TimeZoneInfo.Utc)
954                                 date = date + BaseUtcOffset;
955
956                         if (adjustmentRules != null) {
957                                 foreach (AdjustmentRule rule in adjustmentRules) {
958                                         if (rule.DateStart > date.Date)
959                                                 return null;
960                                         if (rule.DateEnd < date.Date)
961                                                 continue;
962                                         return rule;
963                                 }
964                         }
965                         return null;
966                 }
967
968                 private static DateTime TransitionPoint (TransitionTime transition, int year)
969                 {
970                         if (transition.IsFixedDateRule)
971                                 return new DateTime (year, transition.Month, transition.Day) + transition.TimeOfDay.TimeOfDay;
972
973                         DayOfWeek first = (new DateTime (year, transition.Month, 1)).DayOfWeek;
974                         int day = 1 + (transition.Week - 1) * 7 + (transition.DayOfWeek - first) % 7;
975                         if (day >  DateTime.DaysInMonth (year, transition.Month))
976                                 day -= 7;
977                         return new DateTime (year, transition.Month, day) + transition.TimeOfDay.TimeOfDay;
978                 }
979
980                 static List<AdjustmentRule> ValidateRules (List<AdjustmentRule> adjustmentRules)
981                 {
982                         AdjustmentRule prev = null;
983                         foreach (AdjustmentRule current in adjustmentRules.ToArray ()) {
984                                 if (prev != null && prev.DateEnd > current.DateStart) {
985                                         adjustmentRules.Remove (current);
986                                 }
987                                 prev = current;
988                         }
989                         return adjustmentRules;
990                 }
991
992 #if LIBC || MONODROID
993                 private static bool ValidTZFile (byte [] buffer, int length)
994                 {
995                         StringBuilder magic = new StringBuilder ();
996
997                         for (int i = 0; i < 4; i++)
998                                 magic.Append ((char)buffer [i]);
999                         
1000                         if (magic.ToString () != "TZif")
1001                                 return false;
1002
1003                         if (length >= BUFFER_SIZE)
1004                                 return false;
1005
1006                         return true;
1007                 }
1008
1009                 static int SwapInt32 (int i)
1010                 {
1011                         return (((i >> 24) & 0xff)
1012                                 | ((i >> 8) & 0xff00)
1013                                 | ((i << 8) & 0xff0000)
1014                                 | ((i << 24)));
1015                 }
1016
1017                 static int ReadBigEndianInt32 (byte [] buffer, int start)
1018                 {
1019                         int i = BitConverter.ToInt32 (buffer, start);
1020                         if (!BitConverter.IsLittleEndian)
1021                                 return i;
1022
1023                         return SwapInt32 (i);
1024                 }
1025
1026                 private static TimeZoneInfo ParseTZBuffer (string id, byte [] buffer, int length)
1027                 {
1028                         //Reading the header. 4 bytes for magic, 16 are reserved
1029                         int ttisgmtcnt = ReadBigEndianInt32 (buffer, 20);
1030                         int ttisstdcnt = ReadBigEndianInt32 (buffer, 24);
1031                         int leapcnt = ReadBigEndianInt32 (buffer, 28);
1032                         int timecnt = ReadBigEndianInt32 (buffer, 32);
1033                         int typecnt = ReadBigEndianInt32 (buffer, 36);
1034                         int charcnt = ReadBigEndianInt32 (buffer, 40);
1035
1036                         if (length < 44 + timecnt * 5 + typecnt * 6 + charcnt + leapcnt * 8 + ttisstdcnt + ttisgmtcnt)
1037                                 throw new InvalidTimeZoneException ();
1038
1039                         Dictionary<int, string> abbreviations = ParseAbbreviations (buffer, 44 + 4 * timecnt + timecnt + 6 * typecnt, charcnt);
1040                         Dictionary<int, TimeType> time_types = ParseTimesTypes (buffer, 44 + 4 * timecnt + timecnt, typecnt, abbreviations);
1041                         List<KeyValuePair<DateTime, TimeType>> transitions = ParseTransitions (buffer, 44, timecnt, time_types);
1042
1043                         if (time_types.Count == 0)
1044                                 throw new InvalidTimeZoneException ();
1045
1046                         if (time_types.Count == 1 && ((TimeType)time_types[0]).IsDst)
1047                                 throw new InvalidTimeZoneException ();
1048
1049                         TimeSpan baseUtcOffset = new TimeSpan (0);
1050                         TimeSpan dstDelta = new TimeSpan (0);
1051                         string standardDisplayName = null;
1052                         string daylightDisplayName = null;
1053                         bool dst_observed = false;
1054                         DateTime dst_start = DateTime.MinValue;
1055                         List<AdjustmentRule> adjustmentRules = new List<AdjustmentRule> ();
1056
1057                         for (int i = 0; i < transitions.Count; i++) {
1058                                 var pair = transitions [i];
1059                                 DateTime ttime = pair.Key;
1060                                 TimeType ttype = pair.Value;
1061                                 if (!ttype.IsDst) {
1062                                         if (standardDisplayName != ttype.Name || baseUtcOffset.TotalSeconds != ttype.Offset) {
1063                                                 standardDisplayName = ttype.Name;
1064                                                 daylightDisplayName = null;
1065                                                 baseUtcOffset = new TimeSpan (0, 0, ttype.Offset);
1066                                                 adjustmentRules = new List<AdjustmentRule> ();
1067                                                 dst_observed = false;
1068                                         }
1069                                         if (dst_observed) {
1070                                                 //FIXME: check additional fields for this:
1071                                                 //most of the transitions are expressed in GMT 
1072                                                 dst_start += baseUtcOffset;
1073                                                 DateTime dst_end = ttime + baseUtcOffset + dstDelta;
1074
1075                                                 //some weird timezone (America/Phoenix) have end dates on Jan 1st
1076                                                 if (dst_end.Date == new DateTime (dst_end.Year, 1, 1) && dst_end.Year > dst_start.Year)
1077                                                         dst_end -= new TimeSpan (24, 0, 0);
1078
1079                                                 DateTime dateStart, dateEnd;
1080                                                 if (dst_start.Month < 7)
1081                                                         dateStart = new DateTime (dst_start.Year, 1, 1);
1082                                                 else
1083                                                         dateStart = new DateTime (dst_start.Year, 7, 1);
1084
1085                                                 if (dst_end.Month >= 7)
1086                                                         dateEnd = new DateTime (dst_end.Year, 12, 31);
1087                                                 else
1088                                                         dateEnd = new DateTime (dst_end.Year, 6, 30);
1089
1090                                                 
1091                                                 TransitionTime transition_start = TransitionTime.CreateFixedDateRule (new DateTime (1, 1, 1) + dst_start.TimeOfDay, dst_start.Month, dst_start.Day);
1092                                                 TransitionTime transition_end = TransitionTime.CreateFixedDateRule (new DateTime (1, 1, 1) + dst_end.TimeOfDay, dst_end.Month, dst_end.Day);
1093                                                 if  (transition_start != transition_end) //y, that happened in Argentina in 1943-1946
1094                                                         adjustmentRules.Add (AdjustmentRule.CreateAdjustmentRule (dateStart, dateEnd, dstDelta, transition_start, transition_end));
1095                                         }
1096                                         dst_observed = false;
1097                                 } else {
1098                                         if (daylightDisplayName != ttype.Name || dstDelta.TotalSeconds != ttype.Offset - baseUtcOffset.TotalSeconds) {
1099                                                 daylightDisplayName = ttype.Name;
1100                                                 dstDelta = new TimeSpan(0, 0, ttype.Offset) - baseUtcOffset;
1101                                         }
1102                                         dst_start = ttime;
1103                                         dst_observed = true;
1104                                 }
1105                         }
1106
1107                         if (adjustmentRules.Count == 0) {
1108                                 TimeType t = (TimeType)time_types [0];
1109                                 if (standardDisplayName == null) {
1110                                         standardDisplayName = t.Name;
1111                                         baseUtcOffset = new TimeSpan (0, 0, t.Offset);
1112                                 }
1113                                 return CreateCustomTimeZone (id, baseUtcOffset, id, standardDisplayName);
1114                         } else {
1115                                 return CreateCustomTimeZone (id, baseUtcOffset, id, standardDisplayName, daylightDisplayName, ValidateRules (adjustmentRules).ToArray ());
1116                         }
1117                 }
1118
1119                 static Dictionary<int, string> ParseAbbreviations (byte [] buffer, int index, int count)
1120                 {
1121                         var abbrevs = new Dictionary<int, string> ();
1122                         int abbrev_index = 0;
1123                         var sb = new StringBuilder ();
1124                         for (int i = 0; i < count; i++) {
1125                                 char c = (char) buffer [index + i];
1126                                 if (c != '\0')
1127                                         sb.Append (c);
1128                                 else {
1129                                         abbrevs.Add (abbrev_index, sb.ToString ());
1130                                         //Adding all the substrings too, as it seems to be used, at least for Africa/Windhoek
1131                                         for (int j = 1; j < sb.Length; j++)
1132                                                 abbrevs.Add (abbrev_index + j, sb.ToString (j, sb.Length - j));
1133                                         abbrev_index = i + 1;
1134                                         sb = new StringBuilder ();
1135                                 }
1136                         }
1137                         return abbrevs;
1138                 }
1139
1140                 static Dictionary<int, TimeType> ParseTimesTypes (byte [] buffer, int index, int count, Dictionary<int, string> abbreviations)
1141                 {
1142                         var types = new Dictionary<int, TimeType> (count);
1143                         for (int i = 0; i < count; i++) {
1144                                 int offset = ReadBigEndianInt32 (buffer, index + 6 * i);
1145                                 byte is_dst = buffer [index + 6 * i + 4];
1146                                 byte abbrev = buffer [index + 6 * i + 5];
1147                                 types.Add (i, new TimeType (offset, (is_dst != 0), abbreviations [(int)abbrev]));
1148                         }
1149                         return types;
1150                 }
1151
1152                 static List<KeyValuePair<DateTime, TimeType>> ParseTransitions (byte [] buffer, int index, int count, Dictionary<int, TimeType> time_types)
1153                 {
1154                         var list = new List<KeyValuePair<DateTime, TimeType>> (count);
1155                         for (int i = 0; i < count; i++) {
1156                                 int unixtime = ReadBigEndianInt32 (buffer, index + 4 * i);
1157                                 DateTime ttime = DateTimeFromUnixTime (unixtime);
1158                                 byte ttype = buffer [index + 4 * count + i];
1159                                 list.Add (new KeyValuePair<DateTime, TimeType> (ttime, time_types [(int)ttype]));
1160                         }
1161                         return list;
1162                 }
1163
1164                 static DateTime DateTimeFromUnixTime (long unix_time)
1165                 {
1166                         DateTime date_time = new DateTime (1970, 1, 1);
1167                         return date_time.AddSeconds (unix_time);
1168                 }
1169         }
1170
1171         struct TimeType {
1172                 public readonly int Offset;
1173                 public readonly bool IsDst;
1174                 public string Name;
1175
1176                 public TimeType (int offset, bool is_dst, string abbrev)
1177                 {
1178                         this.Offset = offset;
1179                         this.IsDst = is_dst;
1180                         this.Name = abbrev;
1181                 }
1182
1183                 public override string ToString ()
1184                 {
1185                         return "offset: " + Offset + "s, is_dst: " + IsDst + ", zone name: " + Name;
1186                 }
1187 #else
1188         }
1189 #endif
1190         }
1191 }
1192
1193 #endif