3 * $FreeBSD: src/usr.sbin/zic/zdump.c,v 1.7 1999/08/28 01:21:19 peter Exp $
4 * $DragonFly: src/usr.sbin/zic/zdump.c,v 1.5 2008/10/19 20:15:58 swildner Exp $
7 ** This code has been made independent of the rest of the time
8 ** conversion package to increase confidence in the verification it provides.
9 ** You can use this code to help in verifying other implementations.
12 #include <ctype.h> /* for isalpha et al. */
14 #include <float.h> /* for FLT_MAX and DBL_MAX */
15 #include <stdio.h> /* for stdout, stderr */
16 #include <stdlib.h> /* for exit, malloc, atoi */
17 #include <string.h> /* for strcpy */
18 #include <sys/types.h> /* for time_t */
19 #include <time.h> /* for struct tm */
23 #define ZDUMP_LO_YEAR (-500)
24 #endif /* !defined ZDUMP_LO_YEAR */
27 #define ZDUMP_HI_YEAR 2500
28 #endif /* !defined ZDUMP_HI_YEAR */
30 #ifndef MAX_STRING_LENGTH
31 #define MAX_STRING_LENGTH 1024
32 #endif /* !defined MAX_STRING_LENGTH */
36 #endif /* !defined TRUE */
40 #endif /* !defined FALSE */
43 #define EXIT_SUCCESS 0
44 #endif /* !defined EXIT_SUCCESS */
47 #define EXIT_FAILURE 1
48 #endif /* !defined EXIT_FAILURE */
52 #endif /* !defined SECSPERMIN */
55 #define MINSPERHOUR 60
56 #endif /* !defined MINSPERHOUR */
59 #define SECSPERHOUR (SECSPERMIN * MINSPERHOUR)
60 #endif /* !defined SECSPERHOUR */
63 #define HOURSPERDAY 24
64 #endif /* !defined HOURSPERDAY */
67 #define EPOCH_YEAR 1970
68 #endif /* !defined EPOCH_YEAR */
71 #define TM_YEAR_BASE 1900
72 #endif /* !defined TM_YEAR_BASE */
75 #define DAYSPERNYEAR 365
76 #endif /* !defined DAYSPERNYEAR */
79 #define isleap(y) (((y) % 4) == 0 && (((y) % 100) != 0 || ((y) % 400) == 0))
80 #endif /* !defined isleap */
84 ** See tzfile.h for details on isleap_sum.
86 #define isleap_sum(a, b) isleap((a) % 400 + (b) % 400)
87 #endif /* !defined isleap_sum */
89 #define SECSPERDAY ((long) SECSPERHOUR * HOURSPERDAY)
90 #define SECSPERNYEAR (SECSPERDAY * DAYSPERNYEAR)
91 #define SECSPERLYEAR (SECSPERNYEAR + SECSPERDAY)
96 #else /* !defined lint */
99 #endif /* defined __GNUC__ */
100 #endif /* !defined lint */
101 #endif /* !defined GNUC_or_lint */
105 #define INITIALIZE(x) ((x) = 0)
106 #else /* !defined GNUC_or_lint */
107 #define INITIALIZE(x)
108 #endif /* !defined GNUC_or_lint */
109 #endif /* !defined INITIALIZE */
112 ** For the benefit of GNU folk...
113 ** `_(MSGID)' uses the current locale's message library string for MSGID.
114 ** The default is to use gettext if available, and use MSGID otherwise.
118 #define _(msgid) msgid
119 #endif /* !defined _ */
122 #define TZ_DOMAIN "tz"
123 #endif /* !defined TZ_DOMAIN */
125 extern char ** environ
;
126 extern char * tzname
[2];
128 static time_t absolute_min_time
;
129 static time_t absolute_max_time
;
130 static size_t longest
;
133 static char *abbr(struct tm
*tmp
);
134 static void abbrok(const char *abbrp
, const char *zone
);
135 static long delta(struct tm
*newp
, struct tm
*oldp
);
136 static void dumptime(const struct tm
*tmp
);
137 static time_t hunt(char *name
, time_t lot
, time_t hit
);
138 static void setabsolutes(void);
139 static void show(char *zone
, time_t t
, int v
);
140 static const char *tformat(void);
141 static time_t yeartot(long y
);
142 static void usage(void);
145 #define my_localtime localtime
146 #else /* !defined TYPECHECK */
148 my_localtime(time_t *tp
)
153 if (tp
!= NULL
&& tmp
!= NULL
) {
159 if (t
- *tp
>= 1 || *tp
- t
>= 1) {
161 fprintf(stderr
, "\n%s: ", progname
);
162 fprintf(stderr
, tformat(), *tp
);
163 fprintf(stderr
, " ->");
164 fprintf(stderr
, " year=%d", tmp
->tm_year
);
165 fprintf(stderr
, " mon=%d", tmp
->tm_mon
);
166 fprintf(stderr
, " mday=%d", tmp
->tm_mday
);
167 fprintf(stderr
, " hour=%d", tmp
->tm_hour
);
168 fprintf(stderr
, " min=%d", tmp
->tm_min
);
169 fprintf(stderr
, " sec=%d", tmp
->tm_sec
);
170 fprintf(stderr
, " isdst=%d", tmp
->tm_isdst
);
171 fprintf(stderr
, " -> ");
172 fprintf(stderr
, tformat(), t
);
173 fprintf(stderr
, "\n");
178 #endif /* !defined TYPECHECK */
181 abbrok(const char * const abbrp
, const char * const zone
)
190 while (isascii((unsigned char) *cp
) && isalpha((unsigned char) *cp
))
193 wp
= _("lacks alphabetic at start");
194 else if (cp
- abbrp
< 3)
195 wp
= _("has fewer than 3 alphabetics");
196 else if (cp
- abbrp
> 6)
197 wp
= _("has more than 6 alphabetics");
198 if (wp
== NULL
&& (*cp
== '+' || *cp
== '-')) {
200 if (isascii((unsigned char) *cp
) &&
201 isdigit((unsigned char) *cp
))
202 if (*cp
++ == '1' && *cp
>= '0' && *cp
<= '4')
205 wp
= _("differs from POSIX standard");
210 warnx(_("warning: zone \"%s\" abbreviation \"%s\" %s\n"),
216 main(int argc
, char *argv
[])
222 long cutloyear
= ZDUMP_LO_YEAR
;
223 long cuthiyear
= ZDUMP_HI_YEAR
;
235 INITIALIZE(cutlotime
);
236 INITIALIZE(cuthitime
);
239 while ((c
= getopt(argc
, argv
, "c:v")) == 'c' || c
== 'v')
242 else cutarg
= optarg
;
243 if ((c
!= EOF
&& c
!= -1) ||
244 (optind
== argc
- 1 && strcmp(argv
[optind
], "=") == 0)) {
248 if (cutarg
!= NULL
) {
253 if (sscanf(cutarg
, "%ld%c", &hi
, &dummy
) == 1) {
255 } else if (sscanf(cutarg
, "%ld,%ld%c",
256 &lo
, &hi
, &dummy
) == 2) {
261 _("%s: wild -c argument %s\n"),
266 cutlotime
= yeartot(cutloyear
);
267 cuthitime
= yeartot(cuthiyear
);
271 for (i
= optind
; i
< argc
; ++i
)
272 if (strlen(argv
[i
]) > longest
)
273 longest
= strlen(argv
[i
]);
278 for (i
= 0; environ
[i
] != NULL
; ++i
)
280 fakeenv
= (char **) malloc((size_t) ((i
+ 2) *
282 if (fakeenv
== NULL
||
283 (fakeenv
[0] = (char *) malloc((size_t) (longest
+
286 _("malloc() failed"));
288 strcpy(fakeenv
[to
++], "TZ=");
289 for (from
= 0; environ
[from
] != NULL
; ++from
)
290 if (strncmp(environ
[from
], "TZ=", 3) != 0)
291 fakeenv
[to
++] = environ
[from
];
295 for (i
= optind
; i
< argc
; ++i
) {
296 static char buf
[MAX_STRING_LENGTH
];
298 strcpy(&fakeenv
[0][3], argv
[i
]);
300 show(argv
[i
], now
, FALSE
);
304 t
= absolute_min_time
;
305 show(argv
[i
], t
, TRUE
);
306 t
+= SECSPERHOUR
* HOURSPERDAY
;
307 show(argv
[i
], t
, TRUE
);
310 tmp
= my_localtime(&t
);
313 strncpy(buf
, abbr(&tm
), (sizeof buf
) - 1);
318 newt
= t
+ SECSPERHOUR
* 12;
319 if (newt
>= cuthitime
)
323 newtmp
= localtime(&newt
);
326 if ((tmp
== NULL
|| newtmp
== NULL
) ? (tmp
!= newtmp
) :
327 (delta(&newtm
, &tm
) != (newt
- t
) ||
328 newtm
.tm_isdst
!= tm
.tm_isdst
||
329 strcmp(abbr(&newtm
), buf
) != 0)) {
330 newt
= hunt(argv
[i
], t
, newt
);
331 newtmp
= localtime(&newt
);
332 if (newtmp
!= NULL
) {
343 t
= absolute_max_time
;
344 t
-= SECSPERHOUR
* HOURSPERDAY
;
345 show(argv
[i
], t
, TRUE
);
346 t
+= SECSPERHOUR
* HOURSPERDAY
;
347 show(argv
[i
], t
, TRUE
);
349 if (fflush(stdout
) || ferror(stdout
))
350 errx(EXIT_FAILURE
, _("error writing standard output"));
352 /* If exit fails to exit... */
359 if (0.5 == (time_t) 0.5) {
361 ** time_t is floating.
363 if (sizeof (time_t) == sizeof (float)) {
364 absolute_min_time
= (time_t) -FLT_MAX
;
365 absolute_max_time
= (time_t) FLT_MAX
;
366 } else if (sizeof (time_t) == sizeof (double)) {
367 absolute_min_time
= (time_t) -DBL_MAX
;
368 absolute_max_time
= (time_t) DBL_MAX
;
371 _("use of -v on system with floating time_t other than float or double\n"));
373 } else if (0 > (time_t) -1) {
375 ** time_t is signed. Assume overflow wraps around.
385 absolute_max_time
= t
;
387 absolute_min_time
= t
- 1;
388 if (t
< absolute_min_time
)
389 absolute_min_time
= t
;
392 ** time_t is unsigned.
394 absolute_min_time
= 0;
395 absolute_max_time
= absolute_min_time
- 1;
402 fprintf(stderr
, _("usage: zdump [-v] [-c cutoff] zonename ...\n"));
407 yeartot(const long y
)
417 seconds
= isleap(myy
) ? SECSPERLYEAR
: SECSPERNYEAR
;
419 if (t
> absolute_max_time
- seconds
) {
420 t
= absolute_max_time
;
426 seconds
= isleap(myy
) ? SECSPERLYEAR
: SECSPERNYEAR
;
427 if (t
< absolute_min_time
+ seconds
) {
428 t
= absolute_min_time
;
438 hunt(char *name
, time_t lot
, time_t hit
)
446 char loab
[MAX_STRING_LENGTH
];
448 lotmp
= my_localtime(&lot
);
451 strncpy(loab
, abbr(&lotm
), (sizeof loab
) - 1);
454 diff
= (long) (hit
- lot
);
463 tmp
= my_localtime(&t
);
466 if ((lotmp
== NULL
|| tmp
== NULL
) ? (lotmp
== tmp
) :
467 (delta(&tm
, &lotm
) == (t
- lot
) &&
468 tm
.tm_isdst
== lotm
.tm_isdst
&&
469 strcmp(abbr(&tm
), loab
) == 0)) {
475 show(name
, lot
, TRUE
);
476 show(name
, hit
, TRUE
);
481 ** Thanks to Paul Eggert for logic used in delta.
485 delta(struct tm
*newp
, struct tm
*oldp
)
490 if (newp
->tm_year
< oldp
->tm_year
)
491 return -delta(oldp
, newp
);
493 for (tmy
= oldp
->tm_year
; tmy
< newp
->tm_year
; ++tmy
)
494 result
+= DAYSPERNYEAR
+ isleap_sum(tmy
, TM_YEAR_BASE
);
495 result
+= newp
->tm_yday
- oldp
->tm_yday
;
496 result
*= HOURSPERDAY
;
497 result
+= newp
->tm_hour
- oldp
->tm_hour
;
498 result
*= MINSPERHOUR
;
499 result
+= newp
->tm_min
- oldp
->tm_min
;
500 result
*= SECSPERMIN
;
501 result
+= newp
->tm_sec
- oldp
->tm_sec
;
506 show(char *zone
, time_t t
, int v
)
510 printf("%-*s ", (int) longest
, zone
);
514 printf(tformat(), t
);
521 tmp
= my_localtime(&t
);
524 if (*abbr(tmp
) != '\0')
525 printf(" %s", abbr(tmp
));
527 printf(" isdst=%d", tmp
->tm_isdst
);
529 printf(" gmtoff=%ld", tmp
->TM_GMTOFF
);
530 #endif /* defined TM_GMTOFF */
534 if (tmp
!= NULL
&& *abbr(tmp
) != '\0')
535 abbrok(abbr(tmp
), zone
);
544 if (tmp
->tm_isdst
!= 0 && tmp
->tm_isdst
!= 1)
546 result
= tzname
[tmp
->tm_isdst
];
547 return (result
== NULL
) ? &nada
: result
;
551 ** The code below can fail on certain theoretical systems;
552 ** it works on all known real-world systems as of 2004-12-30.
558 if (0.5 == (time_t) 0.5) { /* floating */
559 if (sizeof (time_t) > sizeof (double))
563 if (0 > (time_t) -1) { /* signed */
564 if (sizeof (time_t) > sizeof (long))
566 if (sizeof (time_t) > sizeof (int))
570 if (sizeof (time_t) > sizeof (unsigned long))
572 if (sizeof (time_t) > sizeof (unsigned int))
578 dumptime(const struct tm
*timeptr
)
580 static const char wday_name
[][3] = {
581 "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
583 static const char mon_name
[][3] = {
584 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
585 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
592 if (timeptr
== NULL
) {
597 ** The packaged versions of localtime and gmtime never put out-of-range
598 ** values in tm_wday or tm_mon, but since this code might be compiled
599 ** with other (perhaps experimental) versions, paranoia is in order.
601 if (timeptr
->tm_wday
< 0 || timeptr
->tm_wday
>=
602 (int) (sizeof wday_name
/ sizeof wday_name
[0]))
604 else wn
= wday_name
[timeptr
->tm_wday
];
605 if (timeptr
->tm_mon
< 0 || timeptr
->tm_mon
>=
606 (int) (sizeof mon_name
/ sizeof mon_name
[0]))
608 else mn
= mon_name
[timeptr
->tm_mon
];
609 printf("%.3s %.3s%3d %.2d:%.2d:%.2d ",
611 timeptr
->tm_mday
, timeptr
->tm_hour
,
612 timeptr
->tm_min
, timeptr
->tm_sec
);
614 trail
= timeptr
->tm_year
% DIVISOR
+ TM_YEAR_BASE
% DIVISOR
;
615 lead
= timeptr
->tm_year
/ DIVISOR
+ TM_YEAR_BASE
/ DIVISOR
+
618 if (trail
< 0 && lead
> 0) {
621 } else if (lead
< 0 && trail
> 0) {
627 else printf("%d%d", lead
, ((trail
< 0) ? -trail
: trail
));