forked from chakra-core/ChakraCore
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDateUtilities.cpp
More file actions
434 lines (377 loc) · 13.5 KB
/
DateUtilities.cpp
File metadata and controls
434 lines (377 loc) · 13.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
//-------------------------------------------------------------------------------------------------------
// Copyright (C) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
//-------------------------------------------------------------------------------------------------------
#include "CommonCommonPch.h"
#define ENABLE_INTSAFE_SIGNED_FUNCTIONS 1
#include <intsafe.h>
#include "Common\DaylightTimeHelper.h"
#include "Common\DateUtilities.h"
#include <Windows.Foundation.h>
namespace Js
{
const INT64 DateUtilities::ticksPerMillisecond = 10000;
const double DateUtilities::ticksPerMillisecondDouble = 10000.0;
const INT64 DateUtilities::ticksPerSecond = ticksPerMillisecond * 1000;
const INT64 DateUtilities::ticksPerMinute = ticksPerSecond * 60;
const INT64 DateUtilities::ticksPerHour = ticksPerMinute * 60;
const INT64 DateUtilities::ticksPerDay = ticksPerHour * 24;
const INT64 DateUtilities::jsEpochMilliseconds = 11644473600000;
const INT64 DateUtilities::jsEpochTicks = jsEpochMilliseconds * ticksPerMillisecond;
// The day numbers for the months of a leap year.
static const int g_rgday[12] =
{
0, 31, 60, 91, 121, 152,
182, 213, 244, 274, 305, 335,
};
const double g_kdblJanuary1st1970 = 25569.0;
const wchar_t g_rgpszDay[7][4] =
{
L"Sun",
L"Mon",
L"Tue",
L"Wed",
L"Thu",
L"Fri",
L"Sat"
};
const wchar_t g_rgpszMonth[12][4] =
{
L"Jan",
L"Feb",
L"Mar",
L"Apr",
L"May",
L"Jun",
L"Jul",
L"Aug",
L"Sep",
L"Oct",
L"Nov",
L"Dec"
};
const wchar_t g_rgpszZone[8][4] =
{
L"EST",
L"EDT",
L"CST",
L"CDT",
L"MST",
L"MDT",
L"PST",
L"PDT"
};
//
// Convert a WinRT DateTime date (in 100ns precision ticks) to an ES5 date
//
// We convert ticks to milliseconds and shift by JS epoch to get the double date
// We go in that order to skip doing underflow checks
//
HRESULT DateUtilities::WinRTDateToES5Date(INT64 ticks, __out double* pRet)
{
Assert(pRet != NULL);
if (pRet == NULL)
{
return E_INVALIDARG;
}
// Divide as INT64 to ensure truncation of all decimal digits,
// since any remaining after conversion will be truncated as a Date value.
INT64 milliseconds = ticks / ticksPerMillisecond;
(*pRet) = (double)(milliseconds - jsEpochMilliseconds);
return S_OK;
}
//
// Convert an ES5 date based on double to a WinRT DateTime
// DateTime is the number of ticks that have elapsed since 1/1/1601 00:00:00 in 100ns precision
// If we return a failure HRESULT other than E_INVALIDARG, the es5 date can't be expressed
// in the WinRT scheme
//
HRESULT DateUtilities::ES5DateToWinRTDate(double es5Date, __out INT64* pRet)
{
Assert(pRet != NULL);
if (pRet == NULL)
{
return E_INVALIDARG;
}
INT64 es5DateAsInt64 = NumberUtilities::TryToInt64(es5Date);
if (!NumberUtilities::IsValidTryToInt64(es5DateAsInt64)) return INTSAFE_E_ARITHMETIC_OVERFLOW;
INT64 numTicks;
// We use the LongLong* functions since that's typedef'd to int64
// First, we rebase it to the WinRT epoch, then we convert the time in milliseconds to ticks
if (SUCCEEDED(::Int64Add(es5DateAsInt64, jsEpochMilliseconds, reinterpret_cast<LONGLONG*>(&numTicks))))
{
INT64 adjustedTicks = 0;
if (SUCCEEDED(::Int64Mult(numTicks, ticksPerMillisecond, reinterpret_cast<LONGLONG*>(&adjustedTicks))))
{
(*pRet) = adjustedTicks;
return S_OK;
}
}
return INTSAFE_E_ARITHMETIC_OVERFLOW;
}
//
// Version 6 Change:
// Previously we would round the TimeSpan to ms precision in order to avoid having the double contain decimal digits.
// Now we allow for the timespan to be represented with integers and digits (no truncation).
//
HRESULT DateUtilities::WinRTTimeSpanToNumberV6(INT64 ticks, __out double* pRet)
{
Assert(pRet != NULL);
if (pRet == NULL)
{
return E_INVALIDARG;
}
// We want to preserve precision as best we could, and for low enough timespan values
// Hence perform a division of doubles, to convert from ticks to milliseconds.
double result = (double)ticks / ticksPerMillisecondDouble;
*pRet = result;
return S_OK;
}
//
// Version 6 Change:
// Same as for WinRTTimeSpanToNumberV6, remove truncation when converting between Number and WinRT TimeSpan.
//
HRESULT DateUtilities::NumberToWinRTTimeSpanV6(double span, __out INT64* pRet)
{
Assert(pRet != NULL);
if (pRet == NULL)
{
return E_INVALIDARG;
}
//Otherwise the double multiplication might overflow
if (span > MAXINT64 / ticksPerMillisecond)
{
return INTSAFE_E_ARITHMETIC_OVERFLOW;
}
//Multiply before converting to Int64, in order to get the 100-nanosecond precision which will get truncated
INT64 spanAsInt64 = NumberUtilities::TryToInt64(span * ticksPerMillisecondDouble);
if (!NumberUtilities::IsValidTryToInt64(spanAsInt64))
{
return INTSAFE_E_ARITHMETIC_OVERFLOW;
}
(*pRet) = spanAsInt64;
return S_OK;
}
///------------------------------------------------------------------------------
/// Get a time value from SYSTEMTIME structure.
///
/// Returns number of milliseconds since Jan 1, 1970
///------------------------------------------------------------------------------
double
DateUtilities::TimeFromSt(SYSTEMTIME *pst)
{
return TvFromDate(pst->wYear,pst->wMonth-1,pst->wDay-1, DayTimeFromSt(pst));
}
///------------------------------------------------------------------------------
/// Get a time value from SYSTEMTIME structure within a day
///
/// Returns number of milliseconds since 12:00 AM
///------------------------------------------------------------------------------
double
DateUtilities::DayTimeFromSt(SYSTEMTIME *pst)
{
return (pst->wHour * 3600000.0) + (pst->wMinute * 60000.0) + (pst->wSecond * 1000.0) + pst->wMilliseconds;
}
///------------------------------------------------------------------------------
/// Get a time value from (year, mon, day, time) values.
///------------------------------------------------------------------------------
double
DateUtilities::TvFromDate(double year, double mon, double day, double time)
{
// For positive month, use fast path: '/' and '%' rather than 'floor()' and 'fmod()'.
// But make sure there is no overflow when casting double -> int -- WOOB 1142298.
if (mon >= 0 && mon <= INT_MAX)
{
year += ((int)mon) / 12;
mon = ((int)mon) % 12;
}
else
{
year += floor(mon/12);
mon = DblModPos(mon,12);
}
day += DayFromYear(year);
AssertMsg(mon >= 0 && mon <= 11, "'mon' must be in the range of [0..11].");
day += g_rgday[(int)mon];
if (mon >= 2 && !FLeap((int)year))
{
day -= 1;
}
return day * 86400000 + time;
}
///------------------------------------------------------------------------------
/// Get the non-negative remainder.
///------------------------------------------------------------------------------
double
DateUtilities::DblModPos(double dbl, double dblDen)
{
AssertMsg(dblDen > 0, "value not positive");
dbl = fmod(dbl, dblDen);
if (dbl < 0)
{
dbl += dblDen;
}
AssertMsg(dbl >= 0 && dbl < dblDen, "");
return dbl;
}
///------------------------------------------------------------------------------
/// DayFromYear is:
/// 365 * y + floor((y+1)/4) - floor((y+69)/100) + floor((y+369)/400).
/// where y is the calendar year minus 1970.
///------------------------------------------------------------------------------
double
DateUtilities::DayFromYear(double year)
{
double day = 365 * (year -= 1970);
if (day > 0)
{
day += ((int)((year + 1) / 4)) - ((int)((year + 69) / 100)) +
((int)((year + 369) / 400));
}
else
{
day += floor((year + 1) / 4) - floor((year + 69) / 100) +
floor((year + 369) / 400);
}
return day;
}
///------------------------------------------------------------------------------
/// Return whether the given year is a leap year.
///------------------------------------------------------------------------------
bool
DateUtilities::FLeap(int year)
{
return (0 == (year & 3)) && (0 != (year % 100) || 0 == (year % 400));
}
///------------------------------------------------------------------------------
/// Get the first day of the year, and if the first day is bigger than the passed day, then get the first day of the previous year.
///------------------------------------------------------------------------------
/*static*/
int DateUtilities::GetDayMinAndUpdateYear(int day, int &year)
{
int dayMin = (int)DayFromYear(year);
if (day < dayMin)
{
year--;
dayMin = (int)DayFromYear(year);
}
return dayMin;
}
///------------------------------------------------------------------------------
/// Converts the time value relative to Jan 1, 1970 into a YMD.
///
/// The year number y and day number d relative to Jan 1, 1970 satisfy the
/// inequalities:
/// floor((400*d-82)/146097) <= y <= floor((400*d+398)/146097)
/// These inequalities get us within one of the correct answer for the year.
/// We then use DayFromYear to adjust if necessary.
///------------------------------------------------------------------------------
void
DateUtilities::GetYmdFromTv(double tv, Js::YMD *pymd)
{
// AssertMem(pymd);
int day;
int dayMin;
int yday;
if (tv > 0)
{
day = (int)(tv / 86400000);
pymd->time = (int)DblModPos(tv, 86400000);
pymd->wday = (day + 4) % 7;
pymd->year = 1970 + (int)((400 * (double)day + 398) / 146097);
dayMin = GetDayMinAndUpdateYear(day, pymd->year);
pymd->yt = (int)((dayMin + 4) % 7);
}
else
{
day = (int)floor(tv / 86400000);
pymd->time = (int)DblModPos(tv, 86400000);
pymd->wday = (int)DblModPos(day + 4, 7);
pymd->year = 1970 + (int)floor(((400 * (double)day + 398) / 146097));
dayMin = GetDayMinAndUpdateYear(day, pymd->year);
pymd->yt = (int)DblModPos(dayMin + 4, 7);
}
yday = (int)(day - dayMin);
// Assert(yday >= 0 && (yday < 365 || yday == 365 && FLeap(pymd->year)));
pymd->yday = yday;
if (FLeap(pymd->year))
{
pymd->yt += 7;
}
else if (yday >= 59)
{
yday++;
}
// Get the month.
if (yday < 182)
{
if (yday < 60)
{
pymd->mon = 0 + ((yday >= 31) ? 1 : 0);
}
else if (yday < 121)
{
pymd->mon = 2 + ((yday >= 91) ? 1 : 0);
}
else
{
pymd->mon = 4 + ((yday >= 152) ? 1 : 0);
}
}
else
{
if (yday < 244)
{
pymd->mon = 6 + ((yday >= 213) ? 1 : 0);
}
else if (yday < 305)
{
pymd->mon = 8 + ((yday >= 274) ? 1 : 0);
}
else
{
pymd->mon = 10 + ((yday >= 335) ? 1 : 0);
}
}
// Assert(pymd->mon >= 0 && pymd->mon < 12);
pymd->mday = yday - g_rgday[pymd->mon];
}
void DateUtilities::GetYearFromTv(double tv, int &year, int &yearType)
{
// AssertMem(pymd);
int day;
int dayMin;
if (tv > 0)
{
day = (int)(tv / 86400000);
year = 1970 + (int)((400 * (double)day + 398) / 146097);
dayMin = GetDayMinAndUpdateYear(day, year);
yearType = (int)((dayMin + 4) % 7);
}
else
{
day = (int)floor(tv / 86400000);
year = 1970 + (int)floor(((400 * (double)day + 398) / 146097));
dayMin = GetDayMinAndUpdateYear(day, year);
yearType = (int)DblModPos(dayMin + 4, 7);
}
if (FLeap(year))
{
yearType += 7;
}
}
double DateUtilities::JsLocalTimeFromVarDate(double dbl)
{
// So that the arithmetic works even for negative dates, convert the
// date to the _actual number of days_ since 0000h 12/30/1899.
if (dbl < 0.0)
dbl = 2.0 * ceil(dbl) - dbl;
// Get the local time value.
dbl = (dbl - g_kdblJanuary1st1970) * 86400000;
if (NumberUtilities::IsNan(dbl))
{
return dbl;
}
return NumberUtilities::IsFinite(dbl) ? floor( dbl + 0.5) : dbl;
}
}