Skip to content

Commit 6f5ad53

Browse files
committed
Move DateTime to shared partition
Depends on dotnet/corefx#17484
1 parent c6ce217 commit 6f5ad53

File tree

7 files changed

+116
-33
lines changed

7 files changed

+116
-33
lines changed

src/mscorlib/System.Private.CoreLib.csproj

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -326,7 +326,6 @@
326326
<Compile Include="$(BclSourcesRoot)\System\Text\StringBuilder.cs" />
327327
<Compile Include="$(BclSourcesRoot)\System\Text\StringBuilderCache.cs" />
328328
<Compile Include="$(BclSourcesRoot)\System\Exception.cs" />
329-
<Compile Include="$(BclSourcesRoot)\System\DateTime.cs" />
330329
<Compile Include="$(BclSourcesRoot)\System\DateTimeOffset.cs" />
331330
<Compile Include="$(BclSourcesRoot)\System\OutOfMemoryException.cs" />
332331
<Compile Include="$(BclSourcesRoot)\System\Delegate.cs" />
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
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+
using System;
6+
using System.Runtime.CompilerServices;
7+
using System.Runtime.InteropServices;
8+
9+
internal static partial class Interop
10+
{
11+
internal unsafe partial class Sys
12+
{
13+
[DllImport(Libraries.SystemNative, EntryPoint = "SystemNative_GetSystemTimeAsTicks")]
14+
internal static extern long GetSystemTimeAsTicks();
15+
}
16+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
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+
using System;
6+
using System.Runtime.InteropServices;
7+
8+
internal partial class Interop
9+
{
10+
internal partial class Kernel32
11+
{
12+
[DllImport(Libraries.Kernel32)]
13+
private extern static unsafe void GetSystemTimeAsFileTime(long* lpSystemTimeAsFileTime);
14+
15+
[DllImport(Libraries.Kernel32)]
16+
private extern static unsafe void GetSystemTimePreciseAsFileTime(long* lpSystemTimeAsFileTime);
17+
18+
private unsafe delegate void GetSystemTimeAsFileTimeFn(long* lpSystemTimeAsFileTime);
19+
20+
private static Lazy<GetSystemTimeAsFileTimeFn> _getSystemTimeAsFileTime = new Lazy<GetSystemTimeAsFileTimeFn>(GetGetSystemTimeAsFileTimeFn);
21+
22+
internal static unsafe long GetSystemTimeAsFileTime()
23+
{
24+
long ret;
25+
_getSystemTimeAsFileTime.Value(&ret);
26+
return ret;
27+
}
28+
29+
private static unsafe GetSystemTimeAsFileTimeFn GetGetSystemTimeAsFileTimeFn()
30+
{
31+
long tmp;
32+
try
33+
{
34+
GetSystemTimePreciseAsFileTime(&tmp);
35+
return GetSystemTimePreciseAsFileTime;
36+
}
37+
catch (EntryPointNotFoundException)
38+
{
39+
}
40+
return GetSystemTimeAsFileTime;
41+
}
42+
}
43+
}

src/mscorlib/shared/System.Private.CoreLib.Shared.projitems

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
<Compile Include="$(MSBuildThisFileDirectory)Interop\Windows\Kernel32\Interop.GetFileType_SafeHandle.cs"/>
3030
<Compile Include="$(MSBuildThisFileDirectory)Interop\Windows\Kernel32\Interop.GetFullPathNameW.cs"/>
3131
<Compile Include="$(MSBuildThisFileDirectory)Interop\Windows\Kernel32\Interop.GetLongPathNameW.cs"/>
32+
<Compile Include="$(MSBuildThisFileDirectory)Interop\Windows\Kernel32\Interop.GetSystemTimeAsFileTime.cs"/>
3233
<Compile Include="$(MSBuildThisFileDirectory)Interop\Windows\Kernel32\Interop.GetTempFileNameW.cs"/>
3334
<Compile Include="$(MSBuildThisFileDirectory)Interop\Windows\Kernel32\Interop.GetTempPathW.cs"/>
3435
<Compile Include="$(MSBuildThisFileDirectory)Interop\Windows\Kernel32\Interop.LockFile.cs"/>
@@ -52,6 +53,7 @@
5253
<Compile Include="$(MSBuildThisFileDirectory)Interop\Unix\System.Native\Interop.FSync.cs"/>
5354
<Compile Include="$(MSBuildThisFileDirectory)Interop\Unix\System.Native\Interop.FTruncate.cs"/>
5455
<Compile Include="$(MSBuildThisFileDirectory)Interop\Unix\System.Native\Interop.GetCwd.cs"/>
56+
<Compile Include="$(MSBuildThisFileDirectory)Interop\Unix\System.Native\Interop.GetSystemTimeAsTicks.cs"/>
5557
<Compile Include="$(MSBuildThisFileDirectory)Interop\Unix\System.Native\Interop.LockFileRegion.cs"/>
5658
<Compile Include="$(MSBuildThisFileDirectory)Interop\Unix\System.Native\Interop.LSeek.cs"/>
5759
<Compile Include="$(MSBuildThisFileDirectory)Interop\Unix\System.Native\Interop.MksTemps.cs"/>
@@ -106,6 +108,7 @@
106108
<Compile Include="$(MSBuildThisFileDirectory)System\Collections\IStructuralEquatable.cs"/>
107109
<Compile Include="$(MSBuildThisFileDirectory)System\ComponentModel\EditorBrowsableAttribute.cs"/>
108110
<Compile Include="$(MSBuildThisFileDirectory)System\DataMisalignedException.cs"/>
111+
<Compile Include="$(MSBuildThisFileDirectory)System\DateTime.cs"/>
109112
<Compile Include="$(MSBuildThisFileDirectory)System\DateTimeKind.cs"/>
110113
<Compile Include="$(MSBuildThisFileDirectory)System\DayOfWeek.cs"/>
111114
<Compile Include="$(MSBuildThisFileDirectory)System\DefaultBinder.cs"/>
@@ -233,6 +236,7 @@
233236
<Compile Include="$(MSBuildThisFileDirectory)Microsoft\Win32\SafeHandles\CriticalHandleZeroOrMinusOneIsInvalid.cs"/>
234237
</ItemGroup>
235238
<ItemGroup Condition="$(TargetsWindows)">
239+
<Compile Include="$(MSBuildThisFileDirectory)System\DateTime.Windows.cs"/>
236240
<Compile Include="$(MSBuildThisFileDirectory)System\IO\FileStream.Win32.cs" Condition="'$(IsProjectNLibrary)' != 'true'" />
237241
<Compile Include="$(MSBuildThisFileDirectory)System\IO\FileStream.WinRT.cs" Condition="'$(IsProjectNLibrary)' == 'true'" />
238242
<Compile Include="$(MSBuildThisFileDirectory)System\IO\FileStream.Windows.cs" />
@@ -245,6 +249,7 @@
245249
<Compile Include="$(MSBuildThisFileDirectory)Microsoft\Win32\SafeHandles\SafeFileHandle.Windows.cs" />
246250
</ItemGroup>
247251
<ItemGroup Condition="$(TargetsUnix)">
252+
<Compile Include="$(MSBuildThisFileDirectory)System\DateTime.Unix.cs"/>
248253
<Compile Include="$(MSBuildThisFileDirectory)System\IO\FileStream.Unix.cs"/>
249254
<Compile Include="$(MSBuildThisFileDirectory)System\IO\Path.Unix.cs"/>
250255
<Compile Include="$(MSBuildThisFileDirectory)System\IO\PathInternal.Unix.cs"/>
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
using System.Diagnostics.Contracts;
2+
3+
namespace System
4+
{
5+
public partial struct DateTime
6+
{
7+
public static unsafe DateTime UtcNow
8+
{
9+
get
10+
{
11+
Contract.Ensures(Contract.Result<DateTime>().Kind == DateTimeKind.Utc);
12+
13+
// For performance, use a private constructor that does not validate arguments.
14+
return new DateTime(((ulong)(Interop.Sys.GetSystemTimeAsTicks() + TicksTo1970)) | KindUtc);
15+
}
16+
}
17+
}
18+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
using System.Diagnostics.Contracts;
2+
3+
namespace System
4+
{
5+
public partial struct DateTime
6+
{
7+
public static unsafe DateTime UtcNow
8+
{
9+
get
10+
{
11+
Contract.Ensures(Contract.Result<DateTime>().Kind == DateTimeKind.Utc);
12+
// following code is tuned for speed. Don't change it without running benchmark.
13+
long ticks = Interop.Kernel32.GetSystemTimeAsFileTime();
14+
15+
return new DateTime(((UInt64)(ticks + FileTimeOffset)) | KindUtc);
16+
}
17+
}
18+
}
19+
}

src/mscorlib/src/System/DateTime.cs renamed to src/mscorlib/shared/System/DateTime.cs

Lines changed: 15 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,21 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33
// See the LICENSE file in the project root for more information.
44

5+
using System;
6+
using System.Threading;
7+
using System.Globalization;
8+
using System.Runtime;
9+
using System.Runtime.InteropServices;
10+
using System.Runtime.CompilerServices;
11+
using System.Runtime.Serialization;
12+
using System.Runtime.Versioning;
13+
using System.Security;
14+
using System.Diagnostics.Contracts;
15+
using CultureInfo = System.Globalization.CultureInfo;
16+
using Calendar = System.Globalization.Calendar;
17+
518
namespace System
619
{
7-
using System;
8-
using System.Threading;
9-
using System.Globalization;
10-
using System.Runtime;
11-
using System.Runtime.InteropServices;
12-
using System.Runtime.CompilerServices;
13-
using System.Runtime.Serialization;
14-
using System.Runtime.Versioning;
15-
using System.Security;
16-
using System.Diagnostics.Contracts;
17-
using CultureInfo = System.Globalization.CultureInfo;
18-
using Calendar = System.Globalization.Calendar;
1920

2021
// This value type represents a date and time. Every DateTime
2122
// object has a private field (Ticks) of type Int64 that stores the
@@ -52,7 +53,7 @@ namespace System
5253
//
5354
[StructLayout(LayoutKind.Auto)]
5455
[Serializable]
55-
public struct DateTime : IComparable, IFormattable, IConvertible, IComparable<DateTime>, IEquatable<DateTime>, ISerializable
56+
public partial struct DateTime : IComparable, IFormattable, IConvertible, IComparable<DateTime>, IEquatable<DateTime>, ISerializable
5657
{
5758
// Number of 100ns ticks per time unit
5859
private const long TicksPerMillisecond = 10000;
@@ -89,6 +90,7 @@ public struct DateTime : IComparable, IFormattable, IConvertible, IComparable<Da
8990
internal const long MaxTicks = DaysTo10000 * TicksPerDay - 1;
9091
private const long MaxMillis = (long)DaysTo10000 * MillisPerDay;
9192

93+
private const long TicksTo1970 = DaysTo1970 * TicksPerDay;
9294
private const long FileTimeOffset = DaysTo1601 * TicksPerDay;
9395
private const long DoubleDateOffset = DaysTo1899 * TicksPerDay;
9496
// The minimum OA date is 0100/01/01 (Note it's year 100).
@@ -1005,25 +1007,6 @@ public static DateTime Now
10051007
}
10061008
}
10071009

1008-
public static DateTime UtcNow
1009-
{
1010-
get
1011-
{
1012-
Contract.Ensures(Contract.Result<DateTime>().Kind == DateTimeKind.Utc);
1013-
// following code is tuned for speed. Don't change it without running benchmark.
1014-
long ticks = 0;
1015-
ticks = GetSystemTimeAsFileTime();
1016-
1017-
return new DateTime(((UInt64)(ticks + FileTimeOffset)) | KindUtc);
1018-
}
1019-
}
1020-
1021-
1022-
[MethodImplAttribute(MethodImplOptions.InternalCall)]
1023-
internal static extern long GetSystemTimeAsFileTime();
1024-
1025-
1026-
10271010
// Returns the second part of this DateTime. The returned value is
10281011
// an integer between 0 and 59.
10291012
//

0 commit comments

Comments
 (0)