Back to index

php5  5.3.10
microtime.c
Go to the documentation of this file.
00001 /*
00002    +----------------------------------------------------------------------+
00003    | PHP Version 5                                                        |
00004    +----------------------------------------------------------------------+
00005    | Copyright (c) 1997-2012 The PHP Group                                |
00006    +----------------------------------------------------------------------+
00007    | This source file is subject to version 3.01 of the PHP license,      |
00008    | that is bundled with this package in the file LICENSE, and is        |
00009    | available through the world-wide-web at the following url:           |
00010    | http://www.php.net/license/3_01.txt                                  |
00011    | If you did not receive a copy of the PHP license and are unable to   |
00012    | obtain it through the world-wide-web, please send a note to          |
00013    | license@php.net so we can mail you a copy immediately.               |
00014    +----------------------------------------------------------------------+
00015    | Author: Paul Panotzki - Bunyip Information Systems                   |
00016    +----------------------------------------------------------------------+
00017  */
00018 
00019 /* $Id: microtime.c 321634 2012-01-01 13:15:04Z felipe $ */
00020 
00021 #include "php.h"
00022 
00023 #ifdef HAVE_SYS_TYPES_H
00024 #include <sys/types.h>
00025 #endif
00026 #ifdef PHP_WIN32
00027 #include "win32/time.h"
00028 #elif defined(NETWARE)
00029 #include <sys/timeval.h>
00030 #include <sys/time.h>
00031 #else
00032 #include <sys/time.h>
00033 #endif
00034 #ifdef HAVE_SYS_RESOURCE_H
00035 #include <sys/resource.h>
00036 #endif
00037 #ifdef HAVE_UNISTD_H
00038 #include <unistd.h>
00039 #endif
00040 #include <stdlib.h>
00041 #include <string.h>
00042 #include <stdio.h>
00043 #include <errno.h>
00044 
00045 #include "microtime.h"
00046 #include "ext/date/php_date.h"
00047 
00048 #define NUL  '\0'
00049 #define MICRO_IN_SEC 1000000.00
00050 #define SEC_IN_MIN 60
00051 
00052 #ifdef HAVE_GETTIMEOFDAY
00053 static void _php_gettimeofday(INTERNAL_FUNCTION_PARAMETERS, int mode)
00054 {
00055        zend_bool get_as_float = 0;
00056        struct timeval tp = {0};
00057 
00058        if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|b", &get_as_float) == FAILURE) {
00059               return;
00060        }
00061 
00062        if (gettimeofday(&tp, NULL)) {
00063               RETURN_FALSE;
00064        }
00065 
00066        if (get_as_float) {
00067               RETURN_DOUBLE((double)(tp.tv_sec + tp.tv_usec / MICRO_IN_SEC));
00068        }
00069 
00070        if (mode) {
00071               timelib_time_offset *offset;
00072 
00073               offset = timelib_get_time_zone_info(tp.tv_sec, get_timezone_info(TSRMLS_C));
00074                             
00075               array_init(return_value);
00076               add_assoc_long(return_value, "sec", tp.tv_sec);
00077               add_assoc_long(return_value, "usec", tp.tv_usec);
00078 
00079               add_assoc_long(return_value, "minuteswest", -offset->offset / SEC_IN_MIN);
00080               add_assoc_long(return_value, "dsttime", offset->is_dst);
00081 
00082               timelib_time_offset_dtor(offset);
00083        } else {
00084               char ret[100];
00085 
00086               snprintf(ret, 100, "%.8F %ld", tp.tv_usec / MICRO_IN_SEC, tp.tv_sec);
00087               RETURN_STRING(ret, 1);
00088        }
00089 }
00090 
00091 /* {{{ proto mixed microtime([bool get_as_float])
00092    Returns either a string or a float containing the current time in seconds and microseconds */
00093 PHP_FUNCTION(microtime)
00094 {
00095        _php_gettimeofday(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0);
00096 }
00097 /* }}} */
00098 
00099 /* {{{ proto array gettimeofday([bool get_as_float])
00100    Returns the current time as array */
00101 PHP_FUNCTION(gettimeofday)
00102 {
00103        _php_gettimeofday(INTERNAL_FUNCTION_PARAM_PASSTHRU, 1);
00104 }
00105 #endif
00106 /* }}} */
00107 
00108 #ifdef HAVE_GETRUSAGE
00109 /* {{{ proto array getrusage([int who])
00110    Returns an array of usage statistics */
00111 PHP_FUNCTION(getrusage)
00112 {
00113        struct rusage usg;
00114        long pwho = 0;
00115        int who = RUSAGE_SELF;
00116 
00117        if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|l", &pwho) == FAILURE) {
00118               return;
00119        }
00120        
00121        if (pwho == 1) {
00122               who = RUSAGE_CHILDREN;
00123        }
00124 
00125        memset(&usg, 0, sizeof(struct rusage));
00126 
00127        if (getrusage(who, &usg) == -1) {
00128               RETURN_FALSE;
00129        }
00130 
00131        array_init(return_value);
00132 #define PHP_RUSAGE_PARA(a) \
00133               add_assoc_long(return_value, #a, usg.a)
00134 #if !defined( _OSD_POSIX) && !defined(__BEOS__) /* BS2000 has only a few fields in the rusage struct */
00135        PHP_RUSAGE_PARA(ru_oublock);
00136        PHP_RUSAGE_PARA(ru_inblock);
00137        PHP_RUSAGE_PARA(ru_msgsnd);
00138        PHP_RUSAGE_PARA(ru_msgrcv);
00139        PHP_RUSAGE_PARA(ru_maxrss);
00140        PHP_RUSAGE_PARA(ru_ixrss);
00141        PHP_RUSAGE_PARA(ru_idrss);
00142        PHP_RUSAGE_PARA(ru_minflt);
00143        PHP_RUSAGE_PARA(ru_majflt);
00144        PHP_RUSAGE_PARA(ru_nsignals);
00145        PHP_RUSAGE_PARA(ru_nvcsw);
00146        PHP_RUSAGE_PARA(ru_nivcsw);
00147        PHP_RUSAGE_PARA(ru_nswap);
00148 #endif /*_OSD_POSIX*/
00149        PHP_RUSAGE_PARA(ru_utime.tv_usec);
00150        PHP_RUSAGE_PARA(ru_utime.tv_sec);
00151        PHP_RUSAGE_PARA(ru_stime.tv_usec);
00152        PHP_RUSAGE_PARA(ru_stime.tv_sec);
00153 #undef PHP_RUSAGE_PARA
00154 }
00155 #endif /* HAVE_GETRUSAGE */
00156 
00157 /* }}} */
00158 
00159 /*
00160  * Local variables:
00161  * tab-width: 4
00162  * c-basic-offset: 4
00163  * End:
00164  * vim600: sw=4 ts=4 fdm=marker
00165  * vim<600: sw=4 ts=4
00166  */