1
static char elsieid[] = "@(#)zdump.c 8.6";
4
** This code has been made independent of the rest of the time
5
** conversion package to increase confidence in the verification it provides.
6
** You can use this code to help in verifying other implementations.
10
#include "stdio.h" /* for stdout, stderr, perror */
11
#include "string.h" /* for strcpy */
12
#include "sys/types.h" /* for time_t */
13
#include "time.h" /* for struct tm */
14
#include "stdlib.h" /* for exit, malloc, atoi */
15
#include "float.h" /* for FLT_MAX and DBL_MAX */
16
#include "ctype.h" /* for isalpha et al. */
19
#endif /* !defined isascii */
22
#define ZDUMP_LO_YEAR (-500)
23
#endif /* !defined ZDUMP_LO_YEAR */
26
#define ZDUMP_HI_YEAR 2500
27
#endif /* !defined ZDUMP_HI_YEAR */
29
#ifndef MAX_STRING_LENGTH
30
#define MAX_STRING_LENGTH 1024
31
#endif /* !defined MAX_STRING_LENGTH */
35
#endif /* !defined TRUE */
39
#endif /* !defined FALSE */
42
#define EXIT_SUCCESS 0
43
#endif /* !defined EXIT_SUCCESS */
46
#define EXIT_FAILURE 1
47
#endif /* !defined EXIT_FAILURE */
51
#endif /* !defined SECSPERMIN */
54
#define MINSPERHOUR 60
55
#endif /* !defined MINSPERHOUR */
58
#define SECSPERHOUR (SECSPERMIN * MINSPERHOUR)
59
#endif /* !defined SECSPERHOUR */
62
#define HOURSPERDAY 24
63
#endif /* !defined HOURSPERDAY */
66
#define EPOCH_YEAR 1970
67
#endif /* !defined EPOCH_YEAR */
70
#define TM_YEAR_BASE 1900
71
#endif /* !defined TM_YEAR_BASE */
74
#define DAYSPERNYEAR 365
75
#endif /* !defined DAYSPERNYEAR */
78
#define isleap(y) (((y) % 4) == 0 && (((y) % 100) != 0 || ((y) % 400) == 0))
79
#endif /* !defined isleap */
83
** See tzfile.h for details on isleap_sum.
85
#define isleap_sum(a, b) isleap((a) % 400 + (b) % 400)
86
#endif /* !defined isleap_sum */
88
#define SECSPERDAY ((long) SECSPERHOUR * HOURSPERDAY)
89
#define SECSPERNYEAR (SECSPERDAY * DAYSPERNYEAR)
90
#define SECSPERLYEAR (SECSPERNYEAR + SECSPERDAY)
93
#define HAVE_GETTEXT 0
96
#include "locale.h" /* for setlocale */
98
#endif /* HAVE_GETTEXT */
103
#else /* !defined lint */
106
#endif /* defined __GNUC__ */
107
#endif /* !defined lint */
108
#endif /* !defined GNUC_or_lint */
112
#define INITIALIZE(x) ((x) = 0)
113
#else /* !defined GNUC_or_lint */
114
#define INITIALIZE(x)
115
#endif /* !defined GNUC_or_lint */
116
#endif /* !defined INITIALIZE */
119
** For the benefit of GNU folk...
120
** `_(MSGID)' uses the current locale's message library string for MSGID.
121
** The default is to use gettext if available, and use MSGID otherwise.
126
#define _(msgid) gettext(msgid)
127
#else /* !HAVE_GETTEXT */
128
#define _(msgid) msgid
129
#endif /* !HAVE_GETTEXT */
130
#endif /* !defined _ */
133
#define TZ_DOMAIN "tz"
134
#endif /* !defined TZ_DOMAIN */
136
extern char ** environ;
137
extern int getopt(int argc, char * const argv[],
138
const char * options);
139
extern char * optarg;
141
extern char * tzname[2];
143
static time_t absolute_min_time;
144
static time_t absolute_max_time;
145
static size_t longest;
146
static char * progname;
149
static char * abbr(struct tm * tmp);
150
static void abbrok(const char * abbrp, const char * zone);
151
static long delta(struct tm * newp, struct tm * oldp);
152
static void dumptime(const struct tm * tmp);
153
static time_t hunt(char * name, time_t lot, time_t hit);
154
static void setabsolutes(void);
155
static void show(char * zone, time_t t, int v);
156
static const char * tformat(void);
157
static time_t yeartot(long y);
160
#define my_localtime localtime
161
#else /* !defined TYPECHECK */
166
register struct tm * tmp;
169
if (tp != NULL && tmp != NULL) {
175
if (t - *tp >= 1 || *tp - t >= 1) {
176
(void) fflush(stdout);
177
(void) fprintf(stderr, "\n%s: ", progname);
178
(void) fprintf(stderr, tformat(), *tp);
179
(void) fprintf(stderr, " ->");
180
(void) fprintf(stderr, " year=%d", tmp->tm_year);
181
(void) fprintf(stderr, " mon=%d", tmp->tm_mon);
182
(void) fprintf(stderr, " mday=%d", tmp->tm_mday);
183
(void) fprintf(stderr, " hour=%d", tmp->tm_hour);
184
(void) fprintf(stderr, " min=%d", tmp->tm_min);
185
(void) fprintf(stderr, " sec=%d", tmp->tm_sec);
186
(void) fprintf(stderr, " isdst=%d", tmp->tm_isdst);
187
(void) fprintf(stderr, " -> ");
188
(void) fprintf(stderr, tformat(), t);
189
(void) fprintf(stderr, "\n");
194
#endif /* !defined TYPECHECK */
198
const char * const abbrp;
199
const char * const zone;
201
register const char * cp;
208
while (isascii((unsigned char) *cp) && isalpha((unsigned char) *cp))
211
wp = _("lacks alphabetic at start");
212
else if (cp - abbrp < 3)
213
wp = _("has fewer than 3 alphabetics");
214
else if (cp - abbrp > 6)
215
wp = _("has more than 6 alphabetics");
216
if (wp == NULL && (*cp == '+' || *cp == '-')) {
218
if (isascii((unsigned char) *cp) &&
219
isdigit((unsigned char) *cp))
220
if (*cp++ == '1' && *cp >= '0' && *cp <= '4')
223
wp = _("differs from POSIX standard");
227
(void) fflush(stdout);
228
(void) fprintf(stderr,
229
_("%s: warning: zone \"%s\" abbreviation \"%s\" %s\n"),
230
progname, zone, abbrp, wp);
235
usage(const char *progname, FILE *stream, int status)
237
(void) fprintf(stream,
238
_("%s: usage is %s [ --version ] [ --help ] [ -v ] [ -c [loyear,]hiyear ] zonename ...\n\
240
For bug reporting instructions, please see:\n\
242
progname, progname, REPORT_BUGS_TO);
254
register char * cutarg;
255
register long cutloyear = ZDUMP_LO_YEAR;
256
register long cuthiyear = ZDUMP_HI_YEAR;
257
register time_t cutlotime;
258
register time_t cuthitime;
259
register char ** fakeenv;
265
register struct tm * tmp;
266
register struct tm * newtmp;
268
INITIALIZE(cutlotime);
269
INITIALIZE(cuthitime);
271
(void) setlocale(LC_ALL, "");
273
(void) bindtextdomain(TZ_DOMAIN, TZ_DOMAINDIR);
274
#endif /* defined TEXTDOMAINDIR */
275
(void) textdomain(TZ_DOMAIN);
276
#endif /* HAVE_GETTEXT */
278
for (i = 1; i < argc; ++i)
279
if (strcmp(argv[i], "--version") == 0) {
280
(void) printf("zdump %s%s\n", PKGVERSION, elsieid);
282
} else if (strcmp(argv[i], "--help") == 0) {
283
usage(progname, stdout, EXIT_SUCCESS);
287
while ((c = getopt(argc, argv, "c:v")) == 'c' || c == 'v')
290
else cutarg = optarg;
291
if ((c != EOF && c != -1) ||
292
(optind == argc - 1 && strcmp(argv[optind], "=") == 0)) {
293
usage(progname, stderr, EXIT_FAILURE);
296
if (cutarg != NULL) {
301
if (sscanf(cutarg, "%ld%c", &hi, &dummy) == 1) {
303
} else if (sscanf(cutarg, "%ld,%ld%c",
304
&lo, &hi, &dummy) == 2) {
308
(void) fprintf(stderr, _("%s: wild -c argument %s\n"),
314
cutlotime = yeartot(cutloyear);
315
cuthitime = yeartot(cuthiyear);
319
for (i = optind; i < argc; ++i)
320
if (strlen(argv[i]) > longest)
321
longest = strlen(argv[i]);
326
for (i = 0; environ[i] != NULL; ++i)
328
fakeenv = (char **) malloc((size_t) ((i + 2) *
330
if (fakeenv == NULL ||
331
(fakeenv[0] = (char *) malloc(longest + 4)) == NULL) {
332
(void) perror(progname);
336
(void) strcpy(fakeenv[to++], "TZ=");
337
for (from = 0; environ[from] != NULL; ++from)
338
if (strncmp(environ[from], "TZ=", 3) != 0)
339
fakeenv[to++] = environ[from];
343
for (i = optind; i < argc; ++i) {
344
static char buf[MAX_STRING_LENGTH];
346
(void) strcpy(&fakeenv[0][3], argv[i]);
348
show(argv[i], now, FALSE);
352
t = absolute_min_time;
353
show(argv[i], t, TRUE);
354
t += SECSPERHOUR * HOURSPERDAY;
355
show(argv[i], t, TRUE);
358
tmp = my_localtime(&t);
361
(void) strncpy(buf, abbr(&tm), (sizeof buf) - 1);
366
newt = t + SECSPERHOUR * 12;
367
if (newt >= cuthitime)
371
newtmp = localtime(&newt);
374
if ((tmp == NULL || newtmp == NULL) ? (tmp != newtmp) :
375
(delta(&newtm, &tm) != (newt - t) ||
376
newtm.tm_isdst != tm.tm_isdst ||
377
strcmp(abbr(&newtm), buf) != 0)) {
378
newt = hunt(argv[i], t, newt);
379
newtmp = localtime(&newt);
380
if (newtmp != NULL) {
391
t = absolute_max_time;
392
t -= SECSPERHOUR * HOURSPERDAY;
393
show(argv[i], t, TRUE);
394
t += SECSPERHOUR * HOURSPERDAY;
395
show(argv[i], t, TRUE);
397
if (fflush(stdout) || ferror(stdout)) {
398
(void) fprintf(stderr, "%s: ", progname);
399
(void) perror(_("Error writing to standard output"));
403
/* If exit fails to exit... */
410
if (0.5 == (time_t) 0.5) {
412
** time_t is floating.
414
if (sizeof (time_t) == sizeof (float)) {
415
absolute_min_time = (time_t) -FLT_MAX;
416
absolute_max_time = (time_t) FLT_MAX;
417
} else if (sizeof (time_t) == sizeof (double)) {
418
absolute_min_time = (time_t) -DBL_MAX;
419
absolute_max_time = (time_t) DBL_MAX;
421
(void) fprintf(stderr,
422
_("%s: use of -v on system with floating time_t other than float or double\n"),
426
} else if (0 > (time_t) -1) {
428
** time_t is signed. Assume overflow wraps around.
438
absolute_max_time = t;
440
absolute_min_time = t - 1;
441
if (t < absolute_min_time)
442
absolute_min_time = t;
445
** time_t is unsigned.
447
absolute_min_time = 0;
448
absolute_max_time = absolute_min_time - 1;
457
register long seconds;
464
seconds = isleap(myy) ? SECSPERLYEAR : SECSPERNYEAR;
466
if (t > absolute_max_time - seconds) {
467
t = absolute_max_time;
473
seconds = isleap(myy) ? SECSPERLYEAR : SECSPERNYEAR;
474
if (t < absolute_min_time + seconds) {
475
t = absolute_min_time;
485
hunt(char *name, time_t lot, time_t hit)
490
register struct tm * lotmp;
492
register struct tm * tmp;
493
char loab[MAX_STRING_LENGTH];
495
lotmp = my_localtime(&lot);
498
(void) strncpy(loab, abbr(&lotm), (sizeof loab) - 1);
501
diff = (long) (hit - lot);
510
tmp = my_localtime(&t);
513
if ((lotmp == NULL || tmp == NULL) ? (lotmp == tmp) :
514
(delta(&tm, &lotm) == (t - lot) &&
515
tm.tm_isdst == lotm.tm_isdst &&
516
strcmp(abbr(&tm), loab) == 0)) {
522
show(name, lot, TRUE);
523
show(name, hit, TRUE);
528
** Thanks to Paul Eggert for logic used in delta.
536
register long result;
539
if (newp->tm_year < oldp->tm_year)
540
return -delta(oldp, newp);
542
for (tmy = oldp->tm_year; tmy < newp->tm_year; ++tmy)
543
result += DAYSPERNYEAR + isleap_sum(tmy, TM_YEAR_BASE);
544
result += newp->tm_yday - oldp->tm_yday;
545
result *= HOURSPERDAY;
546
result += newp->tm_hour - oldp->tm_hour;
547
result *= MINSPERHOUR;
548
result += newp->tm_min - oldp->tm_min;
549
result *= SECSPERMIN;
550
result += newp->tm_sec - oldp->tm_sec;
555
show(char *zone, time_t t, int v)
557
register struct tm * tmp;
559
(void) printf("%-*s ", (int) longest, zone);
563
(void) printf(tformat(), t);
566
(void) printf(" UTC");
568
(void) printf(" = ");
570
tmp = my_localtime(&t);
573
if (*abbr(tmp) != '\0')
574
(void) printf(" %s", abbr(tmp));
576
(void) printf(" isdst=%d", tmp->tm_isdst);
578
(void) printf(" gmtoff=%ld", tmp->TM_GMTOFF);
579
#endif /* defined TM_GMTOFF */
583
if (tmp != NULL && *abbr(tmp) != '\0')
584
abbrok(abbr(tmp), zone);
591
register char * result;
594
if (tmp->tm_isdst != 0 && tmp->tm_isdst != 1)
596
result = tzname[tmp->tm_isdst];
597
return (result == NULL) ? &nada : result;
601
** The code below can fail on certain theoretical systems;
602
** it works on all known real-world systems as of 2004-12-30.
608
if (0.5 == (time_t) 0.5) { /* floating */
609
if (sizeof (time_t) > sizeof (double))
613
if (0 > (time_t) -1) { /* signed */
614
if (sizeof (time_t) > sizeof (long))
616
if (sizeof (time_t) > sizeof (int))
620
if (sizeof (time_t) > sizeof (unsigned long))
622
if (sizeof (time_t) > sizeof (unsigned int))
629
register const struct tm * timeptr;
631
static const char wday_name[][3] = {
632
"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
634
static const char mon_name[][3] = {
635
"Jan", "Feb", "Mar", "Apr", "May", "Jun",
636
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
638
register const char * wn;
639
register const char * mn;
643
if (timeptr == NULL) {
644
(void) printf("NULL");
648
** The packaged versions of localtime and gmtime never put out-of-range
649
** values in tm_wday or tm_mon, but since this code might be compiled
650
** with other (perhaps experimental) versions, paranoia is in order.
652
if (timeptr->tm_wday < 0 || timeptr->tm_wday >=
653
(int) (sizeof wday_name / sizeof wday_name[0]))
655
else wn = wday_name[timeptr->tm_wday];
656
if (timeptr->tm_mon < 0 || timeptr->tm_mon >=
657
(int) (sizeof mon_name / sizeof mon_name[0]))
659
else mn = mon_name[timeptr->tm_mon];
660
(void) printf("%.3s %.3s%3d %.2d:%.2d:%.2d ",
662
timeptr->tm_mday, timeptr->tm_hour,
663
timeptr->tm_min, timeptr->tm_sec);
665
trail = timeptr->tm_year % DIVISOR + TM_YEAR_BASE % DIVISOR;
666
lead = timeptr->tm_year / DIVISOR + TM_YEAR_BASE / DIVISOR +
669
if (trail < 0 && lead > 0) {
672
} else if (lead < 0 && trail > 0) {
677
(void) printf("%d", trail);
678
else (void) printf("%d%d", lead, ((trail < 0) ? -trail : trail));