CsWinRT
CsWinRT copied to clipboard
DateTimeOffset.MaxValue can't be round-tripped through WinRT in locale with positive UTC offset.
Describe the bug Assigning DateTimeOffset.MaxValue to a WinRT DateTime property and then tryin to read the value throws ArgumentOutOfRangeException in locales with positive UTC offset.
To Reproduce The following code shows is essentially equivalent to the marshalling code except for hard-coded timezone:
using System;
public class Program
{
private const long ManagedUtcTicksAtNativeZero = 504911232000000000;
public static void Main()
{
var maxDTO = DateTimeOffset.MaxValue.UtcTicks - ManagedUtcTicksAtNativeZero;
Console.WriteLine(maxDTO);
var utcTime = new global::System.DateTimeOffset(maxDTO + ManagedUtcTicksAtNativeZero, global::System.TimeSpan.Zero);
TimeZoneInfo cst = TimeZoneInfo.FindSystemTimeZoneById("China Standard Time");
var offset = cst.GetUtcOffset(utcTime);
long localTicks = utcTime.Ticks + offset.Ticks;
if (localTicks < DateTime.MinValue.Ticks || localTicks > DateTime.MaxValue.Ticks)
{
throw new ArgumentOutOfRangeException();
}
Console.WriteLine(utcTime.ToLocalTime());
}
}
Expected behavior No crash, round-tripping should work.
Version Info 2.0.1
Additional context WinRT DateTime is always in UTC. Consider always returning DateTimeOffset in UTC as well instead of trying to convert to local time.