time.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  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. ) {
  25. /* no adjustments needed */
  26. } else
  27. /* mm.dd-HH:MM */
  28. if (sscanf(date_str, "%u.%u-%u:%u%c",
  29. &ptm->tm_mon, &ptm->tm_mday,
  30. &ptm->tm_hour, &ptm->tm_min,
  31. &end) >= 4
  32. ) {
  33. /* Adjust month from 1-12 to 0-11 */
  34. ptm->tm_mon -= 1;
  35. } else
  36. /* yyyy.mm.dd-HH:MM */
  37. if (sscanf(date_str, "%u.%u.%u-%u:%u%c", &ptm->tm_year,
  38. &ptm->tm_mon, &ptm->tm_mday,
  39. &ptm->tm_hour, &ptm->tm_min,
  40. &end) >= 5
  41. /* yyyy-mm-dd HH:MM */
  42. || sscanf(date_str, "%u-%u-%u %u:%u%c", &ptm->tm_year,
  43. &ptm->tm_mon, &ptm->tm_mday,
  44. &ptm->tm_hour, &ptm->tm_min,
  45. &end) >= 5
  46. ) {
  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. {
  59. bb_error_msg_and_die(bb_msg_invalid_date, date_str);
  60. }
  61. if (end == ':') {
  62. /* xxx:SS */
  63. if (sscanf(last_colon + 1, "%u%c", &ptm->tm_sec, &end) == 1)
  64. end = '\0';
  65. /* else end != NUL and we error out */
  66. }
  67. } else
  68. if (strchr(date_str, '-')
  69. /* Why strchr('-') check?
  70. * sscanf below will trash ptm->tm_year, this breaks
  71. * if parse_str is "10101010" (iow, "MMddhhmm" form)
  72. * because we destroy year. Do these sscanf
  73. * only if we saw a dash in parse_str.
  74. */
  75. /* yyyy-mm-dd HH */
  76. && (sscanf(date_str, "%u-%u-%u %u%c", &ptm->tm_year,
  77. &ptm->tm_mon, &ptm->tm_mday,
  78. &ptm->tm_hour,
  79. &end) >= 4
  80. /* yyyy-mm-dd */
  81. || sscanf(date_str, "%u-%u-%u%c", &ptm->tm_year,
  82. &ptm->tm_mon, &ptm->tm_mday,
  83. &end) >= 3
  84. )
  85. ) {
  86. ptm->tm_year -= 1900; /* Adjust years */
  87. ptm->tm_mon -= 1; /* Adjust month from 1-12 to 0-11 */
  88. } else
  89. if (date_str[0] == '@') {
  90. time_t t = bb_strtol(date_str + 1, NULL, 10);
  91. if (!errno) {
  92. struct tm *lt = localtime(&t);
  93. if (lt) {
  94. *ptm = *lt;
  95. return;
  96. }
  97. }
  98. end = '1';
  99. } else {
  100. /* Googled the following on an old date manpage:
  101. *
  102. * The canonical representation for setting the date/time is:
  103. * cc Century (either 19 or 20)
  104. * yy Year in abbreviated form (e.g. 89, 06)
  105. * mm Numeric month, a number from 1 to 12
  106. * dd Day, a number from 1 to 31
  107. * HH Hour, a number from 0 to 23
  108. * MM Minutes, a number from 0 to 59
  109. * .SS Seconds, a number from 0 to 61 (with leap seconds)
  110. * Everything but the minutes is optional
  111. *
  112. * "touch -t DATETIME" format: [[[[[YY]YY]MM]DD]hh]mm[.ss]
  113. * Some, but not all, Unix "date DATETIME" commands
  114. * move [[YY]YY] past minutes mm field (!).
  115. * Coreutils date does it, and SUS mandates it.
  116. * (date -s DATETIME does not support this format. lovely!)
  117. * In bbox, this format is special-cased in date applet
  118. * (IOW: this function assumes "touch -t" format).
  119. */
  120. unsigned cur_year = ptm->tm_year;
  121. int len = strchrnul(date_str, '.') - date_str;
  122. /* MM[.SS] */
  123. if (len == 2 && sscanf(date_str, "%2u%2u%2u%2u""%2u%c" + 12,
  124. &ptm->tm_min,
  125. &end) >= 1) {
  126. } else
  127. /* HHMM[.SS] */
  128. if (len == 4 && sscanf(date_str, "%2u%2u%2u""%2u%2u%c" + 9,
  129. &ptm->tm_hour,
  130. &ptm->tm_min,
  131. &end) >= 2) {
  132. } else
  133. /* ddHHMM[.SS] */
  134. if (len == 6 && sscanf(date_str, "%2u%2u""%2u%2u%2u%c" + 6,
  135. &ptm->tm_mday,
  136. &ptm->tm_hour,
  137. &ptm->tm_min,
  138. &end) >= 3) {
  139. } else
  140. /* mmddHHMM[.SS] */
  141. if (len == 8 && sscanf(date_str, "%2u""%2u%2u%2u%2u%c" + 3,
  142. &ptm->tm_mon,
  143. &ptm->tm_mday,
  144. &ptm->tm_hour,
  145. &ptm->tm_min,
  146. &end) >= 4) {
  147. /* Adjust month from 1-12 to 0-11 */
  148. ptm->tm_mon -= 1;
  149. } else
  150. /* yymmddHHMM[.SS] */
  151. if (len == 10 && sscanf(date_str, "%2u%2u%2u%2u%2u%c",
  152. &ptm->tm_year,
  153. &ptm->tm_mon,
  154. &ptm->tm_mday,
  155. &ptm->tm_hour,
  156. &ptm->tm_min,
  157. &end) >= 5) {
  158. /* Adjust month from 1-12 to 0-11 */
  159. ptm->tm_mon -= 1;
  160. if ((int)cur_year >= 50) { /* >= 1950 */
  161. /* Adjust year: */
  162. /* 1. Put it in the current century */
  163. ptm->tm_year += (cur_year / 100) * 100;
  164. /* 2. If too far in the past, +100 years */
  165. if (ptm->tm_year < cur_year - 50)
  166. ptm->tm_year += 100;
  167. /* 3. If too far in the future, -100 years */
  168. if (ptm->tm_year > cur_year + 50)
  169. ptm->tm_year -= 100;
  170. }
  171. } else
  172. /* ccyymmddHHMM[.SS] */
  173. if (len == 12 && sscanf(date_str, "%4u%2u%2u%2u%2u%c",
  174. &ptm->tm_year,
  175. &ptm->tm_mon,
  176. &ptm->tm_mday,
  177. &ptm->tm_hour,
  178. &ptm->tm_min,
  179. &end) >= 5) {
  180. ptm->tm_year -= 1900; /* Adjust years */
  181. ptm->tm_mon -= 1; /* Adjust month from 1-12 to 0-11 */
  182. } else {
  183. err:
  184. bb_error_msg_and_die(bb_msg_invalid_date, date_str);
  185. }
  186. ptm->tm_sec = 0; /* assume zero if [.SS] is not given */
  187. if (end == '.') {
  188. /* xxx.SS */
  189. if (sscanf(strchr(date_str, '.') + 1, "%u%c",
  190. &ptm->tm_sec, &end) == 1)
  191. end = '\0';
  192. /* else end != NUL and we error out */
  193. }
  194. /* Users were confused by "date -s 20180923"
  195. * working (not in the way they were expecting).
  196. * It was interpreted as MMDDhhmm, and not bothered by
  197. * "month #20" in the least. Prevent such cases:
  198. */
  199. if (ptm->tm_sec > 60 /* allow "23:60" leap second */
  200. || ptm->tm_min > 59
  201. || ptm->tm_hour > 23
  202. || ptm->tm_mday > 31
  203. || ptm->tm_mon > 11 /* month# is 0..11, not 1..12 */
  204. ) {
  205. goto err;
  206. }
  207. }
  208. if (end != '\0') {
  209. bb_error_msg_and_die(bb_msg_invalid_date, date_str);
  210. }
  211. }
  212. time_t FAST_FUNC validate_tm_time(const char *date_str, struct tm *ptm)
  213. {
  214. time_t t = mktime(ptm);
  215. if (t == (time_t) -1L) {
  216. bb_error_msg_and_die(bb_msg_invalid_date, date_str);
  217. }
  218. return t;
  219. }
  220. static char* strftime_fmt(char *buf, unsigned len, time_t *tp, const char *fmt)
  221. {
  222. time_t t;
  223. if (!tp) {
  224. tp = &t;
  225. time(tp);
  226. }
  227. /* Returns pointer to NUL */
  228. return buf + strftime(buf, len, fmt, localtime(tp));
  229. }
  230. char* FAST_FUNC strftime_HHMMSS(char *buf, unsigned len, time_t *tp)
  231. {
  232. return strftime_fmt(buf, len, tp, "%H:%M:%S");
  233. }
  234. char* FAST_FUNC strftime_YYYYMMDDHHMMSS(char *buf, unsigned len, time_t *tp)
  235. {
  236. return strftime_fmt(buf, len, tp, "%Y-%m-%d %H:%M:%S");
  237. }
  238. #if ENABLE_MONOTONIC_SYSCALL
  239. #include <sys/syscall.h>
  240. /* Old glibc (< 2.3.4) does not provide this constant. We use syscall
  241. * directly so this definition is safe. */
  242. #ifndef CLOCK_MONOTONIC
  243. #define CLOCK_MONOTONIC 1
  244. #endif
  245. /* libc has incredibly messy way of doing this,
  246. * typically requiring -lrt. We just skip all this mess */
  247. static void get_mono(struct timespec *ts)
  248. {
  249. if (syscall(__NR_clock_gettime, CLOCK_MONOTONIC, ts))
  250. bb_error_msg_and_die("clock_gettime(MONOTONIC) failed");
  251. }
  252. unsigned long long FAST_FUNC monotonic_ns(void)
  253. {
  254. struct timespec ts;
  255. get_mono(&ts);
  256. return ts.tv_sec * 1000000000ULL + ts.tv_nsec;
  257. }
  258. unsigned long long FAST_FUNC monotonic_us(void)
  259. {
  260. struct timespec ts;
  261. get_mono(&ts);
  262. return ts.tv_sec * 1000000ULL + ts.tv_nsec/1000;
  263. }
  264. unsigned long long FAST_FUNC monotonic_ms(void)
  265. {
  266. struct timespec ts;
  267. get_mono(&ts);
  268. return ts.tv_sec * 1000ULL + ts.tv_nsec/1000000;
  269. }
  270. unsigned FAST_FUNC monotonic_sec(void)
  271. {
  272. struct timespec ts;
  273. get_mono(&ts);
  274. return ts.tv_sec;
  275. }
  276. #else
  277. unsigned long long FAST_FUNC monotonic_ns(void)
  278. {
  279. struct timeval tv;
  280. gettimeofday(&tv, NULL);
  281. return tv.tv_sec * 1000000000ULL + tv.tv_usec * 1000;
  282. }
  283. unsigned long long FAST_FUNC monotonic_us(void)
  284. {
  285. struct timeval tv;
  286. gettimeofday(&tv, NULL);
  287. return tv.tv_sec * 1000000ULL + tv.tv_usec;
  288. }
  289. unsigned long long FAST_FUNC monotonic_ms(void)
  290. {
  291. struct timeval tv;
  292. gettimeofday(&tv, NULL);
  293. return tv.tv_sec * 1000ULL + tv.tv_usec / 1000;
  294. }
  295. unsigned FAST_FUNC monotonic_sec(void)
  296. {
  297. return time(NULL);
  298. }
  299. #endif