Skip to content

Commit 0423e89

Browse files
committed
Add DateTime operators to SYSTEMTIME and implement IEquatable
Add Tests for Template structs
1 parent 22521d1 commit 0423e89

File tree

3 files changed

+94
-0
lines changed

3 files changed

+94
-0
lines changed

src/Microsoft.Windows.CsWin32/Generator.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3726,6 +3726,7 @@ private StructDeclarationSyntax DeclareStruct(TypeDefinitionHandle typeDefHandle
37263726
{
37273727
case "RECT":
37283728
case "SIZE":
3729+
case "SYSTEMTIME":
37293730
members.AddRange(this.ExtractMembersFromTemplate(name.Identifier.ValueText));
37303731
break;
37313732
default:
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
partial struct SYSTEMTIME
2+
: IEquatable<SYSTEMTIME>
3+
{
4+
public bool Equals(SYSTEMTIME other)
5+
{
6+
return this.wYear == other.wYear &&
7+
this.wMonth == other.wMonth &&
8+
this.wDayOfWeek == other.wDayOfWeek &&
9+
this.wDay == other.wDay &&
10+
this.wHour == other.wHour &&
11+
this.wMinute == other.wMinute &&
12+
this.wSecond == other.wSecond &&
13+
this.wMilliseconds == other.wMilliseconds;
14+
}
15+
16+
public override bool Equals(object obj) => obj is SYSTEMTIME other && this.Equals(other);
17+
public override int GetHashCode() => (this.wYear, this.wMonth, this.wDayOfWeek, this.wDay, this.wHour, this.wMinute, this.wSecond, this.wMilliseconds).GetHashCode();
18+
19+
public static bool operator ==(SYSTEMTIME d1, SYSTEMTIME d2)
20+
{
21+
return d1.wYear == d2.wYear &&
22+
d1.wMonth == d2.wMonth &&
23+
d1.wDayOfWeek == d2.wDayOfWeek &&
24+
d1.wDay == d2.wDay &&
25+
d1.wHour == d2.wHour &&
26+
d1.wMinute == d2.wMinute &&
27+
d1.wSecond == d2.wSecond &&
28+
d1.wMilliseconds == d2.wMilliseconds;
29+
}
30+
31+
public static bool operator !=(SYSTEMTIME d1, SYSTEMTIME d2)
32+
{
33+
return d1.wYear != d2.wYear &&
34+
d1.wMonth != d2.wMonth &&
35+
d1.wDayOfWeek != d2.wDayOfWeek &&
36+
d1.wDay != d2.wDay &&
37+
d1.wHour != d2.wHour &&
38+
d1.wMinute != d2.wMinute &&
39+
d1.wSecond != d2.wSecond &&
40+
d1.wMilliseconds != d2.wMilliseconds;
41+
}
42+
43+
public static implicit operator global::System.DateTime(SYSTEMTIME sysTime)
44+
{
45+
if (sysTime == default(SYSTEMTIME) || sysTime.wYear <= 0 || sysTime.wMonth <= 0 || sysTime.wDay <= 0)
46+
{
47+
return default;
48+
}
49+
50+
// DateTime gets DayOfWeek automatically
51+
return new global::System.DateTime(sysTime.wYear,
52+
sysTime.wMonth, sysTime.wDay, sysTime.wHour,
53+
sysTime.wMinute, sysTime.wSecond, sysTime.wMilliseconds);
54+
}
55+
56+
public static implicit operator SYSTEMTIME(global::System.DateTime time)
57+
{
58+
if (time == default)
59+
{
60+
return default;
61+
}
62+
63+
return new SYSTEMTIME
64+
{
65+
wYear = checked((ushort)time.Year),
66+
wMonth = checked((ushort)time.Month),
67+
wDayOfWeek = checked((ushort)time.DayOfWeek),
68+
wDay = checked((ushort)time.Day),
69+
wHour = checked((ushort)time.Hour),
70+
wMinute = checked((ushort)time.Minute),
71+
wSecond = checked((ushort)time.Second),
72+
wMilliseconds = checked((ushort)time.Millisecond)
73+
};
74+
}
75+
}

test/Microsoft.Windows.CsWin32.Tests/GeneratorTests.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -605,6 +605,24 @@ public void HandleStructsHaveStaticNullMember(string handleName)
605605
this.AssertGeneratedMember(handleName, "Null", $"internal static {handleName} Null => default;");
606606
}
607607

608+
[Theory]
609+
[InlineData("BOOL")]
610+
[InlineData("BOOLEAN")]
611+
[InlineData("HRESULT")]
612+
[InlineData("NTSTATUS")]
613+
[InlineData("PCSTR")]
614+
[InlineData("PCWSTR")]
615+
[InlineData("RECT")]
616+
[InlineData("SIZE")]
617+
[InlineData("SYSTEMTIME")]
618+
public void TemplateAPIsGenerate(string handleType)
619+
{
620+
this.generator = this.CreateGenerator();
621+
Assert.True(this.generator.TryGenerate(handleType, CancellationToken.None));
622+
this.CollectGeneratedCode(this.generator);
623+
this.AssertNoDiagnostics();
624+
}
625+
608626
[Theory]
609627
[InlineData("HANDLE")]
610628
[InlineData("HGDIOBJ")]

0 commit comments

Comments
 (0)