forked from chakra-core/ChakraCore
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDateImplementation.h
More file actions
498 lines (429 loc) · 18.3 KB
/
Copy pathDateImplementation.h
File metadata and controls
498 lines (429 loc) · 18.3 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
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
//-------------------------------------------------------------------------------------------------------
// Copyright (C) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
//-------------------------------------------------------------------------------------------------------
#pragma once
struct _SYSTEMTIME;
using namespace PlatformAgnostic;
namespace Js {
struct SZS;
class DateImplementation:
public DateUtilities
{
friend class JavascriptDate;
typedef struct tm TM;
static const short kpstDstRuleChangeYear = 2007;
static const double ktvMax;
static const double ktvMin;
static const int g_mpytyear[14];
static const int g_mpytyearpost2006[14] ;
double GetMilliSeconds();
static double NowInMilliSeconds(ScriptContext* scriptContext);
static double NowFromHiResTimer(ScriptContext * scriptContext);
static double UtcTimeFromStr(ScriptContext *scriptContext, JavascriptString *pParseString);
static double DoubleToTvUtc(double tv);
private:
DateImplementation(VirtualTableInfoCtorEnum) { m_modified = false; }
DateImplementation(double value);
BEGIN_ENUM_BYTE(DateStringFormat)
Default,
Locale,
GMT,
Lim,
END_ENUM_BYTE()
BEGIN_ENUM_BYTE(DateValueType)
Local = 1, // Whether tvLcl and tzd are valid.
YearMonthDayLocal = 2,
YearMonthDayUTC = 4,
NotNaN = 8,
END_ENUM_BYTE()
// date data
BEGIN_ENUM_BYTE(DateData)
// WARNING: There are tables that depend on this order.
Year,
FullYear,
Month,
Date,
Hours,
Minutes,
Seconds,
Milliseconds,
Day,
TimezoneOffset,
Lim,
END_ENUM_BYTE()
public:
// Flags which control if date or time or both need to be included.
BEGIN_ENUM_BYTE(DateTimeFlag)
None = 0x00,
NoTime = 0x01,
NoDate = 0x02,
END_ENUM_BYTE()
// Time zone descriptor.
struct TZD
{
Field(int) minutes;
Field(int) offset;
// Indicates whether Daylight savings
Field(bool) fDst;
};
template <class ScriptContext>
static double GetTvLcl(double tv, ScriptContext * scriptContext, TZD *ptzd = nullptr);
template <class ScriptContext>
static double GetTvUtc(double tv, ScriptContext * scriptContext);
static bool UtcTimeFromStrCore(
__in_ecount_z(ulength) const char16 *psz,
unsigned int ulength,
double &retVal,
ScriptContext * const scriptContext);
void SetTvUtc(double tv);
bool IsModified() { return m_modified; }
void ClearModified() { m_modified = false; }
private:
JavascriptString* GetString(DateStringFormat dsf, ScriptContext* requestContext,
DateTimeFlag noDateTime = DateTimeFlag::None);
JavascriptString* GetISOString(ScriptContext* requestContext);
void GetDateComponent(CompoundString *bs, DateData componentType, int adjust,
ScriptContext* requestContext);
void SetTvLcl(double tv, ScriptContext* requestContext);
double GetDateData(DateData dd, bool fUtc, ScriptContext* scriptContext);
double SetDateData(Arguments args, DateData dd, bool fUtc, ScriptContext* scriptContext);
static bool TryParseDecimalDigits(
const char16 *const str,
const size_t length,
const size_t startIndex,
size_t numDigits,
int &value);
static bool TryParseMilliseconds(
const char16 *const str,
const size_t length,
const size_t startIndex,
int &value,
size_t &foundDigits);
static bool TryParseTwoDecimalDigits(
const char16 *const str,
const size_t length,
const size_t startIndex,
int &value,
bool canHaveTrailingDigit = false);
// Tries to parse the string as an ISO-format date/time as defined in the spec. Returns false if the string is not in
// ISO format.
static bool TryParseIsoString(const char16 *const str, const size_t length, double &timeValue, ScriptContext *scriptContext);
static JavascriptString* GetDateDefaultString(DateTime::YMD *pymd, TZD *ptzd,DateTimeFlag noDateTime,ScriptContext* scriptContext);
static JavascriptString* GetDateGmtString(DateTime::YMD *pymd,ScriptContext* scriptContext);
#ifdef ENABLE_GLOBALIZATION // todo-xplat: Implement this ICU?
static JavascriptString* GetDateLocaleString(DateTime::YMD *pymd, TZD *ptzd, DateTimeFlag noDateTime,ScriptContext* scriptContext);
#endif
static double DateFncUTC(ScriptContext* scriptContext, Arguments args);
static bool FBig(char16 ch);
static bool FDateDelimiter(char16 ch);
bool IsNaN()
{
if (m_grfval & DateValueType::NotNaN)
{
return false;
}
if (JavascriptNumber::IsNan(m_tvUtc))
{
return true;
}
m_grfval |= DateValueType::NotNaN;
return false;
}
///------------------------------------------------------------------------------
/// Make sure m_tvLcl is valid. (Shared with hybrid debugging, which may use a fake scriptContext.)
///------------------------------------------------------------------------------
template <class ScriptContext>
inline void EnsureTvLcl(ScriptContext* scriptContext)
{
if (!(m_grfval & DateValueType::Local))
{
m_tvLcl = GetTvLcl(m_tvUtc, scriptContext, &m_tzd);
m_grfval |= DateValueType::Local;
}
}
///------------------------------------------------------------------------------
/// Make sure m_ymdLcl is valid. (Shared with hybrid debugging, which may use a fake scriptContext.)
///------------------------------------------------------------------------------
template <class ScriptContext>
inline void EnsureYmdLcl(ScriptContext* scriptContext)
{
if (m_grfval & DateValueType::YearMonthDayLocal)
{
return;
}
EnsureTvLcl(scriptContext);
GetYmdFromTv(m_tvLcl, &m_ymdLcl);
m_grfval |= DateValueType::YearMonthDayLocal;
}
///------------------------------------------------------------------------------
/// Make sure m_ymdUtc is valid.
///------------------------------------------------------------------------------
inline void EnsureYmdUtc(void)
{
if (m_grfval & DateValueType::YearMonthDayUTC)
{
return;
}
GetYmdFromTv(m_tvUtc, &m_ymdUtc);
m_grfval |= DateValueType::YearMonthDayUTC;
}
inline Var GetFullYear(ScriptContext* requestContext)
{
EnsureYmdLcl(requestContext);
return JavascriptNumber::ToVar(m_ymdLcl.year, requestContext);
}
inline Var GetYear(ScriptContext* requestContext)
{
EnsureYmdLcl(requestContext);
// WOOB bug 1099381: ES5 spec B.2.4: getYear() must return YearFromTime() - 1900.
// Note that negative value is OK for the spec.
int value = m_ymdLcl.year - 1900;
return JavascriptNumber::ToVar(value, requestContext);
}
inline Var GetMonth(ScriptContext* requestContext)
{
EnsureYmdLcl(requestContext);
return JavascriptNumber::ToVar(m_ymdLcl.mon, requestContext);
}
inline Var GetDate(ScriptContext* requestContext)
{
EnsureYmdLcl(requestContext);
return JavascriptNumber::ToVar(m_ymdLcl.mday + 1, requestContext);
}
inline Var GetDay(ScriptContext* requestContext)
{
EnsureYmdLcl(requestContext);
return JavascriptNumber::ToVar(m_ymdLcl.wday, requestContext);
}
inline Var GetHours(ScriptContext* requestContext)
{
EnsureYmdLcl(requestContext);
return JavascriptNumber::ToVar((m_ymdLcl.time / 3600000)%24, requestContext);
}
inline Var GetMinutes(ScriptContext* requestContext)
{
EnsureYmdLcl(requestContext);
return JavascriptNumber::ToVar((m_ymdLcl.time / 60000) % 60, requestContext);
}
inline Var GetSeconds(ScriptContext* requestContext)
{
EnsureYmdLcl(requestContext);
return JavascriptNumber::ToVar((m_ymdLcl.time / 1000) % 60, requestContext);
}
inline Var GetDateMilliSeconds(ScriptContext* requestContext)
{
EnsureYmdLcl(requestContext);
return JavascriptNumber::ToVar(m_ymdLcl.time % 1000, requestContext);
}
template <class StringBuilder, class ScriptContext, class NewStringBuilderFunc>
StringBuilder* GetDiagValueString(ScriptContext* scriptContext, NewStringBuilderFunc newStringBuilder);
template <class StringBuilder, class ScriptContext, class NewStringBuilderFunc>
static StringBuilder* GetDateDefaultString(DateTime::YMD *pymd, TZD *ptzd, DateTimeFlag noDateTime, ScriptContext* scriptContext, NewStringBuilderFunc newStringBuilder);
private:
Field(double) m_tvUtc;
Field(double) m_tvLcl;
FieldNoBarrier(DateTime::YMD) m_ymdUtc;
FieldNoBarrier(DateTime::YMD) m_ymdLcl;
Field(TZD) m_tzd;
Field(uint32) m_grfval; // Which fields are valid. m_tvUtc is always valid.
Field(bool) m_modified : 1; // Whether SetDateData was called on this class
friend JavascriptDate;
};
///
/// Use tv as the UTC time and return the corresponding local time. (Shared with hybrid debugging, which may use a fake scriptContext.)
///
template <class ScriptContext>
double DateImplementation::GetTvLcl(double tv, ScriptContext *scriptContext, TZD *ptzd)
{
Assert(scriptContext);
double tvLcl;
if (nullptr != ptzd)
{
ptzd->minutes = 0;
ptzd->fDst = FALSE;
}
// See if we're out of range before conversion (UTC time value must be within this range)
if (JavascriptNumber::IsNan(tv) || tv < ktvMin || tv > ktvMax)
{
return JavascriptNumber::NaN;
}
int bias;
int offset;
bool isDaylightSavings;
tvLcl = scriptContext->GetDaylightTimeHelper()->UtcToLocal(tv, bias, offset, isDaylightSavings);
if (nullptr != ptzd)
{
ptzd->minutes = -bias;
ptzd->offset = offset;
ptzd->fDst = isDaylightSavings;
}
return tvLcl;
}
///
/// Use tv as the local time and return the corresponding UTC time. (Shared with hybrid debugging, which may use a fake scriptContext.)
///
template <class ScriptContext>
double DateImplementation::GetTvUtc(double tv, ScriptContext *scriptContext)
{
Assert(scriptContext);
double tvUtc;
if (JavascriptNumber::IsNan(tv) || !NumberUtilities::IsFinite(tv))
{
return JavascriptNumber::NaN;
}
tvUtc = scriptContext->GetDaylightTimeHelper()->LocalToUtc(tv);
// See if we're out of range after conversion (UTC time value must be within this range)
if (JavascriptNumber::IsNan(tvUtc) || !NumberUtilities::IsFinite(tv) || tvUtc < ktvMin || tvUtc > ktvMax)
{
return JavascriptNumber::NaN;
}
return tvUtc;
}
//
// Get diag value display for hybrid debugging.
// StringBuilder: A Js::StringBuilder/CompoundString like class, used to build strings.
// ScriptContext: A Js::ScriptContext like class, which provides fields needed by Date stringify.
// NewStringBuilderFunc: A function that returns a StringBuilder*, used to create a StringBuilder.
//
template <class StringBuilder, class ScriptContext, class NewStringBuilderFunc>
StringBuilder* DateImplementation::GetDiagValueString(ScriptContext* scriptContext, NewStringBuilderFunc newStringBuilder)
{
if (JavascriptNumber::IsNan(m_tvUtc))
{
StringBuilder* bs = newStringBuilder(0);
bs->Append(JS_DISPLAY_STRING_INVALID_DATE);
return bs;
}
EnsureYmdLcl(scriptContext);
return GetDateDefaultString<StringBuilder>(&m_ymdLcl, &m_tzd, DateTimeFlag::None, scriptContext, newStringBuilder);
}
const auto ConvertUInt32ToString_ZeroPad_4 = [](const uint32 value, char16 *const buffer, const CharCount charCapacity)
{
Assert(charCapacity >= 4);
if (value < 10)
{
buffer[0] = _u('0');
buffer[1] = _u('0');
buffer[2] = _u('0');
buffer[3] = static_cast<char16>(value + _u('0'));
buffer[4] = 0;
}
else if (value < 100)
{
buffer[0] = _u('0');
buffer[1] = _u('0');
buffer[2] = static_cast<char16>((value / 10) + _u('0'));
buffer[3] = static_cast<char16>((value % 10) + _u('0'));
buffer[4] = 0;
}
else if (value < 1000)
{
buffer[0] = _u('0');
buffer[1] = static_cast<char16>((value / 100) + _u('0'));
buffer[2] = static_cast<char16>(((value / 10) % 10) + _u('0'));
buffer[3] = static_cast<char16>((value % 10) + _u('0'));
buffer[4] = 0;
}
else
{
const errno_t err = _ultow_s(value, buffer, charCapacity, 10);
Assert(err == 0);
}
};
//
// Get default date string, shared with hybrid debugging.
// StringBuilder: A Js::StringBuilder/CompoundString like class, used to build strings.
// ScriptContext: A Js::ScriptContext like class, which provides fields needed by Date stringify.
// NewStringBuilderFunc: A function that returns a StringBuilder*, used to create a StringBuilder.
//
template <class StringBuilder, class ScriptContext, class NewStringBuilderFunc>
StringBuilder* DateImplementation::GetDateDefaultString(DateTime::YMD *pymd, TZD *ptzd, DateTimeFlag noDateTime, ScriptContext* scriptContext, NewStringBuilderFunc newStringBuilder)
{
int hour, min;
StringBuilder* const bs = newStringBuilder(72);
const auto ConvertUInt16ToString_ZeroPad_2 = [](const uint16 value, char16 *const buffer, const CharCount charCapacity)
{
const charcount_t cchWritten = NumberUtilities::UInt16ToString(value, buffer, charCapacity, 2);
Assert(cchWritten != 0);
};
const auto ConvertLongToString = [](const int32 value, char16 *const buffer, const CharCount charCapacity)
{
const errno_t err = _ltow_s(value, buffer, charCapacity, 10);
Assert(err == 0);
};
// PART 1 - DATE part
if( !(noDateTime & DateTimeFlag::NoDate))
{
bs->AppendChars(g_rgpszDay[pymd->wday]);
bs->AppendChars(_u(' '));
bs->AppendChars(g_rgpszMonth[pymd->mon]);
bs->AppendChars(_u(' '));
// sz - as %02d - output is "01" to "31"
bs->AppendChars(static_cast<WORD>(pymd->mday + 1), 2, ConvertUInt16ToString_ZeroPad_2);
bs->AppendChars(_u(' '));
// year is directly after day, month, daydigit for IE11+
if ((pymd->year) > 0)
{
bs->AppendChars(pymd->year, 10, ConvertUInt32ToString_ZeroPad_4);
}
else
{
int positiveYear = -(pymd->year); // pymd->year is negative
bs->AppendChars(_u('-'));
bs->AppendChars(positiveYear, 10, ConvertUInt32ToString_ZeroPad_4);
}
if(!(noDateTime & DateTimeFlag::NoTime))
{
// append a space to delimit PART 2 - if to be outputted
bs->AppendChars(_u(' '));
}
}
// PART 2 - TIME part
if(!(noDateTime & DateTimeFlag::NoTime))
{
// sz - as %02d - HOUR
bs->AppendChars(static_cast<WORD>(pymd->time / 3600000), 2, ConvertUInt16ToString_ZeroPad_2);
bs->AppendChars(_u(':'));
// sz - as %02d - MINUTE
bs->AppendChars(static_cast<WORD>((pymd->time / 60000) % 60), 2, ConvertUInt16ToString_ZeroPad_2);
bs->AppendChars(_u(':'));
// sz - as %02d - SECOND
bs->AppendChars(static_cast<WORD>((pymd->time / 1000) % 60), 2, ConvertUInt16ToString_ZeroPad_2);
bs->AppendChars(_u(" GMT"));
// IE11+
min = ptzd->offset;
if (min < 0)
{
bs->AppendChars(_u('-'));
min = -min;
}
else
{
bs->AppendChars(_u('+'));
}
hour = min / 60;
min %= 60;
// sz - as %02d - HOUR
bs->AppendChars(static_cast<WORD>(hour), 2, ConvertUInt16ToString_ZeroPad_2);
// sz - as %02d - MIN
bs->AppendChars(static_cast<WORD>(min), 2, ConvertUInt16ToString_ZeroPad_2);
bs->AppendChars(_u(" ("));
// check the IsDaylightSavings?
if (ptzd->fDst == false)
{
size_t nameLength;
const WCHAR *const standardName = scriptContext->GetStandardName(&nameLength, pymd);
bs->AppendChars(standardName, static_cast<CharCount>(nameLength));
}
else
{
size_t nameLength;
const WCHAR *const daylightName = scriptContext->GetDaylightName(&nameLength, pymd);
bs->AppendChars(daylightName, static_cast<CharCount>(nameLength));
}
bs->AppendChars(_u(')'));
}
return bs;
}
} // namespace Js