date.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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 "busybox.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 xputenv(char *s)
  29. {
  30. if (putenv(s) != 0)
  31. bb_error_msg_and_die(bb_msg_memory_exhausted);
  32. }
  33. static void maybe_set_utc(int opt)
  34. {
  35. if (opt & DATE_OPT_UTC)
  36. xputenv("TZ=UTC0");
  37. }
  38. int date_main(int argc, char **argv)
  39. {
  40. time_t tm;
  41. struct tm tm_time;
  42. unsigned opt;
  43. int ifmt = -1;
  44. char *date_str = NULL;
  45. char *date_fmt = NULL;
  46. char *filename = NULL;
  47. char *isofmt_arg;
  48. char *hintfmt_arg;
  49. opt_complementary = "?:d--s:s--d"
  50. USE_FEATURE_DATE_ISOFMT(":R--I:I--R");
  51. opt = getopt32(argc, argv, "Rs:ud:r:"
  52. USE_FEATURE_DATE_ISOFMT("I::D:"),
  53. &date_str, &date_str, &filename
  54. USE_FEATURE_DATE_ISOFMT(, &isofmt_arg, &hintfmt_arg));
  55. maybe_set_utc(opt);
  56. if (ENABLE_FEATURE_DATE_ISOFMT && (opt & DATE_OPT_TIMESPEC)) {
  57. if (!isofmt_arg) {
  58. ifmt = 0; /* default is date */
  59. } else {
  60. const char * const isoformats[] =
  61. {"date", "hours", "minutes", "seconds"};
  62. for (ifmt = 0; ifmt < 4; ifmt++)
  63. if (!strcmp(isofmt_arg, isoformats[ifmt])) {
  64. break;
  65. }
  66. if (ifmt == 4) /* parse error */
  67. bb_show_usage();
  68. }
  69. }
  70. /* XXX, date_fmt == NULL from this always */
  71. if ((date_fmt == NULL) && (optind < argc) && (argv[optind][0] == '+')) {
  72. date_fmt = &argv[optind][1]; /* Skip over the '+' */
  73. } else if (date_str == NULL) {
  74. opt |= DATE_OPT_SET;
  75. date_str = argv[optind];
  76. }
  77. /* Now we have parsed all the information except the date format
  78. which depends on whether the clock is being set or read */
  79. if (filename) {
  80. struct stat statbuf;
  81. xstat(filename, &statbuf);
  82. tm = statbuf.st_mtime;
  83. } else
  84. time(&tm);
  85. memcpy(&tm_time, localtime(&tm), sizeof(tm_time));
  86. /* Zero out fields - take her back to midnight! */
  87. if (date_str != NULL) {
  88. tm_time.tm_sec = 0;
  89. tm_time.tm_min = 0;
  90. tm_time.tm_hour = 0;
  91. /* Process any date input to UNIX time since 1 Jan 1970 */
  92. if (ENABLE_FEATURE_DATE_ISOFMT && (opt & DATE_OPT_HINT)) {
  93. strptime(date_str, hintfmt_arg, &tm_time);
  94. } else if (strchr(date_str, ':') != NULL) {
  95. /* Parse input and assign appropriately to tm_time */
  96. if (sscanf(date_str, "%d:%d:%d", &tm_time.tm_hour, &tm_time.tm_min,
  97. &tm_time.tm_sec) == 3) {
  98. /* no adjustments needed */
  99. } else if (sscanf(date_str, "%d:%d", &tm_time.tm_hour,
  100. &tm_time.tm_min) == 2) {
  101. /* no adjustments needed */
  102. } else if (sscanf(date_str, "%d.%d-%d:%d:%d", &tm_time.tm_mon,
  103. &tm_time.tm_mday, &tm_time.tm_hour,
  104. &tm_time.tm_min, &tm_time.tm_sec) == 5) {
  105. /* Adjust dates from 1-12 to 0-11 */
  106. tm_time.tm_mon -= 1;
  107. } else if (sscanf(date_str, "%d.%d-%d:%d", &tm_time.tm_mon,
  108. &tm_time.tm_mday,
  109. &tm_time.tm_hour, &tm_time.tm_min) == 4) {
  110. /* Adjust dates from 1-12 to 0-11 */
  111. tm_time.tm_mon -= 1;
  112. } else if (sscanf(date_str, "%d.%d.%d-%d:%d:%d", &tm_time.tm_year,
  113. &tm_time.tm_mon, &tm_time.tm_mday,
  114. &tm_time.tm_hour, &tm_time.tm_min,
  115. &tm_time.tm_sec) == 6) {
  116. tm_time.tm_year -= 1900; /* Adjust years */
  117. tm_time.tm_mon -= 1; /* Adjust dates from 1-12 to 0-11 */
  118. } else if (sscanf(date_str, "%d.%d.%d-%d:%d", &tm_time.tm_year,
  119. &tm_time.tm_mon, &tm_time.tm_mday,
  120. &tm_time.tm_hour, &tm_time.tm_min) == 5) {
  121. tm_time.tm_year -= 1900; /* Adjust years */
  122. tm_time.tm_mon -= 1; /* Adjust dates from 1-12 to 0-11 */
  123. } else {
  124. bb_error_msg_and_die(bb_msg_invalid_date, date_str);
  125. }
  126. } else {
  127. int nr;
  128. char *cp;
  129. nr = sscanf(date_str, "%2d%2d%2d%2d%d", &tm_time.tm_mon,
  130. &tm_time.tm_mday, &tm_time.tm_hour, &tm_time.tm_min,
  131. &tm_time.tm_year);
  132. if (nr < 4 || nr > 5) {
  133. bb_error_msg_and_die(bb_msg_invalid_date, date_str);
  134. }
  135. cp = strchr(date_str, '.');
  136. if (cp) {
  137. nr = sscanf(cp + 1, "%2d", &tm_time.tm_sec);
  138. if (nr != 1) {
  139. bb_error_msg_and_die(bb_msg_invalid_date, date_str);
  140. }
  141. }
  142. /* correct for century - minor Y2K problem here? */
  143. if (tm_time.tm_year >= 1900) {
  144. tm_time.tm_year -= 1900;
  145. }
  146. /* adjust date */
  147. tm_time.tm_mon -= 1;
  148. }
  149. /* Correct any day of week and day of year etc. fields */
  150. tm_time.tm_isdst = -1; /* Be sure to recheck dst. */
  151. tm = mktime(&tm_time);
  152. if (tm < 0) {
  153. bb_error_msg_and_die(bb_msg_invalid_date, date_str);
  154. }
  155. maybe_set_utc(opt);
  156. /* if setting time, set it */
  157. if ((opt & DATE_OPT_SET) && stime(&tm) < 0) {
  158. bb_perror_msg("cannot set date");
  159. }
  160. }
  161. /* Display output */
  162. /* Deal with format string */
  163. if (date_fmt == NULL) {
  164. int i;
  165. date_fmt = xzalloc(32);
  166. if (ENABLE_FEATURE_DATE_ISOFMT && ifmt >= 0) {
  167. strcpy(date_fmt, "%Y-%m-%d");
  168. if (ifmt > 0) {
  169. i = 8;
  170. date_fmt[i++] = 'T';
  171. date_fmt[i++] = '%';
  172. date_fmt[i++] = 'H';
  173. if (ifmt > 1) {
  174. date_fmt[i++] = ':';
  175. date_fmt[i++] = '%';
  176. date_fmt[i++] = 'M';
  177. }
  178. if (ifmt > 2) {
  179. date_fmt[i++] = ':';
  180. date_fmt[i++] = '%';
  181. date_fmt[i++] = 'S';
  182. }
  183. format_utc:
  184. date_fmt[i++] = '%';
  185. date_fmt[i] = (opt & DATE_OPT_UTC) ? 'Z' : 'z';
  186. }
  187. } else if (opt & DATE_OPT_RFC2822) {
  188. /* Undo busybox.c for date -R */
  189. setlocale(LC_TIME, "C");
  190. strcpy(date_fmt, "%a, %d %b %Y %H:%M:%S ");
  191. i = 22;
  192. goto format_utc;
  193. } else /* default case */
  194. date_fmt = "%a %b %e %H:%M:%S %Z %Y";
  195. }
  196. if (*date_fmt == '\0') {
  197. /* With no format string, just print a blank line */
  198. *bb_common_bufsiz1 = 0;
  199. } else {
  200. /* Handle special conversions */
  201. if (strncmp(date_fmt, "%f", 2) == 0) {
  202. date_fmt = "%Y.%m.%d-%H:%M:%S";
  203. }
  204. /* Generate output string */
  205. strftime(bb_common_bufsiz1, 200, date_fmt, &tm_time);
  206. }
  207. puts(bb_common_bufsiz1);
  208. return EXIT_SUCCESS;
  209. }