htime.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. #include "htime.h"
  2. static const char* s_weekdays[] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
  3. static const char* s_months[] = {"January", "February", "March", "April", "May", "June",
  4. "July", "August", "September", "October", "November", "December"};
  5. static const uint8_t s_days[] = \
  6. // 1 3 5 7 8 10 12
  7. {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
  8. unsigned int gettick_ms() {
  9. #ifdef OS_WIN
  10. return GetTickCount();
  11. #elif HAVE_CLOCK_GETTIME
  12. struct timespec ts;
  13. clock_gettime(CLOCK_MONOTONIC, &ts);
  14. return ts.tv_sec * 1000 + ts.tv_nsec / 1000000;
  15. #else
  16. struct timeval tv;
  17. gettimeofday(&tv, NULL);
  18. return tv.tv_sec * 1000 + tv.tv_usec / 1000;
  19. #endif
  20. }
  21. unsigned long long gethrtime_us() {
  22. #ifdef OS_WIN
  23. static LONGLONG s_freq = 0;
  24. if (s_freq == 0) {
  25. LARGE_INTEGER freq;
  26. QueryPerformanceFrequency(&freq);
  27. s_freq = freq.QuadPart;
  28. }
  29. if (s_freq != 0) {
  30. LARGE_INTEGER count;
  31. QueryPerformanceCounter(&count);
  32. return (unsigned long long)(count.QuadPart / (double)s_freq * 1000000);
  33. }
  34. return 0;
  35. #elif defined(OS_SOLARIS)
  36. return gethrtime() / 1000;
  37. #elif HAVE_CLOCK_GETTIME
  38. struct timespec ts;
  39. clock_gettime(CLOCK_MONOTONIC, &ts);
  40. return ts.tv_sec*(unsigned long long)1000000 + ts.tv_nsec / 1000;
  41. #else
  42. struct timeval tv;
  43. gettimeofday(&tv, NULL);
  44. return tv.tv_sec*(unsigned long long)1000000 + tv.tv_usec;
  45. #endif
  46. }
  47. datetime_t datetime_now() {
  48. datetime_t dt;
  49. #ifdef OS_WIN
  50. SYSTEMTIME tm;
  51. GetLocalTime(&tm);
  52. dt.year = tm.wYear;
  53. dt.month = tm.wMonth;
  54. dt.day = tm.wDay;
  55. dt.hour = tm.wHour;
  56. dt.min = tm.wMinute;
  57. dt.sec = tm.wSecond;
  58. dt.ms = tm.wMilliseconds;
  59. #else
  60. struct timeval tv;
  61. struct tm* tm = NULL;
  62. gettimeofday(&tv, NULL);
  63. time_t tt = tv.tv_sec;
  64. tm = localtime(&tt);
  65. dt.year = tm->tm_year + 1900;
  66. dt.month = tm->tm_mon + 1;
  67. dt.day = tm->tm_mday;
  68. dt.hour = tm->tm_hour;
  69. dt.min = tm->tm_min;
  70. dt.sec = tm->tm_sec;
  71. dt.ms = tv.tv_usec/1000;
  72. #endif
  73. return dt;
  74. }
  75. time_t datetime_mktime(datetime_t* dt) {
  76. struct tm tm;
  77. time_t ts;
  78. time(&ts);
  79. struct tm* ptm = localtime(&ts);
  80. memcpy(&tm, ptm, sizeof(struct tm));
  81. tm.tm_yday = dt->year - 1900;
  82. tm.tm_mon = dt->month - 1;
  83. tm.tm_mday = dt->day;
  84. tm.tm_hour = dt->hour;
  85. tm.tm_min = dt->min;
  86. tm.tm_sec = dt->sec;
  87. return mktime(&tm);
  88. }
  89. int days_of_month(int month, int year) {
  90. if (month < 1 || month > 12) {
  91. return 0;
  92. }
  93. int days = s_days[month-1];
  94. return (month == 2 && IS_LEAP_YEAR(year)) ? ++days : days;
  95. }
  96. datetime_t* datetime_past(datetime_t* dt, int days) {
  97. assert(days >= 0);
  98. int sub = days;
  99. while (sub) {
  100. if (dt->day > sub) {
  101. dt->day -= sub;
  102. break;
  103. }
  104. sub -= dt->day;
  105. if (--dt->month == 0) {
  106. dt->month = 12;
  107. --dt->year;
  108. }
  109. dt->day = days_of_month(dt->month, dt->year);
  110. }
  111. return dt;
  112. }
  113. datetime_t* datetime_future(datetime_t* dt, int days) {
  114. assert(days >= 0);
  115. int sub = days;
  116. int mdays;
  117. while (sub) {
  118. mdays = days_of_month(dt->month, dt->year);
  119. if (dt->day + sub <= mdays) {
  120. dt->day += sub;
  121. break;
  122. }
  123. sub -= (mdays - dt->day + 1);
  124. if (++dt->month > 12) {
  125. dt->month = 1;
  126. ++dt->year;
  127. }
  128. dt->day = 1;
  129. }
  130. return dt;
  131. }
  132. char* duration_fmt(int sec, char* buf) {
  133. int h, m, s;
  134. m = sec / 60;
  135. s = sec % 60;
  136. h = m / 60;
  137. m = m % 60;
  138. sprintf(buf, TIME_FMT, h, m, s);
  139. return buf;
  140. }
  141. char* datetime_fmt(datetime_t* dt, char* buf) {
  142. sprintf(buf, DATETIME_FMT,
  143. dt->year, dt->month, dt->day,
  144. dt->hour, dt->min, dt->sec);
  145. return buf;
  146. }
  147. char* gmtime_fmt(time_t time, char* buf) {
  148. struct tm* tm = gmtime(&time);
  149. //strftime(buf, GMTIME_FMT_BUFLEN, "%a, %d %b %Y %H:%M:%S GMT", tm);
  150. sprintf(buf, GMTIME_FMT,
  151. s_weekdays[tm->tm_wday],
  152. tm->tm_mday, s_months[tm->tm_mon], tm->tm_year + 1900,
  153. tm->tm_hour, tm->tm_min, tm->tm_sec);
  154. return buf;
  155. }
  156. int month_atoi(const char* month) {
  157. for (size_t i = 0; i < 12; ++i) {
  158. if (strnicmp(month, s_months[i], strlen(month)) == 0)
  159. return i+1;
  160. }
  161. return 0;
  162. }
  163. const char* month_itoa(int month) {
  164. assert(month >= 1 && month <= 12);
  165. return s_months[month-1];
  166. }
  167. int weekday_atoi(const char* weekday) {
  168. for (size_t i = 0; i < 7; ++i) {
  169. if (strnicmp(weekday, s_weekdays[i], strlen(weekday)) == 0)
  170. return i;
  171. }
  172. return 0;
  173. }
  174. const char* weekday_itoa(int weekday) {
  175. assert(weekday >= 0 && weekday <= 7);
  176. if (weekday == 7) weekday = 0;
  177. return s_weekdays[weekday];
  178. }
  179. datetime_t hv_compile_datetime() {
  180. datetime_t dt;
  181. char month[32];
  182. sscanf(__DATE__, "%s %d %d", month, &dt.day, &dt.year);
  183. sscanf(__TIME__, "%d:%d:%d", &dt.hour, &dt.min, &dt.sec);
  184. dt.month = month_atoi(month);
  185. return dt;
  186. }
  187. time_t cron_next_timeout(int minute, int hour, int day, int week, int month) {
  188. enum {
  189. UNKOWN,
  190. HOURLY,
  191. DAILY,
  192. WEEKLY,
  193. MONTHLY,
  194. YEARLY,
  195. } period_type = UNKOWN;
  196. struct tm tm;
  197. time_t tt;
  198. time(&tt);
  199. tm = *localtime(&tt);
  200. time_t tt_round = 0;
  201. tm.tm_sec = 0;
  202. if (minute >= 0) {
  203. period_type = HOURLY;
  204. tm.tm_min = minute;
  205. }
  206. if (hour >= 0) {
  207. period_type = DAILY;
  208. tm.tm_hour = hour;
  209. }
  210. if (week >= 0) {
  211. period_type = WEEKLY;
  212. }
  213. else if (day > 0) {
  214. period_type = MONTHLY;
  215. tm.tm_mday = day;
  216. if (month > 0) {
  217. period_type = YEARLY;
  218. tm.tm_mon = month - 1;
  219. }
  220. }
  221. if (period_type == UNKOWN) {
  222. return -1;
  223. }
  224. tt_round = mktime(&tm);
  225. if (week >= 0) {
  226. tt_round = tt + (week-tm.tm_wday)*SECONDS_PER_DAY;
  227. }
  228. if (tt_round > tt) {
  229. return tt_round;
  230. }
  231. switch(period_type) {
  232. case HOURLY:
  233. tt_round += SECONDS_PER_HOUR;
  234. return tt_round;
  235. case DAILY:
  236. tt_round += SECONDS_PER_DAY;
  237. return tt_round;
  238. case WEEKLY:
  239. tt_round += SECONDS_PER_WEEK;
  240. return tt_round;
  241. case MONTHLY:
  242. if (++tm.tm_mon == 12) {
  243. tm.tm_mon = 0;
  244. ++tm.tm_year;
  245. }
  246. break;
  247. case YEARLY:
  248. ++tm.tm_year;
  249. break;
  250. default:
  251. return -1;
  252. }
  253. return mktime(&tm);
  254. }