Move DateTime to shared partition (#10460)
authorAlex Perovich <alperovi@microsoft.com>
Sat, 25 Mar 2017 04:10:49 +0000 (23:10 -0500)
committerGitHub <noreply@github.com>
Sat, 25 Mar 2017 04:10:49 +0000 (23:10 -0500)
* Move DateTime to shared partition

Depends on dotnet/corefx#17484

* Use CoreCLR specific impl of UtcNow on windows

* Use the internal call on unix as well

* Port good corert changes

src/mscorlib/System.Private.CoreLib.csproj
src/mscorlib/shared/System.Private.CoreLib.Shared.projitems
src/mscorlib/shared/System/DateTime.cs [moved from src/mscorlib/src/System/DateTime.cs with 96% similarity]
src/mscorlib/src/System/DateTime.CoreCLR.cs [new file with mode: 0644]

index bac8456..5da3cdd 100644 (file)
     <Compile Include="$(BclSourcesRoot)\System\Text\StringBuilder.CoreCLR.cs" />
     <Compile Include="$(BclSourcesRoot)\System\Text\StringBuilderCache.cs" />
     <Compile Include="$(BclSourcesRoot)\System\Exception.cs" />
-    <Compile Include="$(BclSourcesRoot)\System\DateTime.cs" />
+    <Compile Include="$(BclSourcesRoot)\System\DateTime.CoreCLR.cs" />
     <Compile Include="$(BclSourcesRoot)\System\DateTimeOffset.cs" />
     <Compile Include="$(BclSourcesRoot)\System\OutOfMemoryException.cs" />
     <Compile Include="$(BclSourcesRoot)\System\Delegate.cs" />
index 3cc70b4..e8d180d 100644 (file)
     <Compile Include="$(MSBuildThisFileDirectory)System\ComponentModel\EditorBrowsableAttribute.cs"/>
     <Compile Include="$(MSBuildThisFileDirectory)System\Convert.cs"/>
     <Compile Include="$(MSBuildThisFileDirectory)System\DataMisalignedException.cs"/>
+    <Compile Include="$(MSBuildThisFileDirectory)System\DateTime.cs"/>
     <Compile Include="$(MSBuildThisFileDirectory)System\DateTimeKind.cs"/>
     <Compile Include="$(MSBuildThisFileDirectory)System\DayOfWeek.cs"/>
     <Compile Include="$(MSBuildThisFileDirectory)System\DBNull.cs"/>
similarity index 96%
rename from src/mscorlib/src/System/DateTime.cs
rename to src/mscorlib/shared/System/DateTime.cs
index 894e849..5e6332d 100644 (file)
@@ -2,20 +2,21 @@
 // The .NET Foundation licenses this file to you under the MIT license.
 // See the LICENSE file in the project root for more information.
 
+using System;
+using System.Threading;
+using System.Globalization;
+using System.Runtime;
+using System.Runtime.InteropServices;
+using System.Runtime.CompilerServices;
+using System.Runtime.Serialization;
+using System.Runtime.Versioning;
+using System.Security;
+using System.Diagnostics.Contracts;
+using CultureInfo = System.Globalization.CultureInfo;
+using Calendar = System.Globalization.Calendar;
+
 namespace System
 {
-    using System;
-    using System.Threading;
-    using System.Globalization;
-    using System.Runtime;
-    using System.Runtime.InteropServices;
-    using System.Runtime.CompilerServices;
-    using System.Runtime.Serialization;
-    using System.Runtime.Versioning;
-    using System.Security;
-    using System.Diagnostics.Contracts;
-    using CultureInfo = System.Globalization.CultureInfo;
-    using Calendar = System.Globalization.Calendar;
 
     // This value type represents a date and time.  Every DateTime 
     // object has a private field (Ticks) of type Int64 that stores the 
@@ -52,7 +53,7 @@ namespace System
     // 
     [StructLayout(LayoutKind.Auto)]
     [Serializable]
-    public struct DateTime : IComparable, IFormattable, IConvertible, IComparable<DateTime>, IEquatable<DateTime>, ISerializable
+    public partial struct DateTime : IComparable, IFormattable, IConvertible, IComparable<DateTime>, IEquatable<DateTime>, ISerializable
     {
         // Number of 100ns ticks per time unit
         private const long TicksPerMillisecond = 10000;
@@ -89,6 +90,7 @@ namespace System
         internal const long MaxTicks = DaysTo10000 * TicksPerDay - 1;
         private const long MaxMillis = (long)DaysTo10000 * MillisPerDay;
 
+        private const long TicksTo1970 = DaysTo1970 * TicksPerDay;
         private const long FileTimeOffset = DaysTo1601 * TicksPerDay;
         private const long DoubleDateOffset = DaysTo1899 * TicksPerDay;
         // The minimum OA date is 0100/01/01 (Note it's year 100).
@@ -123,7 +125,7 @@ namespace System
         private const Int32 KindShift = 62;
 
         private const String TicksField = "ticks";
-        private const String DateDataField = "dateData";
+        private const String DateDataField = "_dateData";
 
         // The data is stored as an unsigned 64-bit integeter
         //   Bits 01-62: The value of 100-nanosecond ticks where 0 represents 1/1/0001 12:00am, up until the value
@@ -133,7 +135,7 @@ namespace System
         //               savings time hour and it is in daylight savings time. This allows distinction of these
         //               otherwise ambiguous local times and prevents data loss when round tripping from Local to
         //               UTC time.
-        private UInt64 dateData;
+        private UInt64 _dateData;
 
         // Constructs a DateTime from a tick count. The ticks
         // argument specifies the date as the number of 100-nanosecond intervals
@@ -144,12 +146,12 @@ namespace System
             if (ticks < MinTicks || ticks > MaxTicks)
                 throw new ArgumentOutOfRangeException(nameof(ticks), SR.ArgumentOutOfRange_DateTimeBadTicks);
             Contract.EndContractBlock();
-            dateData = (UInt64)ticks;
+            _dateData = (UInt64)ticks;
         }
 
         private DateTime(UInt64 dateData)
         {
-            this.dateData = dateData;
+            this._dateData = dateData;
         }
 
         public DateTime(long ticks, DateTimeKind kind)
@@ -163,7 +165,7 @@ namespace System
                 throw new ArgumentException(SR.Argument_InvalidDateTimeKind, nameof(kind));
             }
             Contract.EndContractBlock();
-            dateData = ((UInt64)ticks | ((UInt64)kind << KindShift));
+            _dateData = ((UInt64)ticks | ((UInt64)kind << KindShift));
         }
 
         internal DateTime(long ticks, DateTimeKind kind, Boolean isAmbiguousDst)
