Re: [PHP-DEV] [RFC] Duration class

[email protected] (Tim Düsterhus)
Newsgroups php.internals
Message-ID <[email protected]>
Hi

Am 2026-06-25 18:26, schrieb Paweł Kraśnicki:
> /**
>  * Truncates `$events` so 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

Thank 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 `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
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.