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