2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1985, 1987, 1988, 1993
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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
33 static char const copyright[] =
34 "@(#) Copyright (c) 1985, 1987, 1988, 1993\n\
35 The Regents of the University of California. All rights reserved.\n";
40 static char sccsid[] = "@(#)date.c 8.2 (Berkeley) 4/28/95";
44 #include <sys/cdefs.h>
45 __FBSDID("$FreeBSD$");
47 #include <sys/param.h>
65 #define TM_YEAR_BASE 1900
70 static void badformat(void);
71 static void iso8601_usage(const char *);
72 static void multipleformats(void);
73 static void printdate(const char *);
74 static void printisodate(struct tm *);
75 static void setthetime(const char *, const char *, int);
76 static void usage(void);
78 static const struct iso8601_fmt {
80 const char *format_string;
82 { "date", "%Y-%m-%d" },
87 static const struct iso8601_fmt *iso8601_selected;
89 static const char *rfc2822_format = "%a, %d %b %Y %T %z";
92 main(int argc, char *argv[])
95 bool Iflag, jflag, Rflag;
101 const struct vary *badv;
108 (void) setlocale(LC_TIME, "");
110 Iflag = jflag = Rflag = 0;
111 while ((ch = getopt(argc, argv, "f:I::jnRr:uv:")) != -1)
120 if (optarg == NULL) {
121 iso8601_selected = iso8601_fmts;
124 for (i = 0; i < nitems(iso8601_fmts); i++)
125 if (strcmp(optarg, iso8601_fmts[i].refname) == 0)
127 if (i == nitems(iso8601_fmts))
128 iso8601_usage(optarg);
130 iso8601_selected = &iso8601_fmts[i];
133 jflag = 1; /* don't set time */
137 case 'R': /* RFC 2822 datetime format */
142 case 'r': /* user specified seconds */
144 tval = strtoq(optarg, &tmp, 0);
146 if (stat(optarg, &sb) == 0)
147 tval = sb.st_mtim.tv_sec;
152 case 'u': /* do everything in UTC */
153 (void)setenv("TZ", "UTC0", 1);
156 v = vary_append(v, optarg);
164 if (!rflag && time(&tval) == -1)
170 format = rfc2822_format;
172 /* allow the operands in any order */
173 if (*argv && **argv == '+') {
181 setthetime(fmt, *argv, jflag);
183 } else if (fmt != NULL)
186 if (*argv && **argv == '+') {
192 lt = localtime(&tval);
194 errx(1, "invalid time");
195 badv = vary_apply(v, lt);
197 fprintf(stderr, "%s: Cannot apply date adjustment\n",
207 if (format == rfc2822_format)
209 * When using RFC 2822 datetime format, don't honor the
212 setlocale(LC_TIME, "C");
214 (void)strftime(buf, sizeof(buf), format, lt);
219 printdate(const char *buf)
221 (void)printf("%s\n", buf);
228 printisodate(struct tm *lt)
230 const struct iso8601_fmt *it;
231 char fmtbuf[32], buf[32], tzbuf[8];
234 for (it = iso8601_fmts; it <= iso8601_selected; it++)
235 strlcat(fmtbuf, it->format_string, sizeof(fmtbuf));
237 (void)strftime(buf, sizeof(buf), fmtbuf, lt);
239 if (iso8601_selected > iso8601_fmts) {
240 (void)strftime(tzbuf, sizeof(tzbuf), "%z", lt);
241 memmove(&tzbuf[4], &tzbuf[3], 3);
243 strlcat(buf, tzbuf, sizeof(buf));
249 #define ATOI2(s) ((s) += 2, ((s)[-2] - '0') * 10 + ((s)[-1] - '0'))
252 setthetime(const char *fmt, const char *p, int jflag)
260 lt = localtime(&tval);
262 errx(1, "invalid time");
263 lt->tm_isdst = -1; /* divine correct DST */
266 t = strptime(p, fmt, lt);
268 fprintf(stderr, "Failed conversion of ``%s''"
269 " using format ``%s''\n", p, fmt);
271 } else if (*t != '\0')
272 fprintf(stderr, "Warning: Ignoring %ld extraneous"
273 " characters in date string (%s)\n",
274 (long) strlen(t), t);
276 for (t = p, dot = NULL; *t; ++t) {
279 if (*t == '.' && dot == NULL) {
286 if (dot != NULL) { /* .ss */
287 dot++; /* *dot++ = '\0'; */
288 if (strlen(dot) != 2)
290 lt->tm_sec = ATOI2(dot);
297 /* if p has a ".ss" field then let's pretend it's not there */
298 switch (strlen(p) - ((dot != NULL) ? 3 : 0)) {
300 lt->tm_year = ATOI2(p) * 100 - TM_YEAR_BASE;
305 lt->tm_year += ATOI2(p);
307 lt->tm_year = ATOI2(p);
308 if (lt->tm_year < 69) /* hack for 2000 ;-} */
309 lt->tm_year += 2000 - TM_YEAR_BASE;
311 lt->tm_year += 1900 - TM_YEAR_BASE;
315 lt->tm_mon = ATOI2(p);
318 --lt->tm_mon; /* time struct is 0 - 11 */
321 lt->tm_mday = ATOI2(p);
322 if (lt->tm_mday > 31)
326 lt->tm_hour = ATOI2(p);
327 if (lt->tm_hour > 23)
331 lt->tm_min = ATOI2(p);
340 /* convert broken-down time to GMT clock time */
341 if ((tval = mktime(lt)) == -1)
342 errx(1, "nonexistent time");
345 utx.ut_type = OLD_TIME;
346 memset(utx.ut_id, 0, sizeof(utx.ut_id));
347 (void)gettimeofday(&utx.ut_tv, NULL);
351 if (settimeofday(&tv, NULL) != 0)
352 err(1, "settimeofday (timeval)");
353 utx.ut_type = NEW_TIME;
354 (void)gettimeofday(&utx.ut_tv, NULL);
357 if ((p = getlogin()) == NULL)
359 syslog(LOG_AUTH | LOG_NOTICE, "date set by %s", p);
366 warnx("illegal time format");
371 iso8601_usage(const char *badarg)
373 errx(1, "invalid argument '%s' for -I", badarg);
377 multipleformats(void)
379 errx(1, "multiple output formats specified");
385 (void)fprintf(stderr, "%s\n%s\n%s\n",
386 "usage: date [-jnRu] [-r seconds|file] [-v[+|-]val[ymwdHMS]]",
388 "[-I[date | hours | minutes | seconds]]",
390 "[-f fmt date | [[[[[cc]yy]mm]dd]HH]MM[.ss]] [+format]"