]>
git.saurik.com Git - apple/shell_cmds.git/blob - date/date.c
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$");
45 #include <sys/param.h>
60 #include <get_compat.h>
62 #define COMPAT_MODE(a,b) (1)
63 #endif /* __APPLE__ */
69 #define TM_YEAR_BASE 1900
73 #define st_mtim st_mtimespec
78 static int unix2003_std
; /* to determine legacy vs std mode */
80 static void setthetime(const char *, const char *, int, int);
81 static void badformat(void);
82 static void usage(void);
84 static const char *rfc2822_format
= "%a, %d %b %Y %T %z";
87 main(int argc
, char *argv
[])
91 int jflag
, nflag
, Rflag
;
98 const struct vary
*badv
;
102 unix2003_std
= COMPAT_MODE("bin/date", "unix2003"); /* Determine the STD */
106 (void) setlocale(LC_TIME
, "");
107 tz
.tz_dsttime
= tz
.tz_minuteswest
= 0;
109 jflag
= nflag
= Rflag
= 0;
111 while ((ch
= getopt(argc
, argv
, "d:f:jnRr:t:uv:")) != -1)
113 case 'd': /* daylight savings time */
114 tz
.tz_dsttime
= strtol(optarg
, &endptr
, 10) ? 1 : 0;
115 if (endptr
== optarg
|| *endptr
!= '\0')
123 jflag
= 1; /* don't set time */
125 case 'n': /* don't set network */
128 case 'R': /* RFC 2822 datetime format */
131 case 'r': /* user specified seconds */
133 tval
= strtoq(optarg
, &tmp
, 0);
135 if (stat(optarg
, &sb
) == 0)
136 tval
= sb
.st_mtim
.tv_sec
;
141 case 't': /* minutes west of UTC */
142 /* error check; don't allow "PST" */
143 tz
.tz_minuteswest
= strtol(optarg
, &endptr
, 10);
144 if (endptr
== optarg
|| *endptr
!= '\0')
148 case 'u': /* do everything in UTC */
149 (void)setenv("TZ", "UTC0", 1);
152 v
= vary_append(v
, optarg
);
161 * If -d or -t, set the timezone or daylight savings time; this
162 * doesn't belong here; the kernel should not know about either.
164 if (set_timezone
&& settimeofday(NULL
, &tz
) != 0)
165 err(1, "settimeofday (timezone)");
167 if (!rflag
&& time(&tval
) == -1)
173 format
= rfc2822_format
;
175 /* allow the operands in any order */
176 if (*argv
&& **argv
== '+') {
182 setthetime(fmt
, *argv
, jflag
, nflag
);
184 } else if (fmt
!= NULL
)
187 if (*argv
&& **argv
== '+')
192 struct tm
*ltp
= localtime(&tval
);
198 lt
= *localtime(&tval
);
200 badv
= vary_apply(v
, <
);
202 fprintf(stderr
, "%s: Cannot apply date adjustment\n",
209 if (format
== rfc2822_format
)
211 * When using RFC 2822 datetime format, don't honor the
214 setlocale(LC_TIME
, "C");
216 (void)strftime(buf
, sizeof(buf
), format
, <
);
217 (void)printf("%s\n", buf
);
221 * If date/time could not be set/notified in the other hosts as
222 * determined by netsetval(), a return value 2 is set, which is
223 * only propagated back to shell in legacy mode.
230 #define ATOI2(s) ((s) += 2, ((s)[-2] - '0') * 10 + ((s)[-1] - '0'))
231 #define ATOI2_OFFSET(s, o) (((s)[o] - '0') * 10 + ((s)[o + 1] - '0'))
234 setthetime(const char *fmt
, const char *p
, int jflag
, int nflag
)
243 lt
= localtime(&tval
);
244 lt
->tm_isdst
= -1; /* divine correct DST */
247 t
= strptime(p
, fmt
, lt
);
249 fprintf(stderr
, "Failed conversion of ``%s''"
250 " using format ``%s''\n", p
, fmt
);
252 } else if (*t
!= '\0')
253 fprintf(stderr
, "Warning: Ignoring %ld extraneous"
254 " characters in date string (%s)\n",
255 (long) strlen(t
), t
);
257 for (t
= p
, dot
= NULL
; *t
; ++t
) {
260 if (*t
== '.' && dot
== NULL
) {
267 if (dot
!= NULL
) { /* .ss */
268 dot
++; /* *dot++ = '\0'; */
269 if (strlen(dot
) != 2)
271 lt
->tm_sec
= ATOI2(dot
);
278 /* if p has a ".ss" field then let's pretend it's not there */
279 switch (length
= strlen(p
) - ((dot
!= NULL
) ? 3 : 0)) {
281 lt
->tm_year
= (unix2003_std
? ATOI2_OFFSET(p
, length
- 4) : ATOI2(p
)) * 100 - TM_YEAR_BASE
;
286 lt
->tm_year
+= (unix2003_std
? ATOI2_OFFSET(p
, length
- 2) : ATOI2(p
));
288 lt
->tm_year
= (unix2003_std
? ATOI2_OFFSET(p
, length
- 2) : ATOI2(p
));
289 if (lt
->tm_year
< 69) /* hack for 2000 ;-} */
290 lt
->tm_year
+= 2000 - TM_YEAR_BASE
;
292 lt
->tm_year
+= 1900 - TM_YEAR_BASE
;
296 lt
->tm_mon
= ATOI2(p
);
299 --lt
->tm_mon
; /* time struct is 0 - 11 */
302 lt
->tm_mday
= ATOI2(p
);
303 if (lt
->tm_mday
> 31)
307 lt
->tm_hour
= ATOI2(p
);
308 if (lt
->tm_hour
> 23)
312 lt
->tm_min
= ATOI2(p
);
321 /* convert broken-down time to GMT clock time */
322 if ((tval
= mktime(lt
)) == -1)
323 errx(1, "nonexistent time");
327 if (nflag
|| netsettime(tval
)) {
328 utx
.ut_type
= OLD_TIME
;
329 (void)gettimeofday(&utx
.ut_tv
, NULL
);
333 if (settimeofday(&tv
, NULL
) != 0)
334 err(1, "settimeofday (timeval)");
335 utx
.ut_type
= NEW_TIME
;
336 (void)gettimeofday(&utx
.ut_tv
, NULL
);
340 if ((p
= getlogin()) == NULL
)
342 syslog(LOG_AUTH
| LOG_NOTICE
, "date set by %s", p
);
349 warnx("illegal time format");
356 (void)fprintf(stderr
, "%s\n%s\n",
357 "usage: date [-jnRu] [-d dst] [-r seconds] [-t west] "
358 "[-v[+|-]val[ymwdHMS]] ... ",
361 "[-f fmt date | [[[mm]dd]HH]MM[[cc]yy][.ss]] [+format]" :
363 "[-f fmt date | [[[[[cc]yy]mm]dd]HH]MM[.ss]] [+format]");