Merge commit 'b1e7e97d3b60469b243b3b2e22c7d8cbd11c7c90'
[unleashed.git] / usr / src / cmd / date / date.c
blob55e9110d5bc6803e422e377ca5823f7892761c9f
1 /*
2 * CDDL HEADER START
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License, Version 1.0 only
6 * (the "License"). You may not use this file except in compliance
7 * with the License.
9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10 * or http://www.opensolaris.org/os/licensing.
11 * See the License for the specific language governing permissions
12 * and limitations under the License.
14 * When distributing Covered Code, include this CDDL HEADER in each
15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16 * If applicable, add the following below this CDDL HEADER, with the
17 * fields enclosed by brackets "[]" replaced with your own identifying
18 * information: Portions Copyright [yyyy] [name of copyright owner]
20 * CDDL HEADER END
23 * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
27 * Copyright 2012 Nexenta Systems, Inc. All rights reserved.
30 * Copyright (c) 2017, Joyent, Inc.
32 /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
33 /* All Rights Reserved */
36 * University Copyright- Copyright (c) 1982, 1986, 1988
37 * The Regents of the University of California
38 * All Rights Reserved
40 * University Acknowledgment- Portions of this document are derived from
41 * software developed by the University of California, Berkeley, and its
42 * contributors.
46 * date - with format capabilities and international flair
49 #include <locale.h>
50 #include <fcntl.h>
51 #include <langinfo.h>
52 #include <stdio.h>
53 #include <stdlib.h>
54 #include <string.h>
55 #include <time.h>
56 #include <unistd.h>
57 #include <sys/time.h>
58 #include <sys/types.h>
59 #include <sys/stat.h>
60 #include <ctype.h>
61 #include <errno.h>
62 #include <utmpx.h>
63 #include <tzfile.h>
65 #define year_size(A) ((isleap(A)) ? 366 : 365)
66 static char buf[BUFSIZ];
67 static time_t clock_val;
68 static short month_size[12] =
69 { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
70 static struct utmpx wtmpx[2] = {
71 {"", "", OTIME_MSG, 0, OLD_TIME, 0, 0, 0},
72 {"", "", NTIME_MSG, 0, NEW_TIME, 0, 0, 0}
74 static char *usage =
75 "usage:\tdate [-u] mmddHHMM[[cc]yy][.SS]\n"
76 "\tdate [-Ru] [-r seconds | filename] [+format]\n"
77 "\tdate -a [-]sss[.fff]\n";
78 static int uflag = 0;
79 static int Rflag = 0;
80 static int rflag = 0;
82 static int get_adj(char *, struct timeval *);
83 static int setdate(struct tm *, char *);
84 static void fmt_extensions(char *, size_t,
85 const char *, const struct timespec *);
87 int
88 main(int argc, char **argv)
90 struct tm *tp, tm;
91 struct timeval tv;
92 char *fmt, *eptr;
93 char fmtbuf[BUFSIZ];
94 int c, aflag = 0, illflag = 0;
95 struct timespec ts;
97 (void) setlocale(LC_ALL, "");
99 #if !defined(TEXT_DOMAIN)
100 #define TEXT_DOMAIN "SYS_TEST"
101 #endif
102 (void) textdomain(TEXT_DOMAIN);
104 while ((c = getopt(argc, argv, "a:uRr:")) != EOF)
105 switch (c) {
106 case 'a':
107 aflag++;
108 if (get_adj(optarg, &tv) < 0) {
109 (void) fprintf(stderr,
110 gettext("date: invalid argument -- %s\n"),
111 optarg);
112 illflag++;
114 break;
115 case 'u':
116 uflag++;
117 break;
118 case 'R':
119 Rflag++;
120 break;
121 case 'r':
124 * BSD originally used -r to specify a unix time. GNU
125 * used -r to specify a reference to a file. Now, like
126 * some BSDs we attempt to parse the time. If we can,
127 * then we use that, otherwise we fall back and treat it
128 * like GNU.
130 rflag++;
131 errno = 0;
132 ts.tv_sec = strtol(optarg, &eptr, 0);
133 if (errno == EINVAL || *eptr != '\0') {
134 struct stat st;
135 if (stat(optarg, &st) == 0) {
136 ts.tv_sec = st.st_mtime;
137 } else {
138 (void) fprintf(stderr,
139 gettext("date: failed to get stat "
140 "information about %s: %s\n"),
141 optarg, strerror(errno));
142 exit(1);
144 } else if (errno != 0) {
145 (void) fprintf(stderr,
146 gettext("date: failed to parse -r "
147 "argument: %s\n"), optarg);
148 exit(1);
150 break;
151 default:
152 illflag++;
155 argc -= optind;
156 argv = &argv[optind];
158 /* -a is mutually exclusive with -u, -R, and -r */
159 if (uflag && aflag)
160 illflag++;
161 if (Rflag && aflag)
162 illflag++;
163 if (rflag && aflag)
164 illflag++;
166 if (illflag) {
167 (void) fprintf(stderr, gettext(usage));
168 exit(1);
171 if (rflag == 0) {
172 if (clock_gettime(CLOCK_REALTIME, &ts) != 0) {
173 perror(gettext("date: Failed to obtain system time"));
174 exit(1);
177 clock_val = ts.tv_sec;
179 if (aflag) {
180 if (adjtime(&tv, 0) < 0) {
181 perror(gettext("date: Failed to adjust date"));
182 exit(1);
184 exit(0);
187 if (argc > 0) {
188 if (*argv[0] == '+')
189 fmt = &argv[0][1];
190 else {
191 if (setdate(localtime(&clock_val), argv[0])) {
192 (void) fprintf(stderr, gettext(usage));
193 exit(1);
195 fmt = nl_langinfo(_DATE_FMT);
197 } else if (Rflag) {
198 fmt = "%a, %d %h %Y %H:%M:%S %z";
199 } else
200 fmt = nl_langinfo(_DATE_FMT);
202 fmt_extensions(fmtbuf, sizeof (fmtbuf), fmt, &ts);
204 if (uflag) {
205 (void) putenv("TZ=GMT0");
206 tzset();
207 tp = gmtime(&clock_val);
208 } else
209 tp = localtime(&clock_val);
210 (void) memcpy(&tm, tp, sizeof (struct tm));
211 (void) strftime(buf, BUFSIZ, fmtbuf, &tm);
213 (void) puts(buf);
215 return (0);
219 setdate(struct tm *current_date, char *date)
221 int i;
222 int mm;
223 int hh;
224 int min;
225 int sec = 0;
226 char *secptr;
227 int yy;
228 int dd = 0;
229 int minidx = 6;
230 int len;
231 int dd_check;
233 /* Parse date string */
234 if ((secptr = strchr(date, '.')) != NULL && strlen(&secptr[1]) == 2 &&
235 isdigit(secptr[1]) && isdigit(secptr[2]) &&
236 (sec = atoi(&secptr[1])) >= 0 && sec < 60)
237 secptr[0] = '\0'; /* eat decimal point only on success */
239 len = strlen(date);
241 for (i = 0; i < len; i++) {
242 if (!isdigit(date[i])) {
243 (void) fprintf(stderr,
244 gettext("date: bad conversion\n"));
245 exit(1);
248 switch (strlen(date)) {
249 case 12:
250 yy = atoi(&date[8]);
251 date[8] = '\0';
252 break;
253 case 10:
255 * The YY format has the following representation:
256 * 00-68 = 2000 thru 2068
257 * 69-99 = 1969 thru 1999
259 if (atoi(&date[8]) <= 68) {
260 yy = 1900 + (atoi(&date[8]) + 100);
261 } else {
262 yy = 1900 + atoi(&date[8]);
264 date[8] = '\0';
265 break;
266 case 8:
267 yy = 1900 + current_date->tm_year;
268 break;
269 case 4:
270 yy = 1900 + current_date->tm_year;
271 mm = current_date->tm_mon + 1; /* tm_mon goes from 1 to 11 */
272 dd = current_date->tm_mday;
273 minidx = 2;
274 break;
275 default:
276 (void) fprintf(stderr, gettext("date: bad conversion\n"));
277 return (1);
280 min = atoi(&date[minidx]);
281 date[minidx] = '\0';
282 hh = atoi(&date[minidx-2]);
283 date[minidx-2] = '\0';
285 if (!dd) {
287 * if dd is 0 (not between 1 and 31), then
288 * read the value supplied by the user.
290 dd = atoi(&date[2]);
291 date[2] = '\0';
292 mm = atoi(&date[0]);
295 if (hh == 24)
296 hh = 0, dd++;
298 /* Validate date elements */
299 dd_check = 0;
300 if (mm >= 1 && mm <= 12) {
301 dd_check = month_size[mm - 1]; /* get days in this month */
302 if (mm == 2 && isleap(yy)) /* adjust for leap year */
303 dd_check++;
305 if (!((mm >= 1 && mm <= 12) && (dd >= 1 && dd <= dd_check) &&
306 (hh >= 0 && hh <= 23) && (min >= 0 && min <= 59))) {
307 (void) fprintf(stderr, gettext("date: bad conversion\n"));
308 return (1);
311 /* Build date and time number */
312 for (clock_val = 0, i = 1970; i < yy; i++)
313 clock_val += year_size(i);
314 /* Adjust for leap year */
315 if (isleap(yy) && mm >= 3)
316 clock_val += 1;
317 /* Adjust for different month lengths */
318 while (--mm)
319 clock_val += (time_t)month_size[mm - 1];
320 /* Load up the rest */
321 clock_val += (time_t)(dd - 1);
322 clock_val *= 24;
323 clock_val += (time_t)hh;
324 clock_val *= 60;
325 clock_val += (time_t)min;
326 clock_val *= 60;
327 clock_val += sec;
329 if (!uflag) {
330 /* convert to GMT assuming standard time */
331 /* correction is made in localtime(3C) */
334 * call localtime to set up "timezone" variable applicable
335 * for clock_val time, to support Olson timezones which
336 * can allow timezone rules to change.
338 (void) localtime(&clock_val);
340 clock_val += (time_t)timezone;
342 /* correct if daylight savings time in effect */
344 if (localtime(&clock_val)->tm_isdst)
345 clock_val = clock_val - (time_t)(timezone - altzone);
348 (void) time(&wtmpx[0].ut_xtime);
349 if (stime(&clock_val) < 0) {
350 perror("date");
351 return (1);
353 #if defined(i386)
354 /* correct the kernel's "gmt_lag" and the PC's RTC */
355 (void) system("/usr/sbin/rtc -c > /dev/null 2>&1");
356 #endif
357 (void) time(&wtmpx[1].ut_xtime);
358 (void) pututxline(&wtmpx[0]);
359 (void) pututxline(&wtmpx[1]);
360 (void) updwtmpx(WTMPX_FILE, &wtmpx[0]);
361 (void) updwtmpx(WTMPX_FILE, &wtmpx[1]);
362 return (0);
366 get_adj(char *cp, struct timeval *tp)
368 register int mult;
369 int sign;
371 /* arg must be [-]sss[.fff] */
373 tp->tv_sec = tp->tv_usec = 0;
374 if (*cp == '-') {
375 sign = -1;
376 cp++;
377 } else {
378 sign = 1;
381 while (*cp >= '0' && *cp <= '9') {
382 tp->tv_sec *= 10;
383 tp->tv_sec += *cp++ - '0';
385 if (*cp == '.') {
386 cp++;
387 mult = 100000;
388 while (*cp >= '0' && *cp <= '9') {
389 tp->tv_usec += (*cp++ - '0') * mult;
390 mult /= 10;
394 * if there's anything left in the string,
395 * the input was invalid.
397 if (*cp) {
398 return (-1);
399 } else {
400 tp->tv_sec *= sign;
401 tp->tv_usec *= sign;
402 return (0);
407 * Extensions that cannot be interpreted by strftime are interpreted here.
409 void
410 fmt_extensions(char *fmtbuf, size_t len,
411 const char *fmt, const struct timespec *tsp)
413 const char *p;
414 char *q;
416 for (p = fmt, q = fmtbuf; *p != '\0' && q < fmtbuf + len; ++p) {
417 if (*p == '%') {
418 switch (*(p + 1)) {
419 case 'N':
420 ++p;
421 q += snprintf(q, len - (q - fmtbuf),
422 "%09lu", tsp->tv_nsec);
423 continue;
426 *q++ = *p;
429 if (q < fmtbuf + len)
430 *q = '\0';
431 else
432 fmtbuf[len - 1] = '\0';