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/hrtime.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: Niklas Keller <kelunik@php.net>                              |
12
   | Author: Anatol Belski <ab@php.net>                                   |
13
   +----------------------------------------------------------------------+
14
 */
15
16
#include "php.h"
17
#include "zend_hrtime.h"
18
19
#ifdef ZEND_ENABLE_ZVAL_LONG64
20
0
#define PHP_RETURN_HRTIME(t) RETURN_LONG((zend_long)t)
21
#else
22
#ifdef _WIN32
23
# define HRTIME_U64A(i, s, len) _ui64toa_s(i, s, len, 10)
24
#else
25
# define HRTIME_U64A(i, s, len) \
26
  do { \
27
    int st = snprintf(s, len, "%llu", i); \
28
    s[st] = '\0'; \
29
  } while (0)
30
#endif
31
#define PHP_RETURN_HRTIME(t) do { \
32
  char _a[65]; \
33
  double _d; \
34
  HRTIME_U64A(t, _a, sizeof(_a)); \
35
  _d = zend_strtod(_a, NULL); \
36
  RETURN_DOUBLE(_d); \
37
  } while (0)
38
#endif
39
40
/* {{{ Returns an array of integers in form [seconds, nanoseconds] counted
41
  from an arbitrary point in time. If an optional boolean argument is
42
  passed, returns an integer on 64-bit platforms or float on 32-bit
43
  containing the current high-resolution time in nanoseconds. The
44
  delivered timestamp is monotonic and cannot be adjusted. */
45
PHP_FUNCTION(hrtime)
46
0
{
47
0
  bool get_as_num = 0;
48
0
  zend_hrtime_t t = zend_hrtime();
49
50
0
  ZEND_PARSE_PARAMETERS_START(0, 1)
51
0
    Z_PARAM_OPTIONAL
52
0
    Z_PARAM_BOOL(get_as_num)
53
0
  ZEND_PARSE_PARAMETERS_END();
54
55
0
#if ZEND_HRTIME_AVAILABLE
56
0
  if (UNEXPECTED(get_as_num)) {
57
0
    PHP_RETURN_HRTIME(t);
58
0
  } else {
59
0
    zval first, second;
60
0
    ZVAL_LONG(&first, (zend_long)(t / (zend_hrtime_t)ZEND_NANO_IN_SEC));
61
0
    ZVAL_LONG(&second, (zend_long)(t % (zend_hrtime_t)ZEND_NANO_IN_SEC));
62
0
    RETURN_ARR(zend_new_pair(&first, &second));
63
0
  }
64
#else
65
  RETURN_FALSE;
66
#endif
67
0
}
68
/* }}} */