1 /* 2 * Copyright (c) 2006-2022, RT-Thread Development Team 3 * 4 * SPDX-License-Identifier: Apache-2.0 5 * 6 * Change Logs: 7 * Date Author Notes 8 * 2020-09-07 Meco Man combine gcc armcc iccarm 9 * 2021-02-12 Meco Man move all definitions located in <clock_time.h> to this file 10 */ 11 12 #ifndef __SYS_TIME_H__ 13 #define __SYS_TIME_H__ 14 15 #include <rtconfig.h> 16 #include <sys/types.h> 17 #include <stdint.h> 18 #include <time.h> 19 #ifdef _WIN32 20 typedef __time64_t time_t; 21 #endif /* _WIN32 */ 22 23 #ifdef __cplusplus 24 extern "C" { 25 #endif /* __cplusplus */ 26 27 #undef CLOCKS_PER_SEC 28 #define CLOCKS_PER_SEC RT_TICK_PER_SECOND 29 30 /* timezone */ 31 /* this method of representing timezones has been abandoned */ 32 #define DST_NONE 0 /* not on dst */ 33 34 struct timezone 35 { 36 int tz_minuteswest; /* minutes west of Greenwich */ 37 int tz_dsttime; /* type of dst correction */ 38 }; 39 40 /* lightweight timezone and daylight saving time */ 41 #ifdef RT_LIBC_USING_LIGHT_TZ_DST 42 void rt_tz_set(int32_t offset_sec); 43 int32_t rt_tz_get(void); 44 int8_t rt_tz_is_dst(void); 45 #endif /* RT_LIBC_USING_LIGHT_TZ_DST */ 46 47 struct itimerspec; 48 49 /* 'struct timeval' is defined on __x86_64__ toolchain */ 50 #if !defined(__x86_64__) && !defined(_TIMEVAL_DEFINED) 51 #define _TIMEVAL_DEFINED 52 struct timeval 53 { 54 time_t tv_sec; /* seconds */ 55 suseconds_t tv_usec; /* and microseconds */ 56 }; 57 #endif /* _TIMEVAL_DEFINED */ 58 59 #if defined(_GNU_SOURCE) && (defined(__x86_64__) || defined(__i386__) || defined(RT_USING_SMART)) 60 /* linux x86 platform gcc use! */ 61 #define _TIMEVAL_DEFINED 62 /* Values for the first argument to `getitimer' and `setitimer'. */ 63 enum __itimer_which 64 { 65 /* Timers run in real time. */ 66 ITIMER_REAL = 0, 67 #define ITIMER_REAL ITIMER_REAL 68 /* Timers run only when the process is executing. */ 69 ITIMER_VIRTUAL = 1, 70 #define ITIMER_VIRTUAL ITIMER_VIRTUAL 71 /* Timers run when the process is executing and when 72 the system is executing on behalf of the process. */ 73 ITIMER_PROF = 2 74 #define ITIMER_PROF ITIMER_PROF 75 }; 76 77 struct itimerval 78 { 79 /* Value to put into `it_value' when the timer expires. */ 80 struct timeval it_interval; 81 /* Time to the next timer expiration. */ 82 struct timeval it_value; 83 }; 84 #endif /* defined(_GNU_SOURCE) && (defined(__x86_64__) || defined(__i386__)) || defined(RT_USING_SMART) */ 85 86 #if defined(__ARMCC_VERSION) || defined(_WIN32) || (defined(__ICCARM__) && (__VER__ < 8010001)) 87 struct timespec 88 { 89 time_t tv_sec; /* seconds */ 90 long tv_nsec; /* and nanoseconds */ 91 }; 92 #endif /* defined(__ARMCC_VERSION) || defined(_WIN32) || (defined(__ICCARM__) && (__VER__ < 8010001)) */ 93 94 #if !(defined(__GNUC__) && !defined(__ARMCC_VERSION)/*GCC*/) 95 /* 96 * Structure defined by POSIX.1b to be like a itimerval, but with 97 * timespecs. Used in the timer_*() system calls. 98 */ 99 struct itimerspec 100 { 101 struct timespec it_interval; 102 struct timespec it_value; 103 }; 104 #endif /* !(defined(__GNUC__) && !defined(__ARMCC_VERSION)) */ 105 106 int stime(const time_t *t); 107 time_t timegm(struct tm * const t); 108 int gettimeofday(struct timeval *tv, struct timezone *tz); 109 int settimeofday(const struct timeval *tv, const struct timezone *tz); 110 111 #if defined(__ARMCC_VERSION) || defined (__ICCARM__) || defined(_WIN32) 112 struct tm *gmtime_r(const time_t *timep, struct tm *r); 113 char* asctime_r(const struct tm *t, char *buf); 114 char *ctime_r(const time_t * tim_p, char * result); 115 struct tm* localtime_r(const time_t* t, struct tm* r); 116 #endif /* defined(__ARMCC_VERSION) || defined (__ICCARM__) || defined(_WIN32) */ 117 118 #ifdef _WIN32 119 struct tm* gmtime(const time_t* t); 120 struct tm* localtime(const time_t* t); 121 time_t mktime(struct tm* const t); 122 char* ctime(const time_t* tim_p); 123 time_t time(time_t* t); 124 #endif /* _WIN32 */ 125 126 #ifdef RT_USING_POSIX_DELAY 127 int nanosleep(const struct timespec *rqtp, struct timespec *rmtp); 128 #endif /* RT_USING_POSIX_DELAY */ 129 130 #define MILLISECOND_PER_SECOND 1000UL 131 #define MICROSECOND_PER_SECOND 1000000UL 132 #define NANOSECOND_PER_SECOND 1000000000UL 133 134 #define MILLISECOND_PER_TICK (MILLISECOND_PER_SECOND / RT_TICK_PER_SECOND) 135 #define MICROSECOND_PER_TICK (MICROSECOND_PER_SECOND / RT_TICK_PER_SECOND) 136 #define NANOSECOND_PER_TICK (NANOSECOND_PER_SECOND / RT_TICK_PER_SECOND) 137 138 #if defined(RT_USING_POSIX_CLOCK) || defined (RT_USING_POSIX_TIMER) 139 /* POSIX clock and timer */ 140 141 #ifndef CLOCK_REALTIME_COARSE 142 #define CLOCK_REALTIME_COARSE 0 143 #endif /* CLOCK_REALTIME_COARSE */ 144 145 #ifndef CLOCK_REALTIME 146 #define CLOCK_REALTIME 1 147 #endif /* CLOCK_REALTIME */ 148 149 #define CLOCK_CPUTIME_ID 2 150 151 #ifndef CLOCK_PROCESS_CPUTIME_ID 152 #define CLOCK_PROCESS_CPUTIME_ID CLOCK_CPUTIME_ID 153 #endif /* CLOCK_PROCESS_CPUTIME_ID */ 154 155 #ifndef CLOCK_THREAD_CPUTIME_ID 156 #define CLOCK_THREAD_CPUTIME_ID 3 157 #endif /* CLOCK_THREAD_CPUTIME_ID */ 158 159 #ifndef CLOCK_MONOTONIC 160 #define CLOCK_MONOTONIC 4 161 #endif /* CLOCK_MONOTONIC */ 162 163 #ifndef CLOCK_MONOTONIC_RAW 164 #define CLOCK_MONOTONIC_RAW 5 165 #endif /* CLOCK_MONOTONIC_RAW */ 166 167 #ifndef CLOCK_MONOTONIC_COARSE 168 #define CLOCK_MONOTONIC_COARSE 6 169 #endif /* CLOCK_MONOTONIC_COARSE */ 170 171 #ifndef CLOCK_BOOTTIME 172 #define CLOCK_BOOTTIME 7 173 #endif /* CLOCK_BOOTTIME */ 174 175 #ifndef CLOCK_REALTIME_ALARM 176 #define CLOCK_REALTIME_ALARM 8 177 #endif /* CLOCK_REALTIME_ALARM */ 178 179 #ifndef CLOCK_BOOTTIME_ALARM 180 #define CLOCK_BOOTTIME_ALARM 9 181 #endif /* CLOCK_BOOTTIME_ALARM */ 182 183 #ifndef CLOCK_SGI_CYCLE 184 #define CLOCK_SGI_CYCLE 10 // newlib says they don't have this definition, make the compiler happy 185 #endif /* CLOCK_SGI_CYCLE */ 186 187 #ifndef TIMER_ABSTIME 188 #define TIMER_ABSTIME 4 189 #endif /* TIMER_ABSTIME */ 190 191 #ifdef CLOCK_TAI 192 #define CLOCK_ID_MAX CLOCK_TAI 193 #else 194 #define CLOCK_ID_MAX CLOCK_MONOTONIC 195 #endif 196 197 #ifndef CLOCK_TAI 198 #define CLOCK_TAI 11 // newlib says they don't have this definition, make the compiler happy 199 #endif /* CLOCK_TAI */ 200 201 #endif /* defined(RT_USING_POSIX_CLOCK) || defined (RT_USING_POSIX_TIMER) */ 202 203 #ifdef RT_USING_POSIX_CLOCK 204 int clock_getres (clockid_t clockid, struct timespec *res); 205 int clock_gettime (clockid_t clockid, struct timespec *tp); 206 int clock_settime (clockid_t clockid, const struct timespec *tp); 207 int clock_nanosleep(clockid_t clockid, int flags, const struct timespec *rqtp, struct timespec *rmtp); 208 int rt_timespec_to_tick(const struct timespec *time); 209 #endif /* RT_USING_POSIX_CLOCK */ 210 211 #ifdef RT_USING_POSIX_TIMER 212 #include <sys/signal.h> 213 int timer_create(clockid_t clockid, struct sigevent *evp, timer_t *timerid); 214 int timer_delete(timer_t timerid); 215 int timer_getoverrun(timer_t timerid); 216 int timer_gettime(timer_t timerid, struct itimerspec *its); 217 int timer_settime(timer_t timerid, int flags, const struct itimerspec *value, struct itimerspec *ovalue); 218 #endif /* RT_USING_POSIX_TIMER */ 219 220 #ifdef __cplusplus 221 } 222 #endif /* __cplusplus */ 223 224 #endif /* _SYS_TIME_H_ */ 225