3
0

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