Re: [RFC] Duration class
| From: | Tim Düsterhus | Date: | Mon, 29 Jun 2026 17:42:59 +0000 |
| Subject: | Re: [RFC] Duration class | ||
| References: | 1 2 3 4 5 6 7 8 9 10 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-131598@lists.php.net to get a copy of this message | ||
Hi
Am 2026-06-25 18:26, schrieb Paweł Kraśnicki:
/** * TruncatesThank you for providing that example. The restriction was primarily added as a safety: Removing restrictions later is always possible, adding them is not. So absent a good use-case choosing the “error” option is safer. Given you have provided an example, it makes sense to me to lift the restriction. For the$eventsso that they fit within the daylight window. * The allowed time window (dawn to dusk) is expanded on both ends * by$daytimePadding, which can be negative to shrink the window. * Events that would fall entirely outside this window are discarded. * * @param list<Event> $events * @return list<Event> */ function clampEventsToDaylight(DayAstronomicalData $day, Duration $daytimePadding, array $events,): array
fromSeconds() constructor it's not completely obvious how to handle negative values there (see the discussion with Marc). I think requiring both parameters to have the same sign would be the correct solution here, since this will provide for an intuitive representation of magnitude (see also below). What do you think?
I'll also discuss this with Derick before adjusting the RFC.
popular Rust library for complex date/time handling, chrono, provides a TimeDelta type (renamed from Duration) that accepts negative values in its constructors.Thank you for that reference. I have also tested how that implementation represents negative values: Internally it represents the value like the Java implementation, thus negative 1_500 milliseconds is (-2) seconds + 500 milliseconds. But this internal representation is not accessible. Using the getters negative 1_500 milliseconds is represented as (-1) seconds + (-500) milliseconds:
use chrono; // 0.4.44
fn main() {
use chrono::TimeDelta;
let duration = TimeDelta::milliseconds(-1500);
println!("{:#?}", duration);
println!("{:#?}", duration.num_seconds());
println!("{:#?}", duration.subsec_nanos());
}
https://play.rust-lang.org/?version=stable&mode=debug&edition=2024&gist=a66d7e94f932d9e2c64ee966474308a2
Best regards
Tim Düsterhus