root/ext/standard/microtime.c

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. _php_gettimeofday
  2. PHP_FUNCTION
  3. PHP_FUNCTION
  4. PHP_FUNCTION

   1 /*
   2    +----------------------------------------------------------------------+
   3    | PHP Version 7                                                        |
   4    +----------------------------------------------------------------------+
   5    | Copyright (c) 1997-2016 The PHP Group                                |
   6    +----------------------------------------------------------------------+
   7    | This source file is subject to version 3.01 of the PHP license,      |
   8    | that is bundled with this package in the file LICENSE, and is        |
   9    | available through the world-wide-web at the following url:           |
  10    | http://www.php.net/license/3_01.txt                                  |
  11    | If you did not receive a copy of the PHP license and are unable to   |
  12    | obtain it through the world-wide-web, please send a note to          |
  13    | license@php.net so we can mail you a copy immediately.               |
  14    +----------------------------------------------------------------------+
  15    | Author: Paul Panotzki - Bunyip Information Systems                   |
  16    +----------------------------------------------------------------------+
  17  */
  18 
  19 /* $Id$ */
  20 
  21 #include "php.h"
  22 
  23 #ifdef HAVE_SYS_TYPES_H
  24 #include <sys/types.h>
  25 #endif
  26 #ifdef PHP_WIN32
  27 #include "win32/time.h"
  28 #include "win32/getrusage.h"
  29 #elif defined(NETWARE)
  30 #include <sys/timeval.h>
  31 #include <sys/time.h>
  32 #else
  33 #include <sys/time.h>
  34 #endif
  35 #ifdef HAVE_SYS_RESOURCE_H
  36 #include <sys/resource.h>
  37 #endif
  38 #ifdef HAVE_UNISTD_H
  39 #include <unistd.h>
  40 #endif
  41 #include <stdlib.h>
  42 #include <string.h>
  43 #include <stdio.h>
  44 #include <errno.h>
  45 
  46 #include "microtime.h"
  47 #include "ext/date/php_date.h"
  48 
  49 #define NUL  '\0'
  50 #define MICRO_IN_SEC 1000000.00
  51 #define SEC_IN_MIN 60
  52 
  53 #ifdef HAVE_GETTIMEOFDAY
  54 static void _php_gettimeofday(INTERNAL_FUNCTION_PARAMETERS, int mode)
  55 {
  56         zend_bool get_as_float = 0;
  57         struct timeval tp = {0};
  58 
  59         if (zend_parse_parameters(ZEND_NUM_ARGS(), "|b", &get_as_float) == FAILURE) {
  60                 return;
  61         }
  62 
  63         if (gettimeofday(&tp, NULL)) {
  64                 RETURN_FALSE;
  65         }
  66 
  67         if (get_as_float) {
  68                 RETURN_DOUBLE((double)(tp.tv_sec + tp.tv_usec / MICRO_IN_SEC));
  69         }
  70 
  71         if (mode) {
  72                 timelib_time_offset *offset;
  73 
  74                 offset = timelib_get_time_zone_info(tp.tv_sec, get_timezone_info());
  75 
  76                 array_init(return_value);
  77                 add_assoc_long(return_value, "sec", tp.tv_sec);
  78                 add_assoc_long(return_value, "usec", tp.tv_usec);
  79 
  80                 add_assoc_long(return_value, "minuteswest", -offset->offset / SEC_IN_MIN);
  81                 add_assoc_long(return_value, "dsttime", offset->is_dst);
  82 
  83                 timelib_time_offset_dtor(offset);
  84         } else {
  85                 char ret[100];
  86 
  87                 snprintf(ret, 100, "%.8F %ld", tp.tv_usec / MICRO_IN_SEC, tp.tv_sec);
  88                 RETURN_STRING(ret);
  89         }
  90 }
  91 
  92 /* {{{ proto mixed microtime([bool get_as_float])
  93    Returns either a string or a float containing the current time in seconds and microseconds */
  94 PHP_FUNCTION(microtime)
  95 {
  96         _php_gettimeofday(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0);
  97 }
  98 /* }}} */
  99 
 100 /* {{{ proto array gettimeofday([bool get_as_float])
 101    Returns the current time as array */
 102 PHP_FUNCTION(gettimeofday)
 103 {
 104         _php_gettimeofday(INTERNAL_FUNCTION_PARAM_PASSTHRU, 1);
 105 }
 106 #endif
 107 /* }}} */
 108 
 109 #ifdef HAVE_GETRUSAGE
 110 /* {{{ proto array getrusage([int who])
 111    Returns an array of usage statistics */
 112 PHP_FUNCTION(getrusage)
 113 {
 114         struct rusage usg;
 115         zend_long pwho = 0;
 116         int who = RUSAGE_SELF;
 117 
 118         if (zend_parse_parameters(ZEND_NUM_ARGS(), "|l", &pwho) == FAILURE) {
 119                 return;
 120         }
 121 
 122         if (pwho == 1) {
 123                 who = RUSAGE_CHILDREN;
 124         }
 125 
 126         memset(&usg, 0, sizeof(struct rusage));
 127 
 128         if (getrusage(who, &usg) == -1) {
 129                 RETURN_FALSE;
 130         }
 131 
 132         array_init(return_value);
 133 
 134 #define PHP_RUSAGE_PARA(a) \
 135                 add_assoc_long(return_value, #a, usg.a)
 136 
 137 #ifdef PHP_WIN32 /* Windows only implements a limited amount of fields from the rusage struct */
 138         PHP_RUSAGE_PARA(ru_majflt);
 139         PHP_RUSAGE_PARA(ru_maxrss);
 140 #elif !defined( _OSD_POSIX) && !defined(__BEOS__) /* BS2000 has only a few fields in the rusage struct*/
 141         PHP_RUSAGE_PARA(ru_oublock);
 142         PHP_RUSAGE_PARA(ru_inblock);
 143         PHP_RUSAGE_PARA(ru_msgsnd);
 144         PHP_RUSAGE_PARA(ru_msgrcv);
 145         PHP_RUSAGE_PARA(ru_maxrss);
 146         PHP_RUSAGE_PARA(ru_ixrss);
 147         PHP_RUSAGE_PARA(ru_idrss);
 148         PHP_RUSAGE_PARA(ru_minflt);
 149         PHP_RUSAGE_PARA(ru_majflt);
 150         PHP_RUSAGE_PARA(ru_nsignals);
 151         PHP_RUSAGE_PARA(ru_nvcsw);
 152         PHP_RUSAGE_PARA(ru_nivcsw);
 153         PHP_RUSAGE_PARA(ru_nswap);
 154 #endif /*_OSD_POSIX*/
 155         PHP_RUSAGE_PARA(ru_utime.tv_usec);
 156         PHP_RUSAGE_PARA(ru_utime.tv_sec);
 157         PHP_RUSAGE_PARA(ru_stime.tv_usec);
 158         PHP_RUSAGE_PARA(ru_stime.tv_sec);
 159 
 160 #undef PHP_RUSAGE_PARA
 161 }
 162 #endif /* HAVE_GETRUSAGE */
 163 
 164 /* }}} */
 165 
 166 /*
 167  * Local variables:
 168  * tab-width: 4
 169  * c-basic-offset: 4
 170  * End:
 171  * vim600: sw=4 ts=4 fdm=marker
 172  * vim<600: sw=4 ts=4
 173  */

/* [<][>][^][v][top][bottom][index][help] */