Mantis - Quercus
Viewing Issue Advanced Details
3285 block always 01-19-09 09:18 01-21-09 16:06
koreth  
nam  
normal  
closed 4.0.0  
fixed  
none    
none 4.0.0  
0003285: microtime(true) clock discontinuity
<?php
$a = microtime(true);
usleep(500000);
print microtime(true) - $a;

Regular PHP always prints a value around 0.5. But if you load it multiple times under Quercus, you will variously get values around 0.5, 1.5, and -0.5.

Patch:

--- a/modules/quercus/src/com/caucho/quercus/lib/date/DateModule.java
+++ b/modules/quercus/src/com/caucho/quercus/lib/date/DateModule.java
@@ -844,18 +844,25 @@ public class DateModule extends AbstractQuercusModule {
     return value;
   }
 
+ static double initialMillisecTime = -1;
+ static long initialNanosecTime = -1;
+
   /**
    * Returns the time including microseconds
    */
   public static Value microtime(Env env, @Optional boolean getAsFloat)
   {
- long sec = Alarm.getExactTime() / 1000;
- long nanos = Alarm.getExactTimeNanoseconds() % 1000000000L;
-
- if (nanos < 0)
- nanos += 1000000000;
+ // Since nanosec time is not based on the same epoch as system time,
+ // figure out what system time corresponds to the current nanosec time.
+ // Then we can use that system time as a basis to return real system
+ // times based on the nanosec time in subsequent calls.
+ if (initialNanosecTime == -1) {
+ initialMillisecTime = Alarm.getExactTime() * .001;
+ initialNanosecTime = Alarm.getExactTimeNanoseconds();
+ }
 
- double now = sec + nanos * 1e-9;
+ long nanos = Alarm.getExactTimeNanoseconds();
+ double now = (nanos - initialNanosecTime) * 1e-9 + initialMillisecTime;
 
     if (getAsFloat) {
       return new DoubleValue(now);

Notes
(0003769)
nam   
01-21-09 16:06   
php/190v