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