htime.h 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. #ifndef HW_TIME_H_
  2. #define HW_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. #endif
  27. static inline void msleep(unsigned int ms) {
  28. #ifdef OS_WIN
  29. Sleep(ms);
  30. #else
  31. usleep(ms*1000);
  32. #endif
  33. }
  34. // ms
  35. static inline unsigned int gettick() {
  36. #ifdef OS_WIN
  37. return GetTickCount();
  38. #elif defined(OS_LINUX)
  39. struct timespec ts;
  40. clock_gettime(CLOCK_MONOTONIC, &ts);
  41. return ts.tv_sec * 1000 + ts.tv_nsec / 1000000;
  42. #else
  43. struct timeval tv;
  44. gettimeofday(&tv, NULL);
  45. return tv.tv_sec * 1000 + tv.tv_usec / 1000;
  46. #endif
  47. }
  48. // us
  49. unsigned long long gethrtime();
  50. datetime_t datetime_now();
  51. time_t datetime_mktime(datetime_t* dt);
  52. datetime_t* datetime_past(datetime_t* dt, int days DEFAULT(1));
  53. datetime_t* datetime_future(datetime_t* dt, int days DEFAULT(1));
  54. /*
  55. * minute hour day week month action
  56. * 0~59 0~23 1~31 0~6 1~12
  57. * 30 -1 -1 -1 -1 cron.hourly
  58. * 30 1 -1 -1 -1 cron.daily
  59. * 30 1 15 -1 -1 cron.monthly
  60. * 30 1 -1 7 -1 cron.weekly
  61. * 30 1 1 -1 10 cron.yearly
  62. */
  63. time_t calc_next_timeout(int minute, int hour, int day, int week, int month);
  64. int days_of_month(int month, int year);
  65. int month_atoi(const char* month);
  66. const char* month_itoa(int month);
  67. datetime_t get_compile_datetime();
  68. #ifdef __cplusplus
  69. } // extern "C"
  70. #endif
  71. #endif // HW_TIME_H_