htime.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. #ifndef HV_TIME_H_
  2. #define HV_TIME_H_
  3. #ifdef __cplusplus
  4. extern "C" {
  5. #endif
  6. #include "hplatform.h"
  7. #include "hdef.h"
  8. #define SECONDS_PER_HOUR 3600
  9. #define SECONDS_PER_DAY 86400 // 24*3600
  10. #define SECONDS_PER_WEEK 604800 // 7*24*3600
  11. #define IS_LEAP_YEAR(year) (((year)%4 == 0 && (year)%100 != 0) ||\
  12. (year)%100 == 0)
  13. typedef struct datetime_s {
  14. int year;
  15. int month;
  16. int day;
  17. int hour;
  18. int min;
  19. int sec;
  20. int ms;
  21. } datetime_t;
  22. #ifdef OS_WIN
  23. static inline void sleep(unsigned int s) {
  24. Sleep(s*1000);
  25. }
  26. static inline void usleep(unsigned int us) {
  27. Sleep(us/1000);
  28. }
  29. #include <sys/timeb.h>
  30. #ifdef _MSC_VER
  31. /* @see winsock2.h
  32. // Structure used in select() call, taken from the BSD file sys/time.h
  33. struct timeval {
  34. long tv_sec;
  35. long tv_usec;
  36. };
  37. */
  38. struct timezone {
  39. int tz_minuteswest; /* of Greenwich */
  40. int tz_dsttime; /* type of dst correction to apply */
  41. };
  42. #endif
  43. static inline int gettimeofday(struct timeval *tv, struct timezone *tz) {
  44. struct _timeb tb;
  45. _ftime(&tb);
  46. if (tv) {
  47. tv->tv_sec = (long)tb.time;
  48. tv->tv_usec = tb.millitm * 1000;
  49. }
  50. if (tz) {
  51. tz->tz_minuteswest = tb.timezone;
  52. tz->tz_dsttime = tb.dstflag;
  53. }
  54. return 0;
  55. }
  56. #endif
  57. static inline void msleep(unsigned int ms) {
  58. #ifdef OS_WIN
  59. Sleep(ms);
  60. #else
  61. usleep(ms*1000);
  62. #endif
  63. }
  64. // ms
  65. static inline unsigned int gettick() {
  66. #ifdef OS_WIN
  67. return GetTickCount();
  68. #elif HAVE_CLOCK_GETTIME
  69. struct timespec ts;
  70. clock_gettime(CLOCK_MONOTONIC, &ts);
  71. return ts.tv_sec * 1000 + ts.tv_nsec / 1000000;
  72. #else
  73. struct timeval tv;
  74. gettimeofday(&tv, NULL);
  75. return tv.tv_sec * 1000 + tv.tv_usec / 1000;
  76. #endif
  77. }
  78. // us
  79. unsigned long long gethrtime();
  80. static inline unsigned long long timestamp_ms() {
  81. struct timeval tv;
  82. gettimeofday(&tv, NULL);
  83. return tv.tv_sec * (unsigned long long)1000 + tv.tv_usec/1000;
  84. }
  85. datetime_t datetime_now();
  86. time_t datetime_mktime(datetime_t* dt);
  87. datetime_t* datetime_past(datetime_t* dt, int days DEFAULT(1));
  88. datetime_t* datetime_future(datetime_t* dt, int days DEFAULT(1));
  89. /*
  90. * minute hour day week month action
  91. * 0~59 0~23 1~31 0~6 1~12
  92. * 30 -1 -1 -1 -1 cron.hourly
  93. * 30 1 -1 -1 -1 cron.daily
  94. * 30 1 15 -1 -1 cron.monthly
  95. * 30 1 -1 7 -1 cron.weekly
  96. * 30 1 1 -1 10 cron.yearly
  97. */
  98. time_t calc_next_timeout(int minute, int hour, int day, int week, int month);
  99. int days_of_month(int month, int year);
  100. int month_atoi(const char* month);
  101. const char* month_itoa(int month);
  102. datetime_t get_compile_datetime();
  103. #ifdef __cplusplus
  104. } // extern "C"
  105. #endif
  106. #endif // HV_TIME_H_