htime.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  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 (unsigned int)ts.tv_sec * 1000 + (unsigned int)ts.tv_nsec / 1000000;
  15. #else
  16. struct timeval tv;
  17. gettimeofday(&tv, NULL);
  18. return (unsigned int)tv.tv_sec * 1000 + (unsigned int)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. #ifdef OS_WIN
  49. SYSTEMTIME tm;
  50. GetLocalTime(&tm);
  51. datetime_t dt;
  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. return dt;
  60. #else
  61. struct timeval tv;
  62. gettimeofday(&tv, NULL);
  63. datetime_t dt = datetime_localtime(tv.tv_sec);
  64. dt.ms = tv.tv_usec / 1000;
  65. return dt;
  66. #endif
  67. }
  68. datetime_t datetime_localtime(time_t seconds) {
  69. struct tm* tm = localtime(&seconds);
  70. datetime_t dt;
  71. dt.year = tm->tm_year + 1900;
  72. dt.month = tm->tm_mon + 1;
  73. dt.day = tm->tm_mday;
  74. dt.hour = tm->tm_hour;
  75. dt.min = tm->tm_min;
  76. dt.sec = tm->tm_sec;
  77. return dt;
  78. }
  79. time_t datetime_mktime(datetime_t* dt) {
  80. struct tm tm;
  81. time_t ts;
  82. time(&ts);
  83. struct tm* ptm = localtime(&ts);
  84. memcpy(&tm, ptm, sizeof(struct tm));
  85. tm.tm_year = dt->year - 1900;
  86. tm.tm_mon = dt->month - 1;
  87. tm.tm_mday = dt->day;
  88. tm.tm_hour = dt->hour;
  89. tm.tm_min = dt->min;
  90. tm.tm_sec = dt->sec;
  91. return mktime(&tm);
  92. }
  93. int days_of_month(int month, int year) {
  94. if (month < 1 || month > 12) {
  95. return 0;
  96. }
  97. int days = s_days[month-1];
  98. return (month == 2 && IS_LEAP_YEAR(year)) ? ++days : days;
  99. }
  100. datetime_t* datetime_past(datetime_t* dt, int days) {
  101. assert(days >= 0);
  102. int sub = days;
  103. while (sub) {
  104. if (dt->day > sub) {
  105. dt->day -= sub;
  106. break;
  107. }
  108. sub -= dt->day;
  109. if (--dt->month == 0) {
  110. dt->month = 12;
  111. --dt->year;
  112. }
  113. dt->day = days_of_month(dt->month, dt->year);
  114. }
  115. return dt;
  116. }
  117. datetime_t* datetime_future(datetime_t* dt, int days) {
  118. assert(days >= 0);
  119. int sub = days;
  120. int mdays;
  121. while (sub) {
  122. mdays = days_of_month(dt->month, dt->year);
  123. if (dt->day + sub <= mdays) {
  124. dt->day += sub;
  125. break;
  126. }
  127. sub -= (mdays - dt->day + 1);
  128. if (++dt->month > 12) {
  129. dt->month = 1;
  130. ++dt->year;
  131. }
  132. dt->day = 1;
  133. }
  134. return dt;
  135. }
  136. char* duration_fmt(int sec, char* buf) {
  137. int h, m, s;
  138. m = sec / 60;
  139. s = sec % 60;
  140. h = m / 60;
  141. m = m % 60;
  142. sprintf(buf, TIME_FMT, h, m, s);
  143. return buf;
  144. }
  145. char* datetime_fmt(datetime_t* dt, char* buf) {
  146. sprintf(buf, DATETIME_FMT,
  147. dt->year, dt->month, dt->day,
  148. dt->hour, dt->min, dt->sec);
  149. return buf;
  150. }
  151. char* datetime_fmt_iso(datetime_t* dt, char* buf) {
  152. sprintf(buf, DATETIME_FMT_ISO,
  153. dt->year, dt->month, dt->day,
  154. dt->hour, dt->min, dt->sec,
  155. dt->ms);
  156. return buf;
  157. }
  158. char* gmtime_fmt(time_t time, char* buf) {
  159. struct tm* tm = gmtime(&time);
  160. //strftime(buf, GMTIME_FMT_BUFLEN, "%a, %d %b %Y %H:%M:%S GMT", tm);
  161. sprintf(buf, GMTIME_FMT,
  162. s_weekdays[tm->tm_wday],
  163. tm->tm_mday, s_months[tm->tm_mon], tm->tm_year + 1900,
  164. tm->tm_hour, tm->tm_min, tm->tm_sec);
  165. return buf;
  166. }
  167. int month_atoi(const char* month) {
  168. for (size_t i = 0; i < 12; ++i) {
  169. if (strnicmp(month, s_months[i], strlen(month)) == 0)
  170. return i+1;
  171. }
  172. return 0;
  173. }
  174. const char* month_itoa(int month) {
  175. assert(month >= 1 && month <= 12);
  176. return s_months[month-1];
  177. }
  178. int weekday_atoi(const char* weekday) {
  179. for (size_t i = 0; i < 7; ++i) {
  180. if (strnicmp(weekday, s_weekdays[i], strlen(weekday)) == 0)
  181. return i;
  182. }
  183. return 0;
  184. }
  185. const char* weekday_itoa(int weekday) {
  186. assert(weekday >= 0 && weekday <= 7);
  187. if (weekday == 7) weekday = 0;
  188. return s_weekdays[weekday];
  189. }
  190. datetime_t hv_compile_datetime() {
  191. datetime_t dt;
  192. char month[32];
  193. sscanf(__DATE__, "%s %d %d", month, &dt.day, &dt.year);
  194. sscanf(__TIME__, "%d:%d:%d", &dt.hour, &dt.min, &dt.sec);
  195. dt.month = month_atoi(month);
  196. return dt;
  197. }
  198. time_t cron_next_timeout(int minute, int hour, int day, int week, int month) {
  199. enum {
  200. MINUTELY,
  201. HOURLY,
  202. DAILY,
  203. WEEKLY,
  204. MONTHLY,
  205. YEARLY,
  206. } period_type = MINUTELY;
  207. struct tm tm;
  208. time_t tt;
  209. time(&tt);
  210. tm = *localtime(&tt);
  211. time_t tt_round = 0;
  212. tm.tm_sec = 0;
  213. if (minute >= 0) {
  214. period_type = HOURLY;
  215. tm.tm_min = minute;
  216. }
  217. if (hour >= 0) {
  218. period_type = DAILY;
  219. tm.tm_hour = hour;
  220. }
  221. if (week >= 0) {
  222. period_type = WEEKLY;
  223. }
  224. else if (day > 0) {
  225. period_type = MONTHLY;
  226. tm.tm_mday = day;
  227. if (month > 0) {
  228. period_type = YEARLY;
  229. tm.tm_mon = month - 1;
  230. }
  231. }
  232. tt_round = mktime(&tm);
  233. if (week >= 0) {
  234. tt_round += (week-tm.tm_wday)*SECONDS_PER_DAY;
  235. }
  236. if (tt_round > tt) {
  237. return tt_round;
  238. }
  239. switch(period_type) {
  240. case MINUTELY:
  241. tt_round += SECONDS_PER_MINUTE;
  242. return tt_round;
  243. case HOURLY:
  244. tt_round += SECONDS_PER_HOUR;
  245. return tt_round;
  246. case DAILY:
  247. tt_round += SECONDS_PER_DAY;
  248. return tt_round;
  249. case WEEKLY:
  250. tt_round += SECONDS_PER_WEEK;
  251. return tt_round;
  252. case MONTHLY:
  253. if (++tm.tm_mon == 12) {
  254. tm.tm_mon = 0;
  255. ++tm.tm_year;
  256. }
  257. break;
  258. case YEARLY:
  259. ++tm.tm_year;
  260. break;
  261. default:
  262. return -1;
  263. }
  264. return mktime(&tm);
  265. }