xref: /freebsd/bin/date/date.c (revision 06c3fb2749bda94cb5201f81ffdb8fa6c3161b2e)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 1985, 1987, 1988, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. Neither the name of the University nor the names of its contributors
16  *    may be used to endorse or promote products derived from this software
17  *    without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  */
31 
32 #include <sys/param.h>
33 #include <sys/time.h>
34 #include <sys/stat.h>
35 
36 #include <ctype.h>
37 #include <err.h>
38 #include <locale.h>
39 #include <stdbool.h>
40 #include <stdio.h>
41 #include <stdlib.h>
42 #include <string.h>
43 #include <syslog.h>
44 #include <unistd.h>
45 #include <utmpx.h>
46 
47 #include "vary.h"
48 
49 #ifndef	TM_YEAR_BASE
50 #define	TM_YEAR_BASE	1900
51 #endif
52 
53 static time_t tval;
54 
55 static void badformat(void);
56 static void iso8601_usage(const char *) __dead2;
57 static void multipleformats(void);
58 static void printdate(const char *);
59 static void printisodate(struct tm *);
60 static void setthetime(const char *, const char *, int);
61 static void usage(void) __dead2;
62 
63 static const struct iso8601_fmt {
64 	const char *refname;
65 	const char *format_string;
66 } iso8601_fmts[] = {
67 	{ "date", "%Y-%m-%d" },
68 	{ "hours", "T%H" },
69 	{ "minutes", ":%M" },
70 	{ "seconds", ":%S" },
71 };
72 static const struct iso8601_fmt *iso8601_selected;
73 
74 static const char *rfc2822_format = "%a, %d %b %Y %T %z";
75 
76 int
77 main(int argc, char *argv[])
78 {
79 	int ch, rflag;
80 	bool Iflag, jflag, Rflag;
81 	const char *format;
82 	char buf[1024];
83 	char *fmt, *outzone = NULL;
84 	char *tmp;
85 	struct vary *v;
86 	const struct vary *badv;
87 	struct tm *lt;
88 	struct stat sb;
89 	size_t i;
90 
91 	v = NULL;
92 	fmt = NULL;
93 	(void) setlocale(LC_TIME, "");
94 	rflag = 0;
95 	Iflag = jflag = Rflag = 0;
96 	while ((ch = getopt(argc, argv, "f:I::jnRr:uv:z:")) != -1)
97 		switch((char)ch) {
98 		case 'f':
99 			fmt = optarg;
100 			break;
101 		case 'I':
102 			if (Rflag)
103 				multipleformats();
104 			Iflag = 1;
105 			if (optarg == NULL) {
106 				iso8601_selected = iso8601_fmts;
107 				break;
108 			}
109 			for (i = 0; i < nitems(iso8601_fmts); i++)
110 				if (strcmp(optarg, iso8601_fmts[i].refname) == 0)
111 					break;
112 			if (i == nitems(iso8601_fmts))
113 				iso8601_usage(optarg);
114 
115 			iso8601_selected = &iso8601_fmts[i];
116 			break;
117 		case 'j':
118 			jflag = 1;	/* don't set time */
119 			break;
120 		case 'n':
121 			break;
122 		case 'R':		/* RFC 2822 datetime format */
123 			if (Iflag)
124 				multipleformats();
125 			Rflag = 1;
126 			break;
127 		case 'r':		/* user specified seconds */
128 			rflag = 1;
129 			tval = strtoq(optarg, &tmp, 0);
130 			if (*tmp != 0) {
131 				if (stat(optarg, &sb) == 0)
132 					tval = sb.st_mtim.tv_sec;
133 				else
134 					usage();
135 			}
136 			break;
137 		case 'u':		/* do everything in UTC */
138 			(void)setenv("TZ", "UTC0", 1);
139 			break;
140 		case 'z':
141 			outzone = optarg;
142 			break;
143 		case 'v':
144 			v = vary_append(v, optarg);
145 			break;
146 		default:
147 			usage();
148 		}
149 	argc -= optind;
150 	argv += optind;
151 
152 	if (!rflag && time(&tval) == -1)
153 		err(1, "time");
154 
155 	format = "%+";
156 
157 	if (Rflag)
158 		format = rfc2822_format;
159 
160 	/* allow the operands in any order */
161 	if (*argv && **argv == '+') {
162 		if (Iflag)
163 			multipleformats();
164 		format = *argv + 1;
165 		++argv;
166 	}
167 
168 	if (*argv) {
169 		setthetime(fmt, *argv, jflag);
170 		++argv;
171 	} else if (fmt != NULL)
172 		usage();
173 
174 	if (*argv && **argv == '+') {
175 		if (Iflag)
176 			multipleformats();
177 		format = *argv + 1;
178 	}
179 
180 	if (outzone != NULL && setenv("TZ", outzone, 1) != 0)
181 		err(1, "setenv(TZ)");
182 	lt = localtime(&tval);
183 	if (lt == NULL)
184 		errx(1, "invalid time");
185 	badv = vary_apply(v, lt);
186 	if (badv) {
187 		fprintf(stderr, "%s: Cannot apply date adjustment\n",
188 			badv->arg);
189 		vary_destroy(v);
190 		usage();
191 	}
192 	vary_destroy(v);
193 
194 	if (Iflag)
195 		printisodate(lt);
196 
197 	if (format == rfc2822_format)
198 		/*
199 		 * When using RFC 2822 datetime format, don't honor the
200 		 * locale.
201 		 */
202 		setlocale(LC_TIME, "C");
203 
204 
205 	(void)strftime(buf, sizeof(buf), format, lt);
206 	printdate(buf);
207 }
208 
209 static void
210 printdate(const char *buf)
211 {
212 	(void)printf("%s\n", buf);
213 	if (fflush(stdout))
214 		err(1, "stdout");
215 	exit(EXIT_SUCCESS);
216 }
217 
218 static void
219 printisodate(struct tm *lt)
220 {
221 	const struct iso8601_fmt *it;
222 	char fmtbuf[32], buf[32], tzbuf[8];
223 
224 	fmtbuf[0] = 0;
225 	for (it = iso8601_fmts; it <= iso8601_selected; it++)
226 		strlcat(fmtbuf, it->format_string, sizeof(fmtbuf));
227 
228 	(void)strftime(buf, sizeof(buf), fmtbuf, lt);
229 
230 	if (iso8601_selected > iso8601_fmts) {
231 		(void)strftime(tzbuf, sizeof(tzbuf), "%z", lt);
232 		memmove(&tzbuf[4], &tzbuf[3], 3);
233 		tzbuf[3] = ':';
234 		strlcat(buf, tzbuf, sizeof(buf));
235 	}
236 
237 	printdate(buf);
238 }
239 
240 #define	ATOI2(s)	((s) += 2, ((s)[-2] - '0') * 10 + ((s)[-1] - '0'))
241 
242 static void
243 setthetime(const char *fmt, const char *p, int jflag)
244 {
245 	struct utmpx utx;
246 	struct tm *lt;
247 	struct timeval tv;
248 	const char *dot, *t;
249 	int century;
250 
251 	lt = localtime(&tval);
252 	if (lt == NULL)
253 		errx(1, "invalid time");
254 	lt->tm_isdst = -1;		/* divine correct DST */
255 
256 	if (fmt != NULL) {
257 		t = strptime(p, fmt, lt);
258 		if (t == NULL) {
259 			fprintf(stderr, "Failed conversion of ``%s''"
260 				" using format ``%s''\n", p, fmt);
261 			badformat();
262 		} else if (*t != '\0')
263 			fprintf(stderr, "Warning: Ignoring %ld extraneous"
264 				" characters in date string (%s)\n",
265 				(long) strlen(t), t);
266 	} else {
267 		for (t = p, dot = NULL; *t; ++t) {
268 			if (isdigit(*t))
269 				continue;
270 			if (*t == '.' && dot == NULL) {
271 				dot = t;
272 				continue;
273 			}
274 			badformat();
275 		}
276 
277 		if (dot != NULL) {			/* .ss */
278 			dot++; /* *dot++ = '\0'; */
279 			if (strlen(dot) != 2)
280 				badformat();
281 			lt->tm_sec = ATOI2(dot);
282 			if (lt->tm_sec > 61)
283 				badformat();
284 		} else
285 			lt->tm_sec = 0;
286 
287 		century = 0;
288 		/* if p has a ".ss" field then let's pretend it's not there */
289 		switch (strlen(p) - ((dot != NULL) ? 3 : 0)) {
290 		case 12:				/* cc */
291 			lt->tm_year = ATOI2(p) * 100 - TM_YEAR_BASE;
292 			century = 1;
293 			/* FALLTHROUGH */
294 		case 10:				/* yy */
295 			if (century)
296 				lt->tm_year += ATOI2(p);
297 			else {
298 				lt->tm_year = ATOI2(p);
299 				if (lt->tm_year < 69)	/* hack for 2000 ;-} */
300 					lt->tm_year += 2000 - TM_YEAR_BASE;
301 				else
302 					lt->tm_year += 1900 - TM_YEAR_BASE;
303 			}
304 			/* FALLTHROUGH */
305 		case 8:					/* mm */
306 			lt->tm_mon = ATOI2(p);
307 			if (lt->tm_mon > 12)
308 				badformat();
309 			--lt->tm_mon;		/* time struct is 0 - 11 */
310 			/* FALLTHROUGH */
311 		case 6:					/* dd */
312 			lt->tm_mday = ATOI2(p);
313 			if (lt->tm_mday > 31)
314 				badformat();
315 			/* FALLTHROUGH */
316 		case 4:					/* HH */
317 			lt->tm_hour = ATOI2(p);
318 			if (lt->tm_hour > 23)
319 				badformat();
320 			/* FALLTHROUGH */
321 		case 2:					/* MM */
322 			lt->tm_min = ATOI2(p);
323 			if (lt->tm_min > 59)
324 				badformat();
325 			break;
326 		default:
327 			badformat();
328 		}
329 	}
330 
331 	/* convert broken-down time to GMT clock time */
332 	if ((tval = mktime(lt)) == -1)
333 		errx(1, "nonexistent time");
334 
335 	if (!jflag) {
336 		utx.ut_type = OLD_TIME;
337 		memset(utx.ut_id, 0, sizeof(utx.ut_id));
338 		(void)gettimeofday(&utx.ut_tv, NULL);
339 		pututxline(&utx);
340 		tv.tv_sec = tval;
341 		tv.tv_usec = 0;
342 		if (settimeofday(&tv, NULL) != 0)
343 			err(1, "settimeofday (timeval)");
344 		utx.ut_type = NEW_TIME;
345 		(void)gettimeofday(&utx.ut_tv, NULL);
346 		pututxline(&utx);
347 
348 		if ((p = getlogin()) == NULL)
349 			p = "???";
350 		syslog(LOG_AUTH | LOG_NOTICE, "date set by %s", p);
351 	}
352 }
353 
354 static void
355 badformat(void)
356 {
357 	warnx("illegal time format");
358 	usage();
359 }
360 
361 static void
362 iso8601_usage(const char *badarg)
363 {
364 	errx(1, "invalid argument '%s' for -I", badarg);
365 }
366 
367 static void
368 multipleformats(void)
369 {
370 	errx(1, "multiple output formats specified");
371 }
372 
373 static void
374 usage(void)
375 {
376 	(void)fprintf(stderr, "%s\n%s\n%s\n",
377 	    "usage: date [-jnRu] [-I[date|hours|minutes|seconds]] [-f input_fmt]",
378 	    "            "
379 	    "[ -z output_zone ] [-r filename|seconds] [-v[+|-]val[y|m|w|d|H|M|S]]",
380 	    "            "
381 	    "[[[[[[cc]yy]mm]dd]HH]MM[.SS] | new_date] [+output_fmt]"
382 	    );
383 	exit(1);
384 }
385