2 * Copyright (c) 1985, 1987, 1988, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 4. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 static char const copyright[] =
32 "@(#) Copyright (c) 1985, 1987, 1988, 1993\n\
33 The Regents of the University of California. All rights reserved.\n";
38 static char sccsid[] = "@(#)date.c 8.2 (Berkeley) 4/28/95";
42 #include <sys/cdefs.h>
43 __FBSDID("$FreeBSD$");
45 #include <sys/param.h>
62 #define TM_YEAR_BASE 1900
68 static void setthetime(const char *, const char *, int, int);
69 static void badformat(void);
70 static void usage(void);
73 main(int argc, char *argv[])
84 const struct vary *badv;
89 (void) setlocale(LC_TIME, "");
90 tz.tz_dsttime = tz.tz_minuteswest = 0;
94 while ((ch = getopt(argc, argv, "d:f:jnr:t:uv:")) != -1)
96 case 'd': /* daylight savings time */
97 tz.tz_dsttime = strtol(optarg, &endptr, 10) ? 1 : 0;
98 if (endptr == optarg || *endptr != '\0')
106 jflag = 1; /* don't set time */
108 case 'n': /* don't set network */
111 case 'r': /* user specified seconds */
113 tval = strtoq(optarg, &tmp, 0);
117 case 't': /* minutes west of UTC */
118 /* error check; don't allow "PST" */
119 tz.tz_minuteswest = strtol(optarg, &endptr, 10);
120 if (endptr == optarg || *endptr != '\0')
124 case 'u': /* do everything in UTC */
125 (void)setenv("TZ", "UTC0", 1);
128 v = vary_append(v, optarg);
137 * If -d or -t, set the timezone or daylight savings time; this
138 * doesn't belong here; the kernel should not know about either.
140 if (set_timezone && settimeofday(NULL, &tz) != 0)
141 err(1, "settimeofday (timezone)");
143 if (!rflag && time(&tval) == -1)
148 /* allow the operands in any order */
149 if (*argv && **argv == '+') {
155 setthetime(fmt, *argv, jflag, nflag);
157 } else if (fmt != NULL)
160 if (*argv && **argv == '+')
163 lt = *localtime(&tval);
164 badv = vary_apply(v, <);
166 fprintf(stderr, "%s: Cannot apply date adjustment\n",
172 (void)strftime(buf, sizeof(buf), format, <);
173 (void)printf("%s\n", buf);
179 #define ATOI2(s) ((s) += 2, ((s)[-2] - '0') * 10 + ((s)[-1] - '0'))
182 setthetime(const char *fmt, const char *p, int jflag, int nflag)
190 lt = localtime(&tval);
191 lt->tm_isdst = -1; /* divine correct DST */
194 t = strptime(p, fmt, lt);
196 fprintf(stderr, "Failed conversion of ``%s''"
197 " using format ``%s''\n", p, fmt);
199 } else if (*t != '\0')
200 fprintf(stderr, "Warning: Ignoring %ld extraneous"
201 " characters in date string (%s)\n",
202 (long) strlen(t), t);
204 for (t = p, dot = NULL; *t; ++t) {
207 if (*t == '.' && dot == NULL) {
214 if (dot != NULL) { /* .ss */
215 dot++; /* *dot++ = '\0'; */
216 if (strlen(dot) != 2)
218 lt->tm_sec = ATOI2(dot);
225 /* if p has a ".ss" field then let's pretend it's not there */
226 switch (strlen(p) - ((dot != NULL) ? 3 : 0)) {
228 lt->tm_year = ATOI2(p) * 100 - TM_YEAR_BASE;
233 lt->tm_year += ATOI2(p);
235 lt->tm_year = ATOI2(p);
236 if (lt->tm_year < 69) /* hack for 2000 ;-} */
237 lt->tm_year += 2000 - TM_YEAR_BASE;
239 lt->tm_year += 1900 - TM_YEAR_BASE;
243 lt->tm_mon = ATOI2(p);
246 --lt->tm_mon; /* time struct is 0 - 11 */
249 lt->tm_mday = ATOI2(p);
250 if (lt->tm_mday > 31)
254 lt->tm_hour = ATOI2(p);
255 if (lt->tm_hour > 23)
259 lt->tm_min = ATOI2(p);
268 /* convert broken-down time to GMT clock time */
269 if ((tval = mktime(lt)) == -1)
270 errx(1, "nonexistent time");
274 if (nflag || netsettime(tval)) {
275 utx.ut_type = OLD_TIME;
276 (void)gettimeofday(&utx.ut_tv, NULL);
280 if (settimeofday(&tv, NULL) != 0)
281 err(1, "settimeofday (timeval)");
282 utx.ut_type = NEW_TIME;
283 (void)gettimeofday(&utx.ut_tv, NULL);
287 if ((p = getlogin()) == NULL)
289 syslog(LOG_AUTH | LOG_NOTICE, "date set by %s", p);
296 warnx("illegal time format");
303 (void)fprintf(stderr, "%s\n%s\n",
304 "usage: date [-jnu] [-d dst] [-r seconds] [-t west] "
305 "[-v[+|-]val[ymwdHMS]] ... ",
307 "[-f fmt date | [[[[[cc]yy]mm]dd]HH]MM[.ss]] [+format]");