time.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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. #include <winsock.h> /* for struct timeval */
  19. #include <corecrt.h> /* for __time64_t */
  20. typedef __time64_t time_t;
  21. #endif /* _WIN32 */
  22. #ifdef __cplusplus
  23. extern "C" {
  24. #endif
  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 timezone
  38. {
  39. int tz_minuteswest; /* minutes west of Greenwich */
  40. int tz_dsttime; /* type of dst correction */
  41. };
  42. #if !defined(_TIMEVAL_DEFINED) && !defined(_WIN32)
  43. #define _TIMEVAL_DEFINED
  44. struct timeval
  45. {
  46. time_t tv_sec; /* seconds */
  47. suseconds_t tv_usec; /* and microseconds */
  48. };
  49. #endif /* !defined(_TIMEVAL_DEFINED) && !defined(_WIN32) */
  50. #if defined(__ARMCC_VERSION) || defined(_WIN32) || (defined(__ICCARM__) && (__VER__ >= 8010001))
  51. struct timespec
  52. {
  53. time_t tv_sec; /* seconds */
  54. long tv_nsec; /* and nanoseconds */
  55. };
  56. #endif /* defined(__ARMCC_VERSION) || defined(_WIN32) || (defined(__ICCARM__) && (__VER__ >= 8010001)) */
  57. #if !(defined(__GNUC__) && !defined(__ARMCC_VERSION)/*GCC*/)
  58. /*
  59. * Structure defined by POSIX.1b to be like a itimerval, but with
  60. * timespecs. Used in the timer_*() system calls.
  61. */
  62. struct itimerspec
  63. {
  64. struct timespec it_interval;
  65. struct timespec it_value;
  66. };
  67. #endif /* !(defined(__GNUC__) && !defined(__ARMCC_VERSION)) */
  68. int stime(const time_t *t);
  69. time_t timegm(struct tm * const t);
  70. int gettimeofday(struct timeval *tv, struct timezone *tz);
  71. int settimeofday(const struct timeval *tv, const struct timezone *tz);
  72. #if defined(__ARMCC_VERSION) || defined (__ICCARM__)
  73. struct tm *gmtime_r(const time_t *timep, struct tm *r);
  74. struct tm* localtime_r(const time_t* t, struct tm* r);
  75. char* asctime_r(const struct tm *t, char *buf);
  76. char *ctime_r(const time_t * tim_p, char * result);
  77. #elif defined(_WIN32)
  78. struct tm* gmtime_r(const time_t* timep, struct tm* r);
  79. struct tm* gmtime(const time_t* t);
  80. struct tm* localtime_r(const time_t* t, struct tm* r);
  81. struct tm* localtime(const time_t* t);
  82. time_t mktime(struct tm* const t);
  83. char* asctime_r(const struct tm* t, char* buf);
  84. char* ctime_r(const time_t* tim_p, char* result);
  85. char* ctime(const time_t* tim_p);
  86. time_t time(time_t* t);
  87. #endif
  88. #ifdef RT_USING_POSIX_DELAY
  89. int nanosleep(const struct timespec *rqtp, struct timespec *rmtp);
  90. #endif /* RT_USING_POSIX_DELAY */
  91. #if defined(RT_USING_POSIX_CLOCK) || defined (RT_USING_POSIX_TIMER)
  92. /* POSIX clock and timer */
  93. #define MILLISECOND_PER_SECOND 1000UL
  94. #define MICROSECOND_PER_SECOND 1000000UL
  95. #define NANOSECOND_PER_SECOND 1000000000UL
  96. #define MILLISECOND_PER_TICK (MILLISECOND_PER_SECOND / RT_TICK_PER_SECOND)
  97. #define MICROSECOND_PER_TICK (MICROSECOND_PER_SECOND / RT_TICK_PER_SECOND)
  98. #define NANOSECOND_PER_TICK (NANOSECOND_PER_SECOND / RT_TICK_PER_SECOND)
  99. #ifndef CLOCK_REALTIME
  100. #define CLOCK_REALTIME 1
  101. #endif
  102. #define CLOCK_CPUTIME_ID 2
  103. #ifndef CLOCK_PROCESS_CPUTIME_ID
  104. #define CLOCK_PROCESS_CPUTIME_ID CLOCK_CPUTIME_ID
  105. #endif
  106. #ifndef CLOCK_THREAD_CPUTIME_ID
  107. #define CLOCK_THREAD_CPUTIME_ID CLOCK_CPUTIME_ID
  108. #endif
  109. #ifndef CLOCK_MONOTONIC
  110. #define CLOCK_MONOTONIC 4
  111. #endif
  112. #endif /* defined(RT_USING_POSIX_CLOCK) || defined (RT_USING_POSIX_TIMER) */
  113. #ifdef RT_USING_POSIX_CLOCK
  114. int clock_getres (clockid_t clockid, struct timespec *res);
  115. int clock_gettime (clockid_t clockid, struct timespec *tp);
  116. int clock_settime (clockid_t clockid, const struct timespec *tp);
  117. int clock_nanosleep(clockid_t clockid, int flags, const struct timespec *rqtp, struct timespec *rmtp);
  118. int rt_timespec_to_tick(const struct timespec *time);
  119. #endif /* RT_USING_POSIX_CLOCK */
  120. #ifdef RT_USING_POSIX_TIMER
  121. #include "signal.h"
  122. int timer_create(clockid_t clockid, struct sigevent *evp, timer_t *timerid);
  123. int timer_delete(timer_t timerid);
  124. int timer_getoverrun(timer_t timerid);
  125. int timer_gettime(timer_t timerid, struct itimerspec *its);
  126. int timer_settime(timer_t timerid, int flags, const struct itimerspec *value,
  127. struct itimerspec *ovalue);
  128. #endif /* RT_USING_POSIX_TIMER */
  129. /* timezone */
  130. void tz_set(int8_t tz);
  131. int8_t tz_get(void);
  132. int8_t tz_is_dst(void);
  133. #ifdef __cplusplus
  134. }
  135. #endif
  136. #endif /* _SYS_TIME_H_ */