Coverage Report

Created: 2025-06-13 06:43

/src/php-src/ext/standard/microtime.c
Line
Count
Source (jump to first uncovered line)
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
0
#define SEC_IN_MIN 60
44
45
#ifdef HAVE_GETTIMEOFDAY
46
static void _php_gettimeofday(INTERNAL_FUNCTION_PARAMETERS, int mode)
47
0
{
48
0
  bool get_as_float = 0;
49
0
  struct timeval tp = {0};
50
51
0
  ZEND_PARSE_PARAMETERS_START(0, 1)
52
0
    Z_PARAM_OPTIONAL
53
0
    Z_PARAM_BOOL(get_as_float)
54
0
  ZEND_PARSE_PARAMETERS_END();
55
56
0
  if (gettimeofday(&tp, NULL)) {
57
0
    ZEND_ASSERT(0 && "gettimeofday() can't fail");
58
0
  }
59
60
0
  if (get_as_float) {
61
0
    RETURN_DOUBLE((double)(tp.tv_sec + tp.tv_usec / MICRO_IN_SEC));
62
0
  }
63
64
0
  if (mode) {
65
0
    timelib_time_offset *offset;
66
67
0
    offset = timelib_get_time_zone_info(tp.tv_sec, get_timezone_info());
68
69
0
    array_init(return_value);
70
0
    add_assoc_long(return_value, "sec", tp.tv_sec);
71
0
    add_assoc_long(return_value, "usec", tp.tv_usec);
72
73
0
    add_assoc_long(return_value, "minuteswest", -offset->offset / SEC_IN_MIN);
74
0
    add_assoc_long(return_value, "dsttime", offset->is_dst);
75
76
0
    timelib_time_offset_dtor(offset);
77
0
  } else {
78
0
    RETURN_NEW_STR(zend_strpprintf(0, "%.8F %ld", tp.tv_usec / MICRO_IN_SEC, (long)tp.tv_sec));
79
0
  }
80
0
}
81
82
/* {{{ Returns either a string or a float containing the current time in seconds and microseconds */
83
PHP_FUNCTION(microtime)
84
0
{
85
0
  _php_gettimeofday(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0);
86
0
}
87
/* }}} */
88
89
/* {{{ Returns the current time as array */
90
PHP_FUNCTION(gettimeofday)
91
0
{
92
0
  _php_gettimeofday(INTERNAL_FUNCTION_PARAM_PASSTHRU, 1);
93
0
}
94
#endif
95
/* }}} */
96
97
#ifdef HAVE_GETRUSAGE
98
/* {{{ Returns an array of usage statistics */
99
PHP_FUNCTION(getrusage)
100
0
{
101
0
  struct rusage usg;
102
0
  zend_long pwho = 0;
103
0
  int who = RUSAGE_SELF;
104
105
0
  ZEND_PARSE_PARAMETERS_START(0, 1)
106
0
    Z_PARAM_OPTIONAL
107
0
    Z_PARAM_LONG(pwho)
108
0
  ZEND_PARSE_PARAMETERS_END();
109
110
0
  if (pwho == 1) {
111
0
    who = RUSAGE_CHILDREN;
112
0
  }
113
114
0
  memset(&usg, 0, sizeof(struct rusage));
115
116
0
  if (getrusage(who, &usg) == -1) {
117
0
    RETURN_FALSE;
118
0
  }
119
120
0
  array_init(return_value);
121
122
0
#define PHP_RUSAGE_PARA(a) \
123
0
    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 */
126
  PHP_RUSAGE_PARA(ru_majflt);
127
  PHP_RUSAGE_PARA(ru_maxrss);
128
#elif !defined(_OSD_POSIX) && !defined(__HAIKU__)
129
0
  PHP_RUSAGE_PARA(ru_oublock);
130
0
  PHP_RUSAGE_PARA(ru_inblock);
131
0
  PHP_RUSAGE_PARA(ru_msgsnd);
132
0
  PHP_RUSAGE_PARA(ru_msgrcv);
133
0
  PHP_RUSAGE_PARA(ru_maxrss);
134
0
  PHP_RUSAGE_PARA(ru_ixrss);
135
0
  PHP_RUSAGE_PARA(ru_idrss);
136
0
  PHP_RUSAGE_PARA(ru_minflt);
137
0
  PHP_RUSAGE_PARA(ru_majflt);
138
0
  PHP_RUSAGE_PARA(ru_nsignals);
139
0
  PHP_RUSAGE_PARA(ru_nvcsw);
140
0
  PHP_RUSAGE_PARA(ru_nivcsw);
141
0
  PHP_RUSAGE_PARA(ru_nswap);
142
0
#endif /*_OSD_POSIX*/
143
0
  PHP_RUSAGE_PARA(ru_utime.tv_usec);
144
0
  PHP_RUSAGE_PARA(ru_utime.tv_sec);
145
0
  PHP_RUSAGE_PARA(ru_stime.tv_usec);
146
0
  PHP_RUSAGE_PARA(ru_stime.tv_sec);
147
148
0
#undef PHP_RUSAGE_PARA
149
0
}
150
#endif /* HAVE_GETRUSAGE */
151
152
/* }}} */