htime.h 3.2 KB

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