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
TimeOnly - TimeOnly returns a negative interval instead of wrapping around midnight #4148
In .NET, TimeOnly - TimeOnly wraps around midnight, so the result is always between 0 and 24 hours: 00:30 − 23:00 is 01:30. PostgreSQL's time - time is a signed interval, −22:30.
LINQ
Row
.NET
PostgreSQL
SQL
r.Time - r.Late
00:30 − 23:00
01:30:00
−22:30:00
r."Time" - r."Late"
Repro
usingMicrosoft.EntityFrameworkCore;usingMicrosoft.Extensions.Logging;awaitusingvarcontext=newTestContext();awaitcontext.Database.EnsureDeletedAsync();awaitcontext.Database.EnsureCreatedAsync();context.Rows.Add(newRow{Time=newTimeOnly(0,30),Late=newTimeOnly(23,0)});awaitcontext.SaveChangesAsync();varrow=awaitcontext.Rows.AsNoTracking().SingleAsync();// TimeOnly subtraction wraps around midnight in .NET: the result is always between 0 and 24 hoursConsole.WriteLine($"TimeOnly 00:30 - 23:00: .NET {row.Time-row.Late}, database {awaitcontext.Rows.Select(r =>r.Time-r.Late).SingleAsync()}");publicclassTestContext:DbContext{publicDbSet<Row>Rows=>Set<Row>();protectedoverridevoidOnConfiguring(DbContextOptionsBuilderoptionsBuilder)=>optionsBuilder.UseNpgsql(Environment.GetEnvironmentVariable("Test__Npgsql__DefaultConnection")).LogTo(Console.WriteLine,LogLevel.Information).EnableSensitiveDataLogging();}publicclassRow{publicintId{get;set;}publicTimeOnlyTime{get;set;}publicTimeOnlyLate{get;set;}}
The subtraction is NpgsqlSqlExpressionFactory.cs#L477-L499, shared with DateTime, DateOnly and the NodaTime types. For TimeOnly, adding 24 hours when the result is negative (for example CASE WHEN a >= b THEN a - b ELSE a - b + INTERVAL '24 hours' END) matches .NET.
Activity