php-internal-docs 8.4.8
Unofficial docs for php/php-src
Loading...
Searching...
No Matches
microtime.c
Go to the documentation of this file.
1/*
2 +----------------------------------------------------------------------+
3 | Copyright (c) The PHP Group |
4 +----------------------------------------------------------------------+
5 | This source file is subject to version 3.01 of the PHP license, |
6 | that is bundled with this package in the file LICENSE, and is |
7 | available through the world-wide-web at the following url: |
8 | https://www.php.net/license/3_01.txt |
9 | If you did not receive a copy of the PHP license and are unable to |
10 | obtain it through the world-wide-web, please send a note to |
11 | license@php.net so we can mail you a copy immediately. |
12 +----------------------------------------------------------------------+
13 | Author: Paul Panotzki - Bunyip Information Systems |
14 +----------------------------------------------------------------------+
15 */
16
17#include "php.h"
18
19#ifdef HAVE_SYS_TYPES_H
20#include <sys/types.h>
21#endif
22#ifdef PHP_WIN32
23#include "win32/time.h"
24#include "win32/getrusage.h"
25#else
26#include <sys/time.h>
27#endif
28#ifdef HAVE_SYS_RESOURCE_H
29#include <sys/resource.h>
30#endif
31#ifdef HAVE_UNISTD_H
32#include <unistd.h>
33#endif
34#include <stdlib.h>
35#include <string.h>
36#include <stdio.h>
37#include <errno.h>
38
39#include "ext/date/php_date.h"
40
41#define NUL '\0'
42#define MICRO_IN_SEC 1000000.00
43#define SEC_IN_MIN 60
44
45#ifdef HAVE_GETTIMEOFDAY
46static void _php_gettimeofday(INTERNAL_FUNCTION_PARAMETERS, int mode)
47{
48 bool get_as_float = 0;
49 struct timeval tp = {0};
50
53 Z_PARAM_BOOL(get_as_float)
55
56 if (gettimeofday(&tp, NULL)) {
57 ZEND_ASSERT(0 && "gettimeofday() can't fail");
58 }
59
60 if (get_as_float) {
61 RETURN_DOUBLE((double)(tp.tv_sec + tp.tv_usec / MICRO_IN_SEC));
62 }
63
64 if (mode) {
66
68
70 add_assoc_long(return_value, "sec", tp.tv_sec);
71 add_assoc_long(return_value, "usec", tp.tv_usec);
72
73 add_assoc_long(return_value, "minuteswest", -offset->offset / SEC_IN_MIN);
74 add_assoc_long(return_value, "dsttime", offset->is_dst);
75
77 } else {
78 RETURN_NEW_STR(zend_strpprintf(0, "%.8F %ld", tp.tv_usec / MICRO_IN_SEC, (long)tp.tv_sec));
79 }
80}
81
82/* {{{ Returns either a string or a float containing the current time in seconds and microseconds */
84{
85 _php_gettimeofday(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0);
86}
87/* }}} */
88
89/* {{{ Returns the current time as array */
91{
92 _php_gettimeofday(INTERNAL_FUNCTION_PARAM_PASSTHRU, 1);
93}
94#endif
95/* }}} */
96
97#ifdef HAVE_GETRUSAGE
98/* {{{ Returns an array of usage statistics */
100{
101 struct rusage usg;
102 zend_long pwho = 0;
103 int who = RUSAGE_SELF;
104
107 Z_PARAM_LONG(pwho)
109
110 if (pwho == 1) {
111 who = RUSAGE_CHILDREN;
112 }
113
114 memset(&usg, 0, sizeof(struct rusage));
115
116 if (getrusage(who, &usg) == -1) {
118 }
119
121
122#define PHP_RUSAGE_PARA(a) \
123 add_assoc_long(return_value, #a, usg.a)
124
125#ifdef PHP_WIN32 /* Windows only implements a limited amount of fields from the rusage struct */
128#elif !defined(_OSD_POSIX) && !defined(__HAIKU__)
129 PHP_RUSAGE_PARA(ru_oublock);
130 PHP_RUSAGE_PARA(ru_inblock);
131 PHP_RUSAGE_PARA(ru_msgsnd);
132 PHP_RUSAGE_PARA(ru_msgrcv);
134 PHP_RUSAGE_PARA(ru_ixrss);
135 PHP_RUSAGE_PARA(ru_idrss);
136 PHP_RUSAGE_PARA(ru_minflt);
138 PHP_RUSAGE_PARA(ru_nsignals);
139 PHP_RUSAGE_PARA(ru_nvcsw);
140 PHP_RUSAGE_PARA(ru_nivcsw);
141 PHP_RUSAGE_PARA(ru_nswap);
142#endif /*_OSD_POSIX*/
143 PHP_RUSAGE_PARA(ru_utime.tv_usec);
145 PHP_RUSAGE_PARA(ru_stime.tv_usec);
147
148#undef PHP_RUSAGE_PARA
149}
150#endif /* HAVE_GETRUSAGE */
151
152/* }}} */
getrusage(int $mode=0)
gettimeofday(bool $as_float=false)
microtime(bool $as_float=false)
memset(ptr, 0, type->size)
zend_long offset
char * mode
#define NULL
Definition gdcache.h:45
#define RUSAGE_CHILDREN
Definition getrusage.h:28
#define RUSAGE_SELF
Definition getrusage.h:27
foreach($dp as $el) foreach( $dp as $el) if( $pass2< 2) echo ""
#define MICRO_IN_SEC
Definition microtime.c:42
#define SEC_IN_MIN
Definition microtime.c:43
timelib_time_offset * timelib_get_time_zone_info(timelib_sll ts, timelib_tzinfo *tz)
Definition parse_tz.c:881
#define PHP_RUSAGE_PARA(from, to, field)
Definition pcntl.c:315
#define PHP_FUNCTION
Definition php.h:364
PHPAPI timelib_tzinfo * get_timezone_info(void)
Definition php_date.c:573
zend_long ru_maxrss
Definition getrusage.h:69
struct timeval ru_utime
Definition getrusage.h:63
struct timeval ru_stime
Definition getrusage.h:66
zend_long ru_majflt
Definition getrusage.h:72
void timelib_time_offset_dtor(timelib_time_offset *t)
Definition timelib.c:141
struct _timelib_time_offset timelib_time_offset
ZEND_API zend_string * zend_strpprintf(size_t max_len, const char *format,...)
Definition zend.c:353
#define INTERNAL_FUNCTION_PARAMETERS
Definition zend.h:49
#define INTERNAL_FUNCTION_PARAM_PASSTHRU
Definition zend.h:50
#define ZEND_PARSE_PARAMETERS_END()
Definition zend_API.h:1641
#define RETURN_FALSE
Definition zend_API.h:1058
#define RETURN_DOUBLE(d)
Definition zend_API.h:1038
#define Z_PARAM_OPTIONAL
Definition zend_API.h:1667
#define ZEND_PARSE_PARAMETERS_START(min_num_args, max_num_args)
Definition zend_API.h:1620
#define Z_PARAM_LONG(dest)
Definition zend_API.h:1896
#define RETURN_NEW_STR(s)
Definition zend_API.h:1041
#define Z_PARAM_BOOL(dest)
Definition zend_API.h:1726
#define array_init(arg)
Definition zend_API.h:537
int32_t zend_long
Definition zend_long.h:42
#define ZEND_ASSERT(c)
zval * return_value