]>
git.saurik.com Git - apple/shell_cmds.git/blob - date/date.c
23c3b8c6d069efb65b8dcf289110d1d8caaed98a
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 * 4. 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
31 static char const copyright
[] =
32 "@(#) Copyright (c) 1985, 1987, 1988, 1993\n\
33 The Regents of the University of California. All rights reserved.\n";
38 static char sccsid
[] = "@(#)date.c 8.2 (Berkeley) 4/28/95";
42 #include <sys/cdefs.h>
43 __FBSDID("$FreeBSD: src/bin/date/date.c,v 1.47 2005/01/10 08:39:21 imp Exp $");
45 #include <sys/param.h>
53 #endif /* !__APPLE__ */
62 #include <get_compat.h>
65 #define COMPAT_MODE(a,b) (1)
66 #endif /* __APPLE__ */
72 #define TM_YEAR_BASE 1900
77 int unix2003_std
= 0; /* to determine legacy vs std mode */
79 static void setthetime(const char *, const char *, int, int);
80 static void badformat(void);
81 static void usage(void);
84 main(int argc
, char *argv
[])
95 const struct vary
*badv
;
98 unix2003_std
= COMPAT_MODE("bin/date", "unix2003"); /* Determine the STD */
102 (void) setlocale(LC_TIME
, "");
103 tz
.tz_dsttime
= tz
.tz_minuteswest
= 0;
107 while ((ch
= getopt(argc
, argv
, "d:f:jnr:t:uv:")) != -1)
109 case 'd': /* daylight savings time */
110 tz
.tz_dsttime
= strtol(optarg
, &endptr
, 10) ? 1 : 0;
111 if (endptr
== optarg
|| *endptr
!= '\0')
119 jflag
= 1; /* don't set time */
121 case 'n': /* don't set network */
124 case 'r': /* user specified seconds */
126 tval
= strtoq(optarg
, &tmp
, 0);
130 case 't': /* minutes west of UTC */
131 /* error check; don't allow "PST" */
132 tz
.tz_minuteswest
= strtol(optarg
, &endptr
, 10);
133 if (endptr
== optarg
|| *endptr
!= '\0')
137 case 'u': /* do everything in UTC */
138 (void)setenv("TZ", "UTC0", 1);
141 v
= vary_append(v
, optarg
);
150 * If -d or -t, set the timezone or daylight savings time; this
151 * doesn't belong here; the kernel should not know about either.
153 if (set_timezone
&& settimeofday((struct timeval
*)NULL
, &tz
))
154 err(1, "settimeofday (timezone)");
156 if (!rflag
&& time(&tval
) == -1)
161 /* allow the operands in any order */
162 if (*argv
&& **argv
== '+') {
168 setthetime(fmt
, *argv
, jflag
, nflag
);
170 } else if (fmt
!= NULL
)
173 if (*argv
&& **argv
== '+')
178 struct tm
*ltp
= localtime(&tval
);
184 lt
= *localtime(&tval
);
186 badv
= vary_apply(v
, <
);
188 fprintf(stderr
, "%s: Cannot apply date adjustment\n",
194 (void)strftime(buf
, sizeof(buf
), format
, <
);
195 (void)printf("%s\n", buf
);
199 * If date/time could not be set/notified in the other hosts as
200 * determined by netsetval(), a return value 2 is set, which is
201 * only propagated back to shell in legacy mode.
208 #define ATOI2(s) ((s) += 2, ((s)[-2] - '0') * 10 + ((s)[-1] - '0'))
209 #define ATOI2_OFFSET(s, o) (((s)[o] - '0') * 10 + ((s)[o + 1] - '0'))
212 setthetime(const char *fmt
, const char *p
, int jflag
, int nflag
)
220 lt
= localtime(&tval
);
221 t
= strptime(p
, fmt
, lt
);
223 fprintf(stderr
, "Failed conversion of ``%s''"
224 " using format ``%s''\n", p
, fmt
);
226 } else if (*t
!= '\0')
227 fprintf(stderr
, "Warning: Ignoring %ld extraneous"
228 " characters in date string (%s)\n",
229 (long) strlen(t
), t
);
231 for (t
= p
, dot
= NULL
; *t
; ++t
) {
234 if (*t
== '.' && dot
== NULL
) {
241 lt
= localtime(&tval
);
243 if (dot
!= NULL
) { /* .ss */
244 dot
++; /* *dot++ = '\0'; */
245 if (strlen(dot
) != 2)
247 lt
->tm_sec
= ATOI2(dot
);
254 /* if p has a ".ss" field then let's pretend it's not there */
255 switch (length
= strlen(p
) - ((dot
!= NULL
) ? 3 : 0)) {
257 lt
->tm_year
= (unix2003_std
? ATOI2_OFFSET(p
, length
- 4) : ATOI2(p
)) * 100 - TM_YEAR_BASE
;
262 lt
->tm_year
+= (unix2003_std
? ATOI2_OFFSET(p
, length
- 2) : ATOI2(p
));
264 lt
->tm_year
= (unix2003_std
? ATOI2_OFFSET(p
, length
- 2) : ATOI2(p
));
265 if (lt
->tm_year
< 69) /* hack for 2000 ;-} */
266 lt
->tm_year
+= 2000 - TM_YEAR_BASE
;
268 lt
->tm_year
+= 1900 - TM_YEAR_BASE
;
272 lt
->tm_mon
= ATOI2(p
);
275 --lt
->tm_mon
; /* time struct is 0 - 11 */
278 lt
->tm_mday
= ATOI2(p
);
279 if (lt
->tm_mday
> 31)
283 lt
->tm_hour
= ATOI2(p
);
284 if (lt
->tm_hour
> 23)
288 lt
->tm_min
= ATOI2(p
);
297 /* Let mktime() decide whether summer time is in effect. */
300 /* convert broken-down time to GMT clock time */
301 if ((tval
= mktime(lt
)) == -1)
302 errx(1, "nonexistent time");
306 if (nflag
|| netsettime(tval
)) {
308 bzero(&ut
, sizeof(ut
));
309 ut
.ut_type
= OLD_TIME
;
310 (void)gettimeofday(&ut
.ut_tv
, NULL
);
312 ut
.ut_tv
.tv_sec
= tval
;
313 ut
.ut_tv
.tv_usec
= 0;
314 if (settimeofday(&ut
.ut_tv
, NULL
))
315 err(1, "settimeofday (timeval)");
316 ut
.ut_type
= NEW_TIME
;
320 if ((p
= getlogin()) == NULL
)
322 syslog(LOG_AUTH
| LOG_NOTICE
, "date set by %s", p
);
329 warnx("illegal time format");
336 (void)fprintf(stderr
, "%s\n%s\n",
337 "usage: date [-jnu] [-d dst] [-r seconds] [-t west] "
338 "[-v[+|-]val[ymwdHMS]] ... ",
340 "[-f fmt date | [[[mm]dd]HH]MM[[cc]yy][.ss]] [+format]" :
342 "[-f fmt date | [[[[[cc]yy]mm]dd]HH]MM[.ss]] [+format]");