Skip to content

Blazor: DateTime.ToString("o") and JsonSerializer.Serialize(DateTime) has a different result in wasm runtime  #54343

Open

Description

@anranruye

Describe the bug

For local DateTime values which are smaller than 1900-12-31 23:59:17, System.Text.Json.JsonSerializer.Serialize(DateTime) has a different result in wasm runtime(+08:05 vs +08:00).

To Reproduce

<button @onclick="Click">Click</button>

@code{
    void Click()
    {
        DateTime dateTime1 = new DateTime(1900, 12, 31, 23, 59, 16, DateTimeKind.Local);
        DateTime dateTime2 = new DateTime(1900, 12, 31, 23, 59, 17, DateTimeKind.Local);
        //Output: "1900-12-31T23:59:16+08:05"
        Console.WriteLine(System.Text.Json.JsonSerializer.Serialize(dateTime1));
        Console.WriteLine(dateTime1.ToString("o"));
        //Output: "1900-12-31T23:59:17+08:00"
        Console.WriteLine(System.Text.Json.JsonSerializer.Serialize(dateTime2));
        Console.WriteLine(dateTime2.ToString("o"));
    }
}

Exceptions (if any)

Further technical details

  • ASP.NET Core version 5.0.0
  • Include the output of dotnet --info
  • The IDE (VS / VS Code/ VS4Mac) you're running on, and its version

Activity

  1. changed the title [-]Blazor: System.Text.Json.JsonSerializer.Serialize(DateTime) has a different result in wasm runtime [/-] [+]Blazor: JsonSerializer.Serialize(DateTime) has a different result in wasm runtime [/+] on Jun 17, 2021
  2. changed the title [-]Blazor: JsonSerializer.Serialize(DateTime) has a different result in wasm runtime [/-] [+]Blazor: DateTime.ToString("o") and JsonSerializer.Serialize(DateTime) has a different result in wasm runtime [/+] on Jun 17, 2021
  3. ghost added
    untriaged
    on Jun 17, 2021
  4. Joe4evr commented on Jun 18, 2021

    @Joe4evr
    Contributor

    This is likely to do with the history of your local timezone. In the late 1800s it wasn't uncommon for certain regions to run both "Railroad time" (which was agreed-upon throughout the country) and "Local time" (which was different between towns/cities, and therefor could be off by a few minutes relative to Railroad time). The key thing to realize here is that at some point in time, the decision has been made to switch every city/town to "Railroad time" so that it'd all be in sync.

    Evidently, the timezone data that is consulted for the conversion believes that 1900-12-31 23:59:17 is the moment when that switch happened for your zone.

  5. anranruye commented on Jun 18, 2021

    @anranruye
    Author

    @Joe4evr Thank you.

  6. anranruye commented on Jun 18, 2021

    @anranruye
    Author

    Now I understand why this happened. However, this makes difference between .Net implements. We still want the same behavior in all .Net implements.

  7. added this to the 7.0.0 milestone on Jun 28, 2021
  8. removed
    untriaged
    on Jun 28, 2021
  9. added
    arch-wasm
    on Oct 15, 2021
  10. eiriktsarpalis commented on Oct 15, 2021

    @eiriktsarpalis
    Member

    System.Text.Json uses the Utf8Formatter class when serializing date values. I would assume there are semantic differences between that implementation and DateTime.ToString("o") in the case of wasm.

  11. modified the milestones: 7.0.0, Future on Jul 9, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    arch-wasmarea-System.Buffers

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions