1 /** $MirOS: src/lib/libc/time/zdump.c,v 1.8 2008/11/08 23:03:56 tg Exp $ */
2 /* $OpenBSD: zdump.c,v 1.17 2005/08/08 08:05:38 espie Exp $ */
3
4 /*-
5 * Copyright (c) 2005
6 * Thorsten "mirabilos" Glaser <tg@mirbsd.org>
7 * Based upon work placed in the public domain Feb 14, 2003 by
8 * Arthur David Olson (arthur_david_olson@nih.gov)
9 *
10 * Licensee is hereby permitted to deal in this work without restric-
11 * tion, including unlimited rights to use, publicly perform, modify,
12 * merge, distribute, sell, give away or sublicence, provided all co-
13 * pyright notices above, these terms and the disclaimer are retained
14 * in all redistributions or reproduced in accompanying documentation
15 * or other materials provided with binary redistributions.
16 *
17 * Licensor offers the work "AS IS" and WITHOUT WARRANTY of any kind,
18 * express, or implied, to the maximum extent permitted by applicable
19 * law, without malicious intent or gross negligence; in no event may
20 * licensor, an author or contributor be held liable for any indirect
21 * or other damage, or direct damage except proven a consequence of a
22 * direct error of said person and intended use of this work, loss or
23 * other issues arising in any way out of its use, even if advised of
24 * the possibility of such damage or existence of a defect.
25 */
26
27 /*
28 ** This code has been made independent of the rest of the time
29 ** conversion package to increase confidence in the verification it provides.
30 ** You can use this code to help in verifying other implementations.
31 */
32
33 #include "stdio.h" /* for stdout, stderr, perror */
34 #include "string.h" /* for strlcpy */
35 #include "ctype.h" /* for isascii, isalpha, isdigit */
36 #include "sys/types.h" /* for time_t */
37 #include "time.h" /* for struct tm */
38 #include "stdlib.h" /* for exit, malloc, atoi */
39 #include "float.h" /* for FLT_MAX and DBL_MAX */
40
41 #ifndef ZDUMP_LO_YEAR
42 #define ZDUMP_LO_YEAR (-500)
43 #endif /* !defined ZDUMP_LO_YEAR */
44
45 #ifndef ZDUMP_HI_YEAR
46 #define ZDUMP_HI_YEAR 2500
47 #endif /* !defined ZDUMP_HI_YEAR */
48
49 __RCSID("$MirOS: src/lib/libc/time/zdump.c,v 1.8 2008/11/08 23:03:56 tg Exp $");
50
51 #ifndef MAX_STRING_LENGTH
52 #define MAX_STRING_LENGTH 1024
53 #endif /* !defined MAX_STRING_LENGTH */
54
55 #ifndef TRUE
56 #define TRUE 1
57 #endif /* !defined TRUE */
58
59 #ifndef FALSE
60 #define FALSE 0
61 #endif /* !defined FALSE */
62
63 #ifndef EXIT_SUCCESS
64 #define EXIT_SUCCESS 0
65 #endif /* !defined EXIT_SUCCESS */
66
67 #ifndef EXIT_FAILURE
68 #define EXIT_FAILURE 1
69 #endif /* !defined EXIT_FAILURE */
70
71 #ifndef SECSPERMIN
72 #define SECSPERMIN 60
73 #endif /* !defined SECSPERMIN */
74
75 #ifndef MINSPERHOUR
76 #define MINSPERHOUR 60
77 #endif /* !defined MINSPERHOUR */
78
79 #ifndef SECSPERHOUR
80 #define SECSPERHOUR (SECSPERMIN * MINSPERHOUR)
81 #endif /* !defined SECSPERHOUR */
82
83 #ifndef HOURSPERDAY
84 #define HOURSPERDAY 24
85 #endif /* !defined HOURSPERDAY */
86
87 #ifndef EPOCH_YEAR
88 #define EPOCH_YEAR 1970
89 #endif /* !defined EPOCH_YEAR */
90
91 #ifndef TM_YEAR_BASE
92 #define TM_YEAR_BASE 1900
93 #endif /* !defined TM_YEAR_BASE */
94
95 #ifndef DAYSPERNYEAR
96 #define DAYSPERNYEAR 365
97 #endif /* !defined DAYSPERNYEAR */
98
99 #ifndef isleap
100 #define isleap(y) (((y) % 4) == 0 && (((y) % 100) != 0 || ((y) % 400) == 0))
101 #endif /* !defined isleap */
102
103 #ifndef isleap_sum
104 /*
105 ** See tzfile.h for details on isleap_sum.
106 */
107 #define isleap_sum(a, b) isleap((a) % 400 + (b) % 400)
108 #endif /* !defined isleap_sum */
109
110 #define SECSPERDAY ((long) SECSPERHOUR * HOURSPERDAY)
111 #define SECSPERNYEAR (SECSPERDAY * DAYSPERNYEAR)
112 #define SECSPERLYEAR (SECSPERNYEAR + SECSPERDAY)
113
114 #if HAVE_GETTEXT
115 #include "locale.h" /* for setlocale */
116 #include "libintl.h"
117 #endif /* HAVE_GETTEXT */
118
119 #ifndef GNUC_or_lint
120 #ifdef lint
121 #define GNUC_or_lint
122 #else /* !defined lint */
123 #ifdef __GNUC__
124 #define GNUC_or_lint
125 #endif /* defined __GNUC__ */
126 #endif /* !defined lint */
127 #endif /* !defined GNUC_or_lint */
128
129 #ifndef INITIALIZE
130 #ifdef GNUC_or_lint
131 #define INITIALIZE(x) ((x) = 0)
132 #else /* !defined GNUC_or_lint */
133 #define INITIALIZE(x)
134 #endif /* !defined GNUC_or_lint */
135 #endif /* !defined INITIALIZE */
136
137 /*
138 ** For the benefit of GNU folk...
139 ** `_(MSGID)' uses the current locale's message library string for MSGID.
140 ** The default is to use gettext if available, and use MSGID otherwise.
141 */
142
143 #ifndef _
144 #if HAVE_GETTEXT
145 #define _(msgid) gettext(msgid)
146 #else /* !HAVE_GETTEXT */
147 #define _(msgid) msgid
148 #endif /* !HAVE_GETTEXT */
149 #endif /* !defined _ */
150
151 #ifndef TZ_DOMAIN
152 #define TZ_DOMAIN "tz"
153 #endif /* !defined TZ_DOMAIN */
154
155 #ifndef P
156 #ifdef __STDC__
157 #define P(x) x
158 #else /* !defined __STDC__ */
159 #define P(x) ()
160 #endif /* !defined __STDC__ */
161 #endif /* !defined P */
162
163 #define ZDUMP
164
165 extern char ** environ;
166 extern int getopt P((int argc, char * const argv[],
167 const char * options));
168 extern char * optarg;
169 extern int optind;
170 extern char * tzname[2];
171
172 static time_t absolute_min_time;
173 static time_t absolute_max_time;
174 static size_t longest;
175 static char * progname;
176 static int warned;
177
178 static char * abbr P((struct tm * tmp));
179 static void abbrok P((const char * abbr, const char * zone));
180 static long delta P((struct tm * newp, struct tm * oldp));
181 static void dumptime P((const struct tm * tmp));
182 static time_t hunt P((char * name, time_t lot, time_t hit));
183 static void setabsolutes P((void));
184 static void show P((char * zone, time_t t, int v));
185 static const char * tformat P((void));
186 static time_t yeartot P((long y));
187
188 #ifndef TYPECHECK
189 #define my_localtime localtime
190 #else /* !defined TYPECHECK */
191 static struct tm *
my_localtime(tp)192 my_localtime(tp)
193 time_t * tp;
194 {
195 register struct tm * tmp;
196
197 tmp = localtime(tp);
198 if (tp != NULL && tmp != NULL) {
199 struct tm tm;
200 register time_t t;
201
202 tm = *tmp;
203 t = mktime(&tm);
204 if (t - *tp >= 1 || *tp - t >= 1) {
205 (void) fflush(stdout);
206 (void) fprintf(stderr, "\n%s: ", progname);
207 (void) fprintf(stderr, tformat(), *tp);
208 (void) fprintf(stderr, " ->");
209 (void) fprintf(stderr, " year=%d", tmp->tm_year);
210 (void) fprintf(stderr, " mon=%d", tmp->tm_mon);
211 (void) fprintf(stderr, " mday=%d", tmp->tm_mday);
212 (void) fprintf(stderr, " hour=%d", tmp->tm_hour);
213 (void) fprintf(stderr, " min=%d", tmp->tm_min);
214 (void) fprintf(stderr, " sec=%d", tmp->tm_sec);
215 (void) fprintf(stderr, " isdst=%d", tmp->tm_isdst);
216 (void) fprintf(stderr, " -> ");
217 (void) fprintf(stderr, tformat(), t);
218 (void) fprintf(stderr, "\n");
219 }
220 }
221 return tmp;
222 }
223 #endif /* !defined TYPECHECK */
224
225 static void
abbrok(abbr,zone)226 abbrok(abbr, zone)
227 const char * const abbr;
228 const char * const zone;
229 {
230 register const char * cp;
231 register char * wp;
232
233 if (warned)
234 return;
235 cp = abbr;
236 wp = NULL;
237 while (isascii(*cp) && isalpha(*cp))
238 ++cp;
239 if (cp - abbr == 0)
240 wp = _("lacks alphabetic at start");
241 if (cp - abbr < 3)
242 wp = _("has fewer than 3 alphabetics");
243 if (cp - abbr > 6)
244 wp = _("has more than 6 alphabetics");
245 if (wp == NULL && (*cp == '+' || *cp == '-')) {
246 ++cp;
247 if (isascii(*cp) && isdigit(*cp))
248 if (*cp++ == '1' && *cp >= '0' && *cp <= '4')
249 ++cp;
250 }
251 if (*cp != '\0')
252 wp = _("differs from POSIX standard");
253 if (wp == NULL)
254 return;
255 (void) fflush(stdout);
256 (void) fprintf(stderr,
257 "%s: warning: zone \"%s\" abbreviation \"%s\" %s\n",
258 progname, zone, abbr, wp);
259 warned = TRUE;
260 }
261
262 int
main(argc,argv)263 main(argc, argv)
264 int argc;
265 char * argv[];
266 {
267 register int i;
268 register int c;
269 register int vflag;
270 register char * cutarg;
271 register long cutloyear = ZDUMP_LO_YEAR;
272 register long cuthiyear = ZDUMP_HI_YEAR;
273 register time_t cutlotime;
274 register time_t cuthitime;
275 register char ** fakeenv;
276 time_t now;
277 time_t t;
278 time_t newt;
279 struct tm tm;
280 struct tm newtm;
281 register struct tm * tmp;
282 register struct tm * newtmp;
283
284 INITIALIZE(cutlotime);
285 INITIALIZE(cuthitime);
286 #ifndef __MirBSD__
287 #if HAVE_GETTEXT
288 (void) setlocale(LC_ALL, "");
289 #ifdef TZ_DOMAINDIR
290 (void) bindtextdomain(TZ_DOMAIN, TZ_DOMAINDIR);
291 #endif /* defined TEXTDOMAINDIR */
292 (void) textdomain(TZ_DOMAIN);
293 #endif /* HAVE_GETTEXT */
294 #endif
295 progname = argv[0];
296 vflag = 0;
297 cutarg = NULL;
298 while ((c = getopt(argc, argv, "c:v")) == 'c' || c == 'v')
299 if (c == 'v')
300 vflag = 1;
301 else cutarg = optarg;
302 if ((c != EOF && c != -1) ||
303 (optind == argc - 1 && strcmp(argv[optind], "=") == 0)) {
304 (void) fprintf(stderr,
305 _("usage %s [-v] [-c [loyear,]hiyear] zonename ...\n"),
306 progname);
307 (void) exit(EXIT_FAILURE);
308 }
309 if (vflag) {
310 if (cutarg != NULL) {
311 long lo;
312 long hi;
313 char dummy;
314
315 if (sscanf(cutarg, "%ld%c", &hi, &dummy) == 1) {
316 cuthiyear = hi;
317 } else if (sscanf(cutarg, "%ld,%ld%c",
318 &lo, &hi, &dummy) == 2) {
319 cutloyear = lo;
320 cuthiyear = hi;
321 } else {
322 (void) fprintf(stderr, _("%s: wild -c argument %s\n"),
323 progname, cutarg);
324 (void) exit(EXIT_FAILURE);
325 }
326 }
327 setabsolutes();
328 cutlotime = yeartot(cutloyear);
329 cuthitime = yeartot(cuthiyear);
330 }
331 (void) time(&now);
332 longest = 0;
333 for (i = optind; i < argc; ++i)
334 if (strlen(argv[i]) > longest)
335 longest = strlen(argv[i]);
336 {
337 register int from;
338 register int to;
339
340 for (i = 0; environ[i] != NULL; ++i)
341 continue;
342 fakeenv = (char **) malloc((size_t) ((i + 2) *
343 sizeof *fakeenv));
344 if (fakeenv == NULL ||
345 (fakeenv[0] = (char *) malloc(longest + 4)) == NULL) {
346 (void) perror(progname);
347 (void) exit(EXIT_FAILURE);
348 }
349 to = 0;
350 strlcpy(fakeenv[to++], "TZ=", longest + 4);
351 for (from = 0; environ[from] != NULL; ++from)
352 if (strncmp(environ[from], "TZ=", 3) != 0)
353 fakeenv[to++] = environ[from];
354 fakeenv[to] = NULL;
355 environ = fakeenv;
356 }
357 for (i = optind; i < argc; ++i) {
358 static char buf[MAX_STRING_LENGTH];
359
360 strlcpy(&fakeenv[0][3], argv[i], longest + 1);
361 if (!vflag) {
362 show(argv[i], now, FALSE);
363 continue;
364 }
365 warned = FALSE;
366 t = absolute_min_time;
367 show(argv[i], t, TRUE);
368 t += SECSPERHOUR * HOURSPERDAY;
369 show(argv[i], t, TRUE);
370 if (t < cutlotime)
371 t = cutlotime;
372 tmp = my_localtime(&t);
373 if (tmp != NULL) {
374 tm = *tmp;
375 strlcpy(buf, abbr(&tm), sizeof buf);
376 }
377 for ( ; ; ) {
378 if (t >= cuthitime)
379 break;
380 newt = t + SECSPERHOUR * 12;
381 if (newt >= cuthitime)
382 break;
383 if (newt <= t)
384 break;
385 newtmp = localtime(&newt);
386 if (newtmp != NULL)
387 newtm = *newtmp;
388 if ((tmp == NULL || newtmp == NULL) ? (tmp != newtmp) :
389 (delta(&newtm, &tm) != (newt - t) ||
390 newtm.tm_isdst != tm.tm_isdst ||
391 strcmp(abbr(&newtm), buf) != 0)) {
392 newt = hunt(argv[i], t, newt);
393 newtmp = localtime(&newt);
394 if (newtmp != NULL) {
395 newtm = *newtmp;
396 strlcpy(buf, abbr(&newtm),
397 sizeof buf);
398 }
399 }
400 t = newt;
401 tm = newtm;
402 tmp = newtmp;
403 }
404 t = absolute_max_time;
405 t -= SECSPERHOUR * HOURSPERDAY;
406 show(argv[i], t, TRUE);
407 t += SECSPERHOUR * HOURSPERDAY;
408 show(argv[i], t, TRUE);
409 }
410 if (fflush(stdout) || ferror(stdout)) {
411 (void) fprintf(stderr, "%s: ", progname);
412 (void) perror(_("Error writing standard output"));
413 (void) exit(EXIT_FAILURE);
414 }
415 exit(EXIT_SUCCESS);
416 /* If exit fails to exit... */
417 return EXIT_FAILURE;
418 }
419
420 static void
setabsolutes()421 setabsolutes()
422 {
423 if (0.5 == (time_t) 0.5) {
424 /*
425 ** time_t is floating.
426 */
427 if (sizeof (time_t) == sizeof (float)) {
428 absolute_min_time = (time_t) -FLT_MAX;
429 absolute_max_time = (time_t) FLT_MAX;
430 } else if (sizeof (time_t) == sizeof (double)) {
431 absolute_min_time = (time_t) -DBL_MAX;
432 absolute_max_time = (time_t) DBL_MAX;
433 } else {
434 (void) fprintf(stderr,
435 _("%s: use of -v on system with floating time_t other than float or double\n"),
436 progname);
437 (void) exit(EXIT_FAILURE);
438 }
439 } else if (0 > (time_t) -1) {
440 /*
441 ** time_t is signed.
442 */
443 register time_t hibit;
444
445 for (hibit = 1; (hibit * 2) != 0; hibit *= 2)
446 continue;
447 absolute_min_time = hibit;
448 absolute_max_time = -(hibit + 1);
449 } else {
450 /*
451 ** time_t is unsigned.
452 */
453 absolute_min_time = 0;
454 absolute_max_time = absolute_min_time - 1;
455 }
456 }
457
458 static time_t
yeartot(y)459 yeartot(y)
460 const long y;
461 {
462 register long myy;
463 register long seconds;
464 register time_t t;
465
466 myy = EPOCH_YEAR;
467 t = 0;
468 while (myy != y) {
469 if (myy < y) {
470 seconds = isleap(myy) ? SECSPERLYEAR : SECSPERNYEAR;
471 ++myy;
472 if (t > absolute_max_time - seconds) {
473 t = absolute_max_time;
474 break;
475 }
476 t += seconds;
477 } else {
478 --myy;
479 seconds = isleap(myy) ? SECSPERLYEAR : SECSPERNYEAR;
480 if (t < absolute_min_time + seconds) {
481 t = absolute_min_time;
482 break;
483 }
484 t -= seconds;
485 }
486 }
487 return t;
488 }
489
490 static time_t
hunt(name,lot,hit)491 hunt(name, lot, hit)
492 char * name;
493 time_t lot;
494 time_t hit;
495 {
496 time_t t;
497 long diff;
498 struct tm lotm;
499 register struct tm * lotmp;
500 struct tm tm;
501 register struct tm * tmp;
502 char loab[MAX_STRING_LENGTH];
503
504 lotmp = my_localtime(&lot);
505 if (lotmp != NULL) {
506 lotm = *lotmp;
507 (void) strlcpy(loab, abbr(&lotm), sizeof loab);
508 }
509 for ( ; ; ) {
510 diff = (long) (hit - lot);
511 if (diff < 2)
512 break;
513 t = lot;
514 t += diff / 2;
515 if (t <= lot)
516 ++t;
517 else if (t >= hit)
518 --t;
519 tmp = my_localtime(&t);
520 if (tmp != NULL)
521 tm = *tmp;
522 if ((lotmp == NULL || tmp == NULL) ? (lotmp == tmp) :
523 (delta(&tm, &lotm) == (t - lot) &&
524 tm.tm_isdst == lotm.tm_isdst &&
525 strcmp(abbr(&tm), loab) == 0)) {
526 lot = t;
527 lotm = tm;
528 lotmp = tmp;
529 } else hit = t;
530 }
531 show(name, lot, TRUE);
532 show(name, hit, TRUE);
533 return hit;
534 }
535
536 /*
537 ** Thanks to Paul Eggert (eggert@twinsun.com) for logic used in delta.
538 */
539
540 static long
delta(newp,oldp)541 delta(newp, oldp)
542 struct tm * newp;
543 struct tm * oldp;
544 {
545 register long result;
546 register int tmy;
547
548 if (newp->tm_year < oldp->tm_year)
549 return -delta(oldp, newp);
550 result = 0;
551 for (tmy = oldp->tm_year; tmy < newp->tm_year; ++tmy)
552 result += DAYSPERNYEAR + isleap_sum(tmy, TM_YEAR_BASE);
553 result += newp->tm_yday - oldp->tm_yday;
554 result *= HOURSPERDAY;
555 result += newp->tm_hour - oldp->tm_hour;
556 result *= MINSPERHOUR;
557 result += newp->tm_min - oldp->tm_min;
558 result *= SECSPERMIN;
559 result += newp->tm_sec - oldp->tm_sec;
560 return result;
561 }
562
563 static void
show(zone,t,v)564 show(zone, t, v)
565 char * zone;
566 time_t t;
567 int v;
568 {
569 register struct tm * tmp;
570
571 (void) printf("%-*s ", (int) longest, zone);
572 if (v) {
573 tmp = gmtime(&t);
574 if (tmp == NULL) {
575 (void) printf(tformat(), t);
576 } else {
577 dumptime(tmp);
578 (void) printf(" UTC");
579 }
580 (void) printf(" = ");
581 }
582 tmp = my_localtime(&t);
583 dumptime(tmp);
584 if (tmp != NULL) {
585 if (*abbr(tmp) != '\0')
586 (void) printf(" %s", abbr(tmp));
587 if (v) {
588 (void) printf(" isdst=%d", tmp->tm_isdst);
589 #ifdef TM_GMTOFF
590 (void) printf(" gmtoff=%ld", tmp->TM_GMTOFF);
591 #endif /* defined TM_GMTOFF */
592 }
593 }
594 (void) printf("\n");
595 if (tmp != NULL && *abbr(tmp) != '\0')
596 abbrok(abbr(tmp), zone);
597 }
598
599 static char *
abbr(tmp)600 abbr(tmp)
601 struct tm * tmp;
602 {
603 register char * result;
604 static char nada;
605
606 if (tmp->tm_isdst != 0 && tmp->tm_isdst != 1)
607 return &nada;
608 result = tzname[tmp->tm_isdst];
609 return (result == NULL) ? &nada : result;
610 }
611
612 /*
613 ** The code below can fail on certain theoretical systems;
614 ** it works on all known real-world systems as of 2004-12-30.
615 */
616
617 static const char *
tformat()618 tformat()
619 {
620 if (0.5 == (time_t) 0.5) { /* floating */
621 if (sizeof (time_t) > sizeof (double))
622 return "%Lg";
623 return "%g";
624 }
625 if (0 > (time_t) -1) { /* signed */
626 if (sizeof (time_t) > sizeof (long))
627 return "%lld";
628 if (sizeof (time_t) > sizeof (int))
629 return "%ld";
630 return "%d";
631 }
632 if (sizeof (time_t) > sizeof (unsigned long))
633 return "%llu";
634 if (sizeof (time_t) > sizeof (unsigned int))
635 return "%lu";
636 return "%u";
637 }
638
639 static void
dumptime(timeptr)640 dumptime(timeptr)
641 register const struct tm * timeptr;
642 {
643 static const char wday_name[][3] = {
644 "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
645 };
646 static const char mon_name[][3] = {
647 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
648 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
649 };
650 register const char * wn;
651 register const char * mn;
652 #ifndef TM_YEAR_BASE
653 #define TM_YEAR_BASE 1900
654 #endif
655
656 if (timeptr == NULL) {
657 (void) printf("NULL");
658 return;
659 }
660 /*
661 ** The packaged versions of localtime and gmtime never put out-of-range
662 ** values in tm_wday or tm_mon, but since this code might be compiled
663 ** with other (perhaps experimental) versions, paranoia is in order.
664 */
665 if (timeptr->tm_wday < 0 || timeptr->tm_wday >=
666 (int) (sizeof wday_name / sizeof wday_name[0]))
667 wn = "???";
668 else wn = wday_name[timeptr->tm_wday];
669 if (timeptr->tm_mon < 0 || timeptr->tm_mon >=
670 (int) (sizeof mon_name / sizeof mon_name[0]))
671 mn = "???";
672 else mn = mon_name[timeptr->tm_mon];
673 (void) printf(
674 #ifdef ZDUMP
675 "%.3s %.3s%3d %.2d:%.2d:%.2d %lld",
676 wn, mn, timeptr->tm_mday,
677 #else
678 "%.3s %.3s%3d (%d) %lld %.2d:%.2d:%.2d %s (%ld%s)",
679 wn, mn, timeptr->tm_mday, timeptr->tm_yday,
680 (int64_t)timeptr->tm_year + (long long) TM_YEAR_BASE,
681 #endif
682 timeptr->tm_hour, timeptr->tm_min, timeptr->tm_sec,
683 #ifdef ZDUMP
684 (int64_t)timeptr->tm_year + (long long) TM_YEAR_BASE);
685 #else
686 timeptr->tm_zone ? timeptr->tm_zone : "(null)",
687 timeptr->tm_gmtoff,
688 timeptr->tm_isdst ? " DST" : "");
689 #endif
690 }
691