Skip to content

Commit d03416c

Browse files
authored
Rework DateTime and TimeSpan (#3084)
***NO_CI***
1 parent ea43394 commit d03416c

6 files changed

+39
-80
lines changed

src/CLR/CorLib/corlib_native.cpp

+10-6
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//
1+
//
22
// Copyright (c) .NET Foundation and Contributors
33
// See LICENSE file in the project root for full license information.
44
//
@@ -435,8 +435,6 @@ static const CLR_RT_MethodHandler method_lookup[] =
435435
NULL,
436436
Library_corlib_native_System_DateTime::DaysInMonth___STATIC__I4__I4__I4,
437437
NULL,
438-
Library_corlib_native_System_DateTime::get_UtcNow___STATIC__SystemDateTime,
439-
Library_corlib_native_System_DateTime::get_Today___STATIC__SystemDateTime,
440438
NULL,
441439
NULL,
442440
NULL,
@@ -450,9 +448,13 @@ static const CLR_RT_MethodHandler method_lookup[] =
450448
NULL,
451449
NULL,
452450
NULL,
451+
NULL,
452+
Library_corlib_native_System_DateTime::GetUtcNowAsTicks___STATIC__I8,
453+
Library_corlib_native_System_DateTime::GetTodayAsTicks___STATIC__I8,
454+
NULL,
453455
Library_corlib_native_System_Convert::NativeToInt64___STATIC__I8__STRING__BOOLEAN__I8__I8__I4__BOOLEAN__BYREF_BOOLEAN,
454456
Library_corlib_native_System_Convert::NativeToDouble___STATIC__R8__STRING__BOOLEAN__BYREF_BOOLEAN,
455-
Library_corlib_native_System_Convert::NativeToDateTime___STATIC__SystemDateTime__STRING__BOOLEAN__BYREF_BOOLEAN,
457+
Library_corlib_native_System_Convert::NativeToDateTime___STATIC__VOID__STRING__BOOLEAN__BYREF_BOOLEAN__BYREF_SystemDateTime,
456458
NULL,
457459
NULL,
458460
NULL,
@@ -678,6 +680,7 @@ static const CLR_RT_MethodHandler method_lookup[] =
678680
NULL,
679681
NULL,
680682
NULL,
683+
NULL,
681684
Library_corlib_native_System_Runtime_CompilerServices_RuntimeHelpers::InitializeArray___STATIC__VOID__SystemArray__SystemRuntimeFieldHandle,
682685
Library_corlib_native_System_Runtime_CompilerServices_RuntimeHelpers::GetObjectValue___STATIC__OBJECT__OBJECT,
683686
Library_corlib_native_System_Runtime_CompilerServices_RuntimeHelpers::RunClassConstructor___STATIC__VOID__SystemRuntimeTypeHandle,
@@ -1356,6 +1359,7 @@ static const CLR_RT_MethodHandler method_lookup[] =
13561359
NULL,
13571360
NULL,
13581361
NULL,
1362+
NULL,
13591363
Library_corlib_native_System_Runtime_CompilerServices_RuntimeHelpers::InitializeArray___STATIC__VOID__SystemArray__SystemRuntimeFieldHandle,
13601364
Library_corlib_native_System_Runtime_CompilerServices_RuntimeHelpers::GetObjectValue___STATIC__OBJECT__OBJECT,
13611365
Library_corlib_native_System_Runtime_CompilerServices_RuntimeHelpers::RunClassConstructor___STATIC__VOID__SystemRuntimeTypeHandle,
@@ -1483,11 +1487,11 @@ const CLR_RT_NativeAssemblyData g_CLR_AssemblyNative_mscorlib =
14831487

14841488
#if (NANOCLR_REFLECTION == TRUE)
14851489

1486-
0x445C7AF9,
1490+
0xC5D5F755,
14871491

14881492
#elif (NANOCLR_REFLECTION == FALSE)
14891493

1490-
0xE3A4B52F,
1494+
0x29A4F740,
14911495

14921496
#else
14931497
#error "NANOCLR_REFLECTION has to be define either TRUE or FALSE. Check the build options."

src/CLR/CorLib/corlib_native.h

+4-6
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//
1+
//
22
// Copyright (c) .NET Foundation and Contributors
33
// Portions Copyright (c) Microsoft Corporation. All rights reserved.
44
// See LICENSE file in the project root for full license information.
@@ -467,7 +467,6 @@ struct Library_corlib_native_System_TimeSpan
467467

468468
//--//
469469

470-
static CLR_INT64 *NewObject(CLR_RT_HeapBlock &ref);
471470
static CLR_INT64 *GetValuePtr(CLR_RT_StackFrame &stack);
472471
static CLR_INT64 *GetValuePtr(CLR_RT_HeapBlock &ref);
473472

@@ -491,12 +490,11 @@ struct Library_corlib_native_System_DateTime
491490
NANOCLR_NATIVE_DECLARE(_ctor___VOID__I4__I4__I4__I4__I4__I4__I4);
492491
NANOCLR_NATIVE_DECLARE(GetDateTimePart___I4__SystemDateTimeDateTimePart);
493492
NANOCLR_NATIVE_DECLARE(DaysInMonth___STATIC__I4__I4__I4);
494-
NANOCLR_NATIVE_DECLARE(get_UtcNow___STATIC__SystemDateTime);
495-
NANOCLR_NATIVE_DECLARE(get_Today___STATIC__SystemDateTime);
493+
NANOCLR_NATIVE_DECLARE(GetUtcNowAsTicks___STATIC__I8);
494+
NANOCLR_NATIVE_DECLARE(GetTodayAsTicks___STATIC__I8);
496495

497496
//--//
498497

499-
static CLR_INT64 *NewObject(CLR_RT_HeapBlock &ref);
500498
static CLR_INT64 *GetValuePtr(CLR_RT_StackFrame &stack);
501499
static CLR_INT64 *GetValuePtr(CLR_RT_HeapBlock &ref);
502500

@@ -508,7 +506,7 @@ struct Library_corlib_native_System_Convert
508506
{
509507
NANOCLR_NATIVE_DECLARE(NativeToInt64___STATIC__I8__STRING__BOOLEAN__I8__I8__I4__BOOLEAN__BYREF_BOOLEAN);
510508
NANOCLR_NATIVE_DECLARE(NativeToDouble___STATIC__R8__STRING__BOOLEAN__BYREF_BOOLEAN);
511-
NANOCLR_NATIVE_DECLARE(NativeToDateTime___STATIC__SystemDateTime__STRING__BOOLEAN__BYREF_BOOLEAN);
509+
NANOCLR_NATIVE_DECLARE(NativeToDateTime___STATIC__VOID__STRING__BOOLEAN__BYREF_BOOLEAN__BYREF_SystemDateTime);
512510
NANOCLR_NATIVE_DECLARE(ToBase64String___STATIC__STRING__SZARRAY_U1__I4__I4__BOOLEAN);
513511
NANOCLR_NATIVE_DECLARE(FromBase64String___STATIC__SZARRAY_U1__STRING);
514512

src/CLR/CorLib/corlib_native_System_Convert.cpp

+6-9
Original file line numberDiff line numberDiff line change
@@ -518,12 +518,12 @@ HRESULT Library_corlib_native_System_Convert::NativeToDouble___STATIC__R8__STRIN
518518
NANOCLR_CLEANUP_END();
519519
}
520520

521-
HRESULT Library_corlib_native_System_Convert::NativeToDateTime___STATIC__SystemDateTime__STRING__BOOLEAN__BYREF_BOOLEAN(
522-
CLR_RT_StackFrame &stack)
521+
HRESULT Library_corlib_native_System_Convert::
522+
NativeToDateTime___STATIC__VOID__STRING__BOOLEAN__BYREF_BOOLEAN__BYREF_SystemDateTime(CLR_RT_StackFrame &stack)
523523
{
524524
NANOCLR_HEADER();
525525

526-
CLR_INT64 *pRes;
526+
CLR_INT64 *pTicks;
527527

528528
char *str = (char *)stack.Arg0().RecoverString();
529529
char *conversionResult = NULL;
@@ -533,14 +533,9 @@ HRESULT Library_corlib_native_System_Convert::NativeToDateTime___STATIC__SystemD
533533
// grab parameter with flag to throw on failure
534534
bool throwOnFailure = (bool)stack.Arg1().NumericByRefConst().u1;
535535

536-
CLR_RT_HeapBlock &ref = stack.PushValue();
537-
538536
// check string parameter for null
539537
FAULT_ON_NULL_ARG(str);
540538

541-
pRes = Library_corlib_native_System_DateTime::NewObject(ref);
542-
FAULT_ON_NULL(pRes);
543-
544539
// try 'u' Universal time with sortable format (yyyy-MM-dd' 'HH:mm:ss)
545540
conversionResult = Nano_strptime(str, "%Y-%m-%d %H:%M:%SZ", &ticks);
546541
if (conversionResult == NULL)
@@ -569,7 +564,9 @@ HRESULT Library_corlib_native_System_Convert::NativeToDateTime___STATIC__SystemD
569564
}
570565
else
571566
{
572-
*pRes = ticks;
567+
// get pointer to DateTime value type in parameter 3
568+
pTicks = DateTime::GetValuePtr(stack.Arg3());
569+
*pTicks = ticks;
573570
}
574571

575572
NANOCLR_CLEANUP();

src/CLR/CorLib/corlib_native_System_DateTime.cpp

+15-40
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//
1+
//
22
// Copyright (c) .NET Foundation and Contributors
33
// Portions Copyright (c) Microsoft Corporation. All rights reserved.
44
// See LICENSE file in the project root for full license information.
@@ -7,6 +7,11 @@
77
#include "CorLib.h"
88
#include <nanoCLR_Interop.h>
99

10+
/////////////////////////////////////////////////////////////////////////
11+
// !!! KEEP IN SYNC WITH DateTime._ticksAtOrigin (in managed code) !!! //
12+
/////////////////////////////////////////////////////////////////////////
13+
#define TICKS_AT_ORIGIN 504911232000000000
14+
1015
///////////////////////////////////////////////////////////////////////
1116
// !!! KEEP IN SYNC WITH DateTime.DateTimePart (in managed code) !!! //
1217
///////////////////////////////////////////////////////////////////////
@@ -147,60 +152,30 @@ HRESULT Library_corlib_native_System_DateTime::DaysInMonth___STATIC__I4__I4__I4(
147152
NANOCLR_NOCLEANUP();
148153
}
149154

150-
HRESULT Library_corlib_native_System_DateTime::get_UtcNow___STATIC__SystemDateTime(CLR_RT_StackFrame &stack)
155+
HRESULT Library_corlib_native_System_DateTime::GetUtcNowAsTicks___STATIC__I8(CLR_RT_StackFrame &stack)
151156
{
152-
NATIVE_PROFILE_CLR_CORE();
153157
NANOCLR_HEADER();
154158

155-
CLR_INT64 *val;
156-
157-
CLR_RT_HeapBlock &ref = stack.PushValue();
158-
159-
val = Library_corlib_native_System_DateTime::NewObject(ref);
160-
FAULT_ON_NULL(val);
161-
162-
// load with full date&time
163-
// including UTC flag
164-
*val = (CLR_INT64)(HAL_Time_CurrentDateTime(false) | s_UTCMask);
159+
CLR_INT64 value = HAL_Time_CurrentDateTime(false);
160+
value += TICKS_AT_ORIGIN;
161+
stack.SetResult_I8(value);
165162

166-
NANOCLR_NOCLEANUP();
163+
NANOCLR_NOCLEANUP_NOLABEL();
167164
}
168165

169-
HRESULT Library_corlib_native_System_DateTime::get_Today___STATIC__SystemDateTime(CLR_RT_StackFrame &stack)
166+
HRESULT Library_corlib_native_System_DateTime::GetTodayAsTicks___STATIC__I8(CLR_RT_StackFrame &stack)
170167
{
171-
NATIVE_PROFILE_CLR_CORE();
172168
NANOCLR_HEADER();
173169

174-
CLR_RT_HeapBlock &ref = stack.PushValue();
175-
176-
CLR_INT64 *val = NewObject(ref);
177-
178-
// load with date part only
179-
// including UTC flag
180-
*val = (CLR_INT64)(HAL_Time_CurrentDateTime(true) | s_UTCMask);
170+
CLR_INT64 value = HAL_Time_CurrentDateTime(true);
171+
value += TICKS_AT_ORIGIN;
172+
stack.SetResult_I8(value);
181173

182174
NANOCLR_NOCLEANUP_NOLABEL();
183175
}
184176

185177
//--//
186178

187-
CLR_INT64 *Library_corlib_native_System_DateTime::NewObject(CLR_RT_HeapBlock &ref)
188-
{
189-
NATIVE_PROFILE_CLR_CORE();
190-
191-
CLR_RT_TypeDescriptor dtType;
192-
193-
// initialize <DateTime> type descriptor
194-
dtType.InitializeFromType(g_CLR_RT_WellKnownTypes.m_DateTime);
195-
196-
// create an instance of <DateTime>
197-
g_CLR_RT_ExecutionEngine.NewObject(ref, dtType.m_handlerCls);
198-
199-
return GetValuePtr(ref);
200-
}
201-
202-
//--//
203-
204179
CLR_INT64 *Library_corlib_native_System_DateTime::GetValuePtr(CLR_RT_StackFrame &stack)
205180
{
206181
NATIVE_PROFILE_CLR_CORE();

src/CLR/CorLib/corlib_native_System_TimeSpan.cpp

+1-16
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//
1+
//
22
// Copyright (c) .NET Foundation and Contributors
33
// Portions Copyright (c) Microsoft Corporation. All rights reserved.
44
// See LICENSE file in the project root for full license information.
@@ -178,21 +178,6 @@ HRESULT Library_corlib_native_System_TimeSpan::Equals___STATIC__BOOLEAN__SystemT
178178

179179
//--//
180180

181-
CLR_INT64 *Library_corlib_native_System_TimeSpan::NewObject(CLR_RT_HeapBlock &ref)
182-
{
183-
NATIVE_PROFILE_CLR_CORE();
184-
185-
CLR_RT_TypeDescriptor dtType;
186-
187-
// initialize <DateTime> type descriptor
188-
dtType.InitializeFromType(g_CLR_RT_WellKnownTypes.m_TimeSpan);
189-
190-
// create an instance of <TimeSpan>
191-
g_CLR_RT_ExecutionEngine.NewObject(ref, dtType.m_handlerCls);
192-
193-
return GetValuePtr(ref);
194-
}
195-
196181
CLR_INT64 *Library_corlib_native_System_TimeSpan::GetValuePtr(CLR_RT_StackFrame &stack)
197182
{
198183
NATIVE_PROFILE_CLR_CORE();

src/CLR/Core/TypeSystemLookup.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//
1+
//
22
// Copyright (c) .NET Foundation and Contributors
33
// Portions Copyright (c) Microsoft Corporation. All rights reserved.
44
// See LICENSE file in the project root for full license information.
@@ -82,8 +82,8 @@ const CLR_RT_DataTypeLookup c_CLR_RT_DataTypeLookup[] =
8282
{ DT_NUM | DT_INT | DT_SGN | DT_PRIM | DT_DIR | DT_OPT | DT_MT, 64, DT_I8, DT_T(I8 ), DT_CNV(I8 ), DT_CLS(m_Int64 ), DT_NOREL(CLR_RT_HeapBlock ) DT_OPT_NAME(I8 ) }, // DATATYPE_I8
8383
{ DT_NUM | DT_INT | DT_PRIM | DT_DIR | DT_OPT | DT_MT, 64, DT_U8, DT_T(I8 ), DT_CNV(U8 ), DT_CLS(m_UInt64 ), DT_NOREL(CLR_RT_HeapBlock ) DT_OPT_NAME(U8 ) }, // DATATYPE_U8
8484
{ DT_NUM | DT_SGN | DT_PRIM | DT_DIR | DT_OPT | DT_MT, 64, DT_I8, DT_T(R8 ), DT_CNV(R8 ), DT_CLS(m_Double ), DT_NOREL(CLR_RT_HeapBlock ) DT_OPT_NAME(R8 ) }, // DATATYPE_R8
85-
{ DT_INT | DT_SGN | DT_VALUE | DT_DIR | DT_MT, 64, DT_BL, DT_T(DATETIME ), DT_CNV(END ), DT_CLS(m_DateTime ), DT_REL (CLR_RT_HeapBlock ::Relocate_Cls ) DT_OPT_NAME(DATETIME ) }, // DATATYPE_DATETIME
86-
{ DT_INT | DT_SGN | DT_VALUE | DT_DIR | DT_MT, 64, DT_BL, DT_T(TIMESPAN ), DT_CNV(END ), DT_CLS(m_TimeSpan ), DT_REL (CLR_RT_HeapBlock ::Relocate_Cls ) DT_OPT_NAME(TIMESPAN ) }, // DATATYPE_TIMESPAN
85+
{ DT_INT | DT_SGN | DT_VALUE | DT_DIR | DT_OPT | DT_MT, 64, DT_BL, DT_T(DATETIME ), DT_CNV(END ), DT_CLS(m_DateTime ), DT_REL (CLR_RT_HeapBlock ::Relocate_Cls ) DT_OPT_NAME(DATETIME ) }, // DATATYPE_DATETIME
86+
{ DT_INT | DT_SGN | DT_VALUE | DT_DIR | DT_OPT | DT_MT, 64, DT_BL, DT_T(TIMESPAN ), DT_CNV(END ), DT_CLS(m_TimeSpan ), DT_REL (CLR_RT_HeapBlock ::Relocate_Cls ) DT_OPT_NAME(TIMESPAN ) }, // DATATYPE_TIMESPAN
8787
{ DT_REF | DT_PRIM | DT_DIR | DT_MT, DT_VS, DT_BL, DT_T(STRING ), DT_CNV(STRING ), DT_CLS(m_String ), DT_REL (CLR_RT_HeapBlock ::Relocate_String ) DT_OPT_NAME(STRING ) }, // DATATYPE_STRING
8888
//
8989
{ DT_REF | DT_DIR | DT_MT, DT_NA, DT_BL, DT_T(OBJECT ), DT_CNV(OBJECT ), DT_CLS(m_Object ), DT_REL (CLR_RT_HeapBlock ::Relocate_Obj ) DT_OPT_NAME(OBJECT ) }, // DATATYPE_OBJECT

0 commit comments

Comments
 (0)