1 /* $NetBSD: date.c,v 1.51 2008/02/24 04:49:45 dholland Exp $ */
4 * Copyright (c) 1985, 1987, 1988, 1993
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 #include <sys/cdefs.h>
35 "@(#) Copyright (c) 1985, 1987, 1988, 1993\
36 The Regents of the University of California. All rights reserved.");
41 static char sccsid
[] = "@(#)date.c 8.2 (Berkeley) 4/28/95";
43 __RCSID("$NetBSD: date.c,v 1.51 2008/02/24 04:49:45 dholland Exp $");
47 #include <sys/param.h>
66 static int aflag
, jflag
, rflag
, nflag
;
69 static void badformat(void);
70 static void badtime(void);
71 static void badvalue(const char *);
72 static void setthetime(const char *);
73 static void usage(void);
76 main(int argc
, char *argv
[])
84 (void)setlocale(LC_ALL
, "");
86 while ((ch
= getopt(argc
, argv
, "ad:jnr:u")) != -1) {
88 case 'a': /* adjust time slowly */
94 tval
= parsedate(optarg
, NULL
, NULL
);
96 errx(1, "Cannot parse `%s'", optarg
);
98 case 'j': /* don't set time */
101 case 'n': /* don't set network */
104 case 'r': /* user specified seconds */
106 tval
= strtoll(optarg
, NULL
, 0);
108 case 'u': /* do everything in UTC */
109 (void)putenv("TZ=UTC0");
118 if (!rflag
&& time(&tval
) == -1)
119 err(EXIT_FAILURE
, "time");
121 format
= "%a %b %e %H:%M:%S %Z %Y";
123 /* allow the operands in any order */
124 if (*argv
&& **argv
== '+') {
134 if (*argv
&& **argv
== '+')
137 if ((buf
= malloc(bufsiz
= 1024)) == NULL
)
139 while (strftime(buf
, bufsiz
, format
, localtime(&tval
)) == 0)
140 if ((buf
= realloc(buf
, bufsiz
<<= 1)) == NULL
)
142 (void)printf("%s\n", buf
);
146 err(1, "Cannot allocate format buffer");
152 warnx("illegal time format");
159 errx(EXIT_FAILURE
, "illegal time");
164 badvalue(const char *param
)
166 warnx("invalid %s supplied", param
);
170 #define ATOI2(s) ((s) += 2, ((s)[-2] - '0') * 10 + ((s)[-1] - '0'))
173 setthetime(const char *p
)
182 for (t
= p
, dot
= NULL
; *t
; ++t
) {
183 if (isdigit((unsigned char)*t
))
185 if (*t
== '.' && dot
== NULL
) {
192 lt
= localtime(&tval
);
194 lt
->tm_isdst
= -1; /* Divine correct DST */
196 if (dot
!= NULL
) { /* .ss */
201 lt
->tm_sec
= ATOI2(dot
);
210 switch (strlen(p
) - len
) {
212 lt
->tm_year
= ATOI2(p
) * 100 - TM_YEAR_BASE
;
219 lt
->tm_year
+= ATOI2(p
);
223 lt
->tm_year
= yearset
+ 2000 - TM_YEAR_BASE
;
225 lt
->tm_year
= yearset
+ 1900 - TM_YEAR_BASE
;
229 lt
->tm_mon
= ATOI2(p
);
230 if (lt
->tm_mon
> 12 || lt
->tm_mon
== 0)
232 --lt
->tm_mon
; /* time struct is 0 - 11 */
235 lt
->tm_mday
= ATOI2(p
);
236 switch (lt
->tm_mon
) {
244 if (lt
->tm_mday
> 31 || lt
->tm_mday
== 0)
245 badvalue("day of month");
251 if (lt
->tm_mday
> 30 || lt
->tm_mday
== 0)
252 badvalue("day of month");
255 if (lt
->tm_mday
> 29 || lt
->tm_mday
== 0 ||
256 (lt
->tm_mday
== 29 &&
257 !isleap(lt
->tm_year
+ TM_YEAR_BASE
)))
258 badvalue("day of month");
266 lt
->tm_hour
= ATOI2(p
);
267 if (lt
->tm_hour
> 23)
271 lt
->tm_min
= ATOI2(p
);
275 case 0: /* was just .sss */
283 /* convert broken-down time to UTC clock time */
284 if ((new_time
= mktime(lt
)) == -1)
287 /* if jflag is set, don't actually change the time, just return */
294 if (nflag
|| netsettime(new_time
)) {
295 logwtmp("|", "date", "");
297 tv
.tv_sec
= new_time
- tval
;
299 if (adjtime(&tv
, NULL
))
300 err(EXIT_FAILURE
, "adjtime");
305 if (settimeofday(&tv
, NULL
))
306 err(EXIT_FAILURE
, "settimeofday");
308 logwtmp("{", "date", "");
311 if ((p
= getlogin()) == NULL
)
313 syslog(LOG_AUTH
| LOG_NOTICE
, "date set by %s", p
);
319 (void)fprintf(stderr
,
320 "usage: %s [-ajnu] [-d date] [-r seconds] [+format]", getprogname());
321 (void)fprintf(stderr
, " [[[[[[CC]yy]mm]dd]HH]MM[.SS]]\n");