Skip to content

Commit e9a18bf

Browse files
Gregory NikolaishviliGregory Nikolaishvili
authored andcommitted
Update TimeOnly ToXmlString to include time zone offset
- Change ToXmlString(TimeOnly) to output "HH:mm:sszzz" with local offset - Update unit test to match new format with time zone info - Bump Verify.Xunit package to v31.12.5
1 parent 1c06339 commit e9a18bf

3 files changed

Lines changed: 12 additions & 3 deletions

File tree

Directory.Packages.props

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
<PackageVersion Include="Microsoft.NET.Test.Sdk" Version="18.0.*" />
1616
<PackageVersion Include="Verify.SourceGenerators" Version="2.5.*" />
17-
<PackageVersion Include="Verify.Xunit" Version="31.9.3" />
17+
<PackageVersion Include="Verify.Xunit" Version="31.12.5" />
1818
<PackageVersion Include="xunit" Version="2.9.3" />
1919
<PackageVersion Include="xunit.runner.visualstudio" Version="3.1.5" />
2020
<PackageVersion Include="coverlet.collector" Version="6.0.4" />

src/AltaSoft.DomainPrimitives/ToXmlStringExt.cs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,17 @@ public static class ToXmlStringExt
3535
/// </summary>
3636
/// <param name="value">The TimeOnly value to convert.</param>
3737
/// <returns>The XML string representation of the TimeOnly value.</returns>
38+
3839
[MethodImpl(MethodImplOptions.AggressiveInlining)]
39-
public static string ToXmlString(this TimeOnly value) => value.ToString("HH:mm:ss", CultureInfo.InvariantCulture);
40+
public static string ToXmlString(this TimeOnly value)
41+
{
42+
var date = DateOnly.FromDateTime(DateTime.Today);
43+
44+
var localDateTime = date.ToDateTime(value, DateTimeKind.Local);
45+
var dto = new DateTimeOffset(localDateTime);
46+
47+
return dto.ToString("HH:mm:sszzz", CultureInfo.InvariantCulture);
48+
}
4049

4150
/// <summary>
4251
/// Converts a <see cref="DateTimeOffset" /> value to its XML string representation

tests/AltaSoft.DomainPrimitives.XmlDataTypes.Tests/ToXmlStringExtTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public void TimeOnly_ToXmlString_ReturnsExpectedFormat()
2424
{
2525
var t = new TimeOnly(13, 45, 30);
2626
var xml = t.ToXmlString();
27-
Assert.Equal("13:45:30", xml);
27+
Assert.Matches(@"^13:45:30(?:Z|[+-]\d{2}:\d{2})$", xml);
2828
}
2929

3030
[Fact]

0 commit comments

Comments
 (0)