|
| 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 | +#ifndef PHP_WIN32 |
| 33 | +# define tv_sec_t time_t |
| 34 | +# define tv_usec_t suseconds_t |
| 35 | +#else |
| 36 | +# define tv_sec_t long |
| 37 | +# define tv_usec_t long |
| 38 | +#endif |
| 39 | + |
| 40 | +#define ZEND_MILLI_IN_SEC 1000U |
| 41 | +#define ZEND_MICRO_IN_SEC 1000000U |
| 42 | + |
| 43 | +BEGIN_EXTERN_C() |
| 44 | + |
| 45 | +/* Assign seconds to timeval */ |
| 46 | +static zend_always_inline void zend_time_sec2val(time_t s, struct timeval *tv) { |
| 47 | + tv->tv_sec = (tv_sec_t) s; |
| 48 | + tv->tv_usec = 0; |
| 49 | +} |
| 50 | + |
| 51 | +/* Assign microseconds to timeval */ |
| 52 | +static zend_always_inline void zend_time_usec2val(int64_t usec, struct timeval *tv) { |
| 53 | + tv->tv_sec = (tv_sec_t) (usec / ZEND_MICRO_IN_SEC); |
| 54 | + tv->tv_usec = (tv_usec_t) (usec % ZEND_MICRO_IN_SEC); |
| 55 | + |
| 56 | + if (UNEXPECTED(tv->tv_usec < 0)) { |
| 57 | + tv->tv_usec += ZEND_MICRO_IN_SEC; |
| 58 | + tv->tv_sec -= 1; |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +/* Assign double (seconds) to timeval */ |
| 63 | +static zend_always_inline void zend_time_dbl2val(double s, struct timeval *tv) { |
| 64 | + tv->tv_sec = (tv_sec_t) s; |
| 65 | + tv->tv_usec = (tv_usec_t) ((s - tv->tv_sec) * ZEND_MICRO_IN_SEC); |
| 66 | + |
| 67 | + if (UNEXPECTED(tv->tv_usec < 0)) { |
| 68 | + tv->tv_usec += ZEND_MICRO_IN_SEC; |
| 69 | + tv->tv_sec -= 1; |
| 70 | + } else if (UNEXPECTED(tv->tv_usec >= ZEND_MICRO_IN_SEC)) { |
| 71 | + // rare, but protects against rounding up to exactly 1 second |
| 72 | + tv->tv_usec -= ZEND_MICRO_IN_SEC; |
| 73 | + tv->tv_sec += 1; |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +/* Assign timeval to timespec */ |
| 78 | +static zend_always_inline void zend_time_val2spec(struct timeval tv, struct timespec *ts) { |
| 79 | + ts->tv_sec = (time_t) tv.tv_sec; |
| 80 | + ts->tv_nsec = (long) (tv.tv_usec * 1000); |
| 81 | +} |
| 82 | + |
| 83 | +/* Current real/wall-time in seconds */ |
| 84 | +ZEND_API time_t zend_time_real_sec(void); |
| 85 | + |
| 86 | +/* Current real/wall-time in up-to nano seconds */ |
| 87 | +ZEND_API void zend_time_real_spec(struct timespec *ts); |
| 88 | + |
| 89 | +/* Monotonic time in nanoseconds with a fallback to real/wall-time |
| 90 | + if no monotonic timer is available */ |
| 91 | +ZEND_API uint64_t zend_time_mono_fallback_nsec(void); |
| 92 | + |
| 93 | +END_EXTERN_C() |
| 94 | + |
| 95 | +#endif // ZEND_TIME_H |
0 commit comments