Skip to content

Commit

Permalink
Revert "Fix DateTime Ticks to match the behavior of .NET"
Browse files Browse the repository at this point in the history
This reverts commit 79a595a.
  • Loading branch information
mortezag committed Aug 8, 2015
1 parent aa2330e commit 63bda89
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 46 deletions.
53 changes: 25 additions & 28 deletions Framework/Subset_of_CorLib/System/DateTime.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,17 +35,7 @@ public enum DateTimeKind
* This value type represents a date and time. Every DateTime
* object has a private field (Ticks) of type Int64 that stores the
* date and time as the number of 100 nanosecond intervals since
* 12:00 AM January 1, year 1601 A.D. in the proleptic Gregorian Calendar.
* See DeviceCode\PAL\time_decl.h for explanation of why we are taking
* year 1601 as origin for our HAL, PAL, and CLR.
*
* It should be considered that previously DateTime.Ticks used to represent
* the number 100 nanosecond intervals since 12:00 AM January 1, year 1601 A.D.
* causing a 504911232000000000 difference between ticks in .NET and .NET MF
* To fix this disparity, we modified the CLR portion of DateTime type without
* changing the origin of the DateTime. In particular, TicksAtOrigin is subtracted
* from any ticks value provided by user code, and it is added back whenever the
* ticks value is read.
* 12:00 AM January 1, year 1601 A.D. in the proleptic Gregorian Calendar.
*
* <p>For a description of various calendar issues, look at
* <a href="http://serendipity.nofadz.com/hermetic/cal_stud.htm">
Expand Down Expand Up @@ -87,6 +77,11 @@ public struct DateTime
private const int DaysPer100Years = DaysPer4Years * 25 - 1;
// Number of days in 400 years
private const int DaysPer400Years = DaysPer100Years * 4 + 1;

// Number of days from 1/1/0001 to 12/31/1600
private const int DaysTo1601 = DaysPer400Years * 4;
// Number of days from 1/1/0001 to 12/30/1899
private const int DaysTo1899 = DaysPer400Years * 4 + DaysPer100Years * 3 - 367;
// Number of days from 1/1/0001 to 12/31/9999
private const int DaysTo10000 = DaysPer400Years * 25 - 366;

Expand All @@ -98,18 +93,13 @@ public struct DateTime
private const ulong TickMask = 0x7FFFFFFFFFFFFFFFL;
private const ulong UTCMask = 0x8000000000000000L;

// Ticks at 12:00 AM January 1, year 1601 A.D.
private const long TicksAtOrigin = 504911232000000000;

public static readonly DateTime MinValue = new DateTime(MinTicks + TicksAtOrigin);
public static readonly DateTime MaxValue = new DateTime(MaxTicks + TicksAtOrigin);
public static readonly DateTime MinValue = new DateTime(MinTicks);
public static readonly DateTime MaxValue = new DateTime(MaxTicks);

private ulong m_ticks;

public DateTime(long ticks)
{
ticks -= TicksAtOrigin;

if (((ticks & (long)TickMask) < MinTicks) || ((ticks & (long)TickMask) > MaxTicks))
{
throw new ArgumentOutOfRangeException("ticks", "Ticks must be between DateTime.MinValue.Ticks and DateTime.MaxValue.Ticks.");
Expand Down Expand Up @@ -146,12 +136,12 @@ public DateTime(int year, int month, int day, int hour, int minute, int second)

public DateTime Add(TimeSpan val)
{
return new DateTime((long)m_ticks + val.Ticks + TicksAtOrigin);
return new DateTime((long)m_ticks + val.Ticks);
}

private DateTime Add(double val, int scale)
{
return new DateTime((long)((long)m_ticks + (long)(val * scale * TicksPerMillisecond + (val >= 0 ? 0.5 : -0.5))) + TicksAtOrigin);
return new DateTime((long)((long)m_ticks + (long)(val * scale * TicksPerMillisecond + (val >= 0 ? 0.5 : -0.5))));
}

public DateTime AddDays(double val)
Expand Down Expand Up @@ -181,7 +171,7 @@ public DateTime AddSeconds(double val)

public DateTime AddTicks(long val)
{
return new DateTime((long)m_ticks + val + TicksAtOrigin);
return new DateTime((long)m_ticks + val);
}

public static int Compare(DateTime t1, DateTime t2)
Expand Down Expand Up @@ -243,11 +233,11 @@ public DateTime Date
// Need to remove UTC mask before arithmetic operations. Then set it back.
if ((m_ticks & UTCMask) != 0)
{
return new DateTime((long)(((m_ticks & TickMask) - (m_ticks & TickMask) % TicksPerDay) | UTCMask) + TicksAtOrigin);
return new DateTime((long)(((m_ticks & TickMask) - (m_ticks & TickMask) % TicksPerDay) | UTCMask));
}
else
{
return new DateTime((long)(m_ticks - m_ticks % TicksPerDay) + TicksAtOrigin);
return new DateTime((long)(m_ticks - m_ticks % TicksPerDay));
}
}
}
Expand Down Expand Up @@ -301,7 +291,7 @@ public DateTimeKind Kind

public static DateTime SpecifyKind(DateTime value, DateTimeKind kind)
{
DateTime retVal = new DateTime((long)value.m_ticks + TicksAtOrigin);
DateTime retVal = new DateTime((long)value.m_ticks);

if (kind == DateTimeKind.Utc)
{
Expand Down Expand Up @@ -370,11 +360,18 @@ public int Second
}
}

