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. 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
29 * @(#)date.c 8.2 (Berkeley) 4/28/95
30 * $FreeBSD: src/bin/date/date.c,v 1.47 2005/01/10 08:39:21 imp Exp $
33 #include <sys/param.h>
50 #define TM_YEAR_BASE 1900
55 static void badformat(void);
56 static void setthetime(const char *, const char *, int);
57 static void usage(void);
59 static const struct iso8601_fmt {
63 { "date", "%Y-%m-%d" },
64 { "hours", "%Y-%m-%dT%H" },
65 { "minutes", "%Y-%m-%dT%H:%M" },
66 { "seconds", "%Y-%m-%dT%H:%M:%S" },
68 static const struct iso8601_fmt *iso8601_selected;
70 static const char *rfc2822_format = "%a, %d %b %Y %T %z";
73 main(int argc, char **argv)
76 int Iflag, jflag, Rflag;
78 char buf[1024], tzbuf[8];
82 const struct vary *badv;
88 setlocale(LC_TIME, "");
90 Iflag = jflag = Rflag = 0;
91 while ((ch = getopt(argc, argv, "d:f:I::jnRr:uv:")) != -1) {
96 case 'I': /* ISO 8601 datetime format */
98 errx(1, "multiple output formats specified");
100 if (optarg == NULL) {
101 iso8601_selected = iso8601_fmts;
104 for (i = 0; i < nitems(iso8601_fmts); i++) {
105 if (strncmp(optarg, iso8601_fmts[i].refname,
106 strlen(optarg)) == 0)
109 if (i == nitems(iso8601_fmts))
110 errx(1, "invalid argument '%s' for -I", optarg);
111 iso8601_selected = &iso8601_fmts[i];
114 jflag = 1; /* don't set time */
116 case 'n': /* don't set network */
118 case 'R': /* RFC 2822 datetime format */
120 errx(1, "multiple output formats specified");
123 case 'r': /* user specified seconds */
125 tval = strtoll(optarg, &tmp, 0);
129 case 'u': /* do everything in UTC */
130 if (setenv("TZ", "UTC0", 1) != 0)
131 err(1, "setenv: cannot set TZ=UTC0");
135 if (strncmp(optarg, "TZ=", 3) == 0) {
136 tmp = strdup(optarg);
139 for (tzlen = 0; !isspace(tmp[tzlen]); ++tzlen)
141 if (tmp[0] == '"' && tmp[tzlen - 1] == '"') {
143 setenv("TZ", tmp + 1, 1);
146 setenv("TZ", tmp, 1);
148 while (isspace(tmp[tzlen]))
151 v = vary_append(v, optarg + tzlen);
153 v = vary_append(v, optarg);
163 if (!rflag && time(&tval) == -1)
169 format = rfc2822_format;
171 format = iso8601_selected->format;
173 /* allow the operands in any order */
174 if (*argv && **argv == '+') {
176 errx(1, "multiple output formats specified");
182 setthetime(fmt, *argv, jflag);
184 } else if (fmt != NULL)
187 if (*argv && **argv == '+') {
189 errx(1, "multiple output formats specified");
193 badv = vary_apply(v, tval, <);
195 fprintf(stderr, "%s: Cannot apply date adjustment\n",
202 if (format == rfc2822_format) {
204 * When using RFC 2822 datetime format, don't honor the
207 setlocale(LC_TIME, "C");
210 strftime(buf, sizeof(buf), format, <);
211 if (Iflag && iso8601_selected > iso8601_fmts) {
212 strftime(tzbuf, sizeof(tzbuf), "%z", <);
213 memmove(&tzbuf[4], &tzbuf[3], 3);
215 strlcat(buf, tzbuf, sizeof(buf));
218 if (fflush(stdout) != 0)
223 #define ATOI2(s) ((s) += 2, ((s)[-2] - '0') * 10 + ((s)[-1] - '0'))
226 setthetime(const char *fmt, const char *p, int jflag)
234 lt = localtime(&tval);
235 t = strptime(p, fmt, lt);
237 fprintf(stderr, "Failed conversion of ``%s''"
238 " using format ``%s''\n", p, fmt);
240 } else if (*t != '\0')
241 fprintf(stderr, "Warning: Ignoring %ld extraneous"
242 " characters in date string (%s)\n",
243 (long) strlen(t), t);
245 for (t = p, dot = NULL; *t; ++t) {
248 if (*t == '.' && dot == NULL) {
255 lt = localtime(&tval);
257 if (dot != NULL) { /* .ss */
258 dot++; /* *dot++ = '\0'; */
259 if (strlen(dot) != 2)
261 lt->tm_sec = ATOI2(dot);
268 /* if p has a ".ss" field then let's pretend it's not there */
269 switch (strlen(p) - ((dot != NULL) ? 3 : 0)) {
271 lt->tm_year = ATOI2(p) * 100 - TM_YEAR_BASE;
276 lt->tm_year += ATOI2(p);
277 else { /* hack for 2000 ;-} */
278 lt->tm_year = ATOI2(p);
279 if (lt->tm_year < 69)
280 lt->tm_year += 2000 - TM_YEAR_BASE;
282 lt->tm_year += 1900 - TM_YEAR_BASE;
286 lt->tm_mon = ATOI2(p);
289 --lt->tm_mon; /* time struct is 0 - 11 */
292 lt->tm_mday = ATOI2(p);
293 if (lt->tm_mday > 31)
297 lt->tm_hour = ATOI2(p);
298 if (lt->tm_hour > 23)
302 lt->tm_min = ATOI2(p);
311 /* Let mktime() decide whether summer time is in effect. */
314 /* convert broken-down time to GMT clock time */
315 if ((tval = mktime(lt)) == -1)
316 errx(1, "nonexistent time");
319 logwtmpx("|", "date", "", 0, OLD_TIME);
322 if (settimeofday(&tv, NULL))
323 err(1, "settimeofday (timeval)");
324 logwtmpx("{", "date", "", 0, NEW_TIME);
326 if ((p = getlogin()) == NULL)
328 syslog(LOG_AUTH | LOG_NOTICE, "date set by %s", p);
335 warnx("illegal time format");
342 fprintf(stderr, "%s\n%s\n",
343 "usage: date [-jnRu] [-r seconds] [-v[+|-]val[ymwdHMS]] ... ",
345 "[-I [date | hours | minutes | seconds]]"
347 "[-f fmt date | [[[[[cc]yy]mm]dd]HH]MM[.ss]] [+format]");