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
24 #define BB_DECLARE_EXTERN
25 #define bb_need_invalid_date
26 #define bb_need_memory_exhausted
36 /* This 'date' command supports only 2 time setting formats,
37 all the GNU strftime stuff (its in libc, lets use it),
38 setting time using UTC and displaying int, as well as
39 an RFC 822 complient date output for shell scripting
42 static const char date_usage[] = "date [OPTION]... [+FORMAT]\n"
43 " or: date [OPTION] [MMDDhhmm[[CC]YY][.ss]]\n\n"
44 "Display the current time in the given FORMAT, or set the system date.\n"
45 "\nOptions:\n\t-R\t\toutput RFC-822 compliant date string\n"
46 "\t-s\t\tset time described by STRING\n"
48 "\t-u\t\tprint or set Coordinated Universal Time\n";
51 /* Input parsing code is always bulky - used heavy duty libc stuff as
52 much as possible, missed out a lot of bounds checking */
54 /* Default input handling to save suprising some people */
56 struct tm *date_conv_time(struct tm *tm_time, const char *t_string)
60 nr = sscanf(t_string, "%2d%2d%2d%2d%d",
64 &(tm_time->tm_min), &(tm_time->tm_year));
66 if (nr < 4 || nr > 5) {
67 fprintf(stderr, invalid_date, "date", t_string);
71 /* correct for century - minor Y2K problem here? */
72 if (tm_time->tm_year >= 1900)
73 tm_time->tm_year -= 1900;
82 /* The new stuff for LRP */
84 struct tm *date_conv_ftime(struct tm *tm_time, const char *t_string)
86 struct tm itm_time, jtm_time, ktm_time, ltm_time, mtm_time, ntm_time;
95 /* Parse input and assign appropriately to tm_time */
97 if (sscanf(t_string, "%d:%d:%d",
98 &itm_time.tm_hour, &itm_time.tm_min, &itm_time.tm_sec) == 3) {
103 } else if (sscanf(t_string, "%d:%d",
104 &jtm_time.tm_hour, &jtm_time.tm_min) == 2) {
109 } else if (sscanf(t_string, "%d.%d-%d:%d:%d",
113 &ktm_time.tm_min, &ktm_time.tm_sec) == 5) {
115 ktm_time.tm_mon -= 1; /* Adjust dates from 1-12 to 0-11 */
119 } else if (sscanf(t_string, "%d.%d-%d:%d",
122 <m_time.tm_hour, <m_time.tm_min) == 4) {
124 ltm_time.tm_mon -= 1; /* Adjust dates from 1-12 to 0-11 */
128 } else if (sscanf(t_string, "%d.%d.%d-%d:%d:%d",
133 &mtm_time.tm_min, &mtm_time.tm_sec) == 6) {
135 mtm_time.tm_year -= 1900; /* Adjust years */
136 mtm_time.tm_mon -= 1; /* Adjust dates from 1-12 to 0-11 */
140 } else if (sscanf(t_string, "%d.%d.%d-%d:%d",
144 &ntm_time.tm_hour, &ntm_time.tm_min) == 5) {
145 ntm_time.tm_year -= 1900; /* Adjust years */
146 ntm_time.tm_mon -= 1; /* Adjust dates from 1-12 to 0-11 */
152 fprintf(stderr, invalid_date, "date", t_string);
159 int date_main(int argc, char **argv)
161 char *date_str = NULL;
162 char *date_fmt = NULL;
172 /* Interpret command line args */
175 while (i > 0 && **argv) {
177 while (i > 0 && *++(*argv))
184 if (date_str != NULL)
190 if (putenv("TZ=UTC0") != 0) {
191 fprintf(stderr, memory_exhausted, "date");
194 /* Look ma, no break. Don't fix it either. */
197 if (date_str != NULL)
205 if ((date_fmt == NULL) && (**argv == '+'))
206 date_fmt = *argv + 1; /* Skip over the '+' */
207 else if (date_str == NULL) {
219 /* Now we have parsed all the information except the date format
220 which depends on whether the clock is being set or read */
223 memcpy(&tm_time, localtime(&tm), sizeof(tm_time));
224 /* Zero out fields - take her back to midnight! */
225 if (date_str != NULL) {
231 /* Process any date input to UNIX time since 1 Jan 1970 */
232 if (date_str != NULL) {
234 if (strchr(date_str, ':') != NULL) {
235 date_conv_ftime(&tm_time, date_str);
237 date_conv_time(&tm_time, date_str);
240 /* Correct any day of week and day of year etc fields */
241 tm = mktime(&tm_time);
243 fprintf(stderr, invalid_date, "date", date_str);
247 /* if setting time, set it */
249 if (stime(&tm) < 0) {
250 fprintf(stderr, "date: can't set date.\n");
258 /* Deal with format string */
259 if (date_fmt == NULL) {
262 ? "%a, %_d %b %Y %H:%M:%S GMT"
263 : "%a, %_d %b %Y %H:%M:%S %z")
264 : "%a %b %e %H:%M:%S %Z %Y");
266 } else if (*date_fmt == '\0') {
267 /* Imitate what GNU 'date' does with NO format string! */
272 /* Handle special conversions */
274 if (strncmp(date_fmt, "%f", 2) == 0) {
275 date_fmt = "%Y.%m.%d-%H:%M:%S";
278 /* Print OUTPUT (after ALL that!) */
279 t_buff = xmalloc(201);
280 strftime(t_buff, 200, date_fmt, &tm_time);
281 printf("%s\n", t_buff);