Remove excessive shortcut key matching in ToolStrip
[mono.git] / mcs / class / corlib / System / TimeZone.cs
1 //
2 // System.TimeZone.cs
3 //
4 // Authors:
5 //   Duncan Mak (duncan@ximian.com)
6 //   Ajay Kumar Dwivedi (adwiv@yahoo.com)
7 //   Martin Baulig (martin@gnome.org)
8 //
9 // (C) Ximian, Inc.
10 // Copyright (C) 2004-2006 Novell, Inc (http://www.novell.com)
11 // Copyright 2011 Xamarin Inc.
12 //
13 // Permission is hereby granted, free of charge, to any person obtaining
14 // a copy of this software and associated documentation files (the
15 // "Software"), to deal in the Software without restriction, including
16 // without limitation the rights to use, copy, modify, merge, publish,
17 // distribute, sublicense, and/or sell copies of the Software, and to
18 // permit persons to whom the Software is furnished to do so, subject to
19 // the following conditions:
20 // 
21 // The above copyright notice and this permission notice shall be
22 // included in all copies or substantial portions of the Software.
23 // 
24 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
28 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
29 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
30 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 //
32 //
33 // TODO:
34 //
35 //    Rewrite ToLocalTime to use GetLocalTimeDiff(DateTime,TimeSpan),
36 //    this should only leave the validation at the beginning (for MaxValue)
37 //    and then call the helper function.  This would remove all the
38 //    ifdefs in that code, and replace it with only one, for the construction
39 //    of the object.
40 //
41 //    Rewrite ToUniversalTime to use a similar setup to that
42 //
43 using System.Collections.Generic;
44 using System.Globalization;
45 using System.Runtime.CompilerServices;
46 using System.Runtime.Serialization;
47 using System.Runtime.InteropServices;
48
49 namespace System
50 {
51         [Serializable]
52         [ComVisible (true)]
53         public abstract class TimeZone
54         {
55                 // Fields
56                 static TimeZone currentTimeZone;
57
58                 [NonSerialized]
59                 static object tz_lock = new object ();
60                 [NonSerialized]
61                 static long timezone_check;
62
63                 // Constructor
64                 protected TimeZone ()
65                 {
66                 }
67
68                 // Properties
69                 public static TimeZone CurrentTimeZone {
70                         get {
71                                 long now = DateTime.GetNow ();
72                                 TimeZone tz = currentTimeZone;
73                                 
74                                 lock (tz_lock) {
75                                         if (tz == null || Math.Abs (now - timezone_check) > TimeSpan.TicksPerMinute) {
76 #if MONODROID
77                                                 tz = AndroidPlatform.GetCurrentSystemTimeZone ();
78                                                 if (tz == null)
79 #endif
80                                                         tz = new CurrentSystemTimeZone (now);
81                                                 timezone_check = now;
82
83                                                 currentTimeZone = tz;
84                                         }
85                                 }
86                                 
87                                 return tz;
88                         }
89                 }
90
91                 public abstract string DaylightName {
92                         get;
93                 }
94
95                 public abstract string StandardName {
96                         get;
97                 }
98
99                 // Methods
100                 public abstract DaylightTime GetDaylightChanges (int year);
101
102                 public abstract TimeSpan GetUtcOffset (DateTime time);
103
104                 public virtual bool IsDaylightSavingTime (DateTime time)
105                 {
106                         return IsDaylightSavingTime (time, GetDaylightChanges (time.Year));
107                 }
108
109                 public static bool IsDaylightSavingTime (DateTime time, DaylightTime daylightTimes)
110                 {
111                         if (daylightTimes == null)
112                                 throw new ArgumentNullException ("daylightTimes");
113
114                         // If Start == End, then DST is off
115                         if (daylightTimes.Start.Ticks == daylightTimes.End.Ticks)
116                                 return false;
117
118                         //We are in the northern hemisphere.
119                         if (daylightTimes.Start.Ticks < daylightTimes.End.Ticks) {
120                                 if (daylightTimes.Start.Ticks < time.Ticks && daylightTimes.End.Ticks > time.Ticks)
121                                         return true; // time lies between Start and End
122
123                         }
124                         else {  // We are in the southern hemisphere.
125                                 if (time.Year == daylightTimes.Start.Year && time.Year == daylightTimes.End.Year)
126                                         if (time.Ticks < daylightTimes.End.Ticks || time.Ticks > daylightTimes.Start.Ticks)
127                                                 return true; // time is less than End OR more than Start 
128                         }
129
130                         return false;
131                 }
132
133                 public virtual DateTime ToLocalTime (DateTime time)
134                 {
135                         if (time.Kind == DateTimeKind.Local)
136                                 return time;
137
138                         TimeSpan utcOffset = GetUtcOffset (new DateTime (time.Ticks));
139                         if (utcOffset.Ticks > 0) {
140                                 if (DateTime.MaxValue - utcOffset < time)
141                                         return DateTime.SpecifyKind (DateTime.MaxValue, DateTimeKind.Local);
142                         } else if (utcOffset.Ticks < 0) {
143                                 if (time.Ticks + utcOffset.Ticks < DateTime.MinValue.Ticks)
144                                         return DateTime.SpecifyKind (DateTime.MinValue, DateTimeKind.Local);
145                         }
146
147                         DateTime local = DateTime.SpecifyKind (time.Add (utcOffset), DateTimeKind.Local);
148                         DaylightTime dlt = GetDaylightChanges (time.Year);
149                         if (dlt.Delta.Ticks == 0)
150                                 return DateTime.SpecifyKind (local, DateTimeKind.Local);
151
152                         // FIXME: check all of the combination of
153                         //      - basis: local-based or UTC-based
154                         //      - hemisphere: Northern or Southern
155                         //      - offset: positive or negative
156
157                         // PST should work fine here.
158                         if (local < dlt.End && dlt.End.Subtract (dlt.Delta) <= local)
159                                 return DateTime.SpecifyKind (local, DateTimeKind.Local);
160
161                         TimeSpan localOffset = GetUtcOffset (local);
162                         return DateTime.SpecifyKind (time.Add (localOffset), DateTimeKind.Local);
163                 }
164
165                 public virtual DateTime ToUniversalTime (DateTime time)
166                 {
167                         if (time.Kind == DateTimeKind.Utc)
168                                 return time;
169
170                         TimeSpan offset = GetUtcOffset (time);
171
172                         if (offset.Ticks < 0) {
173                                 if (DateTime.MaxValue + offset < time)
174                                         return DateTime.SpecifyKind (DateTime.MaxValue, DateTimeKind.Utc);
175                         } else if (offset.Ticks > 0) {
176                                 if (DateTime.MinValue + offset > time)
177                                         return DateTime.SpecifyKind (DateTime.MinValue, DateTimeKind.Utc);
178                         }
179
180                         return DateTime.SpecifyKind (new DateTime (time.Ticks - offset.Ticks), DateTimeKind.Utc);
181                 }
182
183                 internal static void ClearCachedData ()
184                 {
185                         currentTimeZone = null;
186                 }
187
188                 //
189                 // This routine returns the TimeDiff that would have to be
190                 // added to "time" to turn it into a local time.   This would
191                 // be equivalent to call ToLocalTime.
192                 //
193                 // There is one important consideration:
194                 //
195                 //    This information is only valid during the minute it
196                 //    was called.
197                 //
198                 //    This only works with a real time, not one of the boundary
199                 //    cases like DateTime.MaxValue, so validation must be done
200                 //    before.
201                 // 
202                 //    This is intended to be used by DateTime.Now
203                 //
204                 // We use a minute, just to be conservative and cope with
205                 // any potential time zones that might be defined in the future
206                 // that might not nicely fit in hour or half-hour steps. 
207                 //    
208                 internal TimeSpan GetLocalTimeDiff (DateTime time)
209                 {
210                         return GetLocalTimeDiff (time, GetUtcOffset (time));
211                 }
212
213                 //
214                 // This routine is intended to be called by GetLocalTimeDiff(DatetTime)
215                 // or by ToLocalTime after validation has been performed
216                 //
217                 // time is the time to map, utc_offset is the utc_offset that
218                 // has been computed for calling GetUtcOffset on time.
219                 //
220                 // When called by GetLocalTime, utc_offset is assumed to come
221                 // from a time constructed by new DateTime (DateTime.GetNow ()), that
222                 // is a valid time.
223                 //
224                 // When called by ToLocalTime ranges are checked before this is
225                 // called.
226                 //
227                 internal TimeSpan GetLocalTimeDiff (DateTime time, TimeSpan utc_offset)
228                 {
229                         DaylightTime dlt = GetDaylightChanges (time.Year);
230
231                         if (dlt.Delta.Ticks == 0)
232                                 return utc_offset;
233
234                         DateTime local = time.Add (utc_offset);
235                         if (local < dlt.End && dlt.End.Subtract (dlt.Delta) <= local)
236                                 return utc_offset;
237
238                         if (local >= dlt.Start && dlt.Start.Add (dlt.Delta) > local)
239                                 return utc_offset - dlt.Delta;
240
241                         return GetUtcOffset (local);
242                 }
243         }
244
245         [Serializable]
246         internal class CurrentSystemTimeZone : TimeZone, IDeserializationCallback {
247
248                 // Fields
249                 private string m_standardName;
250                 private string m_daylightName;
251
252                 // A yearwise cache of DaylightTime.
253                 private Dictionary<int, DaylightTime> m_CachedDaylightChanges = new Dictionary<int, DaylightTime> (1);
254
255                 // the offset when daylightsaving is not on (in ticks)
256                 private long m_ticksOffset;
257
258                 // the offset when daylightsaving is not on.
259                 [NonSerialized]
260                 private TimeSpan utcOffsetWithOutDLS;
261   
262                 // the offset when daylightsaving is on.
263                 [NonSerialized]
264                 private TimeSpan utcOffsetWithDLS;
265
266                 internal enum TimeZoneData
267                 {
268                         DaylightSavingStartIdx,
269                         DaylightSavingEndIdx,
270                         UtcOffsetIdx,
271                         AdditionalDaylightOffsetIdx
272                 };
273
274                 internal enum TimeZoneNames
275                 {
276                         StandardNameIdx,
277                         DaylightNameIdx
278                 };
279
280                 // Internal method to get timezone data.
281                 //    data[0]:  start of daylight saving time (in DateTime ticks).
282                 //    data[1]:  end of daylight saving time (in DateTime ticks).
283                 //    data[2]:  utcoffset (in TimeSpan ticks).
284                 //    data[3]:  additional offset when daylight saving (in TimeSpan ticks).
285                 //    name[0]:  name of this timezone when not daylight saving.
286                 //    name[1]:  name of this timezone when daylight saving.
287                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
288                 private static extern bool GetTimeZoneData (int year, out Int64[] data, out string[] names);
289
290                 // Constructor
291                 internal CurrentSystemTimeZone ()
292                 {
293                 }
294
295                 //
296                 // Initialized by the constructor
297                 //
298                 static int this_year;
299                 static DaylightTime this_year_dlt;
300                 
301                 //
302                 // The "lnow" parameter must be the current time, I could have moved
303                 // the code here, but I do not want to interfere with serialization
304                 // which is why I kept the other constructor around
305                 //
306                 internal CurrentSystemTimeZone (long lnow)
307                 {
308                         Int64[] data;
309                         string[] names;
310
311                         DateTime now = new DateTime (lnow);
312                         if (!GetTimeZoneData (now.Year, out data, out names))
313                                 throw new NotSupportedException (Locale.GetText ("Can't get timezone name."));
314
315                         m_standardName = Locale.GetText (names[(int)TimeZoneNames.StandardNameIdx]);
316                         m_daylightName = Locale.GetText (names[(int)TimeZoneNames.DaylightNameIdx]);
317
318                         m_ticksOffset = data[(int)TimeZoneData.UtcOffsetIdx];
319
320                         DaylightTime dlt = GetDaylightTimeFromData (data);
321                         m_CachedDaylightChanges.Add (now.Year, dlt);
322                         OnDeserialization (dlt);
323                 }
324
325                 // Properties
326                 public override string DaylightName {
327                         get { return m_daylightName; }
328                 }
329
330                 public override string StandardName {
331                         get { return m_standardName; }
332                 }
333
334                 // Methods
335                 public override DaylightTime GetDaylightChanges (int year)
336                 {
337                         if (year < 1 || year > 9999)
338                                 throw new ArgumentOutOfRangeException ("year", year +
339                                         Locale.GetText (" is not in a range between 1 and 9999."));
340
341                         //
342                         // First we try the case for this year, very common, and is used
343                         // by DateTime.Now (a popular call) indirectly.
344                         //
345                         if (year == this_year)
346                                 return this_year_dlt;
347                         
348                         lock (m_CachedDaylightChanges) {
349                                 DaylightTime dlt;
350                                 if (!m_CachedDaylightChanges.TryGetValue (year, out dlt)) {
351                                         Int64[] data;
352                                         string[] names;
353
354                                         if (!GetTimeZoneData (year, out data, out names))
355                                                 throw new ArgumentException (Locale.GetText ("Can't get timezone data for " + year));
356
357                                         dlt = GetDaylightTimeFromData (data);
358                                         m_CachedDaylightChanges.Add (year, dlt);
359                                 }
360                                 return dlt;
361                         }
362                 }
363
364                 public override TimeSpan GetUtcOffset (DateTime time)
365                 {
366                         if (time.Kind == DateTimeKind.Utc)
367                                 return TimeSpan.Zero;
368
369                         if (IsDaylightSavingTime (time) && !IsAmbiguousTime (time))
370                                 return utcOffsetWithDLS;
371
372                         return utcOffsetWithOutDLS;
373                 }
374
375                 private bool IsAmbiguousTime (DateTime time)
376                 {
377                         if (time.Kind == DateTimeKind.Utc)
378                                 return false;
379
380                         DaylightTime changes = GetDaylightChanges (time.Year);
381
382                         return time < changes.End && time >= changes.End - changes.Delta;
383                 }
384
385                 void IDeserializationCallback.OnDeserialization (object sender)
386                 {
387                         OnDeserialization (null);
388                 }
389
390                 private void OnDeserialization (DaylightTime dlt)
391                 {
392                         if (dlt == null) {
393                                 Int64[] data;
394                                 string[] names;
395
396                                 this_year = DateTime.Now.Year;
397                                 if (!GetTimeZoneData (this_year, out data, out names))
398                                         throw new ArgumentException (Locale.GetText ("Can't get timezone data for " + this_year));
399                                 dlt = GetDaylightTimeFromData (data);
400                         } else
401                                 this_year = dlt.Start.Year;
402                         
403                         utcOffsetWithOutDLS = new TimeSpan (m_ticksOffset);
404                         utcOffsetWithDLS = new TimeSpan (m_ticksOffset + dlt.Delta.Ticks);
405                         this_year_dlt = dlt;
406                 }
407
408                 private DaylightTime GetDaylightTimeFromData (long[] data)
409                 {
410                         return new DaylightTime (new DateTime (data[(int)TimeZoneData.DaylightSavingStartIdx]),
411                                 new DateTime (data[(int)TimeZoneData.DaylightSavingEndIdx]),
412                                 new TimeSpan (data[(int)TimeZoneData.AdditionalDaylightOffsetIdx]));
413                 }
414
415         }
416 }