@@ -172,9 +174,9 @@ namespace System
             {
                 throw new ArgumentOutOfRangeException(nameof(ticks), SR.ArgumentOutOfRange_DateTimeBadTicks);
             }
-            Contract.Requires(kind == DateTimeKind.Local, "Internal Constructor is for local times only");
+            Debug.Assert(kind == DateTimeKind.Local, "Internal Constructor is for local times only");
             Contract.EndContractBlock();
-            dateData = ((UInt64)ticks | (isAmbiguousDst ? KindLocalAmbiguousDst : KindLocal));
+            _dateData = ((UInt64)ticks | (isAmbiguousDst ? KindLocalAmbiguousDst : KindLocal));
         }
 
         // Constructs a DateTime from a given year, month, and day. The
@@ -182,7 +184,7 @@ namespace System
         //
         public DateTime(int year, int month, int day)
         {
-            dateData = (UInt64)DateToTicks(year, month, day);
+            _dateData = (UInt64)DateToTicks(year, month, day);
         }
 
         // Constructs a DateTime from a given year, month, and day for
@@ -199,7 +201,7 @@ namespace System
         //
         public DateTime(int year, int month, int day, int hour, int minute, int second)
         {
-            dateData = (UInt64)(DateToTicks(year, month, day) + TimeToTicks(hour, minute, second));
+            _dateData = (UInt64)(DateToTicks(year, month, day) + TimeToTicks(hour, minute, second));
         }
 
         public DateTime(int year, int month, int day, int hour, int minute, int second, DateTimeKind kind)
@@ -210,7 +212,7 @@ namespace System
             }
             Contract.EndContractBlock();
             Int64 ticks = DateToTicks(year, month, day) + TimeToTicks(hour, minute, second);
-            dateData = ((UInt64)ticks | ((UInt64)kind << KindShift));
+            _dateData = ((UInt64)ticks | ((UInt64)kind << KindShift));
         }
 
         // Constructs a DateTime from a given year, month, day, hour,
@@ -221,7 +223,7 @@ namespace System
             if (calendar == null)
                 throw new ArgumentNullException(nameof(calendar));
             Contract.EndContractBlock();
-            dateData = (UInt64)calendar.ToDateTime(year, month, day, hour, minute, second, 0).Ticks;
+            _dateData = (UInt64)calendar.ToDateTime(year, month, day, hour, minute, second, 0).Ticks;
         }
 
         // Constructs a DateTime from a given year, month, day, hour,
@@ -238,7 +240,7 @@ namespace System
             ticks += millisecond * TicksPerMillisecond;
             if (ticks < MinTicks || ticks > MaxTicks)
                 throw new ArgumentException(SR.Arg_DateTimeRange);
