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

オープン 初心者向け
#4,088 コメント 0 件 リアクション 0 件 担当者 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分
マージ済み PR(30日)
136

環境構築

このプロジェクトには開発コンテナ、Dockerfile、コントリビューションガイドがありません。まず README を読み、一般的な手順ははじめてのコントリビューションガイドを参照してください。

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

npgsql/efcore.pg のほかの issue

npgsql/efcore.pg の issue をすべて見る

似ている issue

C# の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。