Skip to content

DateTime.Second rounds the fraction, so 23:59:59.999 gives 60 #4090

Open

Description

@CaffeinatedCoder
Environment
PostgreSQL 18.6 (postgres:18), session TimeZone Etc/UTC
.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.

This issue used to bundle four BCL date/time members. TimeOnly.IsBetween moved to #4129 (fixed by #4127); at your request the other two are now #4148 and #4149.

date_part('second', …) includes the fraction, and ::int rounds it, so 59.999 seconds becomes 60. .NET's DateTime.Second is the whole seconds, 0 to 59.

LINQ Row .NET PostgreSQL SQL
r.Timestamp.Second 23:59:59.999 59 60 date_part('second', r."Timestamp" AT TIME ZONE 'UTC')::int
Repro
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;

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

context.Rows.Add(new Row { Timestamp = new DateTime(2026, 2, 28, 23, 59, 59, 999, DateTimeKind.Utc) });
await context.SaveChangesAsync();
var row = await context.Rows.AsNoTracking().SingleAsync();

Console.WriteLine($"DateTime(23:59:59.999).Second: .NET {row.Timestamp.Second}, database {await context.Rows.Select(r => r.Timestamp.Second).SingleAsync()}");

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

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        => optionsBuilder
            .UseNpgsql(Environment.GetEnvironmentVariable("Test__Npgsql__DefaultConnection"))
            .LogTo(Console.WriteLine, LogLevel.Information)
            .EnableSensitiveDataLogging();
}

public class Row
{
    public int Id { get; set; }
    public DateTime Timestamp { get; set; }
}

Output, the same on both lines:

DateTime(23:59:59.999).Second: .NET 59, database 60

The translation is NpgsqlDateTimeMemberTranslator.cs#L132. Millisecond on the next line already floors (NpgsqlDateTimeMemberTranslator.cs#L134); floor(date_part('second', …))::int would match .NET.

Activity

  1. changed the title [-]BCL date/time members: `.Second` rounds (up to 60), `TimeOnly.IsBetween` across midnight, `TimeOnly - TimeOnly`, lifted `DateTimeOffset.DateTime`[/-] [+]BCL date/time members: `.Second` rounds (up to 60), `TimeOnly - TimeOnly`, lifted `DateTimeOffset.DateTime`[/+] on Nov 17, 2026
  2. roji commented on Nov 17, 2026

    @roji
    Member

    Split the TimeOnly.IsBetween midnight-spanning range bug into #4129, with #4127 linked as its fix. Updated this issue’s title to reflect the remaining scope.

    This issue remains open for the .Second rounding, TimeOnly - TimeOnly wraparound, and lifted DateTimeOffset.DateTime problems; the original report and repro are preserved above.

    Posted by copilot

  3. changed the title [-]BCL date/time members: `.Second` rounds (up to 60), `TimeOnly - TimeOnly`, lifted `DateTimeOffset.DateTime`[/-] [+]`DateTime.Second` rounds the fraction, so 23:59:59.999 gives 60[/+] on Nov 24, 2026
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