@@ -35,17 +35,7 @@ public enum DateTimeKind
3535 * This value type represents a date and time. Every DateTime
3636 * object has a private field (Ticks) of type Int64 that stores the
3737 * date and time as the number of 100 nanosecond intervals since
38- * 12:00 AM January 1, year 1601 A.D. in the proleptic Gregorian Calendar.
39- * See DeviceCode\PAL\time_decl.h for explanation of why we are taking
40- * year 1601 as origin for our HAL, PAL, and CLR.
41- *
42- * It should be considered that previously DateTime.Ticks used to represent
43- * the number 100 nanosecond intervals since 12:00 AM January 1, year 1601 A.D.
44- * causing a 504911232000000000 difference between ticks in .NET and .NET MF
45- * To fix this disparity, we modified the CLR portion of DateTime type without
46- * changing the origin of the DateTime. In particular, TicksAtOrigin is subtracted
47- * from any ticks value provided by user code, and it is added back whenever the
48- * ticks value is read.
38+ * 12:00 AM January 1, year 1601 A.D. in the proleptic Gregorian Calendar.
4939 *
5040 * <p>For a description of various calendar issues, look at
5141 * <a href="http://serendipity.nofadz.com/hermetic/cal_stud.htm">
@@ -87,6 +77,11 @@ public struct DateTime
8777 private const int DaysPer100Years = DaysPer4Years * 25 - 1 ;
8878 // Number of days in 400 years
8979 private const int DaysPer400Years = DaysPer100Years * 4 + 1 ;
80+
81+ // Number of days from 1/1/0001 to 12/31/1600
82+ private const int DaysTo1601 = DaysPer400Years * 4 ;
83+ // Number of days from 1/1/0001 to 12/30/1899
84+ private const int DaysTo1899 = DaysPer400Years * 4 + DaysPer100Years * 3 - 367 ;
9085 // Number of days from 1/1/0001 to 12/31/9999
9186 private const int DaysTo10000 = DaysPer400Years * 25 - 366 ;
9287
@@ -98,18 +93,13 @@ public struct DateTime
9893 private const ulong TickMask = 0x7FFFFFFFFFFFFFFFL ;
9994 private const ulong UTCMask = 0x8000000000000000L ;
10095
101- // Ticks at 12:00 AM January 1, year 1601 A.D.
102- private const long TicksAtOrigin = 504911232000000000 ;
103-
104- public static readonly DateTime MinValue = new DateTime ( MinTicks + TicksAtOrigin ) ;
105- public static readonly DateTime MaxValue = new DateTime ( MaxTicks + TicksAtOrigin ) ;
96+ public static readonly DateTime MinValue = new DateTime ( MinTicks ) ;
97+ public static readonly DateTime MaxValue = new DateTime ( MaxTicks ) ;
10698
10799 private ulong m_ticks ;
108100
109101 public DateTime ( long ticks )
110102 {
111- ticks -= TicksAtOrigin ;
112-
113103 if ( ( ( ticks & ( long ) TickMask ) < MinTicks ) || ( ( ticks & ( long ) TickMask ) > MaxTicks ) )
114104 {
115105 throw new ArgumentOutOfRangeException ( "ticks" , "Ticks must be between DateTime.MinValue.Ticks and DateTime.MaxValue.Ticks." ) ;
@@ -146,12 +136,12 @@ public DateTime(int year, int month, int day, int hour, int minute, int second)
146136
147137 public DateTime Add ( TimeSpan val )
148138 {
149- return new DateTime ( ( long ) m_ticks + val . Ticks + TicksAtOrigin ) ;
139+ return new DateTime ( ( long ) m_ticks + val . Ticks ) ;
150140 }
151141
152142 private DateTime Add ( double val , int scale )
153143 {
154- return new DateTime ( ( long ) ( ( long ) m_ticks + ( long ) ( val * scale * TicksPerMillisecond + ( val >= 0 ? 0.5 : - 0.5 ) ) ) + TicksAtOrigin ) ;
144+ return new DateTime ( ( long ) ( ( long ) m_ticks + ( long ) ( val * scale * TicksPerMillisecond + ( val >= 0 ? 0.5 : - 0.5 ) ) ) ) ;
155145 }
156146
157147 public DateTime AddDays ( double val )
@@ -181,7 +171,7 @@ public DateTime AddSeconds(double val)
181171
182172 public DateTime AddTicks ( long val )
183173 {
184- return new DateTime ( ( long ) m_ticks + val + TicksAtOrigin ) ;
174+ return new DateTime ( ( long ) m_ticks + val ) ;
185175 }
186176
187177 public static int Compare ( DateTime t1 , DateTime t2 )
@@ -243,11 +233,11 @@ public DateTime Date
243233 // Need to remove UTC mask before arithmetic operations. Then set it back.
244234 if ( ( m_ticks & UTCMask ) != 0 )
245235 {
246- return new DateTime ( ( long ) ( ( ( m_ticks & TickMask ) - ( m_ticks & TickMask ) % TicksPerDay ) | UTCMask ) + TicksAtOrigin ) ;
236+ return new DateTime ( ( long ) ( ( ( m_ticks & TickMask ) - ( m_ticks & TickMask ) % TicksPerDay ) | UTCMask ) ) ;
247237 }
248238 else
249239 {
250- return new DateTime ( ( long ) ( m_ticks - m_ticks % TicksPerDay ) + TicksAtOrigin ) ;
240+ return new DateTime ( ( long ) ( m_ticks - m_ticks % TicksPerDay ) ) ;
251241 }
252242 }
253243 }
@@ -301,7 +291,7 @@ public DateTimeKind Kind
301291
302292 public static DateTime SpecifyKind ( DateTime value , DateTimeKind kind )
303293 {
304- DateTime retVal = new DateTime ( ( long ) value . m_ticks + TicksAtOrigin ) ;
294+ DateTime retVal = new DateTime ( ( long ) value . m_ticks ) ;
305295
306296 if ( kind == DateTimeKind . Utc )
307297 {
@@ -370,11 +360,18 @@ public int Second
370360 }
371361 }
372362
363+ /// Our origin is at 1601/01/01:00:00:00.000
364+ /// While desktop CLR's origin is at 0001/01/01:00:00:00.000.
365+ /// There are 504911232000000000 ticks between them which we are subtracting.
366+ /// See DeviceCode\PAL\time_decl.h for explanation of why we are taking
367+ /// year 1601 as origin for our HAL, PAL, and CLR.
368+ // static Int64 ticksAtOrigin = 504911232000000000;
369+ static Int64 ticksAtOrigin = 0 ;
373370 public long Ticks
374371 {
375372 get
376373 {
377- return ( long ) ( m_ticks & TickMask ) + TicksAtOrigin ;
374+ return ( long ) ( m_ticks & TickMask ) + ticksAtOrigin ;
378375 }
379376 }
380377
@@ -411,7 +408,7 @@ public TimeSpan Subtract(DateTime val)
411408
412409 public DateTime Subtract ( TimeSpan val )
413410 {
414- return new DateTime ( ( long ) ( m_ticks - ( ulong ) val . m_ticks ) + TicksAtOrigin ) ;
411+ return new DateTime ( ( long ) ( m_ticks - ( ulong ) val . m_ticks ) ) ;
415412 }
416413
417414 [ MethodImplAttribute ( MethodImplOptions . InternalCall ) ]
@@ -432,12 +429,12 @@ public String ToString(String format)
432429
433430 public static DateTime operator + ( DateTime d , TimeSpan t )
434431 {
435- return new DateTime ( ( long ) ( d . m_ticks + ( ulong ) t . m_ticks ) + TicksAtOrigin ) ;
432+ return new DateTime ( ( long ) ( d . m_ticks + ( ulong ) t . m_ticks ) ) ;
436433 }
437434
438435 public static DateTime operator - ( DateTime d , TimeSpan t )
439436 {
440- return new DateTime ( ( long ) ( d . m_ticks - ( ulong ) t . m_ticks ) + TicksAtOrigin ) ;
437+ return new DateTime ( ( long ) ( d . m_ticks - ( ulong ) t . m_ticks ) ) ;
441438 }
442439
443440 public static TimeSpan operator - ( DateTime d1 , DateTime d2 )
0 commit comments