htime.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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. unsigned long long gethrtime() {
  18. #ifdef OS_WIN
  19. static LONGLONG s_freq = 0;
  20. if (s_freq == 0) {
  21. LARGE_INTEGER freq;
  22. QueryPerformanceFrequency(&freq);
  23. s_freq = freq.QuadPart;
  24. }
  25. if (s_freq != 0) {
  26. LARGE_INTEGER count;
  27. QueryPerformanceCounter(&count);
  28. return (unsigned long long)(count.QuadPart / (double)s_freq * 1000000);
  29. }
  30. return 0;
  31. #elif HAVE_CLOCK_GETTIME
  32. struct timespec ts;
  33. clock_gettime(CLOCK_MONOTONIC, &ts);
  34. return ts.tv_sec*(unsigned long long)1000000 + ts.tv_nsec / 1000;
  35. #else
  36. struct timeval tv;
  37. gettimeofday(&tv, NULL);
  38. return tv.tv_sec*(unsigned long long)1000000 + tv.tv_usec;
  39. #endif
  40. }
  41. datetime_t datetime_now() {
  42. datetime_t dt;
  43. #ifdef OS_WIN
  44. SYSTEMTIME tm;
  45. GetLocalTime(&tm);
  46. dt.year = tm.wYear;
  47. dt.month = tm.wMonth;
  48. dt.day = tm.wDay;
  49. dt.hour = tm.wHour;
  50. dt.min = tm.wMinute;
  51. dt.sec = tm.wSecond;
  52. dt.ms = tm.wMilliseconds;
  53. #else
  54. struct timeval tv;
  55. struct tm* tm = NULL;
  56. gettimeofday(&tv, NULL);
  57. time_t tt = tv.tv_sec;
  58. tm = localtime(&tt);
  59. dt.year = tm->tm_year + 1900;
  60. dt.month = tm->tm_mon + 1;
  61. dt.day = tm->tm_mday;
  62. dt.hour = tm->tm_hour;
  63. dt.min = tm->tm_min;
  64. dt.sec = tm->tm_sec;
  65. dt.ms = tv.tv_usec/1000;
  66. #endif
  67. return dt;
  68. }
  69. time_t datetime_mktime(datetime_t* dt) {
  70. struct tm tm;
  71. tm.tm_yday = dt->year - 1900;
  72. tm.tm_mon = dt->month - 1;
  73. tm.tm_mday = dt->day;
  74. tm.tm_hour = dt->hour;
  75. tm.tm_min = dt->min;
  76. tm.tm_sec = dt->sec;
  77. return mktime(&tm);
  78. }
  79. int days_of_month(int month, int year) {
  80. if (month < 1 || month > 12) {
  81. return 0;
  82. }
  83. int days = s_days[month-1];
  84. return (month == 2 && IS_LEAP_YEAR(year)) ? ++days : days;
  85. }
  86. datetime_t* datetime_past(datetime_t* dt, int days) {
  87. assert(days >= 0);
  88. int sub = days;
  89. while (sub) {
  90. if (dt->day > sub) {
  91. dt->day -= sub;
  92. break;
  93. }
  94. sub -= dt->day;
  95. if (--dt->month == 0) {
  96. dt->month = 12;
  97. --dt->year;
  98. }
  99. dt->day = days_of_month(dt->month, dt->year);
  100. }
  101. return dt;
  102. }
  103. datetime_t* datetime_future(datetime_t* dt, int days) {
  104. assert(days >= 0);
  105. int sub = days;
  106. int mdays;
  107. while (sub) {
  108. mdays = days_of_month(dt->month, dt->year);
  109. if (dt->day + sub <= mdays) {
  110. dt->day += sub;
  111. break;
  112. }
  113. sub -= (mdays - dt->day + 1);
  114. if (++dt->month > 12) {
  115. dt->month = 1;
  116. ++dt->year;
  117. }
  118. dt->day = 1;
  119. }
  120. return dt;
  121. }
  122. time_t calc_next_timeout(int minute, int hour, int day, int week, int month) {
  123. enum {
  124. UNKOWN,
  125. HOURLY,
  126. DAILY,
  127. WEEKLY,
  128. MONTHLY,
  129. YEARLY,
  130. } period_type = UNKOWN;
  131. struct tm tm;
  132. time_t tt;
  133. time(&tt);
  134. tm = *localtime(&tt);
  135. time_t tt_round = 0;
  136. tm.tm_sec = 0;
  137. if (minute >= 0) {
  138. period_type = HOURLY;
  139. tm.tm_min = minute;
  140. }
  141. if (hour >= 0) {
  142. period_type = DAILY;
  143. tm.tm_hour = hour;
  144. }
  145. if (week >= 0) {
  146. period_type = WEEKLY;
  147. }
  148. else if (day > 0) {
  149. period_type = MONTHLY;
  150. tm.tm_mday = day;
  151. if (month > 0) {
  152. period_type = YEARLY;
  153. tm.tm_mon = month - 1;
  154. }
  155. }
  156. if (period_type == UNKOWN) {
  157. return -1;
  158. }
  159. tt_round = mktime(&tm);
  160. if (week >= 0) {
  161. tt_round = tt + (week-tm.tm_wday)*SECONDS_PER_DAY;
  162. }
  163. if (tt_round > tt) {
  164. return tt_round;
  165. }
  166. switch(period_type) {
  167. case HOURLY:
  168. tt_round += SECONDS_PER_HOUR;
  169. return tt_round;
  170. case DAILY:
  171. tt_round += SECONDS_PER_DAY;
  172. return tt_round;
  173. case WEEKLY:
  174. tt_round += SECONDS_PER_WEEK;
  175. return tt_round;
  176. case MONTHLY:
  177. if (++tm.tm_mon == 12) {
  178. tm.tm_mon = 0;
  179. ++tm.tm_year;
  180. }
  181. break;
  182. case YEARLY:
  183. ++tm.tm_year;
  184. break;
  185. default:
  186. return -1;
  187. }
  188. return mktime(&tm);
  189. }
  190. int month_atoi(const char* month) {
  191. for (size_t i = 0; i < 12; ++i) {
  192. if (strnicmp(month, s_months[i], strlen(month)) == 0)
  193. return i+1;
  194. }
  195. return 0;
  196. }
  197. const char* month_itoa(int month) {
  198. assert(month >= 1 && month <= 12);
  199. return s_months[month-1];
  200. }
  201. int weekday_atoi(const char* weekday) {
  202. for (size_t i = 0; i < 7; ++i) {
  203. if (strnicmp(weekday, s_weekdays[i], strlen(weekday)) == 0)
  204. return i;
  205. }
  206. return 0;
  207. }
  208. const char* weekday_itoa(int weekday) {
  209. assert(month >= 0 && month <= 7);
  210. if (weekday == 7) weekday = 0;
  211. return s_weekdays[weekday];
  212. }
  213. datetime_t get_compile_datetime() {
  214. static datetime_t dt;
  215. char month[32];
  216. sscanf(__DATE__, "%s %d %d", month, &dt.day, &dt.year);
  217. sscanf(__TIME__, "%d %d %d", &dt.hour, &dt.min, &dt.sec);
  218. dt.month = month_atoi(month);
  219. return dt;
  220. }