php / php/php-src

DateInterval issues with negative directions

オープン
#16,220 コメント 4 件 リアクション 1 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

Bug Extension: date
主要言語
C
スター
40.4k
フォーク
8.1k
平均マージ
2日 13時間
マージ済み PR(30日)
96

説明

Description

I am going to put a few connected issues I have noticed around DateInterval and DatePeriod into a single report:

1: DateInterval->invert is "readonly"

DateInterval:::__construct() only allows positive $duration e.g. P1D not P-1D (I appreciate this is the correct behaviour as it is based around the ISO8601 which has no concept of negative periods). However... the DateInterval->invert which allows the period to be reversed can only be changed by manually setting that property and the docs explicitly say:

The available properties listed below depend on PHP version, and should be considered as readonly.

It would be good to be able to change this property safely.

2: DateInterval::createFromDateString allows negative periods

DateInterval::createFromDateString('-1 day') is accepted but this sets DateInterval->d = -1 rather than using the ::invert property. This is inconsistent with the always-positive periods which __construct allows.

I would expect it to:

  • Reject mixed positive and negative period values, what would +1 year -6 month even mean?
  • Accept if only negative periods passed but then set ::invert = 1 and the properties of ::y..f as +ve
3: DateInterval::createFromDateString('-1 day') leads to infinite loop in DatePeriod
$datePeriod = new \DatePeriod(
    new \DateTimeImmutable('2000-01-01'),
    \DateInterval::createFromDateString('-1 day'),
    new \DateTimeImmutable('2000-01-05'),
);

echo iterator_count($datePeriod); // stuck in infinite loop
4: DateInterval::createFromDateString('-1 day') still doesn't work if start and end reversed in DatePeriod
$datePeriod = new \DatePeriod(
    new \DateTimeImmutable('2000-01-05'),
    \DateInterval::createFromDateString('-1 day'),
    new \DateTimeImmutable('2000-01-01'),
);

echo iterator_count($datePeriod); // 0 (but would expect 4)
5: DatePeriod doesn't respect DateInterval->invert
$dateInterval = new \DateInterval('P1D');
$dateInterval->invert = 1;

# will not go backwards from high to low dates
$datePeriod = new \DatePeriod(
    new \DateTimeImmutable('2000-01-05'),
    $dateInterval,
    new \DateTimeImmutable('2000-01-01'),
);

echo iterator_count($datePeriod); // 0 (but would expect 4)

# incorrectly goes forward from low to high dates
$datePeriod = new \DatePeriod(
    new \DateTimeImmutable('2000-01-01'),
    $dateInterval,
    new \DateTimeImmutable('2000-01-05'),
);

echo iterator_count($datePeriod); // 4 (but would expect 0)
PHP Version

8.3.114

Operating System

Rocky 8

コントリビューションガイド

コントリビューションガイドを開く

はじめの一歩

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

調査の方向性

まず、DateInterval と DatePeriod のエントリポイントを使って、負の間隔、逆順の日付、invert プロパティを含む例を再現します。既存の日付期間の動作を確認し、負の方向、符号が混在する場合、反転された間隔について、一貫したセマンティクスを決定します。完了条件は、報告されたケースの動作が定義され、ループせず、リグレッションカバレッジがあることです。

索引モデルが issue の本文から書いたものです。

評価

技術スタック
php
領域
backend
issue の種類
バグ
難易度
5/5
見積もり時間
1週間以上
活発さ
停滞
明瞭さ
おおむね明確
初心者へのやさしさ
35/100

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

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