htime.h 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. #ifdef _MSC_VER
  24. /* @see winsock2.h
  25. // Structure used in select() call, taken from the BSD file sys/time.h
  26. struct timeval {
  27. long tv_sec;
  28. long tv_usec;
  29. };
  30. */
  31. struct timezone {
  32. int tz_minuteswest; /* of Greenwich */
  33. int tz_dsttime; /* type of dst correction to apply */
  34. };
  35. #include <sys/timeb.h>
  36. static inline int gettimeofday(struct timeval *tv, struct timezone *tz) {
  37. struct _timeb tb;
  38. _ftime(&tb);
  39. if (tv) {
  40. tv->tv_sec = (long)tb.time;
  41. tv->tv_usec = tb.millitm * 1000;
  42. }
  43. if (tz) {
  44. tz->tz_minuteswest = tb.timezone;
  45. tz->tz_dsttime = tb.dstflag;
  46. }
  47. return 0;
  48. }
  49. #endif
  50. #endif
  51. static inline unsigned long long timestamp_ms() {
  52. struct timeval tv;
  53. gettimeofday(&tv, NULL);
  54. return tv.tv_sec * (unsigned long long)1000 + tv.tv_usec/1000;
  55. }
  56. // us
  57. unsigned long long gethrtime();
  58. datetime_t datetime_now();
  59. time_t datetime_mktime(datetime_t* dt);
  60. #define DATETIME_FMT "%04d-%02d-%02d %02d:%02d:%02d.%03d"
  61. #define DATETIME_FMT_BUFLEN 24
  62. char* datetime_fmt(datetime_t* dt, char* buf);
  63. #define GMTIME_FMT "%.3s, %02d %.3s %04d %02d:%02d:%02d GMT"
  64. #define GMTIME_FMT_BUFLEN 30
  65. char* gmtime_fmt(time_t time, char* buf);
  66. datetime_t* datetime_past(datetime_t* dt, int days DEFAULT(1));
  67. datetime_t* datetime_future(datetime_t* dt, int days DEFAULT(1));
  68. /*
  69. * minute hour day week month action
  70. * 0~59 0~23 1~31 0~6 1~12
  71. * 30 -1 -1 -1 -1 cron.hourly
  72. * 30 1 -1 -1 -1 cron.daily
  73. * 30 1 15 -1 -1 cron.monthly
  74. * 30 1 -1 7 -1 cron.weekly
  75. * 30 1 1 -1 10 cron.yearly
  76. */
  77. time_t calc_next_timeout(int minute, int hour, int day, int week, int month);
  78. int days_of_month(int month, int year);
  79. int month_atoi(const char* month);
  80. const char* month_itoa(int month);
  81. int weekday_atoi(const char* weekday);
  82. const char* weekday_itoa(int weekday);
  83. datetime_t hv_compile_datetime();
  84. #ifdef __cplusplus
  85. } // extern "C"
  86. #endif
  87. #endif // HV_TIME_H_