-            dateData = (UInt64)ticks;
+            _dateData = (UInt64)ticks;
         }
 
         public DateTime(int year, int month, int day, int hour, int minute, int second, int millisecond, DateTimeKind kind)
@@ -256,7 +258,7 @@ namespace System
             ticks += millisecond * TicksPerMillisecond;
             if (ticks < MinTicks || ticks > MaxTicks)
                 throw new ArgumentException(SR.Arg_DateTimeRange);
-            dateData = ((UInt64)ticks | ((UInt64)kind << KindShift));
+            _dateData = ((UInt64)ticks | ((UInt64)kind << KindShift));
         }
 
         // Constructs a DateTime from a given year, month, day, hour,
@@ -275,7 +277,7 @@ namespace System
             ticks += millisecond * TicksPerMillisecond;
             if (ticks < MinTicks || ticks > MaxTicks)
                 throw new ArgumentException(SR.Arg_DateTimeRange);
-            dateData = (UInt64)ticks;
+            _dateData = (UInt64)ticks;
         }
 
         public DateTime(int year, int month, int day, int hour, int minute, int second, int millisecond, Calendar calendar, DateTimeKind kind)
@@ -295,7 +297,7 @@ namespace System
             ticks += millisecond * TicksPerMillisecond;
             if (ticks < MinTicks || ticks > MaxTicks)
                 throw new ArgumentException(SR.Arg_DateTimeRange);
-            dateData = ((UInt64)ticks | ((UInt64)kind << KindShift));
+            _dateData = ((UInt64)ticks | ((UInt64)kind << KindShift));
         }
 
         private DateTime(SerializationInfo info, StreamingContext context)
@@ -331,11 +333,11 @@ namespace System
             }
             if (foundDateData)
             {
-                dateData = serializedDateData;
+                _dateData = serializedDateData;
             }
             else if (foundTicks)
             {
-                dateData = (UInt64)serializedTicks;
+                _dateData = (UInt64)serializedTicks;
             }
             else
             {
@@ -354,7 +356,7 @@ namespace System
         {
             get
             {
-                return (Int64)(dateData & TicksMask);
+                return (Int64)(_dateData & TicksMask);
             }
         }
 
@@ -362,7 +364,7 @@ namespace System
         {
             get
             {
-                return (dateData & FlagsMask);
+                return (_dateData & FlagsMask);
             }
         }
 
@@ -747,7 +749,7 @@ namespace System
 
             // Serialize both the old and the new format
             info.AddValue(TicksField, InternalTicks);
-            info.AddValue(DateDataField, dateData);
+            info.AddValue(DateDataField, _dateData);
         }
 
         public Boolean IsDaylightSavingTime()
@@ -789,7 +791,7 @@ namespace System
             }
             else
             {
-                return (Int64)dateData;
+                return (Int64)_dateData;
             }
         }
 
@@ -1005,25 +1007,6 @@ namespace System
             }
         }
 
-        public static DateTime UtcNow
-        {
-            get
-            {
-                Contract.Ensures(Contract.Result<DateTime>().Kind == DateTimeKind.Utc);
-                // following code is tuned for speed. Don't change it without running benchmark.
-                long ticks = 0;
-                ticks = GetSystemTimeAsFileTime();
-
-                return new DateTime(((UInt64)(ticks + FileTimeOffset)) | KindUtc);
-            }
-        }
-
-
-        [MethodImplAttribute(MethodImplOptions.InternalCall)]
-        internal static extern long GetSystemTimeAsFileTime();
-
-
-
         // Returns the second part of this DateTime. The returned value is
         // an integer between 0 and 59.
         //
diff --git a/src/mscorlib/src/System/DateTime.CoreCLR.cs b/src/mscorlib/src/System/DateTime.CoreCLR.cs
new file mode 100644 (file)
index 0000000..0114203
--- /dev/null
@@ -0,0 +1,25 @@
+using System.Diagnostics.Contracts;
+using System.Runtime.CompilerServices;
+
+namespace System
+{
+    public partial struct DateTime
+    {
+        public static DateTime UtcNow
+        {
+            get
+            {
+                Contract.Ensures(Contract.Result<DateTime>().Kind == DateTimeKind.Utc);
+                // following code is tuned for speed. Don't change it without running benchmark.
+                long ticks = 0;
+                ticks = GetSystemTimeAsFileTime();
+
+                return new DateTime(((UInt64)(ticks + FileTimeOffset)) | KindUtc);
+            }
+        }
+
+
+        [MethodImplAttribute(MethodImplOptions.InternalCall)]
+        internal static extern long GetSystemTimeAsFileTime();
+    }
+}