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