time.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * Utility routines.
  4. *
  5. * Copyright (C) 2007 Denys Vlasenko
  6. *
  7. * Licensed under GPLv2, see file LICENSE in this source tree.
  8. */
  9. #include "libbb.h"
  10. void FAST_FUNC parse_datestr(const char *date_str, struct tm *ptm)
  11. {
  12. char end = '\0';
  13. const char *last_colon = strrchr(date_str, ':');
  14. if (last_colon != NULL) {
  15. /* Parse input and assign appropriately to ptm */
  16. #if ENABLE_DESKTOP
  17. const char *endp;
  18. #endif
  19. /* HH:MM */
  20. if (sscanf(date_str, "%u:%u%c",
  21. &ptm->tm_hour,
  22. &ptm->tm_min,
  23. &end) >= 2) {
  24. /* no adjustments needed */
  25. } else
  26. /* mm.dd-HH:MM */
  27. if (sscanf(date_str, "%u.%u-%u:%u%c",
  28. &ptm->tm_mon, &ptm->tm_mday,
  29. &ptm->tm_hour, &ptm->tm_min,
  30. &end) >= 4) {
  31. /* Adjust month from 1-12 to 0-11 */
  32. ptm->tm_mon -= 1;
  33. } else
  34. /* yyyy.mm.dd-HH:MM */
  35. if (sscanf(date_str, "%u.%u.%u-%u:%u%c", &ptm->tm_year,
  36. &ptm->tm_mon, &ptm->tm_mday,
  37. &ptm->tm_hour, &ptm->tm_min,
  38. &end) >= 5) {
  39. ptm->tm_year -= 1900; /* Adjust years */
  40. ptm->tm_mon -= 1; /* Adjust month from 1-12 to 0-11 */
  41. } else
  42. /* yyyy-mm-dd HH:MM */
  43. if (sscanf(date_str, "%u-%u-%u %u:%u%c", &ptm->tm_year,
  44. &ptm->tm_mon, &ptm->tm_mday,
  45. &ptm->tm_hour, &ptm->tm_min,
  46. &end) >= 5) {
  47. ptm->tm_year -= 1900; /* Adjust years */
  48. ptm->tm_mon -= 1; /* Adjust month from 1-12 to 0-11 */
  49. } else
  50. #if ENABLE_DESKTOP /* strptime is BIG: ~1k in uclibc, ~10k in glibc */
  51. /* month_name d HH:MM:SS YYYY. Supported by GNU date */
  52. if ((endp = strptime(date_str, "%b %d %T %Y", ptm)) != NULL
  53. && *endp == '\0'
  54. ) {
  55. return; /* don't fall through to end == ":" check */
  56. } else
  57. #endif
  58. //TODO: coreutils 6.9 also accepts "yyyy-mm-dd HH" (no minutes)
  59. {
  60. bb_error_msg_and_die(bb_msg_invalid_date, date_str);
  61. }
  62. if (end == ':') {
  63. /* xxx:SS */
  64. if (sscanf(last_colon + 1, "%u%c", &ptm->tm_sec, &end) == 1)
  65. end = '\0';
  66. /* else end != NUL and we error out */
  67. }
  68. } else if (date_str[0] == '@') {
  69. time_t t = bb_strtol(date_str + 1, NULL, 10);
  70. if (!errno) {
  71. struct tm *lt = localtime(&t);
  72. if (lt) {
  73. *ptm = *lt;
  74. return;
  75. }
  76. }
  77. end = '1';
  78. } else {
  79. /* Googled the following on an old date manpage:
  80. *
  81. * The canonical representation for setting the date/time is:
  82. * cc Century (either 19 or 20)
  83. * yy Year in abbreviated form (e.g. 89, 06)
  84. * mm Numeric month, a number from 1 to 12
  85. * dd Day, a number from 1 to 31
  86. * HH Hour, a number from 0 to 23
  87. * MM Minutes, a number from 0 to 59
  88. * .SS Seconds, a number from 0 to 61 (with leap seconds)
  89. * Everything but the minutes is optional
  90. *
  91. * This coincides with the format of "touch -t TIME"
  92. */
  93. int len = strchrnul(date_str, '.') - date_str;
  94. /* MM[.SS] */
  95. if (len == 2 && sscanf(date_str, "%2u%2u%2u%2u""%2u%c" + 12,
  96. &ptm->tm_min,
  97. &end) >= 1) {
  98. } else
  99. /* HHMM[.SS] */
  100. if (len == 4 && sscanf(date_str, "%2u%2u%2u""%2u%2u%c" + 9,
  101. &ptm->tm_hour,
  102. &ptm->tm_min,
  103. &end) >= 2) {
  104. } else
  105. /* ddHHMM[.SS] */
  106. if (len == 6 && sscanf(date_str, "%2u%2u""%2u%2u%2u%c" + 6,
  107. &ptm->tm_mday,
  108. &ptm->tm_hour,
  109. &ptm->tm_min,
  110. &end) >= 3) {
  111. } else
  112. /* mmddHHMM[.SS] */
  113. if (len == 8 && sscanf(date_str, "%2u""%2u%2u%2u%2u%c" + 3,
  114. &ptm->tm_mon,
  115. &ptm->tm_mday,
  116. &ptm->tm_hour,
  117. &ptm->tm_min,
  118. &end) >= 4) {
  119. /* Adjust month from 1-12 to 0-11 */
  120. ptm->tm_mon -= 1;
  121. } else
  122. /* yymmddHHMM[.SS] */
  123. if (len == 10 && sscanf(date_str, "%2u%2u%2u%2u%2u%c",
  124. &ptm->tm_year,
  125. &ptm->tm_mon,
  126. &ptm->tm_mday,
  127. &ptm->tm_hour,
  128. &ptm->tm_min,
  129. &end) >= 5) {
  130. /* Adjust month from 1-12 to 0-11 */
  131. ptm->tm_mon -= 1;
  132. } else
  133. /* ccyymmddHHMM[.SS] */
  134. if (len == 12 && sscanf(date_str, "%4u%2u%2u%2u%2u%c",
  135. &ptm->tm_year,
  136. &ptm->tm_mon,
  137. &ptm->tm_mday,
  138. &ptm->tm_hour,
  139. &ptm->tm_min,
  140. &end) >= 5) {
  141. ptm->tm_year -= 1900; /* Adjust years */
  142. ptm->tm_mon -= 1; /* Adjust month from 1-12 to 0-11 */
  143. } else {
  144. bb_error_msg_and_die(bb_msg_invalid_date, date_str);
  145. }
  146. if (end == '.') {
  147. /* xxx.SS */
  148. if (sscanf(strchr(date_str, '.') + 1, "%u%c",
  149. &ptm->tm_sec, &end) == 1)
  150. end = '\0';
  151. /* else end != NUL and we error out */
  152. }
  153. }
  154. if (end != '\0') {
  155. bb_error_msg_and_die(bb_msg_invalid_date, date_str);
  156. }
  157. }
  158. time_t FAST_FUNC validate_tm_time(const char *date_str, struct tm *ptm)
  159. {
  160. time_t t = mktime(ptm);
  161. if (t == (time_t) -1L) {
  162. bb_error_msg_and_die(bb_msg_invalid_date, date_str);
  163. }
  164. return t;
  165. }
  166. #if ENABLE_MONOTONIC_SYSCALL
  167. #include <sys/syscall.h>
  168. /* Old glibc (< 2.3.4) does not provide this constant. We use syscall
  169. * directly so this definition is safe. */
  170. #ifndef CLOCK_MONOTONIC
  171. #define CLOCK_MONOTONIC 1
  172. #endif
  173. /* libc has incredibly messy way of doing this,
  174. * typically requiring -lrt. We just skip all this mess */
  175. static void get_mono(struct timespec *ts)
  176. {
  177. if (syscall(__NR_clock_gettime, CLOCK_MONOTONIC, ts))
  178. bb_error_msg_and_die("clock_gettime(MONOTONIC) failed");
  179. }
  180. unsigned long long FAST_FUNC monotonic_ns(void)
  181. {
  182. struct timespec ts;
  183. get_mono(&ts);
  184. return ts.tv_sec * 1000000000ULL + ts.tv_nsec;
  185. }
  186. unsigned long long FAST_FUNC monotonic_us(void)
  187. {
  188. struct timespec ts;
  189. get_mono(&ts);
  190. return ts.tv_sec * 1000000ULL + ts.tv_nsec/1000;
  191. }
  192. unsigned long long FAST_FUNC monotonic_ms(void)
  193. {
  194. struct timespec ts;
  195. get_mono(&ts);
  196. return ts.tv_sec * 1000ULL + ts.tv_nsec/1000000;
  197. }
  198. unsigned FAST_FUNC monotonic_sec(void)
  199. {
  200. struct timespec ts;
  201. get_mono(&ts);
  202. return ts.tv_sec;
  203. }
  204. #else
  205. unsigned long long FAST_FUNC monotonic_ns(void)
  206. {
  207. struct timeval tv;
  208. gettimeofday(&tv, NULL);
  209. return tv.tv_sec * 1000000000ULL + tv.tv_usec * 1000;
  210. }
  211. unsigned long long FAST_FUNC monotonic_us(void)
  212. {
  213. struct timeval tv;
  214. gettimeofday(&tv, NULL);
  215. return tv.tv_sec * 1000000ULL + tv.tv_usec;
  216. }
  217. unsigned long long FAST_FUNC monotonic_ms(void)
  218. {
  219. struct timeval tv;
  220. gettimeofday(&tv, NULL);
  221. return tv.tv_sec * 1000ULL + tv.tv_usec / 1000;
  222. }
  223. unsigned FAST_FUNC monotonic_sec(void)
  224. {
  225. return time(NULL);
  226. }
  227. #endif