php / php/php-src

DateInterval issues with negative directions

Offen
#16,220 4 Kommentare 1 Reaktion 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

Bug Extension: date
Vorherrschende Sprache
C
Sterne
40.4k
Forks
8.1k
Ø Merge
2 T. 13 Std.
Gemergte PRs (30 T.)
96

Beschreibung

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

Beitragsleitfaden

Beitragsleitfaden öffnen

Erste Schritte

  1. Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
  3. Forke das Repository und arbeite in einem Branch.
  4. Öffne einen Pull Request, der die Issue-Nummer nennt.

Rechercherichtung

Beginne damit, die Beispiele mithilfe der Einstiegspunkte DateInterval und DatePeriod nachzustellen, einschließlich negativer Intervalle, umgekehrter Datumsangaben und der invert-Eigenschaft. Überprüfe das bestehende Verhalten von Datumsperioden und entscheide über konsistente Semantik für negative Richtungen, gemischte Vorzeichen und invertierte Intervalle. Erledigt ist dies, wenn die gemeldeten Fälle ein definiertes, nicht endlos laufendes Verhalten und eine Regressionstestabdeckung haben.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
php
Bereich
backend
Issue-Typ
Bug
Schwierigkeit
5/5
Geschätzter Aufwand
Über eine Woche
Aktivitätsstatus
Veraltet
Klarheit
Größtenteils klar
Anfängerfreundlichkeit
35/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.