From: Alex Perovich Date: Sat, 25 Mar 2017 04:10:49 +0000 (-0500) Subject: Move DateTime to shared partition (dotnet/coreclr#10460) X-Git-Tag: submit/tizen/20210909.063632~11030^2~7556 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=9c4d7b273f867d370c87adedcdf7646de724fbcc;p=platform%2Fupstream%2Fdotnet%2Fruntime.git Move DateTime to shared partition (dotnet/coreclr#10460) * Move DateTime to shared partition Depends on dotnet/corefxdotnet/coreclr#17484 * Use CoreCLR specific impl of UtcNow on windows * Use the internal call on unix as well * Port good corert changes Commit migrated from https://github.com/dotnet/coreclr/commit/92b6ab81670b3e284a58dd282098ce782aed7617 --- diff --git a/src/coreclr/src/mscorlib/System.Private.CoreLib.csproj b/src/coreclr/src/mscorlib/System.Private.CoreLib.csproj index bac8456..5da3cdd 100644 --- a/src/coreclr/src/mscorlib/System.Private.CoreLib.csproj +++ b/src/coreclr/src/mscorlib/System.Private.CoreLib.csproj @@ -326,7 +326,7 @@ - + diff --git a/src/coreclr/src/mscorlib/shared/System.Private.CoreLib.Shared.projitems b/src/coreclr/src/mscorlib/shared/System.Private.CoreLib.Shared.projitems index 3cc70b4..e8d180d 100644 --- a/src/coreclr/src/mscorlib/shared/System.Private.CoreLib.Shared.projitems +++ b/src/coreclr/src/mscorlib/shared/System.Private.CoreLib.Shared.projitems @@ -108,6 +108,7 @@ + diff --git a/src/coreclr/src/mscorlib/src/System/DateTime.cs b/src/coreclr/src/mscorlib/shared/System/DateTime.cs similarity index 96% rename from src/coreclr/src/mscorlib/src/System/DateTime.cs rename to src/coreclr/src/mscorlib/shared/System/DateTime.cs index 894e849..5e6332d 100644 --- a/src/coreclr/src/mscorlib/src/System/DateTime.cs +++ b/src/coreclr/src/mscorlib/shared/System/DateTime.cs @@ -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, IEquatable, ISerializable + public partial struct DateTime : IComparable, IFormattable, IConvertible, IComparable, IEquatable, 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().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/coreclr/src/mscorlib/src/System/DateTime.CoreCLR.cs b/src/coreclr/src/mscorlib/src/System/DateTime.CoreCLR.cs new file mode 100644 index 0000000..0114203 --- /dev/null +++ b/src/coreclr/src/mscorlib/src/System/DateTime.CoreCLR.cs @@ -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().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(); + } +}