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