Skip to content
This repository was archived by the owner on Jun 5, 2019. It is now read-only.

Commit 63bda89

Browse files
committed
Revert "Fix DateTime Ticks to match the behavior of .NET"
This reverts commit 79a595a.
1 parent aa2330e commit 63bda89

File tree

2 files changed

+36
-46
lines changed

2 files changed

+36
-46
lines changed

Framework/Subset_of_CorLib/System/DateTime.cs

Lines changed: 25 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -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)

Test/Platform/Tests/CLR/mscorlib/systemlib/systemlib2/SystemDateTimeTests.cs

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,6 @@ namespace Microsoft.SPOT.Platform.Tests
1111
{
1212
public class SystemDateTimeTests : IMFTestInterface
1313
{
14-
// Ticks at 12:00 AM January 1, year 1601 A.D.
15-
private const long TicksAtOrigin = 504911232000000000;
16-
private const long MaxTicks = 946708127990000000;
17-
1814
[SetUp]
1915
public InitializeResult Initialize()
2016
{
@@ -119,7 +115,7 @@ public MFTestResults DateTime_ConstructorTest3()
119115
Log.Comment("Creating Minimum DateTime and verifying");
120116
DateTime minDT1 = DateTime.MinValue;
121117
DateTime minDT2 = new DateTime();
122-
DateTime minDT3 = new DateTime(TicksAtOrigin);
118+
DateTime minDT3 = new DateTime(0);
123119
DateTime minDT4 = new DateTime(1601, 1, 1, 0, 0, 0, 0);
124120

125121
if ((DateTime.Compare(minDT1, minDT2) != 0) ||
@@ -135,10 +131,10 @@ public MFTestResults DateTime_ConstructorTest3()
135131
}
136132

137133
Log.Comment("Creating Maximum DateTime and verifying");
138-
DateTime maxDateTime = new DateTime(MaxTicks);
134+
DateTime maxDateTime = new DateTime(441796895990000000);
139135
if (!DateTime.MaxValue.Equals(maxDateTime))
140136
{
141-
Log.Comment("Expected Ticks '946708127990000000' but got '" + DateTime.MaxValue.Ticks + "'");
137+
Log.Comment("Expected Ticks '441796895990000000' but got '" + DateTime.MaxValue.Ticks + "'");
142138
testResult = MFTestResults.Fail;
143139
}
144140
}
@@ -1320,7 +1316,7 @@ public MFTestResults DateTime_op_Subtraction_DateTimeTest31()
13201316
for (int i = 0; i < dtArr.Length; i++)
13211317
{
13221318
DateTime dt1 = dtArr[i];
1323-
DateTime dt2 = new DateTime(random.Next(1000) + 1 + TicksAtOrigin);
1319+
DateTime dt2 = new DateTime(random.Next(1000) + 1);
13241320
TimeSpan ts = dt1 - dt2;
13251321
if (ts.Ticks != (dt1.Ticks - dt2.Ticks))
13261322
{
@@ -1566,9 +1562,9 @@ public MFTestResults DateTime_MinValueTest39()
15661562
{
15671563
Log.Comment("Getting the Min. DateTime and Verifying");
15681564
DateTime field = DateTime.MinValue;
1569-
if (field.Ticks != TicksAtOrigin)
1565+
if (field.Ticks != 0)
15701566
{
1571-
Log.Comment("Failure : expected DateTime.MinValue = '504911232000000000' but got '" + field.Ticks + "'");
1567+
Log.Comment("Failure : expected DateTime.MinValue = '0' but got '" + field.Ticks + "'");
15721568
testResult = MFTestResults.Fail;
15731569
}
15741570
}
@@ -1594,9 +1590,9 @@ public MFTestResults DateTime_MaxValueTest40()
15941590
Log.Comment("Getting the Max. DateTime and Verifying");
15951591
DateTime field = DateTime.MaxValue;
15961592
Log.Comment(field.Ticks.ToString());
1597-
if (field.Ticks != MaxTicks)
1593+
if (field.Ticks != 441796895990000000)
15981594
{
1599-
Log.Comment("Failure : expected DateTime.MaxValue = '946708127990000000'" +
1595+
Log.Comment("Failure : expected DateTime.MinValue = '441796895990000000'" +
16001596
" but got '" + field.Ticks + "'");
16011597
testResult = MFTestResults.Fail;
16021598
}
@@ -1944,15 +1940,12 @@ public MFTestResults DateTime_TicksTest52()
19441940
MFTestResults testResult = MFTestResults.Pass;
19451941
try
19461942
{
1947-
// This is an arbitrary ticks value which corresponds to a point of time in year 2015
1948-
long testTicks = 635688622030533832;
1949-
19501943
Log.Comment("Creating a DateTime, getting the Ticks and Verifying");
1951-
DateTime testDateTime = new System.DateTime(testTicks);
1944+
DateTime testDateTime = new System.DateTime(0);
19521945
long _ticks = testDateTime.Ticks;
1953-
if (_ticks != testTicks)
1946+
if (_ticks != 0)
19541947
{
1955-
Log.Comment("Failure : Expected ticks '635688622030533832' but got '" + _ticks + "'");
1948+
Log.Comment("Failure : Expected ticks '0' but got '" + _ticks + "'");
19561949
testResult = MFTestResults.Fail;
19571950
}
19581951
}

0 commit comments

Comments
 (0)