date.c 6.5 KB

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