Skip to content

new DateTimeOffset( default(DateTime)) throws exceptions for regions with UTC+X and OK in UTC-X #47460

Open

Description

@OlegZ-XC

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

  1. ghost added
    untriaged
    on Jan 26, 2021
  2. huoyaoyuan commented on Jan 26, 2021

    @huoyaoyuan
    Member

    Change the behavior treating Unspecified As Local is breaking.

    simple workaround for .NET:
    default(DateTime) = DateTime.MinValue = new DateTime(0, Kind.Utc) // no exceptions with .Utc

    No, this isn't possible. The enum DateTimeKind has value Unspecified = 0.

    The simplest correct way is to use default(DateTimeOffset). There should be an analyzer to warn on potential acquiring of DateTimeKind.Unspecified.

  3. OlegZ-XC commented on Jan 26, 2021

    @OlegZ-XC
    Author
        public DateTimeOffset(DateTime dateTime)
        {
          TimeSpan offset = dateTime.Kind == DateTimeKind.Utc ? new TimeSpan(0L) : TimeZoneInfo.GetLocalUtcOffset(dateTime, TimeZoneInfoOptions.NoThrowOnInvalidTime);
          this.m_offsetMinutes = DateTimeOffset.ValidateOffset(offset);
          this.m_dateTime = DateTimeOffset.ValidateDate(dateTime, offset);
        }
    

    change to

          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, 
    
  4. huoyaoyuan commented on Jan 26, 2021

    @huoyaoyuan
    Member

    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 DateTimeOffset supports it.

  5. 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
  6. BrennanConroy commented on Jun 19, 2021

    @BrennanConroy
    Member

    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.

  7. CuteLeon commented on Jul 8, 2021

    @CuteLeon
    Contributor
  8. roklenardic commented on Jul 9, 2021

    @roklenardic

    Is there a workaround for #33634? JWT authentication is broken and this issue has been present since January...

  9. removed
    untriaged
    on Jul 12, 2021
  10. added this to the Future milestone on Jul 12, 2021
  11. CuteLeon commented on Jul 23, 2021

    @CuteLeon
    Contributor

    Met this issue dotnet/aspnetcore#33634 when upgrading https://github.com/cuteleon/hacksystem from .Net 6 Preview 1 to .Net 6 Preview 5

    Issue gone in Preview 6 version.

  12. roklenardic commented on Jul 23, 2021

    @roklenardic

    I confirm that this is gone in Preview 6 as well.

  13. OlegZ-XC commented on Jul 23, 2021

    @OlegZ-XC
    Author

    Issue gone in Preview 6 version.

    I can't check this before the stable Release, so if you are sure then close the issue.

  14. BrennanConroy commented on Jul 23, 2021

    @BrennanConroy
    Member

    I think they were talking about the linked JWT issue being gone in preview6... not the DateTimeOffset issue being fixed.

  15. CuteLeon commented on Jul 24, 2021

    @CuteLeon
    Contributor

    Yeah actually,I mean JWT works in preview 6 currently,no more time-zone related exceptions thrown anymore.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions