Coverage Report

Created: 2026-06-02 06:36

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/php-src/ext/standard/microtime.c
Line
Count
Source
1
/*
2
   +----------------------------------------------------------------------+
3
   | Copyright © The PHP Group and Contributors.                          |
4
   +----------------------------------------------------------------------+
5
   | This source file is subject to the Modified BSD License that is      |
6
   | bundled with this package in the file LICENSE, and is available      |
7
   | through the World Wide Web at <https://www.php.net/license/>.        |
8
   |                                                                      |
9
   | SPDX-License-Identifier: BSD-3-Clause                                |
10
   +----------------------------------------------------------------------+
11
   | Author: Paul Panotzki - Bunyip Information Systems                   |
12
   +----------------------------------------------------------------------+
13
 */
14
15
#include "php.h"
16
17
#ifdef HAVE_SYS_TYPES_H
18
#include <sys/types.h>
19
#endif
20
#ifdef PHP_WIN32
21
#include "win32/time.h"
22
#include "win32/getrusage.h"
23
#else
24
#include <sys/time.h>
25
#endif
26
#ifdef HAVE_SYS_RESOURCE_H
27
#include <sys/resource.h>
28
#endif
29
#ifdef HAVE_UNISTD_H
30
#include <unistd.h>
31
#endif
32
#include <stdlib.h>
33
#include <string.h>
34
#include <stdio.h>
35
#include <errno.h>
36
37
#include "ext/date/php_date.h"
38
39
#define NUL  '\0'
40
#define MICRO_IN_SEC 1000000.00
41
0
#define SEC_IN_MIN 60
42
43
#ifdef HAVE_GETTIMEOFDAY
44
static void _php_gettimeofday(INTERNAL_FUNCTION_PARAMETERS, int mode)
45
0
{
46
0
  bool get_as_float = 0;
47
0
  struct timeval tp = {0};
48
49
0
  ZEND_PARSE_PARAMETERS_START(0, 1)
50
0
    Z_PARAM_OPTIONAL
51
0
    Z_PARAM_BOOL(get_as_float)
52
0
  ZEND_PARSE_PARAMETERS_END();
53
54
0
  if (gettimeofday(&tp, NULL)) {
55
0
    ZEND_ASSERT(0 && "gettimeofday() can't fail");
56
0
  }
57
58
0
  if (get_as_float) {
59
0
    RETURN_DOUBLE((double)(tp.tv_sec + tp.tv_usec / MICRO_IN_SEC));
60
0
  }
61
62
0
  if (mode) {
63
0
    timelib_time_offset *offset;
64
65
0
    offset = timelib_get_time_zone_info(tp.tv_sec, get_timezone_info());
66
67
0
    array_init(return_value);
68
0
    add_assoc_long(return_value, "sec", tp.tv_sec);
69
0
    add_assoc_long(return_value, "usec", tp.tv_usec);
70
71
0
    add_assoc_long(return_value, "minuteswest", -offset->offset / SEC_IN_MIN);
72
0
    add_assoc_long(return_value, "dsttime", offset->is_dst);
73
74
0
    timelib_time_offset_dtor(offset);
75
0
  } else {
76
0
    RETURN_NEW_STR(zend_strpprintf(0, "%.8F %ld", tp.tv_usec / MICRO_IN_SEC, (long)tp.tv_sec));
77
0
  }
78
0
}
79
80
/* {{{ Returns either a string or a float containing the current time in seconds and microseconds */
81
PHP_FUNCTION(microtime)
82
0
{
83
0
  _php_gettimeofday(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0);
84
0
}
85
/* }}} */
86
87
/* {{{ Returns the current time as array */
88
PHP_FUNCTION(gettimeofday)
89
0
{
90
0
  _php_gettimeofday(INTERNAL_FUNCTION_PARAM_PASSTHRU, 1);
91
0
}
92
#endif
93
/* }}} */
94
95
#ifdef HAVE_GETRUSAGE
96
/* {{{ Returns an array of usage statistics */
97
PHP_FUNCTION(getrusage)
98
0
{
99
0
  struct rusage usg;
100
0
  zend_long pwho = 0;
101
0
  int who = RUSAGE_SELF;
102
103
0
  ZEND_PARSE_PARAMETERS_START(0, 1)
104
0
    Z_PARAM_OPTIONAL
105
0
    Z_PARAM_LONG(pwho)
106
0
  ZEND_PARSE_PARAMETERS_END();
107
108
0
  if (pwho == 1) {
109
0
    who = RUSAGE_CHILDREN;
110
0
  }
111
112
0
  memset(&usg, 0, sizeof(struct rusage));
113
114
0
  if (getrusage(who, &usg) == -1) {
115
0
    RETURN_FALSE;
116
0
  }
117
118
0
  array_init(return_value);
119
120
0
#define PHP_RUSAGE_PARA(a) \
121
0
    add_assoc_long(return_value, #a, usg.a)
122
123
#ifdef PHP_WIN32 /* Windows only implements a limited amount of fields from the rusage struct */
124
  PHP_RUSAGE_PARA(ru_majflt);
125
  PHP_RUSAGE_PARA(ru_maxrss);
126
#elif !defined(_OSD_POSIX) && !defined(__HAIKU__)
127
0
  PHP_RUSAGE_PARA(ru_oublock);
128
0
  PHP_RUSAGE_PARA(ru_inblock);
129
0
  PHP_RUSAGE_PARA(ru_msgsnd);
130
0
  PHP_RUSAGE_PARA(ru_msgrcv);
131
0
  PHP_RUSAGE_PARA(ru_maxrss);
132
0
  PHP_RUSAGE_PARA(ru_ixrss);
133
0
  PHP_RUSAGE_PARA(ru_idrss);
134
0
  PHP_RUSAGE_PARA(ru_minflt);
135
0
  PHP_RUSAGE_PARA(ru_majflt);
136
0
  PHP_RUSAGE_PARA(ru_nsignals);
137
0
  PHP_RUSAGE_PARA(ru_nvcsw);
138
0
  PHP_RUSAGE_PARA(ru_nivcsw);
139
0
  PHP_RUSAGE_PARA(ru_nswap);
140
0
#endif /*_OSD_POSIX*/
141
0
  PHP_RUSAGE_PARA(ru_utime.tv_usec);
142
0
  PHP_RUSAGE_PARA(ru_utime.tv_sec);
143
0
  PHP_RUSAGE_PARA(ru_stime.tv_usec);
144
0
  PHP_RUSAGE_PARA(ru_stime.tv_sec);
145
146
0
#undef PHP_RUSAGE_PARA
147
0
}
148
#endif /* HAVE_GETRUSAGE */
149
150
/* }}} */