Skip to content

(DateTime?)DateTimeOffset.DateTime is shifted by the session time zone #4149

Backlog

Description

@CaffeinatedCoder
Environment
PostgreSQL 18.6 (postgres:18), session TimeZone Europe/Berlin (set in the repro)
.NET 10 runtime 10.0.15, Npgsql.EntityFrameworkCore.PostgreSQL 10.0.4, EF Core 10.0.15, Npgsql 10.0.4
.NET 11 runtime 11.0.0, Npgsql.EntityFrameworkCore.PostgreSQL 11.0.0, EF Core 11.0.0, Npgsql 11.0.0
Also macOS arm64

Both lines print the same output. main at 9a76cd8 has the same code.

Split from #4090 at your request.

Projecting r.Offset.DateTime is correct. Lifting the same member to DateTime? adds a cast back to timestamptz, which reads the UTC wall clock in the session time zone. With the session time zone Europe/Berlin the value moves by two hours, and comes back as Kind=Utc.

LINQ Row .NET PostgreSQL SQL
r.Offset.DateTime 2026-06-15T23:30+00:00 2026-06-15T23:30 2026-06-15T23:30 r."Offset" AT TIME ZONE 'UTC'
(DateTime?)r.Offset.DateTime same 2026-06-15T23:30 2026-06-15T21:30Z CAST(r."Offset" AT TIME ZONE 'UTC' AS timestamp with time zone)
Repro
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
using Npgsql;

await using var context = new TestContext();
await context.Database.EnsureDeletedAsync();
await context.Database.EnsureCreatedAsync();

context.Rows.Add(new Row { Offset = new DateTimeOffset(2026, 6, 15, 23, 30, 0, TimeSpan.Zero) });
await context.SaveChangesAsync();
var row = await context.Rows.AsNoTracking().SingleAsync();

// The session time zone is Europe/Berlin (set in OnConfiguring)
Console.WriteLine($"Offset.DateTime: .NET {row.Offset.DateTime:O}, database {await context.Rows.Select(r => r.Offset.DateTime).SingleAsync():O}");
Console.WriteLine($"(DateTime?)Offset.DateTime: .NET {(DateTime?)row.Offset.DateTime:O}, database {await context.Rows.Select(r => (DateTime?)r.Offset.DateTime).SingleAsync():O}");

public class TestContext : DbContext
{
    public DbSet<Row> Rows => Set<Row>();

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        => optionsBuilder
            .UseNpgsql(new NpgsqlConnectionStringBuilder(Environment.GetEnvironmentVariable("Test__Npgsql__DefaultConnection")) { Timezone = "Europe/Berlin" }.ConnectionString)
            .LogTo(Console.WriteLine, LogLevel.Information)
            .EnableSensitiveDataLogging();
}

public class Row
{
    public int Id { get; set; }
    public DateTimeOffset Offset { get; set; }
}

Output, the same on both lines:

Offset.DateTime: .NET 2026-06-15T23:30:00.0000000, database 2026-06-15T23:30:00.0000000
(DateTime?)Offset.DateTime: .NET 2026-06-15T23:30:00.0000000, database 2026-06-15T21:30:00.0000000Z

DateTimeOffset.DateTime is AtUtc(instance) (NpgsqlDateTimeMemberTranslator.cs#L209), but AtUtc keeps the operand's CLR type, DateTimeOffset, and ignores its typeMapping parameter (NpgsqlSqlExpressionFactory.cs#L98-L101). The DateTime? convert then looks like a conversion from DateTimeOffset and gets the timestamptz mapping. Typing the AT TIME ZONE result as DateTime with the timestamp mapping would make the lift a no-op.

Activity

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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions