5 // Duncan Mak (duncan@ximian.com)
6 // Ajay Kumar Dwivedi (adwiv@yahoo.com)
7 // Martin Baulig (martin@gnome.org)
10 // Copyright (C) 2004-2006 Novell, Inc (http://www.novell.com)
11 // Copyright 2011 Xamarin Inc.
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:
21 // The above copyright notice and this permission notice shall be
22 // included in all copies or substantial portions of the Software.
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.
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
41 // Rewrite ToUniversalTime to use a similar setup to that
43 using System.Collections.Generic;
44 using System.Globalization;
45 using System.Runtime.CompilerServices;
46 using System.Runtime.Serialization;
47 using System.Runtime.InteropServices;
53 public abstract class TimeZone
56 static TimeZone currentTimeZone;
59 static object tz_lock = new object ();
61 static long timezone_check;
69 public static TimeZone CurrentTimeZone {
71 long now = DateTime.UtcNow.Ticks;
72 TimeZone tz = currentTimeZone;
75 if (tz == null || Math.Abs (now - timezone_check) > TimeSpan.TicksPerMinute) {
76 tz = new CurrentSystemTimeZone ();
87 public abstract string DaylightName {
91 public abstract string StandardName {
96 public abstract DaylightTime GetDaylightChanges (int year);
98 public abstract TimeSpan GetUtcOffset (DateTime time);
100 public virtual bool IsDaylightSavingTime (DateTime time)
102 return IsDaylightSavingTime (time, GetDaylightChanges (time.Year));
105 public static bool IsDaylightSavingTime (DateTime time, DaylightTime daylightTimes)
107 if (daylightTimes == null)
108 throw new ArgumentNullException ("daylightTimes");
110 // If Start == End, then DST is off
111 if (daylightTimes.Start.Ticks == daylightTimes.End.Ticks)
114 //We are in the northern hemisphere.
115 if (daylightTimes.Start.Ticks < daylightTimes.End.Ticks) {
116 if (daylightTimes.Start.Ticks < time.Ticks && daylightTimes.End.Ticks > time.Ticks)
117 return true; // time lies between Start and End
120 else { // We are in the southern hemisphere.
121 if (time.Year == daylightTimes.Start.Year && time.Year == daylightTimes.End.Year)
122 if (time.Ticks < daylightTimes.End.Ticks || time.Ticks > daylightTimes.Start.Ticks)
123 return true; // time is less than End OR more than Start
129 public virtual DateTime ToLocalTime (DateTime time)
131 if (time.Kind == DateTimeKind.Local)
134 TimeSpan utcOffset = GetUtcOffset (new DateTime (time.Ticks));
135 if (utcOffset.Ticks > 0) {
136 if (DateTime.MaxValue - utcOffset < time)
137 return DateTime.SpecifyKind (DateTime.MaxValue, DateTimeKind.Local);
138 } else if (utcOffset.Ticks < 0) {
139 if (time.Ticks + utcOffset.Ticks < DateTime.MinValue.Ticks)
140 return DateTime.SpecifyKind (DateTime.MinValue, DateTimeKind.Local);
143 return DateTime.SpecifyKind (time.Add (utcOffset), DateTimeKind.Local);
146 public virtual DateTime ToUniversalTime (DateTime time)
148 if (time.Kind == DateTimeKind.Utc)
151 TimeSpan offset = GetUtcOffset (time);
153 if (offset.Ticks < 0) {
154 if (DateTime.MaxValue + offset < time)
155 return DateTime.SpecifyKind (DateTime.MaxValue, DateTimeKind.Utc);
156 } else if (offset.Ticks > 0) {
157 if (DateTime.MinValue + offset > time)
158 return DateTime.SpecifyKind (DateTime.MinValue, DateTimeKind.Utc);
161 return DateTime.SpecifyKind (new DateTime (time.Ticks - offset.Ticks), DateTimeKind.Utc);
164 internal static void ClearCachedData ()
166 currentTimeZone = null;
171 internal class CurrentSystemTimeZone : TimeZone {
173 readonly TimeZoneInfo LocalTimeZone;
176 internal CurrentSystemTimeZone ()
178 LocalTimeZone = TimeZoneInfo.Local;
181 public override string DaylightName {
183 return LocalTimeZone.DaylightName;
187 public override string StandardName {
189 return LocalTimeZone.StandardName;
193 public override System.Globalization.DaylightTime GetDaylightChanges (int year)
195 return LocalTimeZone.GetDaylightChanges (year);
198 public override TimeSpan GetUtcOffset (DateTime dateTime)
200 if (dateTime.Kind == DateTimeKind.Utc)
201 return TimeSpan.Zero;
203 return LocalTimeZone.GetUtcOffset (dateTime);
206 public override bool IsDaylightSavingTime (DateTime dateTime)
208 if (dateTime.Kind == DateTimeKind.Utc)
211 return LocalTimeZone.IsDaylightSavingTime (dateTime);