From: Andrey Hristov Date: Wed, 17 Dec 2003 22:03:33 +0000 (+0000) Subject: Added optional parameter to microtime so now it can return float if it X-Git-Tag: php-5.0.0b3RC2~78 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=a0e930cff4cf6c1cb05073783d1272a3ab34ad7a;p=php Added optional parameter to microtime so now it can return float if it the user wants. This prevents from getting string representation exploding it and then creating a float. --- diff --git a/ext/standard/microtime.c b/ext/standard/microtime.c index fdd8578829..45e1331536 100644 --- a/ext/standard/microtime.c +++ b/ext/standard/microtime.c @@ -51,29 +51,40 @@ #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 */