1 /* $NetBSD: zdump.c,v 1.25 2012/08/09 12:38:26 christos Exp $ */
3 ** This file is in the public domain, so clarified as of
4 ** 2009-05-17 by Arthur David Olson.
9 __RCSID("$NetBSD: zdump.c,v 1.25 2012/08/09 12:38:26 christos Exp $");
10 #endif /* !defined lint */
14 ** This code has been made independent of the rest of the time
15 ** conversion package to increase confidence in the verification it provides.
16 ** You can use this code to help in verifying other implementations.
19 #include "stdio.h" /* for stdout, stderr */
20 #include "string.h" /* for strcpy */
21 #include "sys/types.h" /* for time_t */
22 #include "time.h" /* for struct tm */
23 #include "stdlib.h" /* for exit, malloc, atoi */
25 #include "float.h" /* for FLT_MAX and DBL_MAX */
26 #include "ctype.h" /* for isalpha et al. */
29 #endif /* !defined isascii */
32 #define ZDUMP_LO_YEAR (-500)
33 #endif /* !defined ZDUMP_LO_YEAR */
36 #define ZDUMP_HI_YEAR 2500
37 #endif /* !defined ZDUMP_HI_YEAR */
39 #ifndef MAX_STRING_LENGTH
40 #define MAX_STRING_LENGTH 1024
41 #endif /* !defined MAX_STRING_LENGTH */
45 #endif /* !defined TRUE */
49 #endif /* !defined FALSE */
52 #define EXIT_SUCCESS 0
53 #endif /* !defined EXIT_SUCCESS */
56 #define EXIT_FAILURE 1
57 #endif /* !defined EXIT_FAILURE */
61 #endif /* !defined SECSPERMIN */
64 #define MINSPERHOUR 60
65 #endif /* !defined MINSPERHOUR */
68 #define SECSPERHOUR (SECSPERMIN * MINSPERHOUR)
69 #endif /* !defined SECSPERHOUR */
72 #define HOURSPERDAY 24
73 #endif /* !defined HOURSPERDAY */
76 #define EPOCH_YEAR 1970
77 #endif /* !defined EPOCH_YEAR */
80 #define TM_YEAR_BASE 1900
81 #endif /* !defined TM_YEAR_BASE */
84 #define DAYSPERNYEAR 365
85 #endif /* !defined DAYSPERNYEAR */
88 #define isleap(y) (((y) % 4) == 0 && (((y) % 100) != 0 || ((y) % 400) == 0))
89 #endif /* !defined isleap */
93 ** See tzfile.h for details on isleap_sum.
95 #define isleap_sum(a, b) isleap((a) % 400 + (b) % 400)
96 #endif /* !defined isleap_sum */
98 #define SECSPERDAY ((long) SECSPERHOUR * HOURSPERDAY)
99 #define SECSPERNYEAR (SECSPERDAY * DAYSPERNYEAR)
100 #define SECSPERLYEAR (SECSPERNYEAR + SECSPERDAY)
103 #define HAVE_GETTEXT 0
106 #include "locale.h" /* for setlocale */
108 #endif /* HAVE_GETTEXT */
113 #else /* !defined lint */
116 #endif /* defined __GNUC__ */
117 #endif /* !defined lint */
118 #endif /* !defined GNUC_or_lint */
122 #define INITIALIZE(x) ((x) = 0)
123 #else /* !defined GNUC_or_lint */
124 #define INITIALIZE(x)
125 #endif /* !defined GNUC_or_lint */
126 #endif /* !defined INITIALIZE */
129 ** For the benefit of GNU folk...
130 ** `_(MSGID)' uses the current locale's message library string for MSGID.
131 ** The default is to use gettext if available, and use MSGID otherwise.
136 #define _(msgid) gettext(msgid)
137 #else /* !HAVE_GETTEXT */
138 #define _(msgid) msgid
139 #endif /* !HAVE_GETTEXT */
140 #endif /* !defined _ */
143 #define TZ_DOMAIN "tz"
144 #endif /* !defined TZ_DOMAIN */
146 extern char ** environ
;
147 extern int getopt(int argc
, char * const argv
[],
148 const char * options
);
149 extern char * optarg
;
152 static time_t absolute_min_time
;
153 static time_t absolute_max_time
;
154 static size_t longest
;
155 static char * progname
;
158 static const char * abbr(struct tm
* tmp
);
159 static void abbrok(const char * abbrp
, const char * zone
);
160 static long delta(struct tm
* newp
, struct tm
* oldp
);
161 static void dumptime(const struct tm
* tmp
);
162 static time_t hunt(char * name
, time_t lot
, time_t hit
);
163 int main(int, char **);
164 static void setabsolutes(void);
165 static void show(char * zone
, time_t t
, int v
);
166 static const char * tformat(void);
167 static time_t yeartot(long y
);
170 #define my_localtime localtime
171 #else /* !defined TYPECHECK */
176 register struct tm
* tmp
;
179 if (tp
!= NULL
&& tmp
!= NULL
) {
185 if (t
- *tp
>= 1 || *tp
- t
>= 1) {
186 (void) fflush(stdout
);
187 (void) fprintf(stderr
, "\n%s: ", progname
);
188 (void) fprintf(stderr
, tformat(), *tp
);
189 (void) fprintf(stderr
, " ->");
190 (void) fprintf(stderr
, " year=%d", tmp
->tm_year
);
191 (void) fprintf(stderr
, " mon=%d", tmp
->tm_mon
);
192 (void) fprintf(stderr
, " mday=%d", tmp
->tm_mday
);
193 (void) fprintf(stderr
, " hour=%d", tmp
->tm_hour
);
194 (void) fprintf(stderr
, " min=%d", tmp
->tm_min
);
195 (void) fprintf(stderr
, " sec=%d", tmp
->tm_sec
);
196 (void) fprintf(stderr
, " isdst=%d", tmp
->tm_isdst
);
197 (void) fprintf(stderr
, " -> ");
198 (void) fprintf(stderr
, tformat(), t
);
199 (void) fprintf(stderr
, "\n");
204 #endif /* !defined TYPECHECK */
208 const char * const abbrp
;
209 const char * const zone
;
211 register const char * cp
;
212 register const char * wp
;
218 while (isascii((unsigned char) *cp
) && isalpha((unsigned char) *cp
))
221 wp
= _("lacks alphabetic at start");
222 else if (cp
- abbrp
< 3)
223 wp
= _("has fewer than 3 alphabetics");
224 else if (cp
- abbrp
> 6)
225 wp
= _("has more than 6 alphabetics");
226 if (wp
== NULL
&& (*cp
== '+' || *cp
== '-')) {
228 if (isascii((unsigned char) *cp
) &&
229 isdigit((unsigned char) *cp
))
230 if (*cp
++ == '1' && *cp
>= '0' && *cp
<= '4')
233 wp
= _("differs from POSIX standard");
237 (void) fflush(stdout
);
238 (void) fprintf(stderr
,
239 _("%s: warning: zone \"%s\" abbreviation \"%s\" %s\n"),
240 progname
, zone
, abbrp
, wp
);
245 usage(FILE *stream
, int status
)
247 (void) fprintf(stream
,
248 _("%s: usage is %s [ --version ] [ --help ] [ -v ] [ -c [loyear,]hiyear ] zonename ...\n\
250 Report bugs to tz@elsie.nci.nih.gov.\n"),
263 register char * cutarg
;
264 register long cutloyear
= ZDUMP_LO_YEAR
;
265 register long cuthiyear
= ZDUMP_HI_YEAR
;
266 register time_t cutlotime
;
267 register time_t cuthitime
;
268 register char ** fakeenv
;
274 register struct tm
* tmp
;
275 register struct tm
* newtmp
;
277 INITIALIZE(cutlotime
);
278 INITIALIZE(cuthitime
);
280 (void) setlocale(LC_ALL
, "");
282 (void) bindtextdomain(TZ_DOMAIN
, TZ_DOMAINDIR
);
283 #endif /* defined TEXTDOMAINDIR */
284 (void) textdomain(TZ_DOMAIN
);
285 #endif /* HAVE_GETTEXT */
287 for (i
= 1; i
< argc
; ++i
)
288 if (strcmp(argv
[i
], "--version") == 0) {
289 (void) printf("%s\n", TZVERSION
);
291 } else if (strcmp(argv
[i
], "--help") == 0) {
292 usage(stdout
, EXIT_SUCCESS
);
296 while ((c
= getopt(argc
, argv
, "c:v")) == 'c' || c
== 'v')
299 else cutarg
= optarg
;
300 if ((c
!= EOF
&& c
!= -1) ||
301 (optind
== argc
- 1 && strcmp(argv
[optind
], "=") == 0)) {
302 usage(stderr
, EXIT_FAILURE
);
305 if (cutarg
!= NULL
) {
310 if (sscanf(cutarg
, "%ld%c", &hi
, &dummy
) == 1) {
312 } else if (sscanf(cutarg
, "%ld,%ld%c",
313 &lo
, &hi
, &dummy
) == 2) {
317 (void) fprintf(stderr
, _("%s: wild -c argument %s\n"),
323 cutlotime
= yeartot(cutloyear
);
324 cuthitime
= yeartot(cuthiyear
);
328 for (i
= optind
; i
< argc
; ++i
)
329 if (strlen(argv
[i
]) > longest
)
330 longest
= strlen(argv
[i
]);
335 for (i
= 0; environ
[i
] != NULL
; ++i
)
337 fakeenv
= (char **) malloc((size_t) ((i
+ 2) *
339 if (fakeenv
== NULL
||
340 (fakeenv
[0] = (char *) malloc(longest
+ 4)) == NULL
) {
341 err(EXIT_FAILURE
, "Can't allocated %zu bytes",
345 (void)strcpy(fakeenv
[to
++], "TZ="); /* XXX strcpy is safe */
346 for (from
= 0; environ
[from
] != NULL
; ++from
)
347 if (strncmp(environ
[from
], "TZ=", 3) != 0)
348 fakeenv
[to
++] = environ
[from
];
352 for (i
= optind
; i
< argc
; ++i
) {
353 static char buf
[MAX_STRING_LENGTH
];
355 (void) strcpy(&fakeenv
[0][3], argv
[i
]); /* XXX strcpy is safe */
357 show(argv
[i
], now
, FALSE
);
361 t
= absolute_min_time
;
362 show(argv
[i
], t
, TRUE
);
363 t
+= SECSPERHOUR
* HOURSPERDAY
;
364 show(argv
[i
], t
, TRUE
);
367 tmp
= my_localtime(&t
);
370 (void) strncpy(buf
, abbr(&tm
), (sizeof buf
) - 1);
373 if (t
>= cuthitime
|| t
>= cuthitime
- SECSPERHOUR
* 12)
375 newt
= t
+ SECSPERHOUR
* 12;
376 newtmp
= localtime(&newt
);
379 if ((tmp
== NULL
|| newtmp
== NULL
) ? (tmp
!= newtmp
) :
380 (delta(&newtm
, &tm
) != (newt
- t
) ||
381 newtm
.tm_isdst
!= tm
.tm_isdst
||
382 strcmp(abbr(&newtm
), buf
) != 0)) {
383 newt
= hunt(argv
[i
], t
, newt
);
384 newtmp
= localtime(&newt
);
385 if (newtmp
!= NULL
) {
396 t
= absolute_max_time
;
397 t
-= SECSPERHOUR
* HOURSPERDAY
;
398 show(argv
[i
], t
, TRUE
);
399 t
+= SECSPERHOUR
* HOURSPERDAY
;
400 show(argv
[i
], t
, TRUE
);
402 if (fflush(stdout
) || ferror(stdout
)) {
403 err(EXIT_FAILURE
, _("Error writing standard output"));
406 /* If exit fails to exit... */
413 if (t
< 0 && t
- 1 >= 0)
422 if (0.5 == (time_t) 0.5) {
424 ** time_t is floating.
426 if (sizeof (time_t) == sizeof (float)) {
427 absolute_min_time
= (time_t) -FLT_MAX
;
428 absolute_max_time
= (time_t) FLT_MAX
;
429 } else if (sizeof (time_t) == sizeof (double)) {
430 absolute_min_time
= (time_t) -DBL_MAX
;
431 absolute_max_time
= (time_t) DBL_MAX
;
433 (void) fprintf(stderr
,
434 _("%s: use of -v on system with floating time_t other than float or double\n"),
438 } else if (0 > (time_t) -1) {
440 ** time_t is signed. Assume overflow wraps around.
450 absolute_max_time
= t
;
452 absolute_min_time
= ovfl_check(t
);
455 ** time_t is unsigned.
457 absolute_min_time
= 0;
458 absolute_max_time
= absolute_min_time
- 1;
467 register long seconds
;
474 seconds
= isleap(myy
) ? SECSPERLYEAR
: SECSPERNYEAR
;
476 if (t
> absolute_max_time
- seconds
) {
477 t
= absolute_max_time
;
483 seconds
= isleap(myy
) ? SECSPERLYEAR
: SECSPERNYEAR
;
484 if (t
< absolute_min_time
+ seconds
) {
485 t
= absolute_min_time
;
495 hunt(char *name
, time_t lot
, time_t hit
)
500 register struct tm
* lotmp
;
502 register struct tm
* tmp
;
503 char loab
[MAX_STRING_LENGTH
];
505 lotmp
= my_localtime(&lot
);
508 (void) strncpy(loab
, abbr(&lotm
), (sizeof loab
) - 1);
511 diff
= (long) (hit
- lot
);
520 tmp
= my_localtime(&t
);
523 if ((lotmp
== NULL
|| tmp
== NULL
) ? (lotmp
== tmp
) :
524 (delta(&tm
, &lotm
) == (t
- lot
) &&
525 tm
.tm_isdst
== lotm
.tm_isdst
&&
526 strcmp(abbr(&tm
), loab
) == 0)) {
532 show(name
, lot
, TRUE
);
533 show(name
, hit
, TRUE
);
538 ** Thanks to Paul Eggert for logic used in delta.
546 register long result
;
549 if (newp
->tm_year
< oldp
->tm_year
)
550 return -delta(oldp
, newp
);
552 for (tmy
= oldp
->tm_year
; tmy
< newp
->tm_year
; ++tmy
)
553 result
+= DAYSPERNYEAR
+ isleap_sum(tmy
, TM_YEAR_BASE
);
554 result
+= newp
->tm_yday
- oldp
->tm_yday
;
555 result
*= HOURSPERDAY
;
556 result
+= newp
->tm_hour
- oldp
->tm_hour
;
557 result
*= MINSPERHOUR
;
558 result
+= newp
->tm_min
- oldp
->tm_min
;
559 result
*= SECSPERMIN
;
560 result
+= newp
->tm_sec
- oldp
->tm_sec
;
565 show(char *zone
, time_t t
, int v
)
567 register struct tm
* tmp
;
569 (void) printf("%-*s ", (int) longest
, zone
);
573 (void) printf(tformat(), t
);
576 (void) printf(" UTC");
578 (void) printf(" = ");
580 tmp
= my_localtime(&t
);
583 if (*abbr(tmp
) != '\0')
584 (void) printf(" %s", abbr(tmp
));
586 (void) printf(" isdst=%d", tmp
->tm_isdst
);
588 (void) printf(" gmtoff=%ld", tmp
->TM_GMTOFF
);
589 #endif /* defined TM_GMTOFF */
593 if (tmp
!= NULL
&& *abbr(tmp
) != '\0')
594 abbrok(abbr(tmp
), zone
);
601 register const char * result
;
602 static const char nada
;
604 if (tmp
->tm_isdst
!= 0 && tmp
->tm_isdst
!= 1)
606 result
= tzname
[tmp
->tm_isdst
];
607 return (result
== NULL
) ? &nada
: result
;
611 ** The code below can fail on certain theoretical systems;
612 ** it works on all known real-world systems as of 2004-12-30.
618 if (0.5 == (time_t) 0.5) { /* floating */
619 if (sizeof (time_t) > sizeof (double))
623 if (0 > (time_t) -1) { /* signed */
624 if (sizeof (time_t) > sizeof (long))
626 if (sizeof (time_t) > sizeof (int))
630 if (sizeof (time_t) > sizeof (unsigned long))
632 if (sizeof (time_t) > sizeof (unsigned int))
639 register const struct tm
* timeptr
;
641 static const char wday_name
[][3] = {
642 "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
644 static const char mon_name
[][3] = {
645 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
646 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
648 register const char * wn
;
649 register const char * mn
;
653 if (timeptr
== NULL
) {
654 (void) printf("NULL");
658 ** The packaged versions of localtime and gmtime never put out-of-range
659 ** values in tm_wday or tm_mon, but since this code might be compiled
660 ** with other (perhaps experimental) versions, paranoia is in order.
662 if (timeptr
->tm_wday
< 0 || timeptr
->tm_wday
>=
663 (int) (sizeof wday_name
/ sizeof wday_name
[0]))
665 else wn
= wday_name
[timeptr
->tm_wday
];
666 if (timeptr
->tm_mon
< 0 || timeptr
->tm_mon
>=
667 (int) (sizeof mon_name
/ sizeof mon_name
[0]))
669 else mn
= mon_name
[timeptr
->tm_mon
];
670 (void) printf("%.3s %.3s%3d %.2d:%.2d:%.2d ",
672 timeptr
->tm_mday
, timeptr
->tm_hour
,
673 timeptr
->tm_min
, timeptr
->tm_sec
);
675 trail
= timeptr
->tm_year
% DIVISOR
+ TM_YEAR_BASE
% DIVISOR
;
676 lead
= timeptr
->tm_year
/ DIVISOR
+ TM_YEAR_BASE
/ DIVISOR
+
679 if (trail
< 0 && lead
> 0) {
682 } else if (lead
< 0 && trail
> 0) {
687 (void) printf("%d", trail
);
688 else (void) printf("%d%d", lead
, ((trail
< 0) ? -trail
: trail
));