2 ** This file is in the public domain, so clarified as of
3 ** 2009-05-17 by Arthur David Olson.
9 ** This code has been made independent of the rest of the time
10 ** conversion package to increase confidence in the verification it provides.
11 ** You can use this code to help in verifying other implementations.
14 #include "stdio.h" /* for stdout, stderr, perror */
15 #include "string.h" /* for strcpy */
16 #include "sys/types.h" /* for time_t */
17 #include "time.h" /* for struct tm */
18 #include "stdlib.h" /* for exit, malloc, atoi */
19 #include "float.h" /* for FLT_MAX and DBL_MAX */
20 #include "limits.h" /* for CHAR_BIT, LLONG_MAX */
21 #include "ctype.h" /* for isalpha et al. */
24 #endif /* !defined isascii */
27 #define ZDUMP_LO_YEAR (-500)
28 #endif /* !defined ZDUMP_LO_YEAR */
31 #define ZDUMP_HI_YEAR 2500
32 #endif /* !defined ZDUMP_HI_YEAR */
34 #ifndef MAX_STRING_LENGTH
35 #define MAX_STRING_LENGTH 1024
36 #endif /* !defined MAX_STRING_LENGTH */
40 #endif /* !defined TRUE */
44 #endif /* !defined FALSE */
47 #define EXIT_SUCCESS 0
48 #endif /* !defined EXIT_SUCCESS */
51 #define EXIT_FAILURE 1
52 #endif /* !defined EXIT_FAILURE */
56 #endif /* !defined SECSPERMIN */
59 #define MINSPERHOUR 60
60 #endif /* !defined MINSPERHOUR */
63 #define SECSPERHOUR (SECSPERMIN * MINSPERHOUR)
64 #endif /* !defined SECSPERHOUR */
67 #define HOURSPERDAY 24
68 #endif /* !defined HOURSPERDAY */
71 #define EPOCH_YEAR 1970
72 #endif /* !defined EPOCH_YEAR */
75 #define TM_YEAR_BASE 1900
76 #endif /* !defined TM_YEAR_BASE */
79 #define DAYSPERNYEAR 365
80 #endif /* !defined DAYSPERNYEAR */
83 #define isleap(y) (((y) % 4) == 0 && (((y) % 100) != 0 || ((y) % 400) == 0))
84 #endif /* !defined isleap */
88 ** See tzfile.h for details on isleap_sum.
90 #define isleap_sum(a, b) isleap((a) % 400 + (b) % 400)
91 #endif /* !defined isleap_sum */
93 #define SECSPERDAY ((long) SECSPERHOUR * HOURSPERDAY)
94 #define SECSPERNYEAR (SECSPERDAY * DAYSPERNYEAR)
95 #define SECSPERLYEAR (SECSPERNYEAR + SECSPERDAY)
98 #define HAVE_GETTEXT 0
101 #include "locale.h" /* for setlocale */
103 #endif /* HAVE_GETTEXT */
108 #else /* !defined lint */
111 #endif /* defined __GNUC__ */
112 #endif /* !defined lint */
113 #endif /* !defined GNUC_or_lint */
117 #define INITIALIZE(x) ((x) = 0)
118 #else /* !defined GNUC_or_lint */
119 #define INITIALIZE(x)
120 #endif /* !defined GNUC_or_lint */
121 #endif /* !defined INITIALIZE */
123 #if 2 < __GNUC__ || (__GNUC__ == 2 && 96 <= __GNUC_MINOR__)
124 # define ATTRIBUTE_PURE __attribute__ ((__pure__))
126 # define ATTRIBUTE_PURE /* empty */
130 ** For the benefit of GNU folk...
131 ** `_(MSGID)' uses the current locale's message library string for MSGID.
132 ** The default is to use gettext if available, and use MSGID otherwise.
137 #define _(msgid) gettext(msgid)
138 #else /* !HAVE_GETTEXT */
139 #define _(msgid) msgid
140 #endif /* !HAVE_GETTEXT */
141 #endif /* !defined _ */
144 #define TZ_DOMAIN "tz"
145 #endif /* !defined TZ_DOMAIN */
147 extern char ** environ
;
148 extern int getopt(int argc
, char * const argv
[],
149 const char * options
);
150 extern char * optarg
;
152 extern char * tzname
[2];
154 /* The minimum and maximum finite time values. Shift 'long long' or
155 'long' instead of 'time_t'; this avoids compile-time errors when
156 time_t is floating-point. In practice, 'long long' is wide enough. */
157 static time_t const absolute_min_time
=
159 ? (sizeof (time_t) == sizeof (float) ? (time_t) -FLT_MAX
160 : sizeof (time_t) == sizeof (double) ? (time_t) -DBL_MAX
161 : sizeof (time_t) == sizeof (long double) ? (time_t) -LDBL_MAX
165 ? (time_t) ((long long) -1 << (CHAR_BIT
* sizeof (time_t) - 1))
167 ? (time_t) ((long) -1 << (CHAR_BIT
* sizeof (time_t) - 1))
170 static time_t const absolute_max_time
=
172 ? (sizeof (time_t) == sizeof (float) ? (time_t) FLT_MAX
173 : sizeof (time_t) == sizeof (double) ? (time_t) DBL_MAX
174 : sizeof (time_t) == sizeof (long double) ? (time_t) LDBL_MAX
178 ? (time_t) (- (~ 0 < 0) - ((long long) -1 << (CHAR_BIT
* sizeof (time_t) - 1)))
180 ? (time_t) (- (~ 0 < 0) - ((long) -1 << (CHAR_BIT
* sizeof (time_t) - 1)))
183 static size_t longest
;
184 static char * progname
;
187 static char * abbr(struct tm
* tmp
);
188 static void abbrok(const char * abbrp
, const char * zone
);
189 static long delta(struct tm
* newp
, struct tm
* oldp
) ATTRIBUTE_PURE
;
190 static void dumptime(const struct tm
* tmp
);
191 static time_t hunt(char * name
, time_t lot
, time_t hit
);
192 static void checkabsolutes(void);
193 static void show(char * zone
, time_t t
, int v
);
194 static const char * tformat(void);
195 static time_t yeartot(long y
) ATTRIBUTE_PURE
;
198 #define my_localtime localtime
199 #else /* !defined TYPECHECK */
201 my_localtime(time_t *tp
)
203 register struct tm
* tmp
;
206 if (tp
!= NULL
&& tmp
!= NULL
) {
212 if (t
- *tp
>= 1 || *tp
- t
>= 1) {
213 (void) fflush(stdout
);
214 (void) fprintf(stderr
, "\n%s: ", progname
);
215 (void) fprintf(stderr
, tformat(), *tp
);
216 (void) fprintf(stderr
, " ->");
217 (void) fprintf(stderr
, " year=%d", tmp
->tm_year
);
218 (void) fprintf(stderr
, " mon=%d", tmp
->tm_mon
);
219 (void) fprintf(stderr
, " mday=%d", tmp
->tm_mday
);
220 (void) fprintf(stderr
, " hour=%d", tmp
->tm_hour
);
221 (void) fprintf(stderr
, " min=%d", tmp
->tm_min
);
222 (void) fprintf(stderr
, " sec=%d", tmp
->tm_sec
);
223 (void) fprintf(stderr
, " isdst=%d", tmp
->tm_isdst
);
224 (void) fprintf(stderr
, " -> ");
225 (void) fprintf(stderr
, tformat(), t
);
226 (void) fprintf(stderr
, "\n");
231 #endif /* !defined TYPECHECK */
234 abbrok(const char *const abbrp
, const char *const zone
)
236 register const char * cp
;
237 register const char * wp
;
243 while (isascii((unsigned char) *cp
) && isalpha((unsigned char) *cp
))
246 wp
= _("lacks alphabetic at start");
247 else if (cp
- abbrp
< 3)
248 wp
= _("has fewer than 3 alphabetics");
249 else if (cp
- abbrp
> 6)
250 wp
= _("has more than 6 alphabetics");
251 if (wp
== NULL
&& (*cp
== '+' || *cp
== '-')) {
253 if (isascii((unsigned char) *cp
) &&
254 isdigit((unsigned char) *cp
))
255 if (*cp
++ == '1' && *cp
>= '0' && *cp
<= '4')
258 wp
= _("differs from POSIX standard");
262 (void) fflush(stdout
);
263 (void) fprintf(stderr
,
264 _("%s: warning: zone \"%s\" abbreviation \"%s\" %s\n"),
265 progname
, zone
, abbrp
, wp
);
270 usage(FILE * const stream
, const int status
)
272 (void) fprintf(stream
,
273 _("%s: usage is %s [ --version ] [ --help ] [ -v ] [ -c [loyear,]hiyear ] zonename ...\n\
275 Report bugs to %s.\n"),
276 progname
, progname
, REPORT_BUGS_TO
);
281 main(int argc
, char *argv
[])
286 register char * cutarg
;
287 register long cutloyear
= ZDUMP_LO_YEAR
;
288 register long cuthiyear
= ZDUMP_HI_YEAR
;
289 register time_t cutlotime
;
290 register time_t cuthitime
;
291 register char ** fakeenv
;
297 register struct tm
* tmp
;
298 register struct tm
* newtmp
;
300 INITIALIZE(cutlotime
);
301 INITIALIZE(cuthitime
);
303 (void) setlocale(LC_ALL
, "");
305 (void) bindtextdomain(TZ_DOMAIN
, TZ_DOMAINDIR
);
306 #endif /* defined TEXTDOMAINDIR */
307 (void) textdomain(TZ_DOMAIN
);
308 #endif /* HAVE_GETTEXT */
310 for (i
= 1; i
< argc
; ++i
)
311 if (strcmp(argv
[i
], "--version") == 0) {
312 (void) printf("zdump %s%s\n", PKGVERSION
, TZVERSION
);
314 } else if (strcmp(argv
[i
], "--help") == 0) {
315 usage(stdout
, EXIT_SUCCESS
);
319 while ((c
= getopt(argc
, argv
, "c:v")) == 'c' || c
== 'v')
322 else cutarg
= optarg
;
323 if ((c
!= EOF
&& c
!= -1) ||
324 (optind
== argc
- 1 && strcmp(argv
[optind
], "=") == 0)) {
325 usage(stderr
, EXIT_FAILURE
);
328 if (cutarg
!= NULL
) {
333 if (sscanf(cutarg
, "%ld%c", &hi
, &dummy
) == 1) {
335 } else if (sscanf(cutarg
, "%ld,%ld%c",
336 &lo
, &hi
, &dummy
) == 2) {
340 (void) fprintf(stderr
, _("%s: wild -c argument %s\n"),
346 cutlotime
= yeartot(cutloyear
);
347 cuthitime
= yeartot(cuthiyear
);
351 for (i
= optind
; i
< argc
; ++i
)
352 if (strlen(argv
[i
]) > longest
)
353 longest
= strlen(argv
[i
]);
358 for (i
= 0; environ
[i
] != NULL
; ++i
)
360 fakeenv
= malloc((i
+ 2) * sizeof *fakeenv
);
362 || (fakeenv
[0] = malloc(longest
+ 4)) == NULL
) {
363 (void) perror(progname
);
367 (void) strcpy(fakeenv
[to
++], "TZ=");
368 for (from
= 0; environ
[from
] != NULL
; ++from
)
369 if (strncmp(environ
[from
], "TZ=", 3) != 0)
370 fakeenv
[to
++] = environ
[from
];
374 for (i
= optind
; i
< argc
; ++i
) {
375 static char buf
[MAX_STRING_LENGTH
];
377 (void) strcpy(&fakeenv
[0][3], argv
[i
]);
379 show(argv
[i
], now
, FALSE
);
383 t
= absolute_min_time
;
384 show(argv
[i
], t
, TRUE
);
385 t
+= SECSPERHOUR
* HOURSPERDAY
;
386 show(argv
[i
], t
, TRUE
);
389 tmp
= my_localtime(&t
);
392 (void) strncpy(buf
, abbr(&tm
), (sizeof buf
) - 1);
395 if (t
>= cuthitime
|| t
>= cuthitime
- SECSPERHOUR
* 12)
397 newt
= t
+ SECSPERHOUR
* 12;
398 newtmp
= localtime(&newt
);
401 if ((tmp
== NULL
|| newtmp
== NULL
) ? (tmp
!= newtmp
) :
402 (delta(&newtm
, &tm
) != (newt
- t
) ||
403 newtm
.tm_isdst
!= tm
.tm_isdst
||
404 strcmp(abbr(&newtm
), buf
) != 0)) {
405 newt
= hunt(argv
[i
], t
, newt
);
406 newtmp
= localtime(&newt
);
407 if (newtmp
!= NULL
) {
418 t
= absolute_max_time
;
419 t
-= SECSPERHOUR
* HOURSPERDAY
;
420 show(argv
[i
], t
, TRUE
);
421 t
+= SECSPERHOUR
* HOURSPERDAY
;
422 show(argv
[i
], t
, TRUE
);
424 if (fflush(stdout
) || ferror(stdout
)) {
425 (void) fprintf(stderr
, "%s: ", progname
);
426 (void) perror(_("Error writing to standard output"));
430 /* If exit fails to exit... */
437 if (absolute_max_time
< absolute_min_time
) {
438 (void) fprintf(stderr
,
439 _("%s: use of -v on system with floating time_t other than float or double\n"),
446 yeartot(const long y
)
449 register long seconds
;
456 seconds
= isleap(myy
) ? SECSPERLYEAR
: SECSPERNYEAR
;
458 if (t
> absolute_max_time
- seconds
) {
459 t
= absolute_max_time
;
465 seconds
= isleap(myy
) ? SECSPERLYEAR
: SECSPERNYEAR
;
466 if (t
< absolute_min_time
+ seconds
) {
467 t
= absolute_min_time
;
477 hunt(char *name
, time_t lot
, time_t hit
)
482 register struct tm
* lotmp
;
484 register struct tm
* tmp
;
485 char loab
[MAX_STRING_LENGTH
];
487 lotmp
= my_localtime(&lot
);
490 (void) strncpy(loab
, abbr(&lotm
), (sizeof loab
) - 1);
493 diff
= (long) (hit
- lot
);
502 tmp
= my_localtime(&t
);
505 if ((lotmp
== NULL
|| tmp
== NULL
) ? (lotmp
== tmp
) :
506 (delta(&tm
, &lotm
) == (t
- lot
) &&
507 tm
.tm_isdst
== lotm
.tm_isdst
&&
508 strcmp(abbr(&tm
), loab
) == 0)) {
514 show(name
, lot
, TRUE
);
515 show(name
, hit
, TRUE
);
520 ** Thanks to Paul Eggert for logic used in delta.
524 delta(struct tm
* newp
, struct tm
*oldp
)
526 register long result
;
529 if (newp
->tm_year
< oldp
->tm_year
)
530 return -delta(oldp
, newp
);
532 for (tmy
= oldp
->tm_year
; tmy
< newp
->tm_year
; ++tmy
)
533 result
+= DAYSPERNYEAR
+ isleap_sum(tmy
, TM_YEAR_BASE
);
534 result
+= newp
->tm_yday
- oldp
->tm_yday
;
535 result
*= HOURSPERDAY
;
536 result
+= newp
->tm_hour
- oldp
->tm_hour
;
537 result
*= MINSPERHOUR
;
538 result
+= newp
->tm_min
- oldp
->tm_min
;
539 result
*= SECSPERMIN
;
540 result
+= newp
->tm_sec
- oldp
->tm_sec
;
545 show(char *zone
, time_t t
, int v
)
547 register struct tm
* tmp
;
549 (void) printf("%-*s ", (int) longest
, zone
);
553 (void) printf(tformat(), t
);
556 (void) printf(" UTC");
558 (void) printf(" = ");
560 tmp
= my_localtime(&t
);
563 if (*abbr(tmp
) != '\0')
564 (void) printf(" %s", abbr(tmp
));
566 (void) printf(" isdst=%d", tmp
->tm_isdst
);
568 (void) printf(" gmtoff=%ld", tmp
->TM_GMTOFF
);
569 #endif /* defined TM_GMTOFF */
573 if (tmp
!= NULL
&& *abbr(tmp
) != '\0')
574 abbrok(abbr(tmp
), zone
);
580 register char * result
;
583 if (tmp
->tm_isdst
!= 0 && tmp
->tm_isdst
!= 1)
585 result
= tzname
[tmp
->tm_isdst
];
586 return (result
== NULL
) ? &nada
: result
;
590 ** The code below can fail on certain theoretical systems;
591 ** it works on all known real-world systems as of 2004-12-30.
597 if (0.5 == (time_t) 0.5) { /* floating */
598 if (sizeof (time_t) > sizeof (double))
602 if (0 > (time_t) -1) { /* signed */
603 if (sizeof (time_t) > sizeof (long))
605 if (sizeof (time_t) > sizeof (int))
609 if (sizeof (time_t) > sizeof (unsigned long))
611 if (sizeof (time_t) > sizeof (unsigned int))
617 dumptime(register const struct tm
*timeptr
)
619 static const char wday_name
[][3] = {
620 "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
622 static const char mon_name
[][3] = {
623 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
624 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
626 register const char * wn
;
627 register const char * mn
;
631 if (timeptr
== NULL
) {
632 (void) printf("NULL");
636 ** The packaged versions of localtime and gmtime never put out-of-range
637 ** values in tm_wday or tm_mon, but since this code might be compiled
638 ** with other (perhaps experimental) versions, paranoia is in order.
640 if (timeptr
->tm_wday
< 0 || timeptr
->tm_wday
>=
641 (int) (sizeof wday_name
/ sizeof wday_name
[0]))
643 else wn
= wday_name
[timeptr
->tm_wday
];
644 if (timeptr
->tm_mon
< 0 || timeptr
->tm_mon
>=
645 (int) (sizeof mon_name
/ sizeof mon_name
[0]))
647 else mn
= mon_name
[timeptr
->tm_mon
];
648 (void) printf("%.3s %.3s%3d %.2d:%.2d:%.2d ",
650 timeptr
->tm_mday
, timeptr
->tm_hour
,
651 timeptr
->tm_min
, timeptr
->tm_sec
);
653 trail
= timeptr
->tm_year
% DIVISOR
+ TM_YEAR_BASE
% DIVISOR
;
654 lead
= timeptr
->tm_year
/ DIVISOR
+ TM_YEAR_BASE
/ DIVISOR
+
657 if (trail
< 0 && lead
> 0) {
660 } else if (lead
< 0 && trail
> 0) {
665 (void) printf("%d", trail
);
666 else (void) printf("%d%d", lead
, ((trail
< 0) ? -trail
: trail
));