Skip to content

DateTime is always initialized as non UTC kind and fails with UTC DateTime #330

@TekuSP

Description

@TekuSP

Description:

WinRT originate error - 0x80070057 : 'The UTC Offset for Utc DateTime instances must be 0. (Parameter 'offset')'.

The issues is originated from here:

internal static class DateTimeExtensions
{
/// <summary>
/// Converts a DateTime to a DateTimeOffset using the local time zone.
/// </summary>
/// <param name="dateTime">The DateTime to convert.</param>
/// <returns>A DateTimeOffset representing the same point in time as the DateTime.</returns>
public static DateTimeOffset ToDateTimeOffset(this DateTime dateTime)
{
return new DateTimeOffset(dateTime, TimeZoneInfo.Local.GetUtcOffset(dateTime));
}

I think the issue is TImeZoneInfo.Local and not TimeZoneInfo.Utc:

I think it could be reproduced with this test:

[TestMethod]
public void ToDateTimeOffset_FromDateTime_UTC_UsesLocalOffset()
{
    var dt = new DateTime(2024, 1, 2, 3, 4, 5, DateTimeKind.Utc);
    var dto = dt.ToDateTimeOffset();
    var expected = new DateTimeOffset(dt, TimeZoneInfo.Utc.GetUtcOffset(dt));
    Assert.AreEqual(expected, dto);
} 

But I did not manage to get tests running locally.

Environment:

  • Package Version: 1.4.0
  • WinAppSDK Version: 1.8.251106002

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions