date.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * Mini date implementation for busybox
  4. *
  5. * by Matthew Grant <grantma@anathoth.gen.nz>
  6. *
  7. * iso-format handling added by Robert Griebl <griebl@gmx.de>
  8. * bugfixes and cleanup by Bernhard Fischer
  9. *
  10. * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
  11. */
  12. #include "libbb.h"
  13. /* This 'date' command supports only 2 time setting formats,
  14. all the GNU strftime stuff (its in libc, lets use it),
  15. setting time using UTC and displaying it, as well as
  16. an RFC 2822 compliant date output for shell scripting
  17. mail commands */
  18. /* Input parsing code is always bulky - used heavy duty libc stuff as
  19. much as possible, missed out a lot of bounds checking */
  20. /* Default input handling to save surprising some people */
  21. #define DATE_OPT_RFC2822 0x01
  22. #define DATE_OPT_SET 0x02
  23. #define DATE_OPT_UTC 0x04
  24. #define DATE_OPT_DATE 0x08
  25. #define DATE_OPT_REFERENCE 0x10
  26. #define DATE_OPT_TIMESPEC 0x20
  27. #define DATE_OPT_HINT 0x40
  28. static void maybe_set_utc(int opt)
  29. {
  30. if (opt & DATE_OPT_UTC)
  31. putenv((char*)"TZ=UTC0");
  32. }
  33. int date_main(int argc, char **argv);
  34. int date_main(int argc, char **argv)
  35. {
  36. time_t tm;
  37. struct tm tm_time;
  38. unsigned opt;
  39. int ifmt = -1;
  40. char *date_str = NULL;
  41. char *date_fmt = NULL;
  42. char *filename = NULL;
  43. char *isofmt_arg;
  44. char *hintfmt_arg;
  45. opt_complementary = "d--s:s--d"
  46. USE_FEATURE_DATE_ISOFMT(":R--I:I--R");
  47. opt = getopt32(argv, "Rs:ud:r:"
  48. USE_FEATURE_DATE_ISOFMT("I::D:"),
  49. &date_str, &date_str, &filename
  50. USE_FEATURE_DATE_ISOFMT(, &isofmt_arg, &hintfmt_arg));
  51. maybe_set_utc(opt);
  52. if (ENABLE_FEATURE_DATE_ISOFMT && (opt & DATE_OPT_TIMESPEC)) {
  53. if (!isofmt_arg) {
  54. ifmt = 0; /* default is date */
  55. } else {
  56. static const char *const isoformats[] = {
  57. "date", "hours", "minutes", "seconds"
  58. };
  59. for (ifmt = 0; ifmt < 4; ifmt++)
  60. if (!strcmp(isofmt_arg, isoformats[ifmt]))
  61. goto found;
  62. /* parse error */
  63. bb_show_usage();
  64. found: ;
  65. }
  66. }
  67. /* XXX, date_fmt == NULL from this always */
  68. if ((date_fmt == NULL) && (optind < argc) && (argv[optind][0] == '+')) {
  69. date_fmt = &argv[optind][1]; /* Skip over the '+' */
  70. } else if (date_str == NULL) {
  71. opt |= DATE_OPT_SET;
  72. date_str = argv[optind];
  73. }
  74. /* Now we have parsed all the information except the date format
  75. which depends on whether the clock is being set or read */
  76. if (filename) {
  77. struct stat statbuf;
  78. xstat(filename, &statbuf);
  79. tm = statbuf.st_mtime;
  80. } else
  81. time(&tm);
  82. memcpy(&tm_time, localtime(&tm), sizeof(tm_time));
  83. /* Zero out fields - take her back to midnight! */
  84. if (date_str != NULL) {
  85. tm_time.tm_sec = 0;
  86. tm_time.tm_min = 0;
  87. tm_time.tm_hour = 0;
  88. /* Process any date input to UNIX time since 1 Jan 1970 */
  89. if (ENABLE_FEATURE_DATE_ISOFMT && (opt & DATE_OPT_HINT)) {
  90. strptime(date_str, hintfmt_arg, &tm_time);
  91. } else if (strchr(date_str, ':') != NULL) {
  92. /* Parse input and assign appropriately to tm_time */
  93. if (sscanf(date_str, "%d:%d:%d", &tm_time.tm_hour, &tm_time.tm_min,
  94. &tm_time.tm_sec) == 3) {
  95. /* no adjustments needed */
  96. } else if (sscanf(date_str, "%d:%d", &tm_time.tm_hour,
  97. &tm_time.tm_min) == 2) {
  98. /* no adjustments needed */
  99. } else if (sscanf(date_str, "%d.%d-%d:%d:%d", &tm_time.tm_mon,
  100. &tm_time.tm_mday, &tm_time.tm_hour,
  101. &tm_time.tm_min, &tm_time.tm_sec) == 5) {
  102. /* Adjust dates from 1-12 to 0-11 */
  103. tm_time.tm_mon -= 1;
  104. } else if (sscanf(date_str, "%d.%d-%d:%d", &tm_time.tm_mon,
  105. &tm_time.tm_mday,
  106. &tm_time.tm_hour, &tm_time.tm_min) == 4) {
  107. /* Adjust dates from 1-12 to 0-11 */
  108. tm_time.tm_mon -= 1;
  109. } else if (sscanf(date_str, "%d.%d.%d-%d:%d:%d", &tm_time.tm_year,
  110. &tm_time.tm_mon, &tm_time.tm_mday,
  111. &tm_time.tm_hour, &tm_time.tm_min,
  112. &tm_time.tm_sec) == 6) {
  113. tm_time.tm_year -= 1900; /* Adjust years */
  114. tm_time.tm_mon -= 1; /* Adjust dates from 1-12 to 0-11 */
  115. } else if (sscanf(date_str, "%d.%d.%d-%d:%d", &tm_time.tm_year,
  116. &tm_time.tm_mon, &tm_time.tm_mday,
  117. &tm_time.tm_hour, &tm_time.tm_min) == 5) {
  118. tm_time.tm_year -= 1900; /* Adjust years */
  119. tm_time.tm_mon -= 1; /* Adjust dates from 1-12 to 0-11 */
  120. } else {
  121. bb_error_msg_and_die(bb_msg_invalid_date, date_str);
  122. }
  123. } else {
  124. int nr;
  125. char *cp;
  126. nr = sscanf(date_str, "%2d%2d%2d%2d%d", &tm_time.tm_mon,
  127. &tm_time.tm_mday, &tm_time.tm_hour, &tm_time.tm_min,
  128. &tm_time.tm_year);
  129. if (nr < 4 || nr > 5) {
  130. bb_error_msg_and_die(bb_msg_invalid_date, date_str);
  131. }
  132. cp = strchr(date_str, '.');
  133. if (cp) {
  134. nr = sscanf(cp + 1, "%2d", &tm_time.tm_sec);
  135. if (nr != 1) {
  136. bb_error_msg_and_die(bb_msg_invalid_date, date_str);
  137. }
  138. }
  139. /* correct for century - minor Y2K problem here? */
  140. if (tm_time.tm_year >= 1900) {
  141. tm_time.tm_year -= 1900;
  142. }
  143. /* adjust date */
  144. tm_time.tm_mon -= 1;
  145. }
  146. /* Correct any day of week and day of year etc. fields */
  147. tm_time.tm_isdst = -1; /* Be sure to recheck dst. */
  148. tm = mktime(&tm_time);
  149. if (tm < 0) {
  150. bb_error_msg_and_die(bb_msg_invalid_date, date_str);
  151. }
  152. maybe_set_utc(opt);
  153. /* if setting time, set it */
  154. if ((opt & DATE_OPT_SET) && stime(&tm) < 0) {
  155. bb_perror_msg("cannot set date");
  156. }
  157. }
  158. /* Display output */
  159. /* Deal with format string */
  160. if (date_fmt == NULL) {
  161. int i;
  162. date_fmt = xzalloc(32);
  163. if (ENABLE_FEATURE_DATE_ISOFMT && ifmt >= 0) {
  164. strcpy(date_fmt, "%Y-%m-%d");
  165. if (ifmt > 0) {
  166. i = 8;
  167. date_fmt[i++] = 'T';
  168. date_fmt[i++] = '%';
  169. date_fmt[i++] = 'H';
  170. if (ifmt > 1) {
  171. date_fmt[i++] = ':';
  172. date_fmt[i++] = '%';
  173. date_fmt[i++] = 'M';
  174. }
  175. if (ifmt > 2) {
  176. date_fmt[i++] = ':';
  177. date_fmt[i++] = '%';
  178. date_fmt[i++] = 'S';
  179. }
  180. format_utc:
  181. date_fmt[i++] = '%';
  182. date_fmt[i] = (opt & DATE_OPT_UTC) ? 'Z' : 'z';
  183. }
  184. } else if (opt & DATE_OPT_RFC2822) {
  185. /* Undo busybox.c for date -R */
  186. if (ENABLE_LOCALE_SUPPORT)
  187. setlocale(LC_TIME, "C");
  188. strcpy(date_fmt, "%a, %d %b %Y %H:%M:%S ");
  189. i = 22;
  190. goto format_utc;
  191. } else /* default case */
  192. date_fmt = (char*)"%a %b %e %H:%M:%S %Z %Y";
  193. }
  194. #define date_buf bb_common_bufsiz1
  195. if (*date_fmt == '\0') {
  196. /* With no format string, just print a blank line */
  197. date_buf[0] = '\0';
  198. } else {
  199. /* Handle special conversions */
  200. if (strncmp(date_fmt, "%f", 2) == 0) {
  201. date_fmt = (char*)"%Y.%m.%d-%H:%M:%S";
  202. }
  203. /* Generate output string */
  204. strftime(date_buf, sizeof(date_buf), date_fmt, &tm_time);
  205. }
  206. puts(date_buf);
  207. return EXIT_SUCCESS;
  208. }