`TimeSpan`/`Duration` component members: negative values are floored, and intervals computed in SQL are read field by field

未关闭 适合新手
#4,088 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 1 天内回复

@jeremy-morren 已经在做这个了。

开始于 2026年10月29日。

  • #4116 来自 @jeremy-morren —— 未关闭

评估

难度
2/5
预计耗时
1-3 小时
新手友好度
70/100
Issue 类型
缺陷
描述清晰度
描述清楚
活跃度
活跃
技术栈
csharp, postgresql, sql
领域
database

调研方向

翻译位于 src/EFCore.PG/Query/ExpressionTranslators/Internal/NpgsqlTimeSpanMemberTranslator.cs (lines 47-53, 67-75) 和 src/EFCore.PG.NodaTime/Query/Internal/NpgsqlNodaTimeMemberTranslatorPlugin.cs (lines 119-122, 234)。将 floor 替换为 trunc,并在读取字段之前将计算出的时间间隔包裹在 justify_interval() 中。使用 issue 的复现 (repro) 确认负跨度与 .NET 匹配且时间间隔已规范化。

由索引模型根据 Issue 内容生成。

描述

Environment
PostgreSQL 18.6 (postgres:18), session TimeZone Etc/UTC
.NET 10 runtime 10.0.11, Npgsql.EntityFrameworkCore.PostgreSQL(.NodaTime) 10.0.3, EF Core 10.0.4
.NET 11 runtime 11.0.0-rc.2, Npgsql.EntityFrameworkCore.PostgreSQL(.NodaTime) 11.0.0-rc.2, EF Core 11.0.0-rc.2
Also Npgsql 10.0.3 on both lines, NodaTime 3.2.2, macOS arm64

Both lines print the same output. main at 150011d has the same code.

.NET and NodaTime truncate the components of a negative span toward zero. The translations floor them. And an interval computed in SQL keeps its days and time fields apart (1 day -02:00:00), so .Days and .Hours read those raw fields.

LINQ Value .NET PostgreSQL SQL
r.Span.Seconds TimeSpan −1.5 s −1 −2 floor(date_part('second', r."Span"))::int
r.Duration.Seconds Duration −1.5 s −1 −2 floor(date_part('second', r."Duration"))::int
(r.SpanA + r.SpanB).Days / .Hours 1 day + (−2 h) 0 / 22 1 / −2 floor(date_part('day', r."SpanA" + r."SpanB"))::int
(r.DurationA + r.DurationB).Days / .Hours 1 day + (−1 h) 0 / 23 1 / −1 date_part('day', r."DurationA" + r."DurationB")::int
Repro
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
using NodaTime;

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

context.Rows.Add(new Row
{
    Span = TimeSpan.FromMilliseconds(-1500),
    Duration = Duration.FromMilliseconds(-1500),
    SpanA = TimeSpan.FromDays(1), SpanB = TimeSpan.FromHours(-2),
    DurationA = Duration.FromDays(1), DurationB = Duration.FromHours(-1),
});
await context.SaveChangesAsync();
var row = await context.Rows.AsNoTracking().SingleAsync();

// 1. .NET and NodaTime truncate a negative component toward zero; the translation floors it
Console.WriteLine($"TimeSpan(-1.5 s).Seconds: .NET {row.Span.Seconds}, database {await context.Rows.Select(r => r.Span.Seconds).SingleAsync()}");
Console.WriteLine($"Duration(-1.5 s).Seconds: .NET {row.Duration.Seconds}, database {await context.Rows.Select(r => r.Duration.Seconds).SingleAsync()}");

// 2. An interval computed in SQL keeps its days and time fields apart (1 day + -2 h is '1 day -02:00:00')
Console.WriteLine($"(1 day + -2 h).Days/.Hours as TimeSpan: .NET {(row.SpanA + row.SpanB).Days}/{(row.SpanA + row.SpanB).Hours}, database {await context.Rows.Select(r => (r.SpanA + r.SpanB).Days).SingleAsync()}/{await context.Rows.Select(r => (r.SpanA + r.SpanB).Hours).SingleAsync()}");
Console.WriteLine($"(1 day + -1 h).Days/.Hours as Duration: .NET {(row.DurationA + row.DurationB).Days}/{(row.DurationA + row.DurationB).Hours}, database {await context.Rows.Select(r => (r.DurationA + r.DurationB).Days).SingleAsync()}/{await context.Rows.Select(r => (r.DurationA + r.DurationB).Hours).SingleAsync()}");

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

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

public class Row
{
    public int Id { get; set; }
    public TimeSpan Span { get; set; }
    public Duration Duration { get; set; }
    public TimeSpan SpanA { get; set; }
    public TimeSpan SpanB { get; set; }
    public Duration DurationA { get; set; }
    public Duration DurationB { get; set; }
}

Output, the same on both lines:

TimeSpan(-1.5 s).Seconds: .NET -1, database -2
Duration(-1.5 s).Seconds: .NET -1, database -2
(1 day + -2 h).Days/.Hours as TimeSpan: .NET 0/22, database 1/-2
(1 day + -1 h).Days/.Hours as Duration: .NET 0/23, database 1/-1

trunc instead of floor matches .NET for negative values. justify_interval() normalizes a computed interval before its fields are read.

主要语言
C#
星标
1.8k
派生
268
平均合并
2 小时 35 分钟
30 天内合并 PR
136

环境准备

这个项目没有提供开发容器、Dockerfile 或贡献指南,环境需要你自己搭建:先看它的 README,通用步骤见我们的新手贡献指南。

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

npgsql/efcore.pg 的其他 Issue

查看 npgsql/efcore.pg 的全部 Issue

相似的 Issue

更多 C# Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。