[corlib] Removed TimeZone unused methods.
[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.UtcNow.Ticks;
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                         return DateTime.SpecifyKind (time.Add (utcOffset), DateTimeKind.Local);
148                 }
149
150                 public virtual DateTime ToUniversalTime (DateTime time)
151                 {
152                         if (time.Kind == DateTimeKind.Utc)
153                                 return time;
154
155                         TimeSpan offset = GetUtcOffset (time);
156
157                         if (offset.Ticks < 0) {
158                                 if (DateTime.MaxValue + offset < time)
159                                         return DateTime.SpecifyKind (DateTime.MaxValue, DateTimeKind.Utc);
160                         } else if (offset.Ticks > 0) {
161                                 if (DateTime.MinValue + offset > time)
162                                         return DateTime.SpecifyKind (DateTime.MinValue, DateTimeKind.Utc);
163                         }
164
165                         return DateTime.SpecifyKind (new DateTime (time.Ticks - offset.Ticks), DateTimeKind.Utc);
166                 }
167
168                 internal static void ClearCachedData ()
169                 {
170                         currentTimeZone = null;
171                 }
172         }
173
174         [Serializable]
175         internal class CurrentSystemTimeZone : TimeZone {
176
177                 readonly  TimeZoneInfo  LocalTimeZone;
178
179                 // Constructor
180                 internal CurrentSystemTimeZone ()
181                 {
182                         LocalTimeZone = TimeZoneInfo.Local;
183                 }
184
185                 public override string DaylightName {
186                         get {
187                                 return LocalTimeZone.DaylightName;
188                         }
189                 }
190
191                 public override string StandardName {
192                         get {
193                                 return LocalTimeZone.StandardName;
194                         }
195                 }
196
197                 public override System.Globalization.DaylightTime GetDaylightChanges (int year)
198                 {
199                         return LocalTimeZone.GetDaylightChanges (year);
200                 }
201
202                 public override TimeSpan GetUtcOffset (DateTime time)
203                 {
204                         return LocalTimeZone.GetUtcOffset (time);
205                 }
206
207                 public override bool IsDaylightSavingTime (DateTime dateTime)
208                 {
209                         return LocalTimeZone.IsDaylightSavingTime (dateTime);
210                 }
211         }
212 }