This repository was archived by the owner on Jan 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4.9k
Add DateTime Leap Seconds Tests #33939
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,6 +7,7 @@ | |
using System.Globalization; | ||
using System.Linq; | ||
using System.Runtime.Serialization; | ||
using System.Runtime.InteropServices; | ||
using Xunit; | ||
|
||
namespace System.Tests | ||
|
@@ -721,7 +722,7 @@ public void Subtract_TimeSpan_ReturnsExpected(DateTime dateTime, TimeSpan timeSp | |
Assert.Equal(expected, dateTime.Subtract(timeSpan)); | ||
Assert.Equal(expected, dateTime - timeSpan); | ||
} | ||
|
||
public static IEnumerable<object[]> Subtract_OutOfRangeTimeSpan_TestData() | ||
{ | ||
yield return new object[] { DateTime.Now, TimeSpan.MinValue }; | ||
|
@@ -2177,6 +2178,70 @@ public void GetObjectData_NullInfo_ThrowsArgumentNullException() | |
AssertExtensions.Throws<ArgumentNullException>("info", () => ((ISerializable)DateTime.Now).GetObjectData(null, new StreamingContext())); | ||
} | ||
|
||
[Fact] | ||
public void TestRoundTrippingDateTimeAndFileTime() | ||
{ | ||
// This test ensure the round tripping of DateTime with the system file time. | ||
// It is important to have this working on systems supporting leap seconds as the conversion wouldn't be simple | ||
// conversion but involve some OS calls to ensure the right conversion is happening. | ||
|
||
DateTime now = DateTime.UtcNow; | ||
long fileTime = now.ToFileTimeUtc(); | ||
DateTime roundTrippedDateTime = DateTime.FromFileTimeUtc(fileTime); | ||
Assert.Equal(now, roundTrippedDateTime); | ||
|
||
now = DateTime.Now; | ||
fileTime = now.ToFileTime(); | ||
roundTrippedDateTime = DateTime.FromFileTime(fileTime); | ||
Assert.Equal(now, roundTrippedDateTime); | ||
} | ||
|
||
[Fact] | ||
[PlatformSpecific(TestPlatforms.Windows)] | ||
public void TestTimeSynchronizationWithTheSystem() | ||
{ | ||
// The reported time by the framework should be synchronized with the OS. | ||
// There shouldn't be any shift by more than one second, otherwise there is something wrong. | ||
// This test is useful when running on a system supporting leap seconds to ensure when the system | ||
// has leap seconds, the framework reported time will still be synchronized. | ||
|
||
SYSTEMTIME st; | ||
SYSTEMTIME st1; | ||
|
||
GetSystemTime(out st); | ||
DateTime dt = DateTime.UtcNow; | ||
tarekgh marked this conversation as resolved.
Show resolved
Hide resolved
|
||
GetSystemTime(out st1); | ||
|
||
DateTime systemDateTimeNow1 = new DateTime(st.wYear, st.wMonth, st.wDay, st.wHour, st.wMinute, st.wSecond, st.wMillisecond, DateTimeKind.Utc); | ||
DateTime systemDateTimeNow2 = new DateTime(st1.wYear, st1.wMonth, st1.wDay, st1.wHour, st1.wMinute, st1.wSecond, st1.wMillisecond, DateTimeKind.Utc); | ||
|
||
// Usually GetSystemTime and DateTime.UtcNow calls doesn't take one second to execute, if this is not the case then | ||
// the thread was sleeping for awhile and we cannot test reliably on that case. | ||
|
||
TimeSpan diff = systemDateTimeNow2 - systemDateTimeNow1; | ||
if (diff < TimeSpan.FromSeconds(1)) | ||
{ | ||
diff = dt - systemDateTimeNow1; | ||
Assert.True(diff < TimeSpan.FromSeconds(1), $"Reported DateTime.UtcNow {dt} is shifted by more than one second then the system time {systemDateTimeNow1}"); | ||
} | ||
} | ||
|
||
[DllImport("Kernel32.dll")] | ||
internal static extern void GetSystemTime(out SYSTEMTIME lpSystemTime); | ||
|
||
[StructLayout(LayoutKind.Sequential)] | ||
internal struct SYSTEMTIME | ||
{ | ||
internal ushort wYear; | ||
internal ushort wMonth; | ||
internal ushort wDayOfWeek; | ||
internal ushort wDay; | ||
internal ushort wHour; | ||
internal ushort wMinute; | ||
internal ushort wSecond; | ||
internal ushort wMillisecond; | ||
} | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we have or need tests that constructs a specific leap second time and validates that the right things happen? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think we can do that in the current time as we don't have a system with a leap seconds. |
||
private class DateMaxCalendar : Calendar | ||
{ | ||
public override int[] Eras => throw new NotImplementedException(); | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we need a similar test for DateTimeOffset?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
DateTimeOffset is using DateTime for such operations.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Isn't that an implementation detail?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We already have similar test for DateTimeOffset https://github.com/dotnet/corefx/blob/master/src/System.Runtime/tests/System/DateTimeOffsetTests.cs#L518