1 /** 2 * @defgroup time Time 3 * @ingroup libc 4 */ 5 6 #ifndef _SYS_TIME_H 7 #define _SYS_TIME_H 8 #ifdef __cplusplus 9 extern "C" { 10 #endif 11 12 #include <features.h> 13 14 #include <sys/select.h> 15 16 #define ITIMER_REAL 0 17 #define ITIMER_VIRTUAL 1 18 #define ITIMER_PROF 2 19 20 struct itimerval { 21 struct timeval it_interval; 22 struct timeval it_value; 23 }; 24 25 int getitimer (int, struct itimerval *); 26 int setitimer (int, const struct itimerval *__restrict, struct itimerval *__restrict); 27 int utimes (const char *, const struct timeval [2]); 28 29 #if defined(_GNU_SOURCE) || defined(_BSD_SOURCE) 30 struct timezone { 31 int tz_minuteswest; 32 int tz_dsttime; 33 }; 34 35 /* gettimeofday() defination in posix and statement is not same with musl */ 36 int gettimeofday(struct timeval* tv, struct timezone* tz); 37 38 int futimes(int, const struct timeval [2]); 39 int futimesat(int, const char *, const struct timeval [2]); 40 int lutimes(const char *, const struct timeval [2]); 41 42 /** 43 * @ingroup time 44 * @par Description: 45 * This function sets the time as well as a timezone. 46 * 47 * @attention 48 * <ul> 49 * <li>The function is not supported to set timezone,So the second parameter is unused</li> 50 * </ul> 51 * 52 * @retval #0 The function is executed successfully. 53 * @retval #-1 The function failed to execute, and corresponding error code is set. 54 * 55 * @par Errors 56 * <ul> 57 * <li><b>EINVAL</b>: An invalid Input.</li> 58 * </ul> 59 * 60 * @par Dependency: 61 * <ul><li>time.h</li></ul> 62 * 63 * @see clock_gettime | time | ctime 64 * 65 */ 66 int settimeofday(const struct timeval *, const struct timezone *); 67 68 /** 69 * @ingroup time 70 * @par Description: 71 * This function gradually adjusts the system clock (as returned by gettimeofday). 72 * 73 * @attention 74 * <ul> 75 * <li>None.</li> 76 * </ul> 77 * 78 * @retval #0 The function is executed successfully. 79 * @retval #-1 The function failed to execute, and corresponding error code is set. 80 * 81 * @par Errors 82 * <ul> 83 * <li><b>EINVAL</b>: An invalid input.</li> 84 * </ul> 85 * 86 * @par Dependency: 87 * <ul><li>time.h</li></ul> 88 * 89 * @see gettimeofday | time 90 * 91 */ 92 int adjtime (const struct timeval *, struct timeval *); 93 #define timerisset(t) ((t)->tv_sec || (t)->tv_usec) 94 #define timerclear(t) ((t)->tv_sec = (t)->tv_usec = 0) 95 #define timercmp(s,t,op) ((s)->tv_sec == (t)->tv_sec ? \ 96 (s)->tv_usec op (t)->tv_usec : (s)->tv_sec op (t)->tv_sec) 97 #define timeradd(s,t,a) (void) ( (a)->tv_sec = (s)->tv_sec + (t)->tv_sec, \ 98 ((a)->tv_usec = (s)->tv_usec + (t)->tv_usec) >= 1000000 && \ 99 ((a)->tv_usec -= 1000000, (a)->tv_sec++) ) 100 #define timersub(s,t,a) (void) ( (a)->tv_sec = (s)->tv_sec - (t)->tv_sec, \ 101 ((a)->tv_usec = (s)->tv_usec - (t)->tv_usec) < 0 && \ 102 ((a)->tv_usec += 1000000, (a)->tv_sec--) ) 103 #endif 104 105 #if defined(_GNU_SOURCE) 106 #define TIMEVAL_TO_TIMESPEC(tv, ts) ( \ 107 (ts)->tv_sec = (tv)->tv_sec, \ 108 (ts)->tv_nsec = (tv)->tv_usec * 1000, \ 109 (void)0 ) 110 #define TIMESPEC_TO_TIMEVAL(tv, ts) ( \ 111 (tv)->tv_sec = (ts)->tv_sec, \ 112 (tv)->tv_usec = (ts)->tv_nsec / 1000, \ 113 (void)0 ) 114 #endif 115 116 /* gettimeofday has been defined in time.c*/ 117 #if _REDIR_TIME64 118 __REDIR(gettimeofday, __gettimeofday_time64); 119 __REDIR(getitimer, __getitimer_time64); 120 __REDIR(setitimer, __setitimer_time64); 121 __REDIR(utimes, __utimes_time64); 122 #if defined(_GNU_SOURCE) || defined(_BSD_SOURCE) 123 __REDIR(futimes, __futimes_time64); 124 __REDIR(futimesat, __futimesat_time64); 125 __REDIR(lutimes, __lutimes_time64); 126 __REDIR(settimeofday, __settimeofday_time64); 127 __REDIR(adjtime, __adjtime64); 128 #endif 129 #endif 130 131 #ifdef __cplusplus 132 } 133 #endif 134 #endif 135