|
| 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: Marc Bennewitz <marc@mabe.berlin> | |
| 14 | + +----------------------------------------------------------------------+ |
| 15 | +*/ |
| 16 | + |
| 17 | +#ifndef ZEND_TIME_H |
| 18 | +#define ZEND_TIME_H |
| 19 | + |
| 20 | +#include "zend_portability.h" |
| 21 | + |
| 22 | +#ifdef PHP_WIN32 |
| 23 | +# include "win32/time.h" |
| 24 | +#endif |
| 25 | +#ifdef HAVE_SYS_TIME_H |
| 26 | +# include <sys/time.h> |
| 27 | +#endif |
| 28 | +#include <time.h> |
| 29 | + |
| 30 | +#include "zend_hrtime.h" |
| 31 | + |
| 32 | +#define ZEND_MILLI_IN_SEC 1000U |
| 33 | +#define ZEND_MICRO_IN_SEC 1000000U |
| 34 | + |
| 35 | +#ifndef PHP_WIN32 |
| 36 | + |
| 37 | +/* Helper macro to assign seconds to timeval */ |
| 38 | +# define zend_time_sec2val(s, tv) \ |
| 39 | + (tv).tv_sec = (time_t) (s); \ |
| 40 | + (tv).tv_usec = 0; |
| 41 | + |
| 42 | +/* Helper macro to assign microseconds to timeval */ |
| 43 | +# define zend_time_usec2val(usec, tv) \ |
| 44 | + (tv).tv_sec = (time_t) ((usec) / ZEND_MICRO_IN_SEC); \ |
| 45 | + (tv).tv_usec = (suseconds_t) ((usec) % ZEND_MICRO_IN_SEC); |
| 46 | + |
| 47 | +/* Helper macro to assign double (seconds) to timeval */ |
| 48 | +# define zend_time_dbl2val(d, tv) \ |
| 49 | + (tv).tv_sec = (time_t) (d); \ |
| 50 | + (tv).tv_usec = (suseconds_t) (((d) - (tv).tv_sec) * ZEND_MICRO_IN_SEC); |
| 51 | + |
| 52 | +#else |
| 53 | + |
| 54 | +# define zend_time_sec2val(s, tv) \ |
| 55 | + (tv).tv_sec = (long) (s); \ |
| 56 | + (tv).tv_usec = 0; |
| 57 | + |
| 58 | +# define zend_time_usec2val(usec, tv) \ |
| 59 | + (tv).tv_sec = (long) ((usec) / ZEND_MICRO_IN_SEC); \ |
| 60 | + (tv).tv_usec = (long) ((usec) % ZEND_MICRO_IN_SEC); |
| 61 | + |
| 62 | +# define zend_time_dbl2val(d, tv) \ |
| 63 | + (tv).tv_sec = (long) (d); \ |
| 64 | + (tv).tv_usec = (long) (((d) - (tv).tv_sec) * ZEND_MICRO_IN_SEC); |
| 65 | + |
| 66 | +#endif |
| 67 | + |
| 68 | +/* Helper macro to copy timeval to timespec */ |
| 69 | +#define zend_time_val2spec(tv, ts) \ |
| 70 | + (ts).tv_sec = (time_t) (tv).tv_sec; \ |
| 71 | + (ts).tv_nsec = (long) ((tv).tv_usec * 1000); |
| 72 | + |
| 73 | +BEGIN_EXTERN_C() |
| 74 | + |
| 75 | +/* Like time() but with up to nanosecond */ |
| 76 | +ZEND_API time_t zend_realtime_get(time_t *sec, long *nsec); |
| 77 | + |
| 78 | +/* wrapper around clock_gettime/timestamp_get/gettimeofday/time */ |
| 79 | +ZEND_API void zend_realtime_spec(struct timespec *ts); |
| 80 | + |
| 81 | +/* Monotonic time in nanoseconds with a fallback to real/wall-time |
| 82 | + if no monotonic timer is available */ |
| 83 | +#if ZEND_HRTIME_AVAILABLE |
| 84 | +# define zend_monotime_fallback() (uint64_t)zend_hrtime() |
| 85 | +#else |
| 86 | +ZEND_API zend_always_inline uint64_t zend_monotime_fallback(void) { |
| 87 | + struct timespec ts; |
| 88 | + zend_realtime_spec(&ts); |
| 89 | + return ((uint64_t) ts.tv_sec * ZEND_NANO_IN_SEC) + ts.tv_nsec; |
| 90 | +} |
| 91 | +#endif |
| 92 | + |
| 93 | +END_EXTERN_C() |
| 94 | + |
| 95 | +#endif // ZEND_TIME_H |
0 commit comments