htime.h 2.7 KB

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