1
0

htime.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  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* duration_fmt(int sec, char* buf) {
  102. int h, m, s;
  103. m = sec / 60;
  104. s = sec % 60;
  105. h = m / 60;
  106. m = m % 60;
  107. sprintf(buf, TIME_FMT, h, m, s);
  108. return buf;
  109. }
  110. char* datetime_fmt(datetime_t* dt, char* buf) {
  111. sprintf(buf, DATETIME_FMT,
  112. dt->year, dt->month, dt->day,
  113. dt->hour, dt->min, dt->sec, dt->ms);
  114. return buf;
  115. }
  116. char* gmtime_fmt(time_t time, char* buf) {
  117. struct tm* tm = gmtime(&time);
  118. //strftime(buf, GMTIME_FMT_BUFLEN, "%a, %d %b %Y %H:%M:%S GMT", tm);
  119. sprintf(buf, GMTIME_FMT,
  120. s_weekdays[tm->tm_wday],
  121. tm->tm_mday, s_months[tm->tm_mon], tm->tm_year + 1900,
  122. tm->tm_hour, tm->tm_min, tm->tm_sec);
  123. return buf;
  124. }
  125. int days_of_month(int month, int year) {
  126. if (month < 1 || month > 12) {
  127. return 0;
  128. }
  129. int days = s_days[month-1];
  130. return (month == 2 && IS_LEAP_YEAR(year)) ? ++days : days;
  131. }
  132. datetime_t* datetime_past(datetime_t* dt, int days) {
  133. assert(days >= 0);
  134. int sub = days;
  135. while (sub) {
  136. if (dt->day > sub) {
  137. dt->day -= sub;
  138. break;
  139. }
  140. sub -= dt->day;
  141. if (--dt->month == 0) {
  142. dt->month = 12;
  143. --dt->year;
  144. }
  145. dt->day = days_of_month(dt->month, dt->year);
  146. }
  147. return dt;
  148. }
  149. datetime_t* datetime_future(datetime_t* dt, int days) {
  150. assert(days >= 0);
  151. int sub = days;
  152. int mdays;
  153. while (sub) {
  154. mdays = days_of_month(dt->month, dt->year);
  155. if (dt->day + sub <= mdays) {
  156. dt->day += sub;
  157. break;
  158. }
  159. sub -= (mdays - dt->day + 1);
  160. if (++dt->month > 12) {
  161. dt->month = 1;
  162. ++dt->year;
  163. }
  164. dt->day = 1;
  165. }
  166. return dt;
  167. }
  168. time_t calc_next_timeout(int minute, int hour, int day, int week, int month) {
  169. enum {
  170. UNKOWN,
  171. HOURLY,
  172. DAILY,
  173. WEEKLY,
  174. MONTHLY,
  175. YEARLY,
  176. } period_type = UNKOWN;
  177. struct tm tm;
  178. time_t tt;
  179. time(&tt);
  180. tm = *localtime(&tt);
  181. time_t tt_round = 0;
  182. tm.tm_sec = 0;
  183. if (minute >= 0) {
  184. period_type = HOURLY;
  185. tm.tm_min = minute;
  186. }
  187. if (hour >= 0) {
  188. period_type = DAILY;
  189. tm.tm_hour = hour;
  190. }
  191. if (week >= 0) {
  192. period_type = WEEKLY;
  193. }
  194. else if (day > 0) {
  195. period_type = MONTHLY;
  196. tm.tm_mday = day;
  197. if (month > 0) {
  198. period_type = YEARLY;
  199. tm.tm_mon = month - 1;
  200. }
  201. }
  202. if (period_type == UNKOWN) {
  203. return -1;
  204. }
  205. tt_round = mktime(&tm);
  206. if (week >= 0) {
  207. tt_round = tt + (week-tm.tm_wday)*SECONDS_PER_DAY;
  208. }
  209. if (tt_round > tt) {
  210. return tt_round;
  211. }
  212. switch(period_type) {
  213. case HOURLY:
  214. tt_round += SECONDS_PER_HOUR;
  215. return tt_round;
  216. case DAILY:
  217. tt_round += SECONDS_PER_DAY;
  218. return tt_round;
  219. case WEEKLY:
  220. tt_round += SECONDS_PER_WEEK;
  221. return tt_round;
  222. case MONTHLY:
  223. if (++tm.tm_mon == 12) {
  224. tm.tm_mon = 0;
  225. ++tm.tm_year;
  226. }
  227. break;
  228. case YEARLY:
  229. ++tm.tm_year;
  230. break;
  231. default:
  232. return -1;
  233. }
  234. return mktime(&tm);
  235. }
  236. int month_atoi(const char* month) {
  237. for (size_t i = 0; i < 12; ++i) {
  238. if (strnicmp(month, s_months[i], strlen(month)) == 0)
  239. return i+1;
  240. }
  241. return 0;
  242. }
  243. const char* month_itoa(int month) {
  244. assert(month >= 1 && month <= 12);
  245. return s_months[month-1];
  246. }
  247. int weekday_atoi(const char* weekday) {
  248. for (size_t i = 0; i < 7; ++i) {
  249. if (strnicmp(weekday, s_weekdays[i], strlen(weekday)) == 0)
  250. return i;
  251. }
  252. return 0;
  253. }
  254. const char* weekday_itoa(int weekday) {
  255. assert(weekday >= 0 && weekday <= 7);
  256. if (weekday == 7) weekday = 0;
  257. return s_weekdays[weekday];
  258. }
  259. datetime_t hv_compile_datetime() {
  260. static datetime_t dt;
  261. char month[32];
  262. sscanf(__DATE__, "%s %d %d", month, &dt.day, &dt.year);
  263. sscanf(__TIME__, "%d %d %d", &dt.hour, &dt.min, &dt.sec);
  264. dt.month = month_atoi(month);
  265. return dt;
  266. }