@@ -4637,30 +4637,30 @@ PHP_FUNCTION(time_nanosleep)
4637
4637
Make the script sleep until the specified time */
4638
4638
PHP_FUNCTION (time_sleep_until )
4639
4639
{
4640
- double d_ts , c_ts ;
4640
+ double target_secs ;
4641
4641
struct timeval tm ;
4642
4642
struct timespec php_req , php_rem ;
4643
+ uint64_t current_ns , target_ns , diff_ns ;
4644
+ const uint64_t ns_per_sec = 1000000000 ;
4643
4645
4644
4646
ZEND_PARSE_PARAMETERS_START (1 , 1 )
4645
- Z_PARAM_DOUBLE (d_ts )
4647
+ Z_PARAM_DOUBLE (target_secs )
4646
4648
ZEND_PARSE_PARAMETERS_END ();
4647
4649
4648
4650
if (gettimeofday ((struct timeval * ) & tm , NULL ) != 0 ) {
4649
4651
RETURN_FALSE ;
4650
4652
}
4651
4653
4652
- c_ts = (double )(d_ts - tm .tv_sec - tm .tv_usec / 1000000.00 );
4653
- if (c_ts < 0 ) {
4654
+ target_ns = (uint64_t ) (target_secs * ns_per_sec );
4655
+ current_ns = ((uint64_t ) tm .tv_sec ) * ns_per_sec + ((uint64_t ) tm .tv_usec ) * 1000 ;
4656
+ if (target_ns < current_ns ) {
4654
4657
php_error_docref (NULL , E_WARNING , "Sleep until to time is less than current time" );
4655
4658
RETURN_FALSE ;
4656
4659
}
4657
4660
4658
- php_req .tv_sec = (time_t ) c_ts ;
4659
- if (php_req .tv_sec > c_ts ) { /* rounding up occurred */
4660
- php_req .tv_sec -- ;
4661
- }
4662
- /* 1sec = 1000000000 nanoseconds */
4663
- php_req .tv_nsec = (long ) ((c_ts - php_req .tv_sec ) * 1000000000.00 );
4661
+ diff_ns = target_ns - current_ns ;
4662
+ php_req .tv_sec = (time_t ) (diff_ns / ns_per_sec );
4663
+ php_req .tv_nsec = (long ) (diff_ns % ns_per_sec );
4664
4664
4665
4665
while (nanosleep (& php_req , & php_rem )) {
4666
4666
if (errno == EINTR ) {
0 commit comments