1 /* vi: set sw=4 ts=4: */
3 * Mini date implementation for busybox
5 * by Matthew Grant <grantma@anathoth.gen.nz>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
32 #define BB_DECLARE_EXTERN
33 #define bb_need_invalid_date
34 #define bb_need_memory_exhausted
38 /* This 'date' command supports only 2 time setting formats,
39 all the GNU strftime stuff (its in libc, lets use it),
40 setting time using UTC and displaying int, as well as
41 an RFC 822 complient date output for shell scripting
44 /* Input parsing code is always bulky - used heavy duty libc stuff as
45 much as possible, missed out a lot of bounds checking */
47 /* Default input handling to save suprising some people */
49 struct tm *date_conv_time(struct tm *tm_time, const char *t_string)
53 nr = sscanf(t_string, "%2d%2d%2d%2d%d",
57 &(tm_time->tm_min), &(tm_time->tm_year));
59 if (nr < 4 || nr > 5) {
60 error_msg_and_die(invalid_date, t_string);
63 /* correct for century - minor Y2K problem here? */
64 if (tm_time->tm_year >= 1900)
65 tm_time->tm_year -= 1900;
74 /* The new stuff for LRP */
76 struct tm *date_conv_ftime(struct tm *tm_time, const char *t_string)
80 /* Parse input and assign appropriately to tm_time */
82 if (t=*tm_time,sscanf(t_string, "%d:%d:%d",
83 &t.tm_hour, &t.tm_min, &t.tm_sec) == 3) {
84 /* no adjustments needed */
86 } else if (t=*tm_time,sscanf(t_string, "%d:%d",
87 &t.tm_hour, &t.tm_min) == 2) {
88 /* no adjustments needed */
91 } else if (t=*tm_time,sscanf(t_string, "%d.%d-%d:%d:%d",
95 &t.tm_min, &t.tm_sec) == 5) {
97 t.tm_mon -= 1; /* Adjust dates from 1-12 to 0-11 */
99 } else if (t=*tm_time,sscanf(t_string, "%d.%d-%d:%d",
102 &t.tm_hour, &t.tm_min) == 4) {
104 t.tm_mon -= 1; /* Adjust dates from 1-12 to 0-11 */
106 } else if (t=*tm_time,sscanf(t_string, "%d.%d.%d-%d:%d:%d",
111 &t.tm_min, &t.tm_sec) == 6) {
113 t.tm_year -= 1900; /* Adjust years */
114 t.tm_mon -= 1; /* Adjust dates from 1-12 to 0-11 */
116 } else if (t=*tm_time,sscanf(t_string, "%d.%d.%d-%d:%d",
120 &t.tm_hour, &t.tm_min) == 5) {
121 t.tm_year -= 1900; /* Adjust years */
122 t.tm_mon -= 1; /* Adjust dates from 1-12 to 0-11 */
125 error_msg_and_die(invalid_date, t_string);
132 int date_main(int argc, char **argv)
134 char *date_str = NULL;
135 char *date_fmt = NULL;
145 /* Interpret command line args */
146 while ((c = getopt(argc, argv, "Rs:ud:")) != EOF) {
153 if ((date_str != NULL) || ((date_str = optarg) == NULL)) {
159 if (putenv("TZ=UTC0") != 0)
160 error_msg_and_die(memory_exhausted);
164 if ((date_str != NULL) || ((date_str = optarg) == NULL))
172 if ((date_fmt == NULL) && (optind < argc) && (argv[optind][0] == '+'))
173 date_fmt = &argv[optind][1]; /* Skip over the '+' */
174 else if (date_str == NULL) {
176 date_str = argv[optind];
180 error_msg("date_str='%s' date_fmt='%s'\n", date_str, date_fmt);
185 /* Now we have parsed all the information except the date format
186 which depends on whether the clock is being set or read */
189 memcpy(&tm_time, localtime(&tm), sizeof(tm_time));
190 /* Zero out fields - take her back to midnight! */
191 if (date_str != NULL) {
197 /* Process any date input to UNIX time since 1 Jan 1970 */
198 if (date_str != NULL) {
200 if (strchr(date_str, ':') != NULL) {
201 date_conv_ftime(&tm_time, date_str);
203 date_conv_time(&tm_time, date_str);
206 /* Correct any day of week and day of year etc fields */
207 tm = mktime(&tm_time);
209 error_msg_and_die(invalid_date, date_str);
211 if (putenv("TZ=UTC0") != 0)
212 error_msg_and_die(memory_exhausted);
215 /* if setting time, set it */
217 if (stime(&tm) < 0) {
218 perror_msg("cannot set date");
225 /* Deal with format string */
226 if (date_fmt == NULL) {
229 ? "%a, %_d %b %Y %H:%M:%S GMT"
230 : "%a, %_d %b %Y %H:%M:%S %z")
231 : "%a %b %e %H:%M:%S %Z %Y");
233 } else if (*date_fmt == '\0') {
234 /* Imitate what GNU 'date' does with NO format string! */
239 /* Handle special conversions */
241 if (strncmp(date_fmt, "%f", 2) == 0) {
242 date_fmt = "%Y.%m.%d-%H:%M:%S";
245 /* Print OUTPUT (after ALL that!) */
246 t_buff = xmalloc(201);
247 strftime(t_buff, 200, date_fmt, &tm_time);
248 printf("%s\n", t_buff);