mscorlib(4.0.0.0) API with additions
TimeZone.cs
3 using System.Threading;
4 
5 namespace System
6 {
9  [ComVisible(true)]
10  public abstract class TimeZone
11  {
12  private static volatile TimeZone currentTimeZone;
13 
14  private static object s_InternalSyncObject;
15 
16  private static object InternalSyncObject
17  {
18  get
19  {
20  if (s_InternalSyncObject == null)
21  {
22  object value = new object();
23  Interlocked.CompareExchange<object>(ref s_InternalSyncObject, value, (object)null);
24  }
25  return s_InternalSyncObject;
26  }
27  }
28 
31  public static TimeZone CurrentTimeZone
32  {
33  get
34  {
35  TimeZone timeZone = currentTimeZone;
36  if (timeZone == null)
37  {
38  lock (InternalSyncObject)
39  {
40  if (currentTimeZone == null)
41  {
42  currentTimeZone = new CurrentSystemTimeZone();
43  }
44  return currentTimeZone;
45  }
46  }
47  return timeZone;
48  }
49  }
50 
54  public abstract string StandardName
55  {
56  get;
57  }
58 
61  public abstract string DaylightName
62  {
63  get;
64  }
65 
66  internal static void ResetTimeZone()
67  {
68  if (currentTimeZone != null)
69  {
70  lock (InternalSyncObject)
71  {
72  currentTimeZone = null;
73  }
74  }
75  }
76 
80  public abstract TimeSpan GetUtcOffset(DateTime time);
81 
85  public virtual DateTime ToUniversalTime(DateTime time)
86  {
87  if (time.Kind == DateTimeKind.Utc)
88  {
89  return time;
90  }
91  long num = time.Ticks - GetUtcOffset(time).Ticks;
92  if (num > 3155378975999999999L)
93  {
94  return new DateTime(3155378975999999999L, DateTimeKind.Utc);
95  }
96  if (num < 0)
97  {
98  return new DateTime(0L, DateTimeKind.Utc);
99  }
100  return new DateTime(num, DateTimeKind.Utc);
101  }
102 
106  public virtual DateTime ToLocalTime(DateTime time)
107  {
108  if (time.Kind == DateTimeKind.Local)
109  {
110  return time;
111  }
112  bool isAmbiguousLocalDst = false;
113  long utcOffsetFromUniversalTime = ((CurrentSystemTimeZone)CurrentTimeZone).GetUtcOffsetFromUniversalTime(time, ref isAmbiguousLocalDst);
114  return new DateTime(time.Ticks + utcOffsetFromUniversalTime, DateTimeKind.Local, isAmbiguousLocalDst);
115  }
116 
122  public abstract DaylightTime GetDaylightChanges(int year);
123 
128  public virtual bool IsDaylightSavingTime(DateTime time)
129  {
130  return IsDaylightSavingTime(time, GetDaylightChanges(time.Year));
131  }
132 
140  public static bool IsDaylightSavingTime(DateTime time, DaylightTime daylightTimes)
141  {
142  return CalculateUtcOffset(time, daylightTimes) != TimeSpan.Zero;
143  }
144 
145  internal static TimeSpan CalculateUtcOffset(DateTime time, DaylightTime daylightTimes)
146  {
147  if (daylightTimes == null)
148  {
149  return TimeSpan.Zero;
150  }
151  DateTimeKind kind = time.Kind;
152  if (kind == DateTimeKind.Utc)
153  {
154  return TimeSpan.Zero;
155  }
156  DateTime dateTime = daylightTimes.Start + daylightTimes.Delta;
157  DateTime end = daylightTimes.End;
158  DateTime t;
159  DateTime t2;
160  if (daylightTimes.Delta.Ticks > 0)
161  {
162  t = end - daylightTimes.Delta;
163  t2 = end;
164  }
165  else
166  {
167  t = dateTime;
168  t2 = dateTime - daylightTimes.Delta;
169  }
170  bool flag = false;
171  if (dateTime > end)
172  {
173  if (time >= dateTime || time < end)
174  {
175  flag = true;
176  }
177  }
178  else if (time >= dateTime && time < end)
179  {
180  flag = true;
181  }
182  if (flag && time >= t && time < t2)
183  {
184  flag = time.IsAmbiguousDaylightSavingTime();
185  }
186  if (flag)
187  {
188  return daylightTimes.Delta;
189  }
190  return TimeSpan.Zero;
191  }
192  }
193 }
virtual bool IsDaylightSavingTime(DateTime time)
Returns a value indicating whether the specified date and time is within a daylight saving time perio...
Definition: TimeZone.cs:128
Definition: __Canon.cs:3
virtual DateTime ToLocalTime(DateTime time)
Returns the local time that corresponds to a specified date and time value.
Definition: TimeZone.cs:106
TimeSpan Delta
Gets the time interval that represents the difference between standard time and daylight saving time.
Definition: DaylightTime.cs:26
Represents an instant in time, typically expressed as a date and time of day. To browse the ....
Definition: DateTime.cs:13
A type representing a date and time value.
abstract string StandardName
Gets the standard time zone name.
Definition: TimeZone.cs:55
long Ticks
Gets the number of ticks that represent the value of the current T:System.TimeSpan structure.
Definition: TimeSpan.cs:84
int Year
Gets the year component of the date represented by this instance.
Definition: DateTime.cs:351
DateTimeKind Kind
Gets a value that indicates whether the time represented by this instance is based on local time,...
Definition: DateTime.cs:208
DateTime End
Gets the object that represents the date and time when the daylight saving period ends.
Definition: DaylightTime.cs:22
static int CompareExchange(ref int location1, int value, int comparand)
Compares two 32-bit signed integers for equality and, if they are equal, replaces the first value.
Represents a time zone.
Definition: TimeZone.cs:10
static bool IsDaylightSavingTime(DateTime time, DaylightTime daylightTimes)
Returns a value indicating whether the specified date and time is within the specified daylight savin...
Definition: TimeZone.cs:140
static readonly TimeSpan Zero
Represents the zero T:System.TimeSpan value. This field is read-only.
Definition: TimeSpan.cs:64
DateTime Start
Gets the object that represents the date and time when the daylight saving period begins.
Definition: DaylightTime.cs:18
static TimeZone CurrentTimeZone
Gets the time zone of the current computer.
Definition: TimeZone.cs:32
abstract string DaylightName
Gets the daylight saving time zone name.
Definition: TimeZone.cs:62
DateTimeKind
Specifies whether a T:System.DateTime object represents a local time, a Coordinated Universal Time (U...
Definition: DateTimeKind.cs:9
Represents a time interval.To browse the .NET Framework source code for this type,...
Definition: TimeSpan.cs:12
Specifies that the class can be serialized.
Defines the period of daylight saving time.
Definition: DaylightTime.cs:8
abstract DaylightTime GetDaylightChanges(int year)
Returns the daylight saving time period for a particular year.
Provides atomic operations for variables that are shared by multiple threads.
Definition: Interlocked.cs:10
virtual DateTime ToUniversalTime(DateTime time)
Returns the Coordinated Universal Time (UTC) that corresponds to a specified time.
Definition: TimeZone.cs:85
abstract TimeSpan GetUtcOffset(DateTime time)
Returns the Coordinated Universal Time (UTC) offset for the specified local time.
long Ticks
Gets the number of ticks that represent the date and time of this instance.
Definition: DateTime.cs:315