-
Notifications
You must be signed in to change notification settings - Fork 8k
Refactor Internal Time Retrieval Handling for Improved Consistency and Resolution #19202
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
marc-mabe
wants to merge
8
commits into
php:master
Choose a base branch
from
marc-mabe:current_time_wrapper
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.
+618
−890
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
8db78f1
Refactor basic time
marc-mabe 7cb47cc
sapi/litespeed
marc-mabe f66cae6
ext/ftp: prefer zend_monotime_fallback over zend_hrtime for timeout h…
marc-mabe d692f13
Replace php_time() with zend_realtime_get()
marc-mabe b25443f
Convert zend_realtime_get() to be a macro wrapper around time(NULL)
marc-mabe 25cd3c9
Replace time() in favor of zend_realtime_get()
marc-mabe dc55bf1
Addressed PR comments
marc-mabe 13a933b
Do not inline current time retrieval to be mockable
marc-mabe 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| /* | ||
| +----------------------------------------------------------------------+ | ||
| | Copyright (c) The PHP Group | | ||
| +----------------------------------------------------------------------+ | ||
| | This source file is subject to version 3.01 of the PHP license, | | ||
| | that is bundled with this package in the file LICENSE, and is | | ||
| | available through the world-wide-web at the following url: | | ||
| | https://www.php.net/license/3_01.txt | | ||
| | If you did not receive a copy of the PHP license and are unable to | | ||
| | obtain it through the world-wide-web, please send a note to | | ||
| | license@php.net so we can mail you a copy immediately. | | ||
| +----------------------------------------------------------------------+ | ||
| | Author: Marc Bennewitz <marc@mabe.berlin> | | ||
| +----------------------------------------------------------------------+ | ||
| */ | ||
|
|
||
| #include "zend_time.h" | ||
|
|
||
| /* Current real/wall-time in seconds */ | ||
| ZEND_API time_t zend_time_real_get(void) { | ||
| return time(NULL); | ||
| } | ||
|
|
||
| ZEND_API void zend_time_real_spec(struct timespec *ts) { | ||
| #if defined(HAVE_CLOCK_GETTIME) | ||
|
|
||
| (void) clock_gettime(CLOCK_REALTIME, ts); | ||
|
|
||
| #elif defined(HAVE_TIMESPEC_GET) | ||
|
|
||
| (void) timespec_get(ts, TIME_UTC); | ||
|
|
||
| #elif defined(HAVE_GETTIMEOFDAY) | ||
|
|
||
| struct timeval tv; | ||
| (void) gettimeofday(&tv, NULL); | ||
| zend_time_val2spec(tv, ts); | ||
|
|
||
| #else | ||
|
|
||
| ts->tv_sec = zend_time_real_get(); | ||
| ts->tv_nsec = 0; | ||
|
|
||
| #endif | ||
| } | ||
|
|
||
| ZEND_API uint64_t zend_time_mono_fallback(void) { | ||
| #if ZEND_HRTIME_AVAILABLE | ||
| return (uint64_t)zend_hrtime(); | ||
| #else | ||
| struct timespec ts; | ||
| zend_time_real_spec(&ts); | ||
| return ((uint64_t) ts.tv_sec * ZEND_NANO_IN_SEC) + ts.tv_nsec; | ||
| #endif | ||
| } |
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,95 @@ | ||
| /* | ||
| +----------------------------------------------------------------------+ | ||
| | Copyright (c) The PHP Group | | ||
| +----------------------------------------------------------------------+ | ||
| | This source file is subject to version 3.01 of the PHP license, | | ||
| | that is bundled with this package in the file LICENSE, and is | | ||
| | available through the world-wide-web at the following url: | | ||
| | https://www.php.net/license/3_01.txt | | ||
| | If you did not receive a copy of the PHP license and are unable to | | ||
| | obtain it through the world-wide-web, please send a note to | | ||
| | license@php.net so we can mail you a copy immediately. | | ||
| +----------------------------------------------------------------------+ | ||
| | Author: Marc Bennewitz <marc@mabe.berlin> | | ||
| +----------------------------------------------------------------------+ | ||
| */ | ||
|
|
||
| #ifndef ZEND_TIME_H | ||
| #define ZEND_TIME_H | ||
|
|
||
| #include "zend_portability.h" | ||
|
|
||
| #ifdef PHP_WIN32 | ||
| # include "win32/time.h" | ||
| #endif | ||
| #ifdef HAVE_SYS_TIME_H | ||
| # include <sys/time.h> | ||
| #endif | ||
| #include <time.h> | ||
|
|
||
| #include "zend_hrtime.h" | ||
|
|
||
| #ifndef PHP_WIN32 | ||
| # define tv_sec_t time_t | ||
| # define tv_usec_t suseconds_t | ||
| #else | ||
| # define tv_sec_t long | ||
| # define tv_usec_t long | ||
| #endif | ||
|
|
||
| #define ZEND_MILLI_IN_SEC 1000U | ||
| #define ZEND_MICRO_IN_SEC 1000000U | ||
|
|
||
| BEGIN_EXTERN_C() | ||
|
|
||
| /* Assign seconds to timeval */ | ||
| static zend_always_inline void zend_time_sec2val(time_t s, struct timeval *tv) { | ||
| tv->tv_sec = (tv_sec_t) s; | ||
| tv->tv_usec = 0; | ||
| } | ||
|
|
||
| /* Assign microseconds to timeval */ | ||
| static zend_always_inline void zend_time_usec2val(int64_t usec, struct timeval *tv) { | ||
| tv->tv_sec = (tv_sec_t) (usec / ZEND_MICRO_IN_SEC); | ||
| tv->tv_usec = (tv_usec_t) (usec % ZEND_MICRO_IN_SEC); | ||
|
|
||
| if (UNEXPECTED(tv->tv_usec < 0)) { | ||
| tv->tv_usec += ZEND_MICRO_IN_SEC; | ||
| tv->tv_sec -= 1; | ||
| } | ||
| } | ||
|
|
||
| /* Assign double (seconds) to timeval */ | ||
| static zend_always_inline void zend_time_dbl2val(double s, struct timeval *tv) { | ||
| tv->tv_sec = (tv_sec_t) s; | ||
| tv->tv_usec = (tv_usec_t) ((s - tv->tv_sec) * ZEND_MICRO_IN_SEC); | ||
|
|
||
| if (UNEXPECTED(tv->tv_usec < 0)) { | ||
| tv->tv_usec += ZEND_MICRO_IN_SEC; | ||
| tv->tv_sec -= 1; | ||
| } else if (UNEXPECTED(tv->tv_usec >= ZEND_MICRO_IN_SEC)) { | ||
| // rare, but protects against rounding up to exactly 1 second | ||
| tv->tv_usec -= ZEND_MICRO_IN_SEC; | ||
| tv->tv_sec += 1; | ||
| } | ||
| } | ||
|
|
||
| /* Assign timeval to timespec */ | ||
| static zend_always_inline void zend_time_val2spec(struct timeval tv, struct timespec *ts) { | ||
| ts->tv_sec = (time_t) tv.tv_sec; | ||
| ts->tv_nsec = (long) (tv.tv_usec * 1000); | ||
| } | ||
|
|
||
| /* Current real/wall-time in seconds */ | ||
| ZEND_API time_t zend_time_real_get(void); | ||
|
|
||
| /* Current real/wall-time in up-to nano seconds */ | ||
| ZEND_API void zend_time_real_spec(struct timespec *ts); | ||
|
|
||
| /* Monotonic time in nanoseconds with a fallback to real/wall-time | ||
| if no monotonic timer is available */ | ||
| ZEND_API uint64_t zend_time_mono_fallback(void); | ||
|
|
||
| END_EXTERN_C() | ||
|
|
||
| #endif // ZEND_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
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
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.