time.h 6.6 KB

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