date.c 6.3 KB

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