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.
4
5
6#include "common.h"
7#include "object.h"
8#include "excep.h"
9#include "frames.h"
10#include "vars.hpp"
11#include "comdatetime.h"
12
13const INT64 COMDateTime::TicksPerMillisecond = 10000;
14const INT64 COMDateTime::TicksPerSecond = TicksPerMillisecond * 1000;
15const INT64 COMDateTime::TicksPerMinute = TicksPerSecond * 60;
16const INT64 COMDateTime::TicksPerHour = TicksPerMinute * 60;
17const INT64 COMDateTime::TicksPerDay = TicksPerHour * 24;
18
19const INT64 COMDateTime::MillisPerSecond = 1000;
20const INT64 COMDateTime::MillisPerDay = MillisPerSecond * 60 * 60 * 24;
21
22const int COMDateTime::DaysPer4Years = 365 * 4 + 1;
23const int COMDateTime::DaysPer100Years = DaysPer4Years * 25 - 1;
24const int COMDateTime::DaysPer400Years = DaysPer100Years * 4 + 1;
25
26// Number of days from 1/1/0001 to 1/1/10000
27const int COMDateTime::DaysTo10000 = DaysPer400Years * 25 - 366;
28
29const int COMDateTime::DaysTo1899 = DaysPer400Years * 4 + DaysPer100Years * 3 - 367;
30
31const INT64 COMDateTime::DoubleDateOffset = DaysTo1899 * TicksPerDay;
32
33// OA Min Date is Jan 1, 100 AD. This is after converting to ticks.
34const INT64 COMDateTime::OADateMinAsTicks = (DaysPer100Years - 365) * TicksPerDay;
35
36// All OA dates must be greater than (not >=) OADateMinAsDouble
37const double COMDateTime::OADateMinAsDouble = -657435.0;
38
39// All OA dates must be less than (not <=) OADateMaxAsDouble
40const double COMDateTime::OADateMaxAsDouble = 2958466.0;
41
42const INT64 COMDateTime::MaxTicks = DaysTo10000 * TicksPerDay;
43const INT64 COMDateTime::MaxMillis = DaysTo10000 * MillisPerDay;
44
45const INT64 TicksMask = I64(0x3FFFFFFFFFFFFFFF);
46
47// This function is duplicated in DateTime.cs
48INT64 COMDateTime::DoubleDateToTicks(const double d)
49{
50 CONTRACTL
51 {
52 THROWS;
53 GC_TRIGGERS;
54 MODE_COOPERATIVE;
55 } CONTRACTL_END;
56
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"));
63
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;
69
70 if (millis < 0 || millis >= MaxMillis)
71 {
72 COMPlusThrow(kArgumentException, W("Arg_OleAutDateScale")); // Cannot be equal to MaxMillis.
73 }
74 return millis * TicksPerMillisecond;
75}
76
77// This function is duplicated in DateTime.cs
78double COMDateTime::TicksToDoubleDate(INT64 ticks)
79{
80 CONTRACTL
81 {
82 THROWS;
83 GC_TRIGGERS;
84 MODE_COOPERATIVE;
85 } CONTRACTL_END;
86
87 //
88
89 // Workaround to handle uninitialized DateTime objects in the CLR
90 // See explanation in DateTime.cs's TicksToOADate function.
91
92 // Strip off the extra kind state
93 ticks = (ticks & TicksMask);
94
95 if (ticks == 0)
96 return 0.0; // OA's 0 date (12/30/1899).
97
98 if (ticks < OADateMinAsTicks)
99 {
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
103 //to their day 0.
104 if (ticks < TicksPerDay)
105 ticks+=DoubleDateOffset;
106 else
107 COMPlusThrow(kOverflowException, W("Arg_OleAutDateInvalid"));
108 }
109
110 INT64 millis = (ticks - DoubleDateOffset) / TicksPerMillisecond;
111 if (millis < 0)
112 {
113 INT64 frac = millis % MillisPerDay;
114 if (frac != 0) millis -= (MillisPerDay + frac) * 2;
115 }
116
117 double d = (double)millis / MillisPerDay;
118
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);
123
124 return d;
125}
126