Skip to content

Commit

Permalink
Feedback comments
Browse files Browse the repository at this point in the history
  • Loading branch information
tarekgh committed Dec 8, 2018
1 parent f79f7c8 commit b503742
Show file tree
Hide file tree
Showing 14 changed files with 114 additions and 164 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ internal partial class Interop
{
internal partial class Kernel32
{
[DllImport(Libraries.Kernel32, SetLastError = true)]
internal static unsafe extern bool FileTimeToSystemTime(ref long lpFileTime, ref FullSystemTime lpSystemTime);
[DllImport(Libraries.Kernel32)]
internal static unsafe extern bool FileTimeToSystemTime(in long lpFileTime, out Interop.Kernel32.SYSTEMTIME lpSystemTime);
}
}
46 changes: 20 additions & 26 deletions src/Common/src/Interop/Windows/kernel32/Interop.FullSystemTime.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,24 @@ internal partial class Interop
{
internal partial class Kernel32
{
// FullSystemTime struct matches Windows SYSTEMTIME struct, except we added the extra nanoSeconds field to store
// FullSystemTime struct matches Windows SYSTEMTIME struct, except we added the extra hundredNanoSecond field to store
// more precise time.
[StructLayout(LayoutKind.Sequential)]
internal struct FullSystemTime

This comment has been minimized.

Copy link
@jkotas

jkotas Dec 8, 2018

FullSystemTime does not seem to have anything to do with interop now. It is just an internal implementation detail of DateTime. It would be nice to move it to the right spot (e.g. as a nested type of DateTime.Windows.cs or something similar).

This comment has been minimized.

Copy link
@jkotas

jkotas Dec 8, 2018

(The argument for GetSystemTime PInvoke should be SYSTEMTIME, not FullSystemTime.)

{
internal Interop.Kernel32.SYSTEMTIME systemTime;
internal long hundredNanoSecond;

internal FullSystemTime(int year, int month, DayOfWeek dayOfWeek, int day, int hour, int minute, int second)
{
wYear = (ushort) year;
wMonth = (ushort) month;
wDayOfWeek = (ushort) dayOfWeek;
wDay = (ushort) day;
wHour = (ushort) hour;
wMinute = (ushort) minute;
wSecond = (ushort) second;
wMillisecond = 0;
systemTime.Year = (ushort) year;
systemTime.Month = (ushort) month;
systemTime.DayOfWeek = (ushort) dayOfWeek;
systemTime.Day = (ushort) day;
systemTime.Hour = (ushort) hour;
systemTime.Minute = (ushort) minute;
systemTime.Second = (ushort) second;
systemTime.Milliseconds = 0;
hundredNanoSecond = 0;
}

Expand All @@ -34,26 +37,17 @@ internal FullSystemTime(long ticks)
int year, month, day;
dt.GetDatePart(out year, out month, out day);

wYear = (ushort) year;
wMonth = (ushort) month;
wDayOfWeek = (ushort) dt.DayOfWeek;
wDay = (ushort) day;
wHour = (ushort) dt.Hour;
wMinute = (ushort) dt.Minute;
wSecond = (ushort) dt.Second;
wMillisecond = (ushort) dt.Millisecond;
systemTime.Year = (ushort) year;
systemTime.Month = (ushort) month;
systemTime.DayOfWeek = (ushort) dt.DayOfWeek;
systemTime.Day = (ushort) day;
systemTime.Hour = (ushort) dt.Hour;
systemTime.Minute = (ushort) dt.Minute;
systemTime.Second = (ushort) dt.Second;
systemTime.Milliseconds = (ushort) dt.Millisecond;
hundredNanoSecond = 0;
}

internal ushort wYear;
internal ushort wMonth;
internal ushort wDayOfWeek;
internal ushort wDay;
internal ushort wHour;
internal ushort wMinute;
internal ushort wSecond;
internal ushort wMillisecond;
internal long hundredNanoSecond;
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ internal partial class Kernel32
{
internal const int ProcessLeapSecondInfo = 8;

[DllImport(Libraries.Kernel32, SetLastError = true, CharSet = CharSet.Unicode, ExactSpelling = true)]
[DllImport(Libraries.Kernel32)]
internal static extern bool GetProcessInformation(IntPtr hProcess, int ProcessInformationClass, ref long processInformation, int ProcessInformationSize);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ internal partial class Interop
{
internal partial class Kernel32
{
// api-ms-win-core-timezone-l1-1-0.dll
[DllImport(Libraries.Kernel32, SetLastError = true)]
internal static extern bool SystemTimeToFileTime(ref FullSystemTime lpSystemTime, out long lpFileTime);
[DllImport(Libraries.Kernel32)]
internal static extern bool SystemTimeToFileTime(in Interop.Kernel32.SYSTEMTIME lpSystemTime, out long lpFileTime);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@ internal partial class Interop
{
internal partial class Kernel32
{
[DllImport(Libraries.Kernel32, SetLastError = true)]
internal static extern bool TzSpecificLocalTimeToSystemTime(IntPtr lpTimeZoneInformation, ref FullSystemTime lpLocalTime, ref FullSystemTime lpUniversalTime);
[DllImport(Libraries.Kernel32)]
internal static extern bool TzSpecificLocalTimeToSystemTime(
IntPtr lpTimeZoneInformation,
in Interop.Kernel32.SYSTEMTIME lpLocalTime,
out Interop.Kernel32.SYSTEMTIME lpUniversalTime);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ internal partial class Interop
{
internal partial class NtDll
{
[DllImport(Libraries.NtDll, SetLastError = true)]
internal static unsafe extern int NtQuerySystemInformation(int SystemInformationClass, void* SystemInformation, int SystemInformationLength, IntPtr ReturnLength);
[DllImport(Libraries.NtDll)]
internal static unsafe extern int NtQuerySystemInformation(int SystemInformationClass, void* SystemInformation, int SystemInformationLength, uint* ReturnLength);

[StructLayout(LayoutKind.Sequential)]
internal struct SYSTEM_LEAP_SECOND_INFORMATION
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -859,6 +859,7 @@
<Compile Include="$(MSBuildThisFileDirectory)Interop\Windows\Kernel32\Interop.WriteFile_SafeHandle_NativeOverlapped.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Interop\Windows\Normaliz\Interop.Idna.cs" Condition="'$(EnableDummyGlobalizationImplementation)' != 'true'" />
<Compile Include="$(MSBuildThisFileDirectory)Interop\Windows\Normaliz\Interop.Normalization.cs" Condition="'$(EnableDummyGlobalizationImplementation)' != 'true'" />
<Compile Include="$(MSBuildThisFileDirectory)Interop\Windows\NtDll\Interop.NtQuerySystemInformation.cs" Condition="'$(EnableWinRT)' != 'true'" />
<Compile Include="$(MSBuildThisFileDirectory)Interop\Windows\Ole32\Interop.CoCreateGuid.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Interop\Windows\OleAut32\Interop.SysAllocStringLen.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Interop\Windows\OleAut32\Interop.SysFreeString.cs" />
Expand Down
54 changes: 44 additions & 10 deletions src/System.Private.CoreLib/shared/System/DateTime.cs
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ public DateTime(int year, int month, int day, Calendar calendar)
//
public DateTime(int year, int month, int day, int hour, int minute, int second)
{
if (second == 60 && s_isLeapSecondsSupportedSystem && IsValidTimeWithLeapSeconds(year, month, day, hour, minute, second, DateTimeKind.Unspecified))
if (second == 60 && s_systemSupportsLeapSeconds && IsValidTimeWithLeapSeconds(year, month, day, hour, minute, second, DateTimeKind.Unspecified))
{
// if we have leap second (second = 60) then we'll need to check if it is valid time.
// if it is valid, then we adjust the second to 59 so DateTime will consider this second is last second
Expand All @@ -214,7 +214,7 @@ public DateTime(int year, int month, int day, int hour, int minute, int second,
throw new ArgumentException(SR.Argument_InvalidDateTimeKind, nameof(kind));
}

if (second == 60 && s_isLeapSecondsSupportedSystem && IsValidTimeWithLeapSeconds(year, month, day, hour, minute, second, kind))
if (second == 60 && s_systemSupportsLeapSeconds && IsValidTimeWithLeapSeconds(year, month, day, hour, minute, second, kind))
{
// if we have leap second (second = 60) then we'll need to check if it is valid time.
// if it is valid, then we adjust the second to 59 so DateTime will consider this second is last second
Expand All @@ -236,7 +236,7 @@ public DateTime(int year, int month, int day, int hour, int minute, int second,
throw new ArgumentNullException(nameof(calendar));

int originalSecond = second;
if (second == 60 && s_isLeapSecondsSupportedSystem)
if (second == 60 && s_systemSupportsLeapSeconds)
{
// Reset the second value now and then we'll validate it later when we get the final Gregorian date.
second = 59;
Expand Down Expand Up @@ -264,7 +264,7 @@ public DateTime(int year, int month, int day, int hour, int minute, int second,
throw new ArgumentOutOfRangeException(nameof(millisecond), SR.Format(SR.ArgumentOutOfRange_Range, 0, MillisPerSecond - 1));
}

if (second == 60 && s_isLeapSecondsSupportedSystem && IsValidTimeWithLeapSeconds(year, month, day, hour, minute, second, DateTimeKind.Unspecified))
if (second == 60 && s_systemSupportsLeapSeconds && IsValidTimeWithLeapSeconds(year, month, day, hour, minute, second, DateTimeKind.Unspecified))
{
// if we have leap second (second = 60) then we'll need to check if it is valid time.
// if it is valid, then we adjust the second to 59 so DateTime will consider this second is last second
Expand All @@ -291,7 +291,7 @@ public DateTime(int year, int month, int day, int hour, int minute, int second,
throw new ArgumentException(SR.Argument_InvalidDateTimeKind, nameof(kind));
}

if (second == 60 && s_isLeapSecondsSupportedSystem && IsValidTimeWithLeapSeconds(year, month, day, hour, minute, second, kind))
if (second == 60 && s_systemSupportsLeapSeconds && IsValidTimeWithLeapSeconds(year, month, day, hour, minute, second, kind))
{
// if we have leap second (second = 60) then we'll need to check if it is valid time.
// if it is valid, then we adjust the second to 59 so DateTime will consider this second is last second
Expand Down Expand Up @@ -320,7 +320,7 @@ public DateTime(int year, int month, int day, int hour, int minute, int second,
}

int originalSecond = second;
if (second == 60 && s_isLeapSecondsSupportedSystem)
if (second == 60 && s_systemSupportsLeapSeconds)
{
// Reset the second value now and then we'll validate it later when we get the final Gregorian date.
second = 59;
Expand Down Expand Up @@ -356,7 +356,7 @@ public DateTime(int year, int month, int day, int hour, int minute, int second,
}

int originalSecond = second;
if (second == 60 && s_isLeapSecondsSupportedSystem)
if (second == 60 && s_systemSupportsLeapSeconds)
{
// Reset the second value now and then we'll validate it later when we get the final Gregorian date.
second = 59;
Expand Down Expand Up @@ -427,8 +427,6 @@ private DateTime(SerializationInfo info, StreamingContext context)
}
}



internal long InternalTicks
{
get
Expand Down Expand Up @@ -804,6 +802,23 @@ public static DateTime FromFileTime(long fileTime)
return FromFileTimeUtc(fileTime).ToLocalTime();
}

public static DateTime FromFileTimeUtc(long fileTime)
{
if (fileTime < 0 || fileTime > MaxTicks - FileTimeOffset)
{
throw new ArgumentOutOfRangeException(nameof(fileTime), SR.ArgumentOutOfRange_FileTimeInvalid);
}

if (s_systemSupportsLeapSeconds)
{
return FromFileTimeLeapSecondsAware(fileTime);
}

// This is the ticks in Universal time for this fileTime.
long universalTicks = fileTime + FileTimeOffset;
return new DateTime(universalTicks, DateTimeKind.Utc);
}

// Creates a DateTime from an OLE Automation Date.
//
public static DateTime FromOADate(double d)
Expand Down Expand Up @@ -1296,6 +1311,25 @@ public long ToFileTime()
return ToUniversalTime().ToFileTimeUtc();
}

public long ToFileTimeUtc()
{
// Treats the input as universal if it is not specified
long ticks = ((InternalKind & LocalMask) != 0) ? ToUniversalTime().InternalTicks : this.InternalTicks;

if (s_systemSupportsLeapSeconds)
{
return ToFileTimeLeapSecondsAware(ticks);
}

ticks -= FileTimeOffset;
if (ticks < 0)
{
throw new ArgumentOutOfRangeException(null, SR.ArgumentOutOfRange_FileTimeInvalid);
}

return ticks;
}

public DateTime ToLocalTime()
{
return ToLocalTime(false);
Expand Down Expand Up @@ -1650,7 +1684,7 @@ internal static bool TryCreate(int year, int month, int day, int hour, int minut

if (second == 60)
{
if (s_isLeapSecondsSupportedSystem && IsValidTimeWithLeapSeconds(year, month, day, hour, minute, second, DateTimeKind.Unspecified))
if (s_systemSupportsLeapSeconds && IsValidTimeWithLeapSeconds(year, month, day, hour, minute, second, DateTimeKind.Unspecified))
{
// if we have leap second (second = 60) then we'll need to check if it is valid time.
// if it is valid, then we adjust the second to 59 so DateTime will consider this second is last second
Expand Down
6 changes: 3 additions & 3 deletions src/System.Private.CoreLib/shared/System/DateTimeOffset.cs
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ public DateTimeOffset(int year, int month, int day, int hour, int minute, int se
_offsetMinutes = ValidateOffset(offset);

int originalSecond = second;
if (second == 60 && DateTime.s_isLeapSecondsSupportedSystem)
if (second == 60 && DateTime.s_systemSupportsLeapSeconds)
{
// Reset the leap second to 59 for now and then we'll validate it after getting the final UTC time.
second = 59;
Expand All @@ -133,7 +133,7 @@ public DateTimeOffset(int year, int month, int day, int hour, int minute, int se
_offsetMinutes = ValidateOffset(offset);

int originalSecond = second;
if (second == 60 && DateTime.s_isLeapSecondsSupportedSystem)
if (second == 60 && DateTime.s_systemSupportsLeapSeconds)
{
// Reset the leap second to 59 for now and then we'll validate it after getting the final UTC time.
second = 59;
Expand All @@ -155,7 +155,7 @@ public DateTimeOffset(int year, int month, int day, int hour, int minute, int se
_offsetMinutes = ValidateOffset(offset);

int originalSecond = second;
if (second == 60 && DateTime.s_isLeapSecondsSupportedSystem)
if (second == 60 && DateTime.s_systemSupportsLeapSeconds)
{
// Reset the leap second to 59 for now and then we'll validate it after getting the final UTC time.
second = 59;
Expand Down
3 changes: 0 additions & 3 deletions src/System.Private.CoreLib/src/System.Private.CoreLib.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -370,9 +370,6 @@
<Compile Include="..\..\Common\src\Interop\Windows\kernel32\Interop.TzSpecificLocalTimeToSystemTime.cs">
<Link>Interop\Windows\kernel32\Interop.TzSpecificLocalTimeToSystemTime.cs</Link>
</Compile>
<Compile Include="..\..\Common\src\Interop\Windows\NtDll\Interop.NtQuerySystemInformation.cs" Condition="'$(EnableWinRT)'!='true'">
<Link>Interop\Windows\NtDll\Interop.NtQuerySystemInformation.cs</Link>
</Compile>
<Compile Include="..\..\Common\src\Interop\Windows\kernel32\Interop.GetProcessInformation.cs" Condition="'$(EnableWinRT)'=='true'">
<Link>Interop\Windows\kernel32\Interop.GetProcessInformation.cs</Link>
</Compile>
Expand Down
28 changes: 3 additions & 25 deletions src/System.Private.CoreLib/src/System/DateTime.Unix.CoreRT.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,40 +6,18 @@ namespace System
{
public readonly partial struct DateTime
{
internal static readonly bool s_isLeapSecondsSupportedSystem = false;
internal const bool s_systemSupportsLeapSeconds = false;

public static DateTime UtcNow
{
get
{
// For performance, use a private constructor that does not validate arguments.
return new DateTime(((ulong)(Interop.Sys.GetSystemTimeAsTicks() + DateTime.UnixEpochTicks)) | KindUtc);
}
}

public static DateTime FromFileTimeUtc(long fileTime)
{
if (fileTime < 0 || fileTime > MaxTicks - FileTimeOffset)
{
throw new ArgumentOutOfRangeException(nameof(fileTime), SR.ArgumentOutOfRange_FileTimeInvalid);
}

// This is the ticks in Universal time for this fileTime.
long universalTicks = fileTime + FileTimeOffset;
return new DateTime(universalTicks, DateTimeKind.Utc);
}

public long ToFileTimeUtc()
{
// Treats the input as universal if it is not specified
long ticks = ((InternalKind & LocalMask) != 0) ? ToUniversalTime().InternalTicks : this.InternalTicks;
ticks -= FileTimeOffset;
if (ticks < 0)
{
throw new ArgumentOutOfRangeException(null, SR.ArgumentOutOfRange_FileTimeInvalid);
}
return ticks;
}
internal static DateTime FromFileTimeLeapSecondsAware(long fileTime) => default;
internal static long ToFileTimeLeapSecondsAware(long ticks) => default;

// IsValidTimeWithLeapSeconds is not expected to be called at all for now on non-Windows platforms
internal static bool IsValidTimeWithLeapSeconds(int year, int month, int day, int hour, int minute, int second, DateTimeKind kind) => false;
Expand Down
4 changes: 2 additions & 2 deletions src/System.Private.CoreLib/src/System/DateTime.Win32.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ namespace System
{
public readonly partial struct DateTime
{
private static unsafe bool IsLeapSecondsSupportedSystem()
private static unsafe bool SystemSupportsLeapSeconds()
{
Interop.NtDll.SYSTEM_LEAP_SECOND_INFORMATION slsi = new Interop.NtDll.SYSTEM_LEAP_SECOND_INFORMATION();
return Interop.NtDll.NtQuerySystemInformation(
Interop.NtDll.SystemLeapSecondInformation,
(void *) &slsi,
sizeof(Interop.NtDll.SYSTEM_LEAP_SECOND_INFORMATION),
IntPtr.Zero) == 0 && slsi.Enabled;
null) == 0 && slsi.Enabled;
}
}
}
2 changes: 1 addition & 1 deletion src/System.Private.CoreLib/src/System/DateTime.WinRT.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace System
{
public readonly partial struct DateTime
{
private static unsafe bool IsLeapSecondsSupportedSystem()
private static unsafe bool SystemSupportsLeapSeconds()
{
long l = 0;

Expand Down
Loading

0 comments on commit b503742

Please sign in to comment.