time.h 6.4 KB

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