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 * @(#) Copyright (c) 1985, 1987, 1988, 1993 The Regents of the University of California. All rights reserved.
30 * @(#)date.c 8.2 (Berkeley) 4/28/95
31 * $FreeBSD: src/bin/date/date.c,v 1.47 2005/01/10 08:39:21 imp Exp $
34 #include <sys/param.h>
51 #define TM_YEAR_BASE 1900
56 static void setthetime(const char *, const char *, int);
57 static void badformat(void);
58 static void usage(void);
60 static const char *rfc2822_format
="%a, %d %b %Y %T %z";
63 main(int argc
, char **argv
)
72 const struct vary
*badv
;
77 setlocale(LC_TIME
, "");
80 while ((ch
= getopt(argc
, argv
, "f:jnRr:uv:")) != -1)
86 jflag
= 1; /* don't set time */
88 case 'n': /* don't set network */
91 Rflag
= 1; /*RFC 2822 datetime format */
93 case 'r': /* user specified seconds */
95 tval
= strtoll(optarg
, &tmp
, 0);
99 case 'u': /* do everything in UTC */
100 if (setenv("TZ", "UTC0", 1) != 0)
101 err(1, "setenv: cannot set TZ=UTC0");
104 v
= vary_append(v
, optarg
);
112 if (!rflag
&& time(&tval
) == -1)
118 format
= rfc2822_format
;
120 /* allow the operands in any order */
121 if (*argv
&& **argv
== '+') {
127 setthetime(fmt
, *argv
, jflag
);
129 } else if (fmt
!= NULL
)
132 if (*argv
&& **argv
== '+')
135 lt
= *localtime(&tval
);
136 badv
= vary_apply(v
, <
);
138 fprintf(stderr
, "%s: Cannot apply date adjustment\n",
145 if (format
== rfc2822_format
) {
147 * When using RFC 2822 datetime format, don't honor the
150 setlocale(LC_TIME
, "C");
153 strftime(buf
, sizeof(buf
), format
, <
);
155 if (fflush(stdout
) != 0)
160 #define ATOI2(s) ((s) += 2, ((s)[-2] - '0') * 10 + ((s)[-1] - '0'))
163 setthetime(const char *fmt
, const char *p
, int jflag
)
171 lt
= localtime(&tval
);
172 t
= strptime(p
, fmt
, lt
);
174 fprintf(stderr
, "Failed conversion of ``%s''"
175 " using format ``%s''\n", p
, fmt
);
177 } else if (*t
!= '\0')
178 fprintf(stderr
, "Warning: Ignoring %ld extraneous"
179 " characters in date string (%s)\n",
180 (long) strlen(t
), t
);
182 for (t
= p
, dot
= NULL
; *t
; ++t
) {
185 if (*t
== '.' && dot
== NULL
) {
192 lt
= localtime(&tval
);
194 if (dot
!= NULL
) { /* .ss */
195 dot
++; /* *dot++ = '\0'; */
196 if (strlen(dot
) != 2)
198 lt
->tm_sec
= ATOI2(dot
);
205 /* if p has a ".ss" field then let's pretend it's not there */
206 switch (strlen(p
) - ((dot
!= NULL
) ? 3 : 0)) {
208 lt
->tm_year
= ATOI2(p
) * 100 - TM_YEAR_BASE
;
213 lt
->tm_year
+= ATOI2(p
);
214 else { /* hack for 2000 ;-} */
215 lt
->tm_year
= ATOI2(p
);
216 if (lt
->tm_year
< 69)
217 lt
->tm_year
+= 2000 - TM_YEAR_BASE
;
219 lt
->tm_year
+= 1900 - TM_YEAR_BASE
;
223 lt
->tm_mon
= ATOI2(p
);
226 --lt
->tm_mon
; /* time struct is 0 - 11 */
229 lt
->tm_mday
= ATOI2(p
);
230 if (lt
->tm_mday
> 31)
234 lt
->tm_hour
= ATOI2(p
);
235 if (lt
->tm_hour
> 23)
239 lt
->tm_min
= ATOI2(p
);
248 /* Let mktime() decide whether summer time is in effect. */
251 /* convert broken-down time to GMT clock time */
252 if ((tval
= mktime(lt
)) == -1)
253 errx(1, "nonexistent time");
256 logwtmpx("|", "date", "", 0, OLD_TIME
);
259 if (settimeofday(&tv
, NULL
))
260 err(1, "settimeofday (timeval)");
261 logwtmpx("{", "date", "", 0, NEW_TIME
);
263 if ((p
= getlogin()) == NULL
)
265 syslog(LOG_AUTH
| LOG_NOTICE
, "date set by %s", p
);
272 warnx("illegal time format");
279 fprintf(stderr
, "%s\n%s\n",
280 "usage: date [-jnRu] [-r seconds] [-v[+|-]val[ymwdHMS]] ... ",
282 "[-f fmt date | [[[[[cc]yy]mm]dd]HH]MM[.ss]] [+format]");