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.
| object_init_ex(return_value, php_date_ce_time_duration); | ||
|
|
||
| php_date_time_duration *original = Z_DATE_TIME_DURATION_P(ZEND_THIS); | ||
| php_date_time_duration *additional = php_date_time_duration_from_obj(duration); |
There was a problem hiding this comment.
| php_date_time_duration *additional = php_date_time_duration_from_obj(duration); | |
| php_date_time_duration *addend = php_date_time_duration_from_obj(duration); |
|
|
||
| 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
…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.
| $durations = [ | ||
| ...$durations, | ||
| null, | ||
| ...array_map(negate(...), $durations), |
There was a problem hiding this comment.
This should not include the negated 0 (because redundant). Also applies to sub(), multiplyBy() and divideBy().
| @@ -0,0 +1,140 @@ | |||
| --TEST-- | |||
| Time\Duration::div() | |||
There was a problem hiding this comment.
| Time\Duration::div() | |
| Time\Duration::divideBy() |
and fix the filename.
| @@ -0,0 +1,211 @@ | |||
| --TEST-- | |||
| Time\Duration::mul() | |||
There was a problem hiding this comment.
| Time\Duration::mul() | |
| Time\Duration::multiplyBy() |
and fix the filename.
| new_obj->duration = obj->duration; | ||
|
|
||
| zend_objects_clone_members(&new_obj->std, &obj->std); |
There was a problem hiding this comment.
| new_obj->duration = obj->duration; | |
| zend_objects_clone_members(&new_obj->std, &obj->std); | |
| new_obj->duration = obj->duration; | |
| zend_objects_clone_members(&new_obj->std, &obj->std); |
| return &obj->std; | ||
| } | ||
|
|
||
| PHPAPI zend_object *php_date_time_duration_object_clone(zend_object *object) |
There was a problem hiding this comment.
| PHPAPI zend_object *php_date_time_duration_object_clone(zend_object *object) | |
| static zend_object *php_date_time_duration_object_clone(zend_object *object) |
|
|
||
| # define Z_DATE_TIME_DURATION_P(zv) php_date_time_duration_from_obj(Z_OBJ_P((zv))) | ||
|
|
||
| #define Z_PARAM_DATE_TIME_DURATION(d) do { \ |
There was a problem hiding this comment.
| #define Z_PARAM_DATE_TIME_DURATION(d) do { \ | |
| # define Z_PARAM_DATE_TIME_DURATION(d) do { \ |
| d = php_date_time_duration_from_obj(__d); \ | ||
| } while (0); | ||
|
|
||
| #define Z_PARAM_DATE_TIME_DURATION_OR_NULL(d) do { \ |
There was a problem hiding this comment.
| #define Z_PARAM_DATE_TIME_DURATION_OR_NULL(d) do { \ | |
| # define Z_PARAM_DATE_TIME_DURATION_OR_NULL(d) do { \ |

RFC: https://wiki.php.net/rfc/duration_class