htime.c 6.6 KB

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