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 * @(#) Copyright (c) 1985, 1987, 1988, 1993 The Regents of the University of California. All rights reserved.
34 * @(#)date.c 8.2 (Berkeley) 4/28/95
35 * $FreeBSD: src/bin/date/date.c,v 1.47 2005/01/10 08:39:21 imp Exp $
36 * $DragonFly: src/bin/date/date.c,v 1.13 2005/07/20 19:51:56 cpressey Exp $
39 #include <sys/param.h>
57 #define TM_YEAR_BASE 1900
63 static void setthetime(const char *, const char *, int, int);
64 static void badformat(void);
65 static void usage(void);
68 main(int argc, char **argv)
79 const struct vary *badv;
84 setlocale(LC_TIME, "");
85 tz.tz_dsttime = tz.tz_minuteswest = 0;
89 while ((ch = getopt(argc, argv, "d:f:jnr:t:uv:")) != -1)
91 case 'd': /* daylight savings time */
92 tz.tz_dsttime = strtol(optarg, &endptr, 10) ? 1 : 0;
93 if (endptr == optarg || *endptr != '\0')
101 jflag = 1; /* don't set time */
103 case 'n': /* don't set network */
106 case 'r': /* user specified seconds */
108 tval = strtoll(optarg, &tmp, 0);
112 case 't': /* minutes west of UTC */
113 /* error check; don't allow "PST" */
114 tz.tz_minuteswest = strtol(optarg, &endptr, 10);
115 if (endptr == optarg || *endptr != '\0')
119 case 'u': /* do everything in UTC */
120 setenv("TZ", "UTC0", 1);
123 v = vary_append(v, optarg);
132 * If -d or -t, set the timezone or daylight savings time; this
133 * doesn't belong here; the kernel should not know about either.
135 if (set_timezone && settimeofday((struct timeval *)NULL, &tz))
136 err(1, "settimeofday (timezone)");
138 if (!rflag && time(&tval) == -1)
141 format = nl_langinfo(_DATE_FMT);
143 /* allow the operands in any order */
144 if (*argv && **argv == '+') {
150 setthetime(fmt, *argv, jflag, nflag);
152 } else if (fmt != NULL)
155 if (*argv && **argv == '+')
158 lt = *localtime(&tval);
159 badv = vary_apply(v, <);
161 fprintf(stderr, "%s: Cannot apply date adjustment\n",
167 strftime(buf, sizeof(buf), format, <);
169 if (fflush(stdout) != 0)
174 #define ATOI2(s) ((s) += 2, ((s)[-2] - '0') * 10 + ((s)[-1] - '0'))
177 setthetime(const char *fmt, const char *p, int jflag, int nflag)
185 lt = localtime(&tval);
186 t = strptime(p, fmt, lt);
188 fprintf(stderr, "Failed conversion of ``%s''"
189 " using format ``%s''\n", p, fmt);
191 } else if (*t != '\0')
192 fprintf(stderr, "Warning: Ignoring %ld extraneous"
193 " characters in date string (%s)\n",
194 (long) strlen(t), t);
196 for (t = p, dot = NULL; *t; ++t) {
199 if (*t == '.' && dot == NULL) {
206 lt = localtime(&tval);
208 if (dot != NULL) { /* .ss */
209 dot++; /* *dot++ = '\0'; */
210 if (strlen(dot) != 2)
212 lt->tm_sec = ATOI2(dot);
219 /* if p has a ".ss" field then let's pretend it's not there */
220 switch (strlen(p) - ((dot != NULL) ? 3 : 0)) {
222 lt->tm_year = ATOI2(p) * 100 - TM_YEAR_BASE;
227 lt->tm_year += ATOI2(p);
228 else { /* hack for 2000 ;-} */
229 lt->tm_year = ATOI2(p);
230 if (lt->tm_year < 69)
231 lt->tm_year += 2000 - TM_YEAR_BASE;
233 lt->tm_year += 1900 - TM_YEAR_BASE;
237 lt->tm_mon = ATOI2(p);
240 --lt->tm_mon; /* time struct is 0 - 11 */
243 lt->tm_mday = ATOI2(p);
244 if (lt->tm_mday > 31)
248 lt->tm_hour = ATOI2(p);
249 if (lt->tm_hour > 23)
253 lt->tm_min = ATOI2(p);
262 /* Let mktime() decide whether summer time is in effect. */
265 /* convert broken-down time to GMT clock time */
266 if ((tval = mktime(lt)) == -1)
267 errx(1, "nonexistent time");
271 if (nflag || netsettime(tval)) {
272 logwtmp("|", "date", "");
275 if (settimeofday(&tv, (struct timezone *)NULL))
276 err(1, "settimeofday (timeval)");
277 logwtmp("{", "date", "");
280 if ((p = getlogin()) == NULL)
282 syslog(LOG_AUTH | LOG_NOTICE, "date set by %s", p);
289 warnx("illegal time format");
296 fprintf(stderr, "%s\n%s\n",
297 "usage: date [-jnu] [-d dst] [-r seconds] [-t west] "
298 "[-v[+|-]val[ymwdHMS]] ... ",
300 "[-f fmt date | [[[[[cc]yy]mm]dd]HH]MM[.ss]] [+format]");