-
Notifications
You must be signed in to change notification settings - Fork 85
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add DateTime operators to SYSTEMTIME and implement IEquatable
Add Tests for Template structs Fix up output formatting
- Loading branch information
Showing
3 changed files
with
87 additions
and
1 deletion.
There are no files selected for viewing
This file contains 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
This file contains 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 |
---|---|---|
@@ -0,0 +1,42 @@ | ||
partial struct SYSTEMTIME | ||
: IEquatable<SYSTEMTIME> | ||
{ | ||
public bool Equals(SYSTEMTIME other) => this.wYear == other.wYear && this.wMonth == other.wMonth && this.wDayOfWeek == other.wDayOfWeek && this.wDay == other.wDay && this.wHour == other.wHour && this.wMinute == other.wMinute && this.wSecond == other.wSecond && this.wMilliseconds == other.wMilliseconds; | ||
public override bool Equals(object obj) => obj is SYSTEMTIME other && this.Equals(other); | ||
public override int GetHashCode() => (this.wYear, this.wMonth, this.wDayOfWeek, this.wDay, this.wHour, this.wMinute, this.wSecond, this.wMilliseconds).GetHashCode(); | ||
public static bool operator ==(SYSTEMTIME d1, SYSTEMTIME d2) => d1.wYear == d2.wYear && d1.wMonth == d2.wMonth && d1.wDayOfWeek == d2.wDayOfWeek && d1.wDay == d2.wDay && d1.wHour == d2.wHour && d1.wMinute == d2.wMinute && d1.wSecond == d2.wSecond && d1.wMilliseconds == d2.wMilliseconds; | ||
public static bool operator !=(SYSTEMTIME d1, SYSTEMTIME d2) => d1.wYear != d2.wYear && d1.wMonth != d2.wMonth && d1.wDayOfWeek != d2.wDayOfWeek && d1.wDay != d2.wDay && d1.wHour != d2.wHour && d1.wMinute != d2.wMinute && d1.wSecond != d2.wSecond && d1.wMilliseconds != d2.wMilliseconds; | ||
|
||
public static explicit operator global::System.DateTime(SYSTEMTIME sysTime) | ||
{ | ||
if (sysTime == default(SYSTEMTIME) || sysTime.wYear <= 0 || sysTime.wMonth <= 0 || sysTime.wDay <= 0) | ||
{ | ||
return default; | ||
} | ||
|
||
// DateTime gets DayOfWeek automatically | ||
return new global::System.DateTime(sysTime.wYear, | ||
sysTime.wMonth, sysTime.wDay, sysTime.wHour, | ||
sysTime.wMinute, sysTime.wSecond, sysTime.wMilliseconds); | ||
} | ||
|
||
public static explicit operator SYSTEMTIME(global::System.DateTime time) | ||
{ | ||
if (time == default) | ||
{ | ||
return default; | ||
} | ||
|
||
return new SYSTEMTIME | ||
{ | ||
wYear = checked((ushort)time.Year), | ||
wMonth = checked((ushort)time.Month), | ||
wDayOfWeek = checked((ushort)time.DayOfWeek), | ||
wDay = checked((ushort)time.Day), | ||
wHour = checked((ushort)time.Hour), | ||
wMinute = checked((ushort)time.Minute), | ||
wSecond = checked((ushort)time.Second), | ||
wMilliseconds = checked((ushort)time.Millisecond) | ||
}; | ||
} | ||
} |
This file contains 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