Description
Description
The NodaTime ZonedDateTime JSON reader cannot deserialize the writer's output for a value in a non-UTC zone. For example, a Europe/Berlin value in April serializes with +02, but deserialization throws NodaTime.Text.UnparsableValueException.
Minimal repro
Run this as a .NET 11 console application referencing src/EFCore.PG.NodaTime/EFCore.PG.NodaTime.csproj from this repository. No PostgreSQL server is required.
using System.Text;
using Microsoft.EntityFrameworkCore.Storage.Json;
using NodaTime;
using Npgsql.EntityFrameworkCore.PostgreSQL.Storage.Internal;
var value = new LocalDateTime(2018, 4, 20, 10, 31, 33)
.InZoneLeniently(DateTimeZoneProviders.Tzdb["Europe/Berlin"]);
var readerWriter = TimestampTzZonedDateTimeMapping.Default.JsonValueReaderWriter!;
var json = readerWriter.ToJsonString(value);
Console.WriteLine(json);
var reader = new Utf8JsonReaderManager(
new JsonReaderData(Encoding.UTF8.GetBytes(json)), null);
reader.MoveNext();
Console.WriteLine(readerWriter.FromJson(ref reader, existingObject: null));Actual behavior
The writer emits a JSON string containing 2018-04-20T10:31:33+02 (the plus sign is JSON-escaped as \u002B). Reading that string throws:
NodaTime.Text.UnparsableValueException:
The specified offset is invalid for the given date/time.
Value being parsed: '2018-04-20T10:31:33+02'.
Expected behavior
The JSON reader should deserialize the existing writer's output without throwing, preserving the stored local date/time, offset, and instant. Returning a fixed-offset zone is sufficient; this fix should not change the existing JSON representation.
Preserving the original named zone is a separate feature tracked in #4036 and is outside this issue's scope.
Cause
TimestampTzZonedDateTimeMapping uses a pattern containing the local date/time and offset. Parsing treats that text as a ZonedDateTime in the pattern's default UTC zone, so a nonzero offset can be rejected. Reading the stored offset directly avoids this failure without changing the writer.
Environment
Confirmed against main at d98928a7, using .NET SDK 11.0.100-rc.2.26504.105 and EF Core 11.0.0-rc.2.26504.105. Discovered while auditing date/time formatting for #3532; this is independent of BCE formatting.
Activity
[10.0] Fix date/time mapping round-tripping and cloning (#4037)
[8.0] Fix date/time mapping round-tripping and cloning (#4039)
Fix date/time mapping round-tripping and cloning (npgsql#4022)