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
Invalid PostgreSQL SQL literals for BCE NodaTime dates and timestamps #3532
NodaTime BCE values generate SQL literals using ISO astronomical years (negative years or year zero), which PostgreSQL rejects. PostgreSQL requires year-of-era notation with a BC suffix instead.
Original report
In NpgSql package with NodaTime seeding data like this:
BirthDate = new LocalDate(Era.BeforeCommon, 384, 1, 1), DeathDate = new LocalDate(Era.BeforeCommon, 322, 1, 1)
leads to the mistake:
ERROR: time zone displacement out of range: "-0384-01-01"
LINE 1: SELECT DATE '-0384-01-01';
The right format for BCE date is 0384-01-01 BC .
For the exact constructor above, the generated literal is actually DATE '-0383-01-01': NodaTime uses astronomical year numbering, so 384 BCE has year -383.
Confirmed scope
The same formatting bug affects more than LocalDate:
LocalDateTime mapped to timestamp without time zone.
Instant, OffsetDateTime, and ZonedDateTime mapped to timestamp with time zone.
The legacy Instant mapping to timestamp without time zone.
Date/time range and multirange SQL literals, including NpgsqlRange<T>, DateInterval, and Interval.
SQL literals for these values must use PostgreSQL-compatible BCE notation while preserving time components, fractional seconds, and offsets where applicable. For example, 384 BCE must produce DATE '0384-01-01 BC', not a negative ISO year; astronomical year zero must produce year 0001 with a BC suffix.
The BCL date/time types cannot represent BCE dates and do not have this equivalent bug.
JSON compatibility
The fix preserves existing ISO JSON serialization and infinity handling. Casting BCE ISO values extracted from JSON to PostgreSQL date/time types already fails; resolving that separate JSON-to-PostgreSQL conversion limitation is outside this issue's fix.
The below is an AI-generated analysis and may contain inaccuracies.
Confirmed bug in Npgsql's EF NodaTime SQL literal generation. Seeding a single LocalDate property with HasData() reproduces the failure on PostgreSQL 18.4:
PostgreSQL rejects this with SQLSTATE 22009, time zone displacement out of range. For the exact constructor in the report, the generated year is -0383, not -0384: NodaTime uses astronomical year numbering, so 384 BCE has Year == -383. The correct PostgreSQL literal is DATE '0384-01-01 BC'.
Cause and scope. The provider's DateMapping.FormatLocalDate() formats SQL literal contents with LocalDatePattern.Iso, which does not produce PostgreSQL's BCE notation. This is not a failure to support BCE values in PostgreSQL or in Npgsql's parameter handling: inserting both dates from the report through Add() / SaveChangesAsync() and reading them back succeeds on provider 10.0.3. PostgreSQL stores them as 0384-01-01 BC and 0322-01-01 BC; the correctly formatted SQL literal also round-trips.
Version/regression check. The original report does not specify a package version or a previous working version. The same seeding failure was reproduced with Npgsql.EntityFrameworkCore.PostgreSQL.NodaTime6.0.0, 8.0.0, 9.0.0, 9.0.3, 10.0.3, and 11.0.0-rc.1.1. This is therefore long-standing, not newly introduced in 9.x or 10.x. No working version was identified; the original introducing version remains undetermined.
Other providers. With the stock SQL Server and SQLite EF providers 10.0.3, this model fails validation because they do not natively map LocalDate, so neither provides an equivalent BCE reproduction without changing the mapping. The faulty formatter is in this provider, and the issue should remain here rather than be moved to EF Core.
Duplicate search. No matching open or closed issue was found in dotnet/efcore. The closest related result, dotnet/efcore#12979, concerns generating C# literals for seed data, not formatting PostgreSQL SQL literals. In this repository, #872 concerns BCE values outside DateTime's supported range and recommends NodaTime; it is related background, not a duplicate of this NodaTime formatting bug.
minimal repro
Use a .NET 10 console app with Npgsql.EntityFrameworkCore.PostgreSQL.NodaTime version 10.0.3. Set Test__Npgsql__DefaultConnection to a PostgreSQL connection string targeting a disposable database. This program deletes and recreates that database.
usingMicrosoft.EntityFrameworkCore;usingMicrosoft.Extensions.Logging;usingNodaTime;usingNodaTime.Calendars;awaitusingvarcontext=newTestContext();awaitcontext.Database.EnsureDeletedAsync();awaitcontext.Database.EnsureCreatedAsync();publicclassTestContext:DbContext{publicDbSet<Blog>Blogs{get;set;}protectedoverridevoidOnConfiguring(DbContextOptionsBuilderoptionsBuilder)=>optionsBuilder.UseNpgsql(Environment.GetEnvironmentVariable("Test__Npgsql__DefaultConnection"), o =>o.UseNodaTime()).LogTo(Console.WriteLine,LogLevel.Information).EnableSensitiveDataLogging();protectedoverridevoidOnModelCreating(ModelBuildermodelBuilder)=>modelBuilder.Entity<Blog>().HasData(newBlog{Id=1,BirthDate=newLocalDate(Era.BeforeCommon,384,1,1)});}publicclassBlog{publicintId{get;set;}publicLocalDateBirthDate{get;set;}}
changed the title [-]LocalDate for BCE dates transform into the invalid date[/-][+]Invalid PostgreSQL SQL literals for BCE NodaTime dates and timestamps[/+]on Oct 18, 2026
Activity
roji commentedon Oct 18, 2026
AI Triage
The below is an AI-generated analysis and may contain inaccuracies.
Confirmed bug in Npgsql's EF NodaTime SQL literal generation. Seeding a single
LocalDateproperty withHasData()reproduces the failure on PostgreSQL 18.4:PostgreSQL rejects this with SQLSTATE
22009,time zone displacement out of range. For the exact constructor in the report, the generated year is-0383, not-0384: NodaTime uses astronomical year numbering, so 384 BCE hasYear == -383. The correct PostgreSQL literal isDATE '0384-01-01 BC'.Cause and scope. The provider's
DateMapping.FormatLocalDate()formats SQL literal contents withLocalDatePattern.Iso, which does not produce PostgreSQL's BCE notation. This is not a failure to support BCE values in PostgreSQL or in Npgsql's parameter handling: inserting both dates from the report throughAdd()/SaveChangesAsync()and reading them back succeeds on provider 10.0.3. PostgreSQL stores them as0384-01-01 BCand0322-01-01 BC; the correctly formatted SQL literal also round-trips.Version/regression check. The original report does not specify a package version or a previous working version. The same seeding failure was reproduced with
Npgsql.EntityFrameworkCore.PostgreSQL.NodaTime6.0.0, 8.0.0, 9.0.0, 9.0.3, 10.0.3, and 11.0.0-rc.1.1. This is therefore long-standing, not newly introduced in 9.x or 10.x. No working version was identified; the original introducing version remains undetermined.Other providers. With the stock SQL Server and SQLite EF providers 10.0.3, this model fails validation because they do not natively map
LocalDate, so neither provides an equivalent BCE reproduction without changing the mapping. The faulty formatter is in this provider, and the issue should remain here rather than be moved to EF Core.Duplicate search. No matching open or closed issue was found in
dotnet/efcore. The closest related result, dotnet/efcore#12979, concerns generating C# literals for seed data, not formatting PostgreSQL SQL literals. In this repository, #872 concerns BCE values outsideDateTime's supported range and recommends NodaTime; it is related background, not a duplicate of this NodaTime formatting bug.minimal repro
Use a .NET 10 console app with
Npgsql.EntityFrameworkCore.PostgreSQL.NodaTimeversion10.0.3. SetTest__Npgsql__DefaultConnectionto a PostgreSQL connection string targeting a disposable database. This program deletes and recreates that database.[-]LocalDate for BCE dates transform into the invalid date[/-][+]Invalid PostgreSQL SQL literals for BCE NodaTime dates and timestamps[/+][10.0] Fix BCE NodaTime SQL literal formatting (#4027)