You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
new DateTimeOffset( default(DateTime)) throws exceptions for regions with UTC+X and OK in UTC-X #47460
next code throws exception when machine time/region in UTC+X but OK in UTC-X
using System;
class P { static void Main() {
DateTimeOffset dto = default(DateTime); // OK in LA/NY but failed in Berlin/Moscow/Beijing
Console.WriteLine(dto);
} }
When u create some struct with defaults and then assign it to DateTimeOffset app behaviour is changed depends from user/machine region. imo its wrong.
simple workaround for .NET: default(DateTime) = DateTime.MinValue = new DateTime(0, Kind.Utc) // no exceptions with .Utc
OR change DateTimeOffset.ctor(DateTime) // default(DateTime) as special case
and for user code:
struct SomeStruct {
public DateTime RequestTime { get; set; } = new DateTime(0, Kind.Utc);
}
Activity
ghost added
untriagedNew issue has not been triaged by the area owner
var useZeroOffset = dateTime == default(DateTime) || dateTime.Kind == DateTimeKind.Utc; // which equality better to check first?
TimeSpan offset = useZeroOffset ? new TimeSpan(0L) : TimeZoneInfo.GetLocalUtcOffset(dateTime,
or
// imo better if dto.ToLocalTime()/ToUniversalTime() can throw Exception. need more tests here
if (dateTime == default(DateTime)) dateTime = new DateTime(0, Kind.Utc);
// and then as current code.
TimeSpan offset = dateTime.Kind == DateTimeKind.Utc ? new TimeSpan(0L) : TimeZoneInfo.GetLocalUtcOffset(dateTime,
changed the title [-]DateTime.MinValue (default) is wrong probably[/-][+]new DateTimeOffset( default(DateTime)) throws exceptions for regions with UTC+X and OK in UTC-X[/+]on Jan 26, 2021
I understand that this is "by design", but there really shouldn't be different behavior for something like this based on time zones. Especially when the difference is between working or throwing.
Activity
huoyaoyuan commentedon Jan 26, 2021
Change the behavior treating
UnspecifiedAsLocalis breaking.No, this isn't possible. The
enum DateTimeKindhas valueUnspecified = 0.The simplest correct way is to use
default(DateTimeOffset). There should be an analyzer to warn on potential acquiring ofDateTimeKind.Unspecified.OlegZ-XC commentedon Jan 26, 2021
change to
or
huoyaoyuan commentedon Jan 26, 2021
Yes, changing the behavior in the conversion constructor is possible, but it's still breaking because it changes behavior.
The correct approach should be allowing 0 ticks with positive offset, because the internal representation of
DateTimeOffsetsupports it.[-]DateTime.MinValue (default) is wrong probably[/-][+]new DateTimeOffset( default(DateTime)) throws exceptions for regions with UTC+X and OK in UTC-X[/+]BrennanConroy commentedon Jun 19, 2021
FYI, ASP.NET Core just hit this in preview5 dotnet/aspnetcore#33634
I understand that this is "by design", but there really shouldn't be different behavior for something like this based on time zones. Especially when the difference is between working or throwing.
CuteLeon commentedon Jul 8, 2021
Met this issue dotnet/aspnetcore#33634 when upgrading https://github.com/cuteleon/hacksystem from .Net 6 Preview 1 to .Net 6 Preview 5
roklenardic commentedon Jul 9, 2021
Is there a workaround for #33634? JWT authentication is broken and this issue has been present since January...
CuteLeon commentedon Jul 23, 2021
Issue gone in Preview 6 version.
roklenardic commentedon Jul 23, 2021
I confirm that this is gone in Preview 6 as well.
OlegZ-XC commentedon Jul 23, 2021
I can't check this before the stable Release, so if you are sure then close the issue.
BrennanConroy commentedon Jul 23, 2021
I think they were talking about the linked JWT issue being gone in preview6... not the DateTimeOffset issue being fixed.
CuteLeon commentedon Jul 24, 2021
Yeah actually,I mean JWT works in preview 6 currently,no more time-zone related exceptions thrown anymore.