time.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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. #define DST_NONE 0 /* not on dst */
  27. #define DST_USA 1 /* USA style dst */
  28. #define DST_AUST 2 /* Australian style dst */
  29. #define DST_WET 3 /* Western European dst */
  30. #define DST_MET 4 /* Middle European dst */
  31. #define DST_EET 5 /* Eastern European dst */
  32. #define DST_CAN 6 /* Canada */
  33. #define DST_GB 7 /* Great Britain and Eire */
  34. #define DST_RUM 8 /* Rumania */
  35. #define DST_TUR 9 /* Turkey */
  36. #define DST_AUSTALT 10 /* Australian style with shift in 1986 */
  37. struct itimerspec;
  38. struct timezone
  39. {
  40. int tz_minuteswest; /* minutes west of Greenwich */
  41. int tz_dsttime; /* type of dst correction */
  42. };
  43. #if defined(_GNU_SOURCE) && (defined(__x86_64__) || defined(__i386__))
  44. /* linux x86 platform gcc use! */
  45. #define _TIMEVAL_DEFINED
  46. /* Values for the first argument to `getitimer' and `setitimer'. */
  47. enum __itimer_which
  48. {
  49. /* Timers run in real time. */
  50. ITIMER_REAL = 0,
  51. #define ITIMER_REAL ITIMER_REAL
  52. /* Timers run only when the process is executing. */
  53. ITIMER_VIRTUAL = 1,
  54. #define ITIMER_VIRTUAL ITIMER_VIRTUAL
  55. /* Timers run when the process is executing and when
  56. the system is executing on behalf of the process. */
  57. ITIMER_PROF = 2
  58. #define ITIMER_PROF ITIMER_PROF
  59. };
  60. struct itimerval
  61. {
  62. /* Value to put into `it_value' when the timer expires. */
  63. struct timeval it_interval;
  64. /* Time to the next timer expiration. */
  65. struct timeval it_value;
  66. };
  67. #endif /* defined(_GNU_SOURCE) && (defined(__x86_64__) || defined(__i386__)) */
  68. #ifndef _TIMEVAL_DEFINED
  69. #define _TIMEVAL_DEFINED
  70. struct timeval
  71. {
  72. time_t tv_sec; /* seconds */
  73. suseconds_t tv_usec; /* and microseconds */
  74. };
  75. #endif /* _TIMEVAL_DEFINED */
  76. #if defined(__ARMCC_VERSION) || defined(_WIN32) || (defined(__ICCARM__) && (__VER__ < 8010001))
  77. struct timespec
  78. {
  79. time_t tv_sec; /* seconds */
  80. long tv_nsec; /* and nanoseconds */
  81. };
  82. #endif /* defined(__ARMCC_VERSION) || defined(_WIN32) || (defined(__ICCARM__) && (__VER__ < 8010001)) */
  83. #if !(defined(__GNUC__) && !defined(__ARMCC_VERSION)/*GCC*/)
  84. /*
  85. * Structure defined by POSIX.1b to be like a itimerval, but with
  86. * timespecs. Used in the timer_*() system calls.
  87. */
  88. struct itimerspec
  89. {
  90. struct timespec it_interval;
  91. struct timespec it_value;
  92. };
  93. #endif /* !(defined(__GNUC__) && !defined(__ARMCC_VERSION)) */
  94. int stime(const time_t *t);
  95. time_t timegm(struct tm * const t);
  96. int gettimeofday(struct timeval *tv, struct timezone *tz);
  97. int settimeofday(const struct timeval *tv, const struct timezone *tz);
  98. #if defined(__ARMCC_VERSION) || defined (__ICCARM__) || defined(_WIN32)
  99. struct tm *gmtime_r(const time_t *timep, struct tm *r);
  100. char* asctime_r(const struct tm *t, char *buf);
  101. char *ctime_r(const time_t * tim_p, char * result);
  102. struct tm* localtime_r(const time_t* t, struct tm* r);
  103. #endif /* defined(__ARMCC_VERSION) || defined (__ICCARM__) || defined(_WIN32) */
  104. #ifdef _WIN32
  105. struct tm* gmtime(const time_t* t);
  106. struct tm* localtime(const time_t* t);
  107. time_t mktime(struct tm* const t);
  108. char* ctime(const time_t* tim_p);
  109. time_t time(time_t* t);
  110. #endif /* _WIN32 */
  111. #ifdef RT_USING_POSIX_DELAY
  112. int nanosleep(const struct timespec *rqtp, struct timespec *rmtp);
  113. #endif /* RT_USING_POSIX_DELAY */
  114. #define MILLISECOND_PER_SECOND 1000UL
  115. #define MICROSECOND_PER_SECOND 1000000UL
  116. #define NANOSECOND_PER_SECOND 1000000000UL
  117. #define MILLISECOND_PER_TICK (MILLISECOND_PER_SECOND / RT_TICK_PER_SECOND)
  118. #define MICROSECOND_PER_TICK (MICROSECOND_PER_SECOND / RT_TICK_PER_SECOND)
  119. #define NANOSECOND_PER_TICK (NANOSECOND_PER_SECOND / RT_TICK_PER_SECOND)
  120. #if defined(RT_USING_POSIX_CLOCK) || defined (RT_USING_POSIX_TIMER)
  121. /* POSIX clock and timer */
  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 CLOCK_CPUTIME_ID
  131. #endif /* CLOCK_THREAD_CPUTIME_ID */
  132. #ifndef CLOCK_MONOTONIC
  133. #define CLOCK_MONOTONIC 4
  134. #endif /* CLOCK_MONOTONIC */
  135. #ifndef TIMER_ABSTIME
  136. #define TIMER_ABSTIME 4
  137. #endif /* TIMER_ABSTIME */
  138. #ifdef CLOCK_TAI
  139. #define CLOCK_ID_MAX CLOCK_TAI
  140. #else
  141. #define CLOCK_ID_MAX CLOCK_MONOTONIC
  142. #endif
  143. #endif /* defined(RT_USING_POSIX_CLOCK) || defined (RT_USING_POSIX_TIMER) */
  144. #ifdef RT_USING_POSIX_CLOCK
  145. int clock_getres (clockid_t clockid, struct timespec *res);
  146. int clock_gettime (clockid_t clockid, struct timespec *tp);
  147. int clock_settime (clockid_t clockid, const struct timespec *tp);
  148. int clock_nanosleep(clockid_t clockid, int flags, const struct timespec *rqtp, struct timespec *rmtp);
  149. int rt_timespec_to_tick(const struct timespec *time);
  150. #endif /* RT_USING_POSIX_CLOCK */
  151. #ifdef RT_USING_POSIX_TIMER
  152. #include <sys/signal.h>
  153. int timer_create(clockid_t clockid, struct sigevent *evp, timer_t *timerid);
  154. int timer_delete(timer_t timerid);
  155. int timer_getoverrun(timer_t timerid);
  156. int timer_gettime(timer_t timerid, struct itimerspec *its);
  157. int timer_settime(timer_t timerid, int flags, const struct itimerspec *value, struct itimerspec *ovalue);
  158. #endif /* RT_USING_POSIX_TIMER */
  159. /* timezone */
  160. void tz_set(int8_t tz);
  161. int8_t tz_get(void);
  162. int8_t tz_is_dst(void);
  163. #ifdef __cplusplus
  164. }
  165. #endif /* __cplusplus */
  166. #endif /* _SYS_TIME_H_ */