Skip to content

Invalid PostgreSQL SQL literals for BCE NodaTime dates and timestamps #3532

Closed

Description

@Meteorych

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.

Fixed by #4023.

Activity

  1. added theissue type on Oct 18, 2026
  2. roji commented on Oct 18, 2026

    @roji
    Member

    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 LocalDate property with HasData() reproduces the failure on PostgreSQL 18.4:

    INSERT INTO "Blogs" ("Id", "BirthDate")
    VALUES (1, DATE '-0383-01-01');

    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.NodaTime 6.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.

    using Microsoft.EntityFrameworkCore;
    using Microsoft.Extensions.Logging;
    using NodaTime;
    using NodaTime.Calendars;
    
    await using var context = new TestContext();
    await context.Database.EnsureDeletedAsync();
    await context.Database.EnsureCreatedAsync();
    
    public class TestContext : DbContext
    {
        public DbSet<Blog> Blogs { get; set; }
    
        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
            => optionsBuilder
                .UseNpgsql(Environment.GetEnvironmentVariable("Test__Npgsql__DefaultConnection"), o => o.UseNodaTime())
                .LogTo(Console.WriteLine, LogLevel.Information)
                .EnableSensitiveDataLogging();
    
        protected override void OnModelCreating(ModelBuilder modelBuilder)
            => modelBuilder.Entity<Blog>().HasData(
                new Blog { Id = 1, BirthDate = new LocalDate(Era.BeforeCommon, 384, 1, 1) });
    }
    
    public class Blog
    {
        public int Id { get; set; }
        public LocalDate BirthDate { get; set; }
    }
  3. 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
  4. linked a pull request that will close this issue[9.0] Fix BCE NodaTime SQL literal formatting #4025on Oct 18, 2026
  5. linked a pull request that will close this issue[8.0] Fix BCE NodaTime SQL literal formatting #4026on Oct 18, 2026
  6. linked a pull request that will close this issue[10.0] Fix BCE NodaTime SQL literal formatting #4027on Oct 18, 2026
  7. added a commit that references this issue on Oct 18, 2026
    251be35
  8. self-assigned this
    on Oct 18, 2026
  9. added this to the 10.0.4 milestone on Oct 18, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions