Time\Duration - #23073
Conversation
|
|
||
| PHP_NEW_EXTENSION([date], | ||
| [php_date.c], | ||
| [php_date.c php_date_time.c php_date_time_duration.c], |
There was a problem hiding this comment.
I was hoping to get rid of the php_date prefix for all of the new stuff. We don't really need it, and it's inconsistent with all other extensions (something I got wrong in 2004). My intention was to move it all to just the class name (with namespace), such as:
date_time_immutable.c(ForDateTimeImmutable)time_duration.c(ForTime/Duration)
It also helps auto-complete in the file browser opening files.
There was a problem hiding this comment.
I've renamed php_date_time_duration.c to just time_duration.c and php_date_time.[ch] to php_time.[ch].
Just time.[ch] didn't work, because I assume something is erroneously including the system-wise time.h header with "" instead of <> or something. At least I got weird compiler errors I didn't want to investigate.
|
|
||
| static void throw_out_of_range_exception(void) | ||
| { | ||
| zend_throw_exception(php_date_ce_time_timeexception, "The maximum representable range is 9_223_372_035 seconds (roughly 292 years)", 0); |
There was a problem hiding this comment.
There is the timelib_get_error_message() function that returns a string for each of the TIMELIB_ERROR codes. We can tweak these message, in case you don't think they're good enough for PHP.
There was a problem hiding this comment.
I needed a custom implementation anyway for 32-bit PHP and to handle the overflows in the fromMinutes() and fromHours() constructors, so I would leave this as a special case.
|
|
||
| php_date_time_duration *original = Z_DATE_TIME_DURATION_P(ZEND_THIS); | ||
| php_date_time_duration *additional = php_date_time_duration_from_obj(duration); | ||
| php_date_time_duration *new = Z_DATE_TIME_DURATION_P(return_value); |
There was a problem hiding this comment.
When either original or additional has recount==1, it may be possible to reuse it?
| ZEND_STATIC_ASSERT(MICROS_IN_NANOS * MICROS_IN_SEC == NANOS_IN_SEC, ""); | ||
| ZEND_STATIC_ASSERT(MILLIS_IN_NANOS * MILLIS_IN_SEC == NANOS_IN_SEC, ""); | ||
|
|
||
| static void sync_properties(php_date_time_duration *object) |
There was a problem hiding this comment.
Could we avoid this with hooks?
There was a problem hiding this comment.
Before implementing this I had a quick chat with @iluuu1994 about this:
- The class is specified to be
readonlyin the RFC and this needs to be specified in the stub to be visible to Reflection. - Hooks are semantically not legal on
readonlyproperties. Marking the property as@virtualin the stub would be visible to reflection. - Also marking a property as
@virtualwould require me to manually implementread_property(),get_properties(),write_property(), andget_property_ptr_ptr()to obtain the correct behavior.
Since there is no such thing as a “internal only hook” for a property as far as I’m aware and given the upcoming freeze, I opted to materialize this into real properties for the initial version. This can (and probably should) be optimized with PHP 8.7.
ebb8107 to
5f203d8
Compare
89e45ab to
6390166
Compare
074da03 to
1f0ff1f
Compare
…om*()`
This is useful for patterns like the following:
for (;;) {
$watchers = $poll->wait(Time\Duration::fromSeconds(1));
// …
}
which is repeatedly creating identical duration objects for every loop
iteration.
| P1W : Time\TimeException: The ISO8601 duration string may only contain the time (T) aspect | ||
| P1M : Time\TimeException: The ISO8601 duration string may only contain the time (T) aspect | ||
| P1DT0S : Time\TimeException: The ISO8601 duration string may only contain the time (T) aspect | ||
| 2000-01-01T00:00:00Z : Time\TimeException: The ISO8601 duration string is missing the perido (P) aspect |
There was a problem hiding this comment.
There's a typo here
- Time\TimeException: The ISO8601 duration string is missing the perido (P) aspect
+ Time\TimeException: The ISO8601 duration string is missing the period (P) aspect
RFC: https://wiki.php.net/rfc/duration_class