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>
66 #define TM_YEAR_BASE 1900
72 static void badformat(void);
73 static void iso8601_usage(const char *);
74 static void multipleformats(void);
75 static void printdate(const char *);
76 static void printisodate(struct tm *);
77 static void setthetime(const char *, const char *, int, int);
78 static void usage(void);
80 static const struct iso8601_fmt {
82 const char *format_string;
84 { "date", "%Y-%m-%d" },
89 static const struct iso8601_fmt *iso8601_selected;
91 static const char *rfc2822_format = "%a, %d %b %Y %T %z";
94 main(int argc, char *argv[])
98 bool Iflag, jflag, nflag, Rflag;
105 const struct vary *badv;
112 (void) setlocale(LC_TIME, "");
113 tz.tz_dsttime = tz.tz_minuteswest = 0;
115 Iflag = jflag = nflag = Rflag = 0;
117 while ((ch = getopt(argc, argv, "d:f:I::jnRr:t:uv:")) != -1)
119 case 'd': /* daylight savings time */
120 tz.tz_dsttime = strtol(optarg, &endptr, 10) ? 1 : 0;
121 if (endptr == optarg || *endptr != '\0')
132 if (optarg == NULL) {
133 iso8601_selected = iso8601_fmts;
136 for (i = 0; i < nitems(iso8601_fmts); i++)
137 if (strcmp(optarg, iso8601_fmts[i].refname) == 0)
139 if (i == nitems(iso8601_fmts))
140 iso8601_usage(optarg);
142 iso8601_selected = &iso8601_fmts[i];
145 jflag = 1; /* don't set time */
147 case 'n': /* don't set network */
150 case 'R': /* RFC 2822 datetime format */
155 case 'r': /* user specified seconds */
157 tval = strtoq(optarg, &tmp, 0);
159 if (stat(optarg, &sb) == 0)
160 tval = sb.st_mtim.tv_sec;
165 case 't': /* minutes west of UTC */
166 /* error check; don't allow "PST" */
167 tz.tz_minuteswest = strtol(optarg, &endptr, 10);
168 if (endptr == optarg || *endptr != '\0')
172 case 'u': /* do everything in UTC */
173 (void)setenv("TZ", "UTC0", 1);
176 v = vary_append(v, optarg);
185 * If -d or -t, set the timezone or daylight savings time; this
186 * doesn't belong here; the kernel should not know about either.
188 if (set_timezone && settimeofday(NULL, &tz) != 0)
189 err(1, "settimeofday (timezone)");
191 if (!rflag && time(&tval) == -1)
197 format = rfc2822_format;
199 /* allow the operands in any order */
200 if (*argv && **argv == '+') {
208 setthetime(fmt, *argv, jflag, nflag);
210 } else if (fmt != NULL)
213 if (*argv && **argv == '+') {
219 lt = localtime(&tval);
221 errx(1, "invalid time");
222 badv = vary_apply(v, lt);
224 fprintf(stderr, "%s: Cannot apply date adjustment\n",
234 if (format == rfc2822_format)
236 * When using RFC 2822 datetime format, don't honor the
239 setlocale(LC_TIME, "C");
241 (void)strftime(buf, sizeof(buf), format, lt);
246 printdate(const char *buf)
248 (void)printf("%s\n", buf);
255 printisodate(struct tm *lt)
257 const struct iso8601_fmt *it;
258 char fmtbuf[32], buf[32], tzbuf[8];
261 for (it = iso8601_fmts; it <= iso8601_selected; it++)
262 strlcat(fmtbuf, it->format_string, sizeof(fmtbuf));
264 (void)strftime(buf, sizeof(buf), fmtbuf, lt);
266 if (iso8601_selected > iso8601_fmts) {
267 (void)strftime(tzbuf, sizeof(tzbuf), "%z", lt);
268 memmove(&tzbuf[4], &tzbuf[3], 3);
270 strlcat(buf, tzbuf, sizeof(buf));
276 #define ATOI2(s) ((s) += 2, ((s)[-2] - '0') * 10 + ((s)[-1] - '0'))
279 setthetime(const char *fmt, const char *p, int jflag, int nflag)
287 lt = localtime(&tval);
289 errx(1, "invalid time");
290 lt->tm_isdst = -1; /* divine correct DST */
293 t = strptime(p, fmt, lt);
295 fprintf(stderr, "Failed conversion of ``%s''"
296 " using format ``%s''\n", p, fmt);
298 } else if (*t != '\0')
299 fprintf(stderr, "Warning: Ignoring %ld extraneous"
300 " characters in date string (%s)\n",
301 (long) strlen(t), t);
303 for (t = p, dot = NULL; *t; ++t) {
306 if (*t == '.' && dot == NULL) {
313 if (dot != NULL) { /* .ss */
314 dot++; /* *dot++ = '\0'; */
315 if (strlen(dot) != 2)
317 lt->tm_sec = ATOI2(dot);
324 /* if p has a ".ss" field then let's pretend it's not there */
325 switch (strlen(p) - ((dot != NULL) ? 3 : 0)) {
327 lt->tm_year = ATOI2(p) * 100 - TM_YEAR_BASE;
332 lt->tm_year += ATOI2(p);
334 lt->tm_year = ATOI2(p);
335 if (lt->tm_year < 69) /* hack for 2000 ;-} */
336 lt->tm_year += 2000 - TM_YEAR_BASE;
338 lt->tm_year += 1900 - TM_YEAR_BASE;
342 lt->tm_mon = ATOI2(p);
345 --lt->tm_mon; /* time struct is 0 - 11 */
348 lt->tm_mday = ATOI2(p);
349 if (lt->tm_mday > 31)
353 lt->tm_hour = ATOI2(p);
354 if (lt->tm_hour > 23)
358 lt->tm_min = ATOI2(p);
367 /* convert broken-down time to GMT clock time */
368 if ((tval = mktime(lt)) == -1)
369 errx(1, "nonexistent time");
373 if (nflag || netsettime(tval)) {
374 utx.ut_type = OLD_TIME;
375 memset(utx.ut_id, 0, sizeof(utx.ut_id));
376 (void)gettimeofday(&utx.ut_tv, NULL);
380 if (settimeofday(&tv, NULL) != 0)
381 err(1, "settimeofday (timeval)");
382 utx.ut_type = NEW_TIME;
383 (void)gettimeofday(&utx.ut_tv, NULL);
387 if ((p = getlogin()) == NULL)
389 syslog(LOG_AUTH | LOG_NOTICE, "date set by %s", p);
396 warnx("illegal time format");
401 iso8601_usage(const char *badarg)
403 errx(1, "invalid argument '%s' for -I", badarg);
407 multipleformats(void)
409 errx(1, "multiple output formats specified");
415 (void)fprintf(stderr, "%s\n%s\n%s\n",
416 "usage: date [-jnRu] [-d dst] [-r seconds|file] [-t west] "
417 "[-v[+|-]val[ymwdHMS]]",
419 "[-I[date | hours | minutes | seconds]]",
421 "[-f fmt date | [[[[[cc]yy]mm]dd]HH]MM[.ss]] [+format]"