3 static char elsieid
[] = "@(#)zdump.c 7.24";
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 */
114 extern char ** environ
;
116 extern char * optarg
;
118 extern time_t time();
119 extern char * tzname
[2];
121 static char * abbr();
123 static time_t hunt();
125 static char * progname
;
136 register char * cutoff
;
137 register int cutyear
;
138 register long cuttime
;
149 (void) setlocale(LC_MESSAGES
, "");
151 (void) bindtextdomain(TZ_DOMAIN
, TZ_DOMAINDIR
);
152 #endif /* defined(TEXTDOMAINDIR) */
153 (void) textdomain(TZ_DOMAIN
);
154 #endif /* HAVE_GETTEXT - 0 */
158 while ((c
= getopt(argc
, argv
, "c:v")) == 'c' || c
== 'v')
161 else cutoff
= optarg
;
163 (optind
== argc
- 1 && strcmp(argv
[optind
], "=") == 0)) {
164 (void) fprintf(stderr
,
165 _("%s: usage is %s [ -v ] [ -c cutoff ] zonename ...\n"),
167 (void) exit(EXIT_FAILURE
);
169 if (cutoff
!= NULL
) {
172 cutyear
= atoi(cutoff
);
174 for (y
= EPOCH_YEAR
; y
< cutyear
; ++y
)
175 cuttime
+= DAYSPERNYEAR
+ isleap(y
);
176 cuttime
*= SECSPERHOUR
* HOURSPERDAY
;
180 for (i
= optind
; i
< argc
; ++i
)
181 if (strlen(argv
[i
]) > longest
)
182 longest
= strlen(argv
[i
]);
183 for (hibit
= 1; (hibit
<< 1) != 0; hibit
<<= 1)
189 for (i
= 0; environ
[i
] != NULL
; ++i
)
191 fakeenv
= (char **) malloc((size_t) ((i
+ 2) *
193 if (fakeenv
== NULL
||
194 (fakeenv
[0] = (char *) malloc((size_t) (longest
+
196 (void) perror(progname
);
197 (void) exit(EXIT_FAILURE
);
200 (void) strcpy(fakeenv
[to
++], "TZ=");
201 for (from
= 0; environ
[from
] != NULL
; ++from
)
202 if (strncmp(environ
[from
], "TZ=", 3) != 0)
203 fakeenv
[to
++] = environ
[from
];
207 for (i
= optind
; i
< argc
; ++i
) {
208 static char buf
[MAX_STRING_LENGTH
];
210 (void) strcpy(&fakeenv
[0][3], argv
[i
]);
212 show(argv
[i
], now
, FALSE
);
216 ** Get lowest value of t.
219 if (t
> 0) /* time_t is unsigned */
221 show(argv
[i
], t
, TRUE
);
222 t
+= SECSPERHOUR
* HOURSPERDAY
;
223 show(argv
[i
], t
, TRUE
);
225 (void) strncpy(buf
, abbr(&tm
), (sizeof buf
) - 1);
227 if (cutoff
!= NULL
&& t
>= cuttime
)
229 newt
= t
+ SECSPERHOUR
* 12;
230 if (cutoff
!= NULL
&& newt
>= cuttime
)
234 newtm
= *localtime(&newt
);
235 if (delta(&newtm
, &tm
) != (newt
- t
) ||
236 newtm
.tm_isdst
!= tm
.tm_isdst
||
237 strcmp(abbr(&newtm
), buf
) != 0) {
238 newt
= hunt(argv
[i
], t
, newt
);
239 newtm
= *localtime(&newt
);
240 (void) strncpy(buf
, abbr(&newtm
),
247 ** Get highest value of t.
250 if (t
< 0) /* time_t is signed */
252 t
-= SECSPERHOUR
* HOURSPERDAY
;
253 show(argv
[i
], t
, TRUE
);
254 t
+= SECSPERHOUR
* HOURSPERDAY
;
255 show(argv
[i
], t
, TRUE
);
257 if (fflush(stdout
) || ferror(stdout
)) {
258 (void) fprintf(stderr
, _("%s: Error writing standard output "),
260 (void) perror(_("standard output"));
261 (void) exit(EXIT_FAILURE
);
265 /* gcc -Wall pacifier */
279 static char loab
[MAX_STRING_LENGTH
];
281 lotm
= *localtime(&lot
);
282 (void) strncpy(loab
, abbr(&lotm
), (sizeof loab
) - 1);
283 while ((hit
- lot
) >= 2) {
284 t
= lot
/ 2 + hit
/ 2;
290 if (delta(&tm
, &lotm
) == (t
- lot
) &&
291 tm
.tm_isdst
== lotm
.tm_isdst
&&
292 strcmp(abbr(&tm
), loab
) == 0) {
297 show(name
, lot
, TRUE
);
298 show(name
, hit
, TRUE
);
303 ** Thanks to Paul Eggert (eggert@twinsun.com) for logic used in delta.
314 if (newp
->tm_year
< oldp
->tm_year
)
315 return -delta(oldp
, newp
);
317 for (tmy
= oldp
->tm_year
; tmy
< newp
->tm_year
; ++tmy
)
318 result
+= DAYSPERNYEAR
+ isleap(tmy
+ TM_YEAR_BASE
);
319 result
+= newp
->tm_yday
- oldp
->tm_yday
;
320 result
*= HOURSPERDAY
;
321 result
+= newp
->tm_hour
- oldp
->tm_hour
;
322 result
*= MINSPERHOUR
;
323 result
+= newp
->tm_min
- oldp
->tm_min
;
324 result
*= SECSPERMIN
;
325 result
+= newp
->tm_sec
- oldp
->tm_sec
;
329 extern struct tm
* localtime();
339 (void) printf("%-*s ", longest
, zone
);
341 (void) printf("%.24s GMT = ", asctime(gmtime(&t
)));
343 (void) printf("%.24s", asctime(tmp
));
344 if (*abbr(tmp
) != '\0')
345 (void) printf(" %s", abbr(tmp
));
347 (void) printf(" isdst=%d", tmp
->tm_isdst
);
349 (void) printf(" gmtoff=%ld", tmp
->TM_GMTOFF
);
350 #endif /* defined TM_GMTOFF */
359 register char * result
;
362 if (tmp
->tm_isdst
!= 0 && tmp
->tm_isdst
!= 1)
364 result
= tzname
[tmp
->tm_isdst
];
365 return (result
== NULL
) ? &nada
: result
;