3 static char elsieid
[] = "@(#)zdump.c 7.29";
4 #endif /* !defined NOID */
5 #endif /* !defined lint */
8 ** This code has been made independent of the rest of the time
9 ** conversion package to increase confidence in the verification it provides.
10 ** You can use this code to help in verifying other implementations.
13 #include "stdio.h" /* for stdout, stderr, perror */
14 #include "string.h" /* for strcpy */
15 #include "sys/types.h" /* for time_t */
16 #include "time.h" /* for struct tm */
17 #include "stdlib.h" /* for exit, malloc, atoi */
19 #ifndef MAX_STRING_LENGTH
20 #define MAX_STRING_LENGTH 1024
21 #endif /* !defined MAX_STRING_LENGTH */
25 #endif /* !defined TRUE */
29 #endif /* !defined FALSE */
32 #define EXIT_SUCCESS 0
33 #endif /* !defined EXIT_SUCCESS */
36 #define EXIT_FAILURE 1
37 #endif /* !defined EXIT_FAILURE */
41 #endif /* !defined SECSPERMIN */
44 #define MINSPERHOUR 60
45 #endif /* !defined MINSPERHOUR */
48 #define SECSPERHOUR (SECSPERMIN * MINSPERHOUR)
49 #endif /* !defined SECSPERHOUR */
52 #define HOURSPERDAY 24
53 #endif /* !defined HOURSPERDAY */
56 #define EPOCH_YEAR 1970
57 #endif /* !defined EPOCH_YEAR */
60 #define TM_YEAR_BASE 1900
61 #endif /* !defined TM_YEAR_BASE */
64 #define DAYSPERNYEAR 365
65 #endif /* !defined DAYSPERNYEAR */
68 #define isleap(y) ((((y) % 4) == 0 && ((y) % 100) != 0) || ((y) % 400) == 0)
69 #endif /* !defined isleap */
72 #include "locale.h" /* for setlocale */
74 #endif /* HAVE_GETTEXT - 0 */
79 #endif /* defined lint */
83 #endif /* defined __GNUC__ */
84 #endif /* !defined lint */
85 #endif /* !defined GNUC_or_lint */
89 #define INITIALIZE(x) ((x) = 0)
90 #endif /* defined GNUC_or_lint */
93 #endif /* !defined GNUC_or_lint */
94 #endif /* !defined INITIALIZE */
97 ** For the benefit of GNU folk...
98 ** `_(MSGID)' uses the current locale's message library string for MSGID.
99 ** The default is to use gettext if available, and use MSGID otherwise.
104 #define _(msgid) gettext(msgid)
105 #else /* !(HAVE_GETTEXT - 0) */
106 #define _(msgid) msgid
107 #endif /* !(HAVE_GETTEXT - 0) */
108 #endif /* !defined _ */
111 #define TZ_DOMAIN "tz"
112 #endif /* !defined TZ_DOMAIN */
117 #endif /* defined __STDC__ */
120 #endif /* !defined __STDC__ */
121 #endif /* !defined P */
123 extern char ** environ
;
124 extern int getopt
P((int argc
, char * const argv
[],
125 const char * options
));
126 extern char * optarg
;
128 extern char * tzname
[2];
130 static char * abbr
P((struct tm
* tmp
));
131 static long delta
P((struct tm
* newp
, struct tm
* oldp
));
132 static time_t hunt
P((char * name
, time_t lot
, time_t hit
));
133 static size_t longest
;
134 static char * progname
;
135 static void show
P((char * zone
, time_t t
, int v
));
145 register char * cutoff
;
146 register int cutyear
;
147 register long cuttime
;
158 (void) setlocale(LC_CTYPE
, "");
159 (void) setlocale(LC_MESSAGES
, "");
161 (void) bindtextdomain(TZ_DOMAIN
, TZ_DOMAINDIR
);
162 #endif /* defined(TEXTDOMAINDIR) */
163 (void) textdomain(TZ_DOMAIN
);
164 #endif /* HAVE_GETTEXT - 0 */
168 while ((c
= getopt(argc
, argv
, "c:v")) == 'c' || c
== 'v')
171 else cutoff
= optarg
;
172 if ((c
!= EOF
&& c
!= -1) ||
173 (optind
== argc
- 1 && strcmp(argv
[optind
], "=") == 0)) {
174 (void) fprintf(stderr
,
175 _("%s: usage is %s [ -v ] [ -c cutoff ] zonename ...\n"),
177 (void) exit(EXIT_FAILURE
);
179 if (cutoff
!= NULL
) {
182 cutyear
= atoi(cutoff
);
184 for (y
= EPOCH_YEAR
; y
< cutyear
; ++y
)
185 cuttime
+= DAYSPERNYEAR
+ isleap(y
);
186 cuttime
*= SECSPERHOUR
* HOURSPERDAY
;
190 for (i
= optind
; i
< argc
; ++i
)
191 if (strlen(argv
[i
]) > longest
)
192 longest
= strlen(argv
[i
]);
193 for (hibit
= 1; (hibit
<< 1) != 0; hibit
<<= 1)
199 for (i
= 0; environ
[i
] != NULL
; ++i
)
201 fakeenv
= (char **) malloc((size_t) ((i
+ 2) *
203 if (fakeenv
== NULL
||
204 (fakeenv
[0] = (char *) malloc(longest
+ 4)) == NULL
) {
205 (void) perror(progname
);
206 (void) exit(EXIT_FAILURE
);
209 (void) strcpy(fakeenv
[to
++], "TZ=");
210 for (from
= 0; environ
[from
] != NULL
; ++from
)
211 if (strncmp(environ
[from
], "TZ=", 3) != 0)
212 fakeenv
[to
++] = environ
[from
];
216 for (i
= optind
; i
< argc
; ++i
) {
217 static char buf
[MAX_STRING_LENGTH
];
219 (void) strcpy(&fakeenv
[0][3], argv
[i
]);
221 show(argv
[i
], now
, FALSE
);
225 ** Get lowest value of t.
228 if (t
> 0) /* time_t is unsigned */
230 show(argv
[i
], t
, TRUE
);
231 t
+= SECSPERHOUR
* HOURSPERDAY
;
232 show(argv
[i
], t
, TRUE
);
234 (void) strncpy(buf
, abbr(&tm
), (sizeof buf
) - 1);
236 if (cutoff
!= NULL
&& t
>= cuttime
)
238 newt
= t
+ SECSPERHOUR
* 12;
239 if (cutoff
!= NULL
&& newt
>= cuttime
)
243 newtm
= *localtime(&newt
);
244 if (delta(&newtm
, &tm
) != (newt
- t
) ||
245 newtm
.tm_isdst
!= tm
.tm_isdst
||
246 strcmp(abbr(&newtm
), buf
) != 0) {
247 newt
= hunt(argv
[i
], t
, newt
);
248 newtm
= *localtime(&newt
);
249 (void) strncpy(buf
, abbr(&newtm
),
256 ** Get highest value of t.
259 if (t
< 0) /* time_t is signed */
261 t
-= SECSPERHOUR
* HOURSPERDAY
;
262 show(argv
[i
], t
, TRUE
);
263 t
+= SECSPERHOUR
* HOURSPERDAY
;
264 show(argv
[i
], t
, TRUE
);
266 if (fflush(stdout
) || ferror(stdout
)) {
267 (void) fprintf(stderr
, "%s: ", argv
[0]);
268 (void) perror(_("Error writing standard output"));
269 (void) exit(EXIT_FAILURE
);
273 /* gcc -Wall pacifier */
287 static char loab
[MAX_STRING_LENGTH
];
289 lotm
= *localtime(&lot
);
290 (void) strncpy(loab
, abbr(&lotm
), (sizeof loab
) - 1);
291 while ((hit
- lot
) >= 2) {
292 t
= lot
/ 2 + hit
/ 2;
298 if (delta(&tm
, &lotm
) == (t
- lot
) &&
299 tm
.tm_isdst
== lotm
.tm_isdst
&&
300 strcmp(abbr(&tm
), loab
) == 0) {
305 show(name
, lot
, TRUE
);
306 show(name
, hit
, TRUE
);
311 ** Thanks to Paul Eggert (eggert@twinsun.com) for logic used in delta.
322 if (newp
->tm_year
< oldp
->tm_year
)
323 return -delta(oldp
, newp
);
325 for (tmy
= oldp
->tm_year
; tmy
< newp
->tm_year
; ++tmy
)
326 result
+= DAYSPERNYEAR
+ isleap(tmy
+ TM_YEAR_BASE
);
327 result
+= newp
->tm_yday
- oldp
->tm_yday
;
328 result
*= HOURSPERDAY
;
329 result
+= newp
->tm_hour
- oldp
->tm_hour
;
330 result
*= MINSPERHOUR
;
331 result
+= newp
->tm_min
- oldp
->tm_min
;
332 result
*= SECSPERMIN
;
333 result
+= newp
->tm_sec
- oldp
->tm_sec
;
345 (void) printf("%-*s ", (int) longest
, zone
);
347 (void) printf("%.24s UTC = ", asctime(gmtime(&t
)));
349 (void) printf("%.24s", asctime(tmp
));
350 if (*abbr(tmp
) != '\0')
351 (void) printf(" %s", abbr(tmp
));
353 (void) printf(" isdst=%d", tmp
->tm_isdst
);
355 (void) printf(" gmtoff=%ld", tmp
->TM_GMTOFF
);
356 #endif /* defined TM_GMTOFF */
365 register char * result
;
368 if (tmp
->tm_isdst
!= 0 && tmp
->tm_isdst
!= 1)
370 result
= tzname
[tmp
->tm_isdst
];
371 return (result
== NULL
) ? &nada
: result
;