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
71 static void setthetime(const char *, const char *, int, int);
72 static void badformat(void);
73 static void usage(void);
75 static const char *rfc2822_format = "%a, %d %b %Y %T %z";
78 main(int argc, char *argv[])
82 int jflag, nflag, Rflag;
89 const struct vary *badv;
95 (void) setlocale(LC_TIME, "");
96 tz.tz_dsttime = tz.tz_minuteswest = 0;
98 jflag = nflag = Rflag = 0;
100 while ((ch = getopt(argc, argv, "d:f:jnRr:t:uv:")) != -1)
102 case 'd': /* daylight savings time */
103 tz.tz_dsttime = strtol(optarg, &endptr, 10) ? 1 : 0;
104 if (endptr == optarg || *endptr != '\0')
112 jflag = 1; /* don't set time */
114 case 'n': /* don't set network */
117 case 'R': /* RFC 2822 datetime format */
120 case 'r': /* user specified seconds */
122 tval = strtoq(optarg, &tmp, 0);
124 if (stat(optarg, &sb) == 0)
125 tval = sb.st_mtim.tv_sec;
130 case 't': /* minutes west of UTC */
131 /* error check; don't allow "PST" */
132 tz.tz_minuteswest = strtol(optarg, &endptr, 10);
133 if (endptr == optarg || *endptr != '\0')
137 case 'u': /* do everything in UTC */
138 (void)setenv("TZ", "UTC0", 1);
141 v = vary_append(v, optarg);
150 * If -d or -t, set the timezone or daylight savings time; this
151 * doesn't belong here; the kernel should not know about either.
153 if (set_timezone && settimeofday(NULL, &tz) != 0)
154 err(1, "settimeofday (timezone)");
156 if (!rflag && time(&tval) == -1)
162 format = rfc2822_format;
164 /* allow the operands in any order */
165 if (*argv && **argv == '+') {
171 setthetime(fmt, *argv, jflag, nflag);
173 } else if (fmt != NULL)
176 if (*argv && **argv == '+')
179 lt = localtime(&tval);
181 errx(1, "invalid time");
182 badv = vary_apply(v, lt);
184 fprintf(stderr, "%s: Cannot apply date adjustment\n",
191 if (format == rfc2822_format)
193 * When using RFC 2822 datetime format, don't honor the
196 setlocale(LC_TIME, "C");
198 (void)strftime(buf, sizeof(buf), format, lt);
199 (void)printf("%s\n", buf);
205 #define ATOI2(s) ((s) += 2, ((s)[-2] - '0') * 10 + ((s)[-1] - '0'))
208 setthetime(const char *fmt, const char *p, int jflag, int nflag)
216 lt = localtime(&tval);
218 errx(1, "invalid time");
219 lt->tm_isdst = -1; /* divine correct DST */
222 t = strptime(p, fmt, lt);
224 fprintf(stderr, "Failed conversion of ``%s''"
225 " using format ``%s''\n", p, fmt);
227 } else if (*t != '\0')
228 fprintf(stderr, "Warning: Ignoring %ld extraneous"
229 " characters in date string (%s)\n",
230 (long) strlen(t), t);
232 for (t = p, dot = NULL; *t; ++t) {
235 if (*t == '.' && dot == NULL) {
242 if (dot != NULL) { /* .ss */
243 dot++; /* *dot++ = '\0'; */
244 if (strlen(dot) != 2)
246 lt->tm_sec = ATOI2(dot);
253 /* if p has a ".ss" field then let's pretend it's not there */
254 switch (strlen(p) - ((dot != NULL) ? 3 : 0)) {
256 lt->tm_year = ATOI2(p) * 100 - TM_YEAR_BASE;
261 lt->tm_year += ATOI2(p);
263 lt->tm_year = ATOI2(p);
264 if (lt->tm_year < 69) /* hack for 2000 ;-} */
265 lt->tm_year += 2000 - TM_YEAR_BASE;
267 lt->tm_year += 1900 - TM_YEAR_BASE;
271 lt->tm_mon = ATOI2(p);
274 --lt->tm_mon; /* time struct is 0 - 11 */
277 lt->tm_mday = ATOI2(p);
278 if (lt->tm_mday > 31)
282 lt->tm_hour = ATOI2(p);
283 if (lt->tm_hour > 23)
287 lt->tm_min = ATOI2(p);
296 /* convert broken-down time to GMT clock time */
297 if ((tval = mktime(lt)) == -1)
298 errx(1, "nonexistent time");
302 if (nflag || netsettime(tval)) {
303 utx.ut_type = OLD_TIME;
304 memset(utx.ut_id, 0, sizeof(utx.ut_id));
305 (void)gettimeofday(&utx.ut_tv, NULL);
309 if (settimeofday(&tv, NULL) != 0)
310 err(1, "settimeofday (timeval)");
311 utx.ut_type = NEW_TIME;
312 (void)gettimeofday(&utx.ut_tv, NULL);
316 if ((p = getlogin()) == NULL)
318 syslog(LOG_AUTH | LOG_NOTICE, "date set by %s", p);
325 warnx("illegal time format");
332 (void)fprintf(stderr, "%s\n%s\n",
333 "usage: date [-jnRu] [-d dst] [-r seconds] [-t west] "
334 "[-v[+|-]val[ymwdHMS]] ... ",
336 "[-f fmt date | [[[[[cc]yy]mm]dd]HH]MM[.ss]] [+format]");