]>
git.saurik.com Git - apple/shell_cmds.git/blob - date/date.c
1 /* $NetBSD: date.c,v 1.25 1998/07/28 11:41:47 mycroft 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. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by the University of
18 * California, Berkeley and its contributors.
19 * 4. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 #include <sys/cdefs.h>
39 "@(#) Copyright (c) 1985, 1987, 1988, 1993\n\
40 The Regents of the University of California. All rights reserved.\n");
45 static char sccsid
[] = "@(#)date.c 8.2 (Berkeley) 4/28/95";
47 __RCSID("$NetBSD: date.c,v 1.25 1998/07/28 11:41:47 mycroft Exp $");
51 #include <sys/param.h>
67 #include "get_compat.h"
74 int unix2003_std
= 0; /* to determine legacy vs std mode */
76 int main
__P((int, char *[]));
77 static void setthetime
__P((char *));
78 static void badformat
__P((void));
79 static void badtime
__P((void));
80 static void usage
__P((void));
90 char *format
, buf
[1024];
92 (void)setlocale(LC_ALL
, "");
94 if (compat_mode("bin/date", "unix2003")) /* Determine the STD */
100 while ((ch
= getopt(argc
, argv
, "nr:u")) != -1)
102 case 'n': /* don't set network */
105 case 'r': /* user specified seconds */
109 case 'u': /* do everything in GMT */
110 (void)setenv("TZ", "GMT0", 1);
118 if (!rflag
&& time(&tval
) == -1)
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 (void)strftime(buf
, sizeof(buf
), format
, localtime(&tval
));
138 (void)printf("%s\n", buf
);
140 /* if date/time could not be set/notified in the other hosts as
141 determined by netsetval() a return value 2 is set, which is
142 to be propogated back to shell in the legacy mode.
145 exit(0); /* set/notify time thru NTPD isn't stds */
147 exit(retval
); /* Propogate the error condition set, if any */
150 #define ATOI2(s) ((s) += 2, ((s)[-2] - '0') * 10 + ((s)[-1] - '0'))
162 char tmp1_p
[5] = ""; /* to hold ccyy and reformat */
163 char tmp2_p
[16] = ""; /* ccyyMMddhhmm.ss is 15 chars */
165 for (t
= p
, dot
= NULL
; *t
; ++t
) {
168 if (*t
== '.' && dot
== NULL
) {
175 lt
= localtime(&tval
);
177 lt
->tm_isdst
= -1; /* Divine correct DST */
179 if (dot
!= NULL
) { /* .ss */
184 lt
->tm_sec
= ATOI2(dot
);
192 switch (strlen(p
) - len
) {
195 /* The last 4 chars are ccyy;
196 reformat it to be in the first */
197 strncpy(tmp1_p
, &p
[8], 4);
199 p
[8] = '\0'; /* .ss already processed; so no harm */
205 lt
->tm_year
= ATOI2(p
) * 100 - TM_YEAR_BASE
;
210 /* The last 2 chars are yy; reformat it to be in the
211 first, only if already not done. */
212 if (tmp1_p
[0] == '\0') {
213 strncpy(tmp1_p
, &p
[8], 2);
215 p
[8] = '\0'; /* .ss done; so no harm */
220 ; /* do nothing, already reformatted */
224 lt
->tm_year
+= ATOI2(p
);
228 lt
->tm_year
= yearset
+ 2000 - TM_YEAR_BASE
;
230 lt
->tm_year
= yearset
+ 1900 - TM_YEAR_BASE
;
234 lt
->tm_mon
= ATOI2(p
);
235 --lt
->tm_mon
; /* time struct is 0 - 11 */
238 lt
->tm_mday
= ATOI2(p
);
241 lt
->tm_hour
= ATOI2(p
);
244 lt
->tm_min
= ATOI2(p
);
250 /* convert broken-down time to GMT clock time */
251 if ((tval
= mktime(lt
)) == -1)
255 if (nflag
|| netsettime(tval
)) {
256 logwtmp("|", "date", "");
259 if (settimeofday(&tv
, NULL
)) {
260 perror("date: settimeofday");
263 logwtmp("{", "date", "");
266 if ((p
= getlogin()) == NULL
)
268 syslog(LOG_AUTH
| LOG_NOTICE
, "date set by %s", p
);
274 warnx("illegal time format");
281 errx(1, "illegal time");
287 (void)fprintf(stderr
,
288 "usage: date [-nu] [-r seconds] [+format]\n");
290 (void)fprintf(stderr
, " date [-u] mmddhhmm[[cc]yy]\n");
292 (void)fprintf(stderr
, " date [[[[[cc]yy]mm]dd]hh]mm[.ss]\n");