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 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * $Id: date.c,v 1.21 1997/10/03 12:49:28 danny Exp $
37 static char const copyright[] =
38 "@(#) Copyright (c) 1985, 1987, 1988, 1993\n\
39 The Regents of the University of California. All rights reserved.\n";
43 static char const sccsid[] = "@(#)date.c 8.2 (Berkeley) 4/28/95";
46 #include <sys/param.h>
65 static void setthetime __P((const char *, const char *));
66 static void badformat __P((void));
67 static void usage __P((void));
69 int logwtmp __P((char *, char *, char *));
80 char *format, buf[1024];
84 const struct vary *badv;
89 (void) setlocale(LC_TIME, "");
90 tz.tz_dsttime = tz.tz_minuteswest = 0;
93 while ((ch = getopt(argc, argv, "d:f:nr:t:uv:")) != -1)
95 case 'd': /* daylight savings time */
96 tz.tz_dsttime = strtol(optarg, &endptr, 10) ? 1 : 0;
97 if (endptr == optarg || *endptr != '\0')
104 case 'n': /* don't set network */
107 case 'r': /* user specified seconds */
111 case 't': /* minutes west of GMT */
112 /* error check; don't allow "PST" */
113 tz.tz_minuteswest = strtol(optarg, &endptr, 10);
114 if (endptr == optarg || *endptr != '\0')
118 case 'u': /* do everything in GMT */
119 (void)setenv("TZ", "GMT0", 1);
122 v = vary_append(v, optarg);
131 * If -d or -t, set the timezone or daylight savings time; this
132 * doesn't belong here; the kernel should not know about either.
134 if (set_timezone && settimeofday((struct timeval *)NULL, &tz))
135 err(1, "settimeofday (timezone)");
137 if (!rflag && time(&tval) == -1)
142 /* allow the operands in any order */
143 if (*argv && **argv == '+') {
149 setthetime(fmt, *argv);
151 } else if (fmt != NULL)
154 if (*argv && **argv == '+')
157 lt = *localtime(&tval);
158 badv = vary_apply(v, <);
160 fprintf(stderr, "%s: Cannot apply date adjustment\n",
166 (void)strftime(buf, sizeof(buf), format, <);
167 (void)printf("%s\n", buf);
171 #define ATOI2(ar) ((ar)[0] - '0') * 10 + ((ar)[1] - '0'); (ar) += 2;
175 register const char *p;
177 register struct tm *lt;
182 lt = localtime(&tval);
183 t = strptime(p, fmt, lt);
185 fprintf(stderr, "Failed conversion of ``%s''"
186 " using format ``%s''\n", p, fmt);
187 lt = localtime(&tval);
189 } else if (*t != '\0')
190 fprintf(stderr, "Warning: Ignoring %d extraneous"
191 " characters in date string (%s)\n",
194 for (t = p, dot = NULL; *t; ++t) {
197 if (*t == '.' && dot == NULL) {
204 lt = localtime(&tval);
206 if (dot != NULL) { /* .ss */
207 dot++; /* *dot++ = '\0'; */
208 if (strlen(dot) != 2)
210 lt->tm_sec = ATOI2(dot);
216 /* if p has a ".ss" field then let's pretend it's not there */
217 switch (strlen(p) - ((dot != NULL) ? 3 : 0)) {
219 lt->tm_year = ATOI2(p);
220 if (lt->tm_year < 69) /* hack for 2000 ;-} */
224 lt->tm_mon = ATOI2(p);
227 --lt->tm_mon; /* time struct is 0 - 11 */
230 lt->tm_mday = ATOI2(p);
231 if (lt->tm_mday > 31)
235 lt->tm_hour = ATOI2(p);
236 if (lt->tm_hour > 23)
240 lt->tm_min = ATOI2(p);
249 /* convert broken-down time to GMT clock time */
250 if ((tval = mktime(lt)) == -1)
251 errx(1, "nonexistent time");
254 if (nflag || netsettime(tval)) {
255 logwtmp("|", "date", "");
258 if (settimeofday(&tv, (struct timezone *)NULL))
259 err(1, "settimeofday (timeval)");
260 logwtmp("{", "date", "");
263 if ((p = getlogin()) == NULL)
265 syslog(LOG_AUTH | LOG_NOTICE, "date set by %s", p);
271 warnx("illegal time format");
278 (void)fprintf(stderr, "%s\n%s\n",
279 "usage: date [-nu] [-d dst] [-r seconds] [-t west] "
280 "[-v[+|-]val[ymwdHM]] ... ",
281 " [-f fmt date | [[[[yy]mm]dd]HH]MM[.ss]] [+format]");