php / php/php-src

DateInterval issues with negative directions

Đang mở
#16,220 4 bình luận 1 reaction 0 người được giao Xem trên GitHub

Chưa có ai nhận issue này.

Bug Extension: date
Ngôn ngữ chính
C
Star
40.4k
Fork
8.1k
Merge trung bình
2 ngày 13 giờ
Pull request đã merge (30 ngày)
96

Mô tả

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

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Hướng nghiên cứu

Bắt đầu bằng cách tái hiện các ví dụ sử dụng các điểm vào DateInterval và DatePeriod, bao gồm các khoảng âm, ngày đảo ngược và thuộc tính invert. Xem xét hành vi hiện có của các khoảng thời gian ngày tháng và quyết định ngữ nghĩa nhất quán cho các hướng âm, dấu hỗn hợp và các khoảng bị đảo ngược. Được xem là hoàn tất khi các trường hợp được báo cáo có hành vi được xác định, không bị lặp vô hạn và có độ bao phủ hồi quy.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Đánh giá

Công nghệ
php
Lĩnh vực
backend
Loại issue
Lỗi
Độ khó
5/5
Thời gian dự kiến
Hơn một tuần
Mức độ hoạt động
Đình trệ
Độ rõ ràng
Khá rõ ràng
Mức phù hợp với người mới
35/100

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.