]> granicus.if.org Git - php/commitdiff
Added optional parameter to microtime so now it can return float if it
authorAndrey Hristov <andrey@php.net>
Wed, 17 Dec 2003 22:03:33 +0000 (22:03 +0000)
committerAndrey Hristov <andrey@php.net>
Wed, 17 Dec 2003 22:03:33 +0000 (22:03 +0000)
the user wants. This prevents from getting string representation exploding
it and then creating a float.

ext/standard/microtime.c

index fdd8578829a3a49f8ee5cb9952cba13a402b95b1..45e1331536a1b3cbd243cda3ed650cdb62abc367 100644 (file)
 #define MICRO_IN_SEC 1000000.00
 #define SEC_IN_MIN 60
 
-/* {{{ proto string microtime(void)
-   Returns a string containing the current time in seconds and microseconds */
 #ifdef HAVE_GETTIMEOFDAY
+/* {{{ proto string microtime([bool get_as_float])
+   Returns either a string or a float containing the current time in seconds and microseconds */
 PHP_FUNCTION(microtime)
 {
        struct timeval tp;
        long sec = 0L;
        double msec = 0.0;
-       char ret[100];
-       
+       zend_bool get_as_float = 0;
+
+       if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|b", &get_as_float) == FAILURE) {
+               return;
+       }
+                
        if (gettimeofday((struct timeval *) &tp, (NUL)) == 0) {
                msec = (double) (tp.tv_usec / MICRO_IN_SEC);
                sec = tp.tv_sec;
        
                if (msec >= 1.0) msec -= (long) msec;
-               snprintf(ret, 100, "%.8f %ld", msec, sec);
-               RETVAL_STRING(ret,1);
+               if (get_as_float == 0) {
+                       char ret[100];
+               
+                       snprintf(ret, 100, "%.8f %ld", msec, sec);
+                       RETURN_STRING(ret,1);
+               } else {
+                       RETURN_DOUBLE((double) (sec + msec));
+               }
        } else {
                RETURN_FALSE;
        }
+
 }
-#endif
 /* }}} */
+#endif
 
 /* {{{ proto array gettimeofday(void)
    Returns the current time as array */