/// Our origin is at 1601/01/01:00:00:00.000
/// While desktop CLR's origin is at 0001/01/01:00:00:00.000.
/// There are 504911232000000000 ticks between them which we are subtracting.
/// See DeviceCode\PAL\time_decl.h for explanation of why we are taking
/// year 1601 as origin for our HAL, PAL, and CLR.
// static Int64 ticksAtOrigin = 504911232000000000;
static Int64 ticksAtOrigin = 0;
public long Ticks
{
get
{
return (long)(m_ticks & TickMask) + TicksAtOrigin;
return (long)(m_ticks & TickMask) + ticksAtOrigin;
}
}

Expand Down Expand Up @@ -411,7 +408,7 @@ public TimeSpan Subtract(DateTime val)

public DateTime Subtract(TimeSpan val)
{
return new DateTime((long)(m_ticks - (ulong)val.m_ticks) + TicksAtOrigin);
return new DateTime((long)(m_ticks - (ulong)val.m_ticks));
}

[MethodImplAttribute(MethodImplOptions.InternalCall)]
Expand All @@ -432,12 +429,12 @@ public String ToString(String format)

public static DateTime operator +(DateTime d, TimeSpan t)
{
return new DateTime((long)(d.m_ticks + (ulong)t.m_ticks) + TicksAtOrigin);
return new DateTime((long)(d.m_ticks + (ulong)t.m_ticks));
}

public static DateTime operator -(DateTime d, TimeSpan t)
{
return new DateTime((long)(d.m_ticks - (ulong)t.m_ticks) + TicksAtOrigin);
return new DateTime((long)(d.m_ticks - (ulong)t.m_ticks));
}

public static TimeSpan operator -(DateTime d1, DateTime d2)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,6 @@ namespace Microsoft.SPOT.Platform.Tests
{
public class SystemDateTimeTests : IMFTestInterface
{
// Ticks at 12:00 AM January 1, year 1601 A.D.
private const long TicksAtOrigin = 504911232000000000;
private const long MaxTicks = 946708127990000000;

[SetUp]
public InitializeResult Initialize()
{
Expand Down Expand Up @@ -119,7 +115,7 @@ public MFTestResults DateTime_ConstructorTest3()
Log.Comment("Creating Minimum DateTime and verifying");
DateTime minDT1 = DateTime.MinValue;
DateTime minDT2 = new DateTime();
DateTime minDT3 = new DateTime(TicksAtOrigin);
DateTime minDT3 = new DateTime(0);
DateTime minDT4 = new DateTime(1601, 1, 1, 0, 0, 0, 0);

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

Log.Comment("Creating Maximum DateTime and verifying");
DateTime maxDateTime = new DateTime(MaxTicks);
DateTime maxDateTime = new DateTime(441796895990000000);
if (!DateTime.MaxValue.Equals(maxDateTime))
{
Log.Comment("Expected Ticks '946708127990000000' but got '" + DateTime.MaxValue.Ticks + "'");
Log.Comment("Expected Ticks '441796895990000000' but got '" + DateTime.MaxValue.Ticks + "'");
testResult = MFTestResults.Fail;
}
}
Expand Down Expand Up @@ -1320,7 +1316,7 @@ public MFTestResults DateTime_op_Subtraction_DateTimeTest31()
for (int i = 0; i < dtArr.Length; i++)
{
DateTime dt1 = dtArr[i];
DateTime dt2 = new DateTime(random.Next(1000) + 1 + TicksAtOrigin);
DateTime dt2 = new DateTime(random.Next(1000) + 1);
TimeSpan ts = dt1 - dt2;
if (ts.Ticks != (dt1.Ticks - dt2.Ticks))
{
Expand Down Expand Up @@ -1566,9 +1562,9 @@ public MFTestResults DateTime_MinValueTest39()
{
Log.Comment("Getting the Min. DateTime and Verifying");
DateTime field = DateTime.MinValue;
if (field.Ticks != TicksAtOrigin)
if (field.Ticks != 0)
{
Log.Comment("Failure : expected DateTime.MinValue = '504911232000000000' but got '" + field.Ticks + "'");
Log.Comment("Failure : expected DateTime.MinValue = '0' but got '" + field.Ticks + "'");
testResult = MFTestResults.Fail;
}
}
Expand All @@ -1594,9 +1590,9 @@ public MFTestResults DateTime_MaxValueTest40()
Log.Comment("Getting the Max. DateTime and Verifying");
DateTime field = DateTime.MaxValue;
Log.Comment(field.Ticks.ToString());
if (field.Ticks != MaxTicks)
if (field.Ticks != 441796895990000000)
{
Log.Comment("Failure : expected DateTime.MaxValue = '946708127990000000'" +
Log.Comment("Failure : expected DateTime.MinValue = '441796895990000000'" +
" but got '" + field.Ticks + "'");
testResult = MFTestResults.Fail;
}
Expand Down Expand Up @@ -1944,15 +1940,12 @@ public MFTestResults DateTime_TicksTest52()
MFTestResults testResult = MFTestResults.Pass;
try
{
// This is an arbitrary ticks value which corresponds to a point of time in year 2015
long testTicks = 635688622030533832;

Log.Comment("Creating a DateTime, getting the Ticks and Verifying");
DateTime testDateTime = new System.DateTime(testTicks);
DateTime testDateTime = new System.DateTime(0);
long _ticks = testDateTime.Ticks;
if (_ticks != testTicks)
if (_ticks != 0)
{
Log.Comment("Failure : Expected ticks '635688622030533832' but got '" + _ticks + "'");
Log.Comment("Failure : Expected ticks '0' but got '" + _ticks + "'");
testResult = MFTestResults.Fail;
}
}
Expand Down

0 comments on commit 63bda89

Please sign in to comment.