-
Notifications
You must be signed in to change notification settings - Fork 8.1k
Time\Duration #23073
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
TimWolla
wants to merge
6
commits into
php:master
Choose a base branch
from
TimWolla:time-duration
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Time\Duration #23073
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
f01814c
timelib: Update to latest master
TimWolla d91e918
date: Add `Time\Duration`
TimWolla 7d32f93
standard: Take `Time\Duration` in `Io\Poll\Context::wait()`
TimWolla 1829a49
date: Cache the most recently `Time\Duration` object created with `fr…
TimWolla 811d26c
date: Add `Z_PARAM_ULONG()` helper to `time_duration.c`
TimWolla 67cea52
date: Move `php_time.h` include from `php_date.h` to `php_date.c`
TimWolla File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| /* | ||
| +----------------------------------------------------------------------+ | ||
| | Copyright © The PHP Group and Contributors. | | ||
| +----------------------------------------------------------------------+ | ||
| | This source file is subject to the Modified BSD License that is | | ||
| | bundled with this package in the file LICENSE, and is available | | ||
| | through the World Wide Web at <https://www.php.net/license/>. | | ||
| | | | ||
| | SPDX-License-Identifier: BSD-3-Clause | | ||
| +----------------------------------------------------------------------+ | ||
| | Authors: Derick Rethans <derick@derickrethans.nl> | | ||
| | Tim Düsterhus <timwolla@php.net> | | ||
| +----------------------------------------------------------------------+ | ||
| */ | ||
|
|
||
| #include "php.h" | ||
| #include "Zend/zend_exceptions.h" | ||
|
|
||
| #include "php_time.h" | ||
| #include "time_arginfo.h" | ||
|
|
||
| zend_class_entry *php_date_ce_time_duration; | ||
| zend_class_entry *php_date_ce_time_timeexception; | ||
|
|
||
| static zend_object_handlers time_duration_object_handlers; | ||
|
|
||
| static zend_object *time_duration_object_create(zend_class_entry *ce) | ||
| { | ||
| php_date_time_duration *obj = zend_object_alloc(sizeof(*obj), ce); | ||
|
|
||
| zend_object_std_init(&obj->std, ce); | ||
| object_properties_init(&obj->std, ce); | ||
|
|
||
| timelib_duration_ctor_static(&obj->duration, 0,0, false); | ||
|
|
||
| return &obj->std; | ||
| } | ||
|
|
||
| static zend_object *time_duration_object_clone(zend_object *object) | ||
| { | ||
| const php_date_time_duration *obj = php_date_time_duration_from_obj(object); | ||
|
|
||
| php_date_time_duration *new_obj = php_date_time_duration_from_obj(object->ce->create_object(object->ce)); | ||
|
|
||
| new_obj->duration = obj->duration; | ||
| zend_objects_clone_members(&new_obj->std, &obj->std); | ||
|
TimWolla marked this conversation as resolved.
|
||
|
|
||
| return &new_obj->std; | ||
| } | ||
|
|
||
| PHP_MINIT_FUNCTION(date_time) | ||
| { | ||
| /* Time\TimeException */ | ||
| php_date_ce_time_timeexception = register_class_Time_TimeException(zend_ce_exception); | ||
|
|
||
| /* Time\Duration */ | ||
| memcpy(&time_duration_object_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers)); | ||
| time_duration_object_handlers.offset = offsetof(php_date_time_duration, std); | ||
| time_duration_object_handlers.clone_obj = time_duration_object_clone; | ||
| php_date_ce_time_duration = register_class_Time_Duration(); | ||
| php_date_ce_time_duration->create_object = time_duration_object_create; | ||
| php_date_ce_time_duration->default_object_handlers = &time_duration_object_handlers; | ||
|
|
||
| return SUCCESS; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| /* | ||
| +----------------------------------------------------------------------+ | ||
| | Copyright © The PHP Group and Contributors. | | ||
| +----------------------------------------------------------------------+ | ||
| | This source file is subject to the Modified BSD License that is | | ||
| | bundled with this package in the file LICENSE, and is available | | ||
| | through the World Wide Web at <https://www.php.net/license/>. | | ||
| | | | ||
| | SPDX-License-Identifier: BSD-3-Clause | | ||
| +----------------------------------------------------------------------+ | ||
| | Authors: Derick Rethans <derick@derickrethans.nl> | | ||
| | Tim Düsterhus <timwolla@php.net> | | ||
| +----------------------------------------------------------------------+ | ||
| */ | ||
|
|
||
| #ifndef PHP_DATE_TIME_H | ||
| # define PHP_DATE_TIME_H | ||
|
|
||
| # include "php.h" | ||
| # include "lib/timelib.h" | ||
|
|
||
| typedef struct php_date_time_duration { | ||
| timelib_duration duration; | ||
| zend_object std; | ||
| } php_date_time_duration; | ||
|
|
||
| # define php_date_time_duration_from_obj(obj) ZEND_CONTAINER_OF(obj, php_date_time_duration, std) | ||
|
|
||
| # 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 { \ | ||
| zend_object *__d; \ | ||
| Z_PARAM_OBJ_OF_CLASS(__d, php_date_ce_time_duration); \ | ||
| d = php_date_time_duration_from_obj(__d); \ | ||
| } while (0); | ||
|
|
||
| # define Z_PARAM_DATE_TIME_DURATION_OR_NULL(d) do { \ | ||
| zend_object *__d; \ | ||
| Z_PARAM_OBJ_OF_CLASS_OR_NULL(__d, php_date_ce_time_duration); \ | ||
| d = __d ? php_date_time_duration_from_obj(__d) : NULL; \ | ||
| } while (0); | ||
|
|
||
| PHPAPI extern zend_class_entry *php_date_ce_time_duration; | ||
| PHPAPI extern zend_class_entry *php_date_ce_time_timeexception; | ||
|
derickr marked this conversation as resolved.
|
||
|
|
||
| PHP_MINIT_FUNCTION(date_time); | ||
|
|
||
| #endif /* PHP_DATE_TIME_H */ | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| --TEST-- | ||
| Time\Duration: clone | ||
| --FILE-- | ||
| <?php | ||
|
|
||
| require __DIR__ . '/helper.inc'; | ||
|
|
||
| function fc(Time\Duration $d): string { | ||
| return f(clone($d)); | ||
| } | ||
|
|
||
| echo fc(Time\Duration::fromSeconds(0, 0)), PHP_EOL; | ||
| echo fc(Time\Duration::fromSeconds(0, 1)), PHP_EOL; | ||
| echo fc(Time\Duration::fromSeconds(1, 1)), PHP_EOL; | ||
| echo fc(Time\Duration::fromSeconds(0, 1)->negate()), PHP_EOL; | ||
| echo fc(Time\Duration::fromSeconds(1, 1)->negate()), PHP_EOL; | ||
|
|
||
| ?> | ||
| --EXPECT-- | ||
| +0.000000000 | ||
| +0.000000001 | ||
| +1.000000001 | ||
| -0.000000001 | ||
| -1.000000001 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| <?php | ||
|
|
||
| function f(Time\Duration $d, bool $pad = true, bool $plus_sign = true) { | ||
| $result = sprintf("%s%d.%09d", ($d->negative ? "-" : ($plus_sign ? "+" : " ")), $d->seconds, $d->nanoseconds); | ||
| if ($pad) { | ||
| $result = str_pad($result, 21, pad_type: STR_PAD_LEFT); | ||
| } | ||
|
|
||
| return $result; | ||
| } | ||
|
|
||
| function as_int(Time\Duration $d): int { | ||
| return ($d->negative ? -1 : 1) * ($d->seconds * 1_000_000_000 + $d->nanoseconds); | ||
| } | ||
|
|
||
| function negate(Time\Duration $d): Time\Duration { | ||
| return $d->negate(); | ||
| } | ||
|
|
||
| function is_negative(Time\Duration $d): bool { | ||
| return $d->negative; | ||
| } | ||
|
|
||
| /** @return Time\Duration[] */ | ||
| function negate_all( | ||
| array $d /** @param Time\Duration[] */, | ||
| ): array { | ||
| return $d | ||
| |> array_map(negate(?), ?) | ||
| |> array_filter(?, is_negative(?)); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| --TEST-- | ||
| Time\Duration::absolute() | ||
| --FILE-- | ||
| <?php | ||
|
|
||
| require __DIR__ . '/../helper.inc'; | ||
|
|
||
| echo f(Time\Duration::fromSeconds(0, 0)->absolute()), PHP_EOL; | ||
| echo f(Time\Duration::fromSeconds(0, 0)->negate()->absolute()), PHP_EOL; | ||
|
|
||
| echo f(Time\Duration::fromSeconds(1, 0)->absolute()), PHP_EOL; | ||
| echo f(Time\Duration::fromSeconds(1, 0)->negate()->absolute()), PHP_EOL; | ||
|
|
||
| echo f(Time\Duration::fromSeconds(0, 1)->absolute()), PHP_EOL; | ||
| echo f(Time\Duration::fromSeconds(0, 1)->negate()->absolute()), PHP_EOL; | ||
|
|
||
| echo f(Time\Duration::fromSeconds(1, 1)->absolute()), PHP_EOL; | ||
| echo f(Time\Duration::fromSeconds(1, 1)->negate()->absolute()), PHP_EOL; | ||
|
|
||
| ?> | ||
| --EXPECT-- | ||
| +0.000000000 | ||
| +0.000000000 | ||
| +1.000000000 | ||
| +1.000000000 | ||
| +0.000000001 | ||
| +0.000000001 | ||
| +1.000000001 | ||
| +1.000000001 |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.