1 // Licensed to the .NET Foundation under one or more agreements.
2 // The .NET Foundation licenses this file to you under the MIT license.
3 // See the LICENSE file in the project root for more information.
11 #include "comdatetime.h"
13 const INT64 COMDateTime::TicksPerMillisecond = 10000;
14 const INT64 COMDateTime::TicksPerSecond = TicksPerMillisecond * 1000;
15 const INT64 COMDateTime::TicksPerMinute = TicksPerSecond * 60;
16 const INT64 COMDateTime::TicksPerHour = TicksPerMinute * 60;
17 const INT64 COMDateTime::TicksPerDay = TicksPerHour * 24;
19 const INT64 COMDateTime::MillisPerSecond = 1000;
20 const INT64 COMDateTime::MillisPerDay = MillisPerSecond * 60 * 60 * 24;
22 const int COMDateTime::DaysPer4Years = 365 * 4 + 1;
23 const int COMDateTime::DaysPer100Years = DaysPer4Years * 25 - 1;
24 const int COMDateTime::DaysPer400Years = DaysPer100Years * 4 + 1;
26 // Number of days from 1/1/0001 to 1/1/10000
27 const int COMDateTime::DaysTo10000 = DaysPer400Years * 25 - 366;
29 const int COMDateTime::DaysTo1899 = DaysPer400Years * 4 + DaysPer100Years * 3 - 367;
31 const INT64 COMDateTime::DoubleDateOffset = DaysTo1899 * TicksPerDay;
33 // OA Min Date is Jan 1, 100 AD. This is after converting to ticks.
34 const INT64 COMDateTime::OADateMinAsTicks = (DaysPer100Years - 365) * TicksPerDay;
36 // All OA dates must be greater than (not >=) OADateMinAsDouble
37 const double COMDateTime::OADateMinAsDouble = -657435.0;
39 // All OA dates must be less than (not <=) OADateMaxAsDouble
40 const double COMDateTime::OADateMaxAsDouble = 2958466.0;
42 const INT64 COMDateTime::MaxTicks = DaysTo10000 * TicksPerDay;
43 const INT64 COMDateTime::MaxMillis = DaysTo10000 * MillisPerDay;
45 const INT64 TicksMask = I64(0x3FFFFFFFFFFFFFFF);
47 // This function is duplicated in DateTime.cs
48 INT64 COMDateTime::DoubleDateToTicks(const double d)
57 // Make sure this date is a valid OleAut date. This is the check from the internal
58 // OleAut macro IsValidDate, found in oledisp.h. Eventually at least the 64 bit
59 // build of oleaut will define these gregorian max and min values as public constants.
60 // The check done this way will take care of NaN
61 if (!(d < OADateMaxAsDouble) || !(d > OADateMinAsDouble))
62 COMPlusThrow(kArgumentException, W("Arg_OleAutDateInvalid"));
64 // Conversion to int64 will not cause an overflow here, as at this point the "d" is in between OADateMinAsDouble and OADateMaxAsDouble
65 INT64 millis = (INT64)(d * MillisPerDay + (d >= 0? 0.5: -0.5));
66 if (millis < 0) millis -= (millis % MillisPerDay) * 2;
67 // There are cases when we are very close to -1 and 1 in which case millis%MillisPerDay is 0 since we have exactly one day due to rounding issues.
68 millis += DoubleDateOffset / TicksPerMillisecond;
70 if (millis < 0 || millis >= MaxMillis)
72 COMPlusThrow(kArgumentException, W("Arg_OleAutDateScale")); // Cannot be equal to MaxMillis.
74 return millis * TicksPerMillisecond;
77 // This function is duplicated in DateTime.cs
78 double COMDateTime::TicksToDoubleDate(INT64 ticks)
89 // Workaround to handle uninitialized DateTime objects in the CLR
90 // See explanation in DateTime.cs's TicksToOADate function.
92 // Strip off the extra kind state
93 ticks = (ticks & TicksMask);
96 return 0.0; // OA's 0 date (12/30/1899).
98 if (ticks < OADateMinAsTicks)
100 //We've special-cased day 0 (01/01/0001 in the Gregorian Calendar) such that the
101 //date can be used to represent a DateTime the contains only a time. OA uses
102 //day 0 (12/30/1899) for the same purpose, so we'll do a mapping from our day 0
104 if (ticks < TicksPerDay)
105 ticks+=DoubleDateOffset;
107 COMPlusThrow(kOverflowException, W("Arg_OleAutDateInvalid"));
110 INT64 millis = (ticks - DoubleDateOffset) / TicksPerMillisecond;
113 INT64 frac = millis % MillisPerDay;
114 if (frac != 0) millis -= (MillisPerDay + frac) * 2;
117 double d = (double)millis / MillisPerDay;
119 // Make sure this date is a valid OleAut date. This is the check from the internal
120 // OleAut macro IsValidDate, found in oledisp.h. Eventually at least the 64 bit
121 // build of oleaut will define these gregorian max and min values as public constants.
122 _ASSERTE(d < OADateMaxAsDouble && d > OADateMinAsDouble);