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
37 /* This 'date' command supports only 2 time setting formats,
38 all the GNU strftime stuff (its in libc, lets use it),
39 setting time using UTC and displaying int, as well as
40 an RFC 822 complient date output for shell scripting
43 /* Input parsing code is always bulky - used heavy duty libc stuff as
44 much as possible, missed out a lot of bounds checking */
46 /* Default input handling to save suprising some people */
48 struct tm *date_conv_time(struct tm *tm_time, const char *t_string)
52 nr = sscanf(t_string, "%2d%2d%2d%2d%d",
56 &(tm_time->tm_min), &(tm_time->tm_year));
58 if (nr < 4 || nr > 5) {
59 fatalError(invalid_date, t_string);
62 /* correct for century - minor Y2K problem here? */
63 if (tm_time->tm_year >= 1900)
64 tm_time->tm_year -= 1900;
73 /* The new stuff for LRP */
75 struct tm *date_conv_ftime(struct tm *tm_time, const char *t_string)
77 struct tm itm_time, jtm_time, ktm_time, ltm_time, mtm_time, ntm_time;
86 /* Parse input and assign appropriately to tm_time */
88 if (sscanf(t_string, "%d:%d:%d",
89 &itm_time.tm_hour, &itm_time.tm_min, &itm_time.tm_sec) == 3) {
94 } else if (sscanf(t_string, "%d:%d",
95 &jtm_time.tm_hour, &jtm_time.tm_min) == 2) {
100 } else if (sscanf(t_string, "%d.%d-%d:%d:%d",
104 &ktm_time.tm_min, &ktm_time.tm_sec) == 5) {
106 ktm_time.tm_mon -= 1; /* Adjust dates from 1-12 to 0-11 */
110 } else if (sscanf(t_string, "%d.%d-%d:%d",
113 <m_time.tm_hour, <m_time.tm_min) == 4) {
115 ltm_time.tm_mon -= 1; /* Adjust dates from 1-12 to 0-11 */
119 } else if (sscanf(t_string, "%d.%d.%d-%d:%d:%d",
124 &mtm_time.tm_min, &mtm_time.tm_sec) == 6) {
126 mtm_time.tm_year -= 1900; /* Adjust years */
127 mtm_time.tm_mon -= 1; /* Adjust dates from 1-12 to 0-11 */
131 } else if (sscanf(t_string, "%d.%d.%d-%d:%d",
135 &ntm_time.tm_hour, &ntm_time.tm_min) == 5) {
136 ntm_time.tm_year -= 1900; /* Adjust years */
137 ntm_time.tm_mon -= 1; /* Adjust dates from 1-12 to 0-11 */
143 fatalError(invalid_date, t_string);
147 int date_main(int argc, char **argv)
149 char *date_str = NULL;
150 char *date_fmt = NULL;
160 /* Interpret command line args */
161 while ((c = getopt(argc, argv, "Rs:ud:")) != EOF) {
168 if ((date_str != NULL) || ((date_str = optarg) == NULL)) {
174 if (putenv("TZ=UTC0") != 0)
175 fatalError(memory_exhausted);
179 if ((date_str != NULL) || ((date_str = optarg) == NULL))
187 if ((date_fmt == NULL) && (optind < argc) && (argv[optind][0] == '+'))
188 date_fmt = &argv[optind][1]; /* Skip over the '+' */
189 else if (date_str == NULL) {
191 date_str = argv[optind];
195 errorMsg("date_str='%s' date_fmt='%s'\n", date_str, date_fmt);
200 /* Now we have parsed all the information except the date format
201 which depends on whether the clock is being set or read */
204 memcpy(&tm_time, localtime(&tm), sizeof(tm_time));
205 /* Zero out fields - take her back to midnight! */
206 if (date_str != NULL) {
212 /* Process any date input to UNIX time since 1 Jan 1970 */
213 if (date_str != NULL) {
215 if (strchr(date_str, ':') != NULL) {
216 date_conv_ftime(&tm_time, date_str);
218 date_conv_time(&tm_time, date_str);
221 /* Correct any day of week and day of year etc fields */
222 tm = mktime(&tm_time);
224 fatalError(invalid_date, date_str);
226 if (putenv("TZ=UTC0") != 0)
227 fatalError(memory_exhausted);
230 /* if setting time, set it */
232 if (stime(&tm) < 0) {
233 perrorMsg("cannot set date");
240 /* Deal with format string */
241 if (date_fmt == NULL) {
244 ? "%a, %_d %b %Y %H:%M:%S GMT"
245 : "%a, %_d %b %Y %H:%M:%S %z")
246 : "%a %b %e %H:%M:%S %Z %Y");
248 } else if (*date_fmt == '\0') {
249 /* Imitate what GNU 'date' does with NO format string! */
254 /* Handle special conversions */
256 if (strncmp(date_fmt, "%f", 2) == 0) {
257 date_fmt = "%Y.%m.%d-%H:%M:%S";
260 /* Print OUTPUT (after ALL that!) */
261 t_buff = xmalloc(201);
262 strftime(t_buff, 200, date_fmt, &tm_time);
263 printf("%s\n", t_buff);