1 static char elsieid
[] = "@(#)zdump.c 7.40";
4 ** This code has been made independent of the rest of the time
5 ** conversion package to increase confidence in the verification it provides.
6 ** You can use this code to help in verifying other implementations.
9 #include "stdio.h" /* for stdout, stderr, perror */
10 #include "string.h" /* for strcpy */
11 #include "sys/types.h" /* for time_t */
12 #include "time.h" /* for struct tm */
13 #include "stdlib.h" /* for exit, malloc, atoi */
15 #ifndef MAX_STRING_LENGTH
16 #define MAX_STRING_LENGTH 1024
17 #endif /* !defined MAX_STRING_LENGTH */
21 #endif /* !defined TRUE */
25 #endif /* !defined FALSE */
28 #define EXIT_SUCCESS 0
29 #endif /* !defined EXIT_SUCCESS */
32 #define EXIT_FAILURE 1
33 #endif /* !defined EXIT_FAILURE */
37 #endif /* !defined SECSPERMIN */
40 #define MINSPERHOUR 60
41 #endif /* !defined MINSPERHOUR */
44 #define SECSPERHOUR (SECSPERMIN * MINSPERHOUR)
45 #endif /* !defined SECSPERHOUR */
48 #define HOURSPERDAY 24
49 #endif /* !defined HOURSPERDAY */
52 #define EPOCH_YEAR 1970
53 #endif /* !defined EPOCH_YEAR */
56 #define TM_YEAR_BASE 1900
57 #endif /* !defined TM_YEAR_BASE */
60 #define DAYSPERNYEAR 365
61 #endif /* !defined DAYSPERNYEAR */
64 #define isleap(y) ((((y) % 4) == 0 && ((y) % 100) != 0) || ((y) % 400) == 0)
65 #endif /* !defined isleap */
68 #include "locale.h" /* for setlocale */
70 #endif /* HAVE_GETTEXT */
75 #else /* !defined lint */
78 #endif /* defined __GNUC__ */
79 #endif /* !defined lint */
80 #endif /* !defined GNUC_or_lint */
84 #define INITIALIZE(x) ((x) = 0)
85 #else /* !defined GNUC_or_lint */
87 #endif /* !defined GNUC_or_lint */
88 #endif /* !defined INITIALIZE */
91 ** For the benefit of GNU folk...
92 ** `_(MSGID)' uses the current locale's message library string for MSGID.
93 ** The default is to use gettext if available, and use MSGID otherwise.
98 #define _(msgid) gettext(msgid)
99 #else /* !HAVE_GETTEXT */
100 #define _(msgid) msgid
101 #endif /* !HAVE_GETTEXT */
102 #endif /* !defined _ */
105 #define TZ_DOMAIN "tz"
106 #endif /* !defined TZ_DOMAIN */
111 #else /* !defined __STDC__ */
113 #endif /* !defined __STDC__ */
114 #endif /* !defined P */
116 extern char ** environ
;
117 extern int getopt
P((int argc
, char * const argv
[],
118 const char * options
));
119 extern char * optarg
;
121 extern char * tzname
[2];
123 static char * abbr
P((struct tm
* tmp
));
124 static long delta
P((struct tm
* newp
, struct tm
* oldp
));
125 static time_t hunt
P((char * name
, time_t lot
, time_t hit
));
126 static size_t longest
;
127 static char * progname
;
128 static void show
P((char * zone
, time_t t
, int v
));
129 static void dumptime
P((const struct tm
* tmp
));
139 register char * cutoff
;
140 register int cutyear
;
141 register long cuttime
;
152 (void) setlocale(LC_MESSAGES
, "");
154 (void) bindtextdomain(TZ_DOMAIN
, TZ_DOMAINDIR
);
155 #endif /* defined TEXTDOMAINDIR */
156 (void) textdomain(TZ_DOMAIN
);
157 #endif /* HAVE_GETTEXT */
159 for (i
= 1; i
< argc
; ++i
)
160 if (strcmp(argv
[i
], "--version") == 0) {
161 (void) printf("%s\n", elsieid
);
162 (void) exit(EXIT_SUCCESS
);
166 while ((c
= getopt(argc
, argv
, "c:v")) == 'c' || c
== 'v')
169 else cutoff
= optarg
;
170 if ((c
!= EOF
&& c
!= -1) ||
171 (optind
== argc
- 1 && strcmp(argv
[optind
], "=") == 0)) {
172 (void) fprintf(stderr
,
173 _("%s: usage is %s [ --version ] [ -v ] [ -c cutoff ] zonename ...\n"),
175 (void) exit(EXIT_FAILURE
);
177 if (cutoff
!= NULL
) {
180 cutyear
= atoi(cutoff
);
182 for (y
= EPOCH_YEAR
; y
< cutyear
; ++y
)
183 cuttime
+= DAYSPERNYEAR
+ isleap(y
);
184 cuttime
*= SECSPERHOUR
* HOURSPERDAY
;
188 for (i
= optind
; i
< argc
; ++i
)
189 if (strlen(argv
[i
]) > longest
)
190 longest
= strlen(argv
[i
]);
191 for (hibit
= 1; (hibit
<< 1) != 0; hibit
<<= 1)
197 for (i
= 0; environ
[i
] != NULL
; ++i
)
199 fakeenv
= (char **) malloc((size_t) ((i
+ 2) *
201 if (fakeenv
== NULL
||
202 (fakeenv
[0] = (char *) malloc(longest
+ 4)) == NULL
) {
203 (void) perror(progname
);
204 (void) exit(EXIT_FAILURE
);
207 (void) strcpy(fakeenv
[to
++], "TZ=");
208 for (from
= 0; environ
[from
] != NULL
; ++from
)
209 if (strncmp(environ
[from
], "TZ=", 3) != 0)
210 fakeenv
[to
++] = environ
[from
];
214 for (i
= optind
; i
< argc
; ++i
) {
215 static char buf
[MAX_STRING_LENGTH
];
217 (void) strcpy(&fakeenv
[0][3], argv
[i
]);
219 show(argv
[i
], now
, FALSE
);
223 ** Get lowest value of t.
226 if (t
> 0) /* time_t is unsigned */
228 show(argv
[i
], t
, TRUE
);
229 t
+= SECSPERHOUR
* HOURSPERDAY
;
230 show(argv
[i
], t
, TRUE
);
232 (void) strncpy(buf
, abbr(&tm
), (sizeof buf
) - 1);
234 if (cutoff
!= NULL
&& t
>= cuttime
)
236 newt
= t
+ SECSPERHOUR
* 12;
237 if (cutoff
!= NULL
&& newt
>= cuttime
)
241 newtm
= *localtime(&newt
);
242 if (delta(&newtm
, &tm
) != (newt
- t
) ||
243 newtm
.tm_isdst
!= tm
.tm_isdst
||
244 strcmp(abbr(&newtm
), buf
) != 0) {
245 newt
= hunt(argv
[i
], t
, newt
);
246 newtm
= *localtime(&newt
);
247 (void) strncpy(buf
, abbr(&newtm
),
254 ** Get highest value of t.
257 if (t
< 0) /* time_t is signed */
259 t
-= SECSPERHOUR
* HOURSPERDAY
;
260 show(argv
[i
], t
, TRUE
);
261 t
+= SECSPERHOUR
* HOURSPERDAY
;
262 show(argv
[i
], t
, TRUE
);
264 if (fflush(stdout
) || ferror(stdout
)) {
265 (void) fprintf(stderr
, "%s: ", argv
[0]);
266 (void) perror(_("Error writing standard output"));
267 (void) exit(EXIT_FAILURE
);
271 /* gcc -Wall pacifier */
285 static char loab
[MAX_STRING_LENGTH
];
287 lotm
= *localtime(&lot
);
288 (void) strncpy(loab
, abbr(&lotm
), (sizeof loab
) - 1);
289 while ((hit
- lot
) >= 2) {
290 t
= lot
/ 2 + hit
/ 2;
296 if (delta(&tm
, &lotm
) == (t
- lot
) &&
297 tm
.tm_isdst
== lotm
.tm_isdst
&&
298 strcmp(abbr(&tm
), loab
) == 0) {
303 show(name
, lot
, TRUE
);
304 show(name
, hit
, TRUE
);
309 ** Thanks to Paul Eggert (eggert@twinsun.com) for logic used in delta.
320 if (newp
->tm_year
< oldp
->tm_year
)
321 return -delta(oldp
, newp
);
323 for (tmy
= oldp
->tm_year
; tmy
< newp
->tm_year
; ++tmy
)
324 result
+= DAYSPERNYEAR
+ isleap(tmy
+ (long) TM_YEAR_BASE
);
325 result
+= newp
->tm_yday
- oldp
->tm_yday
;
326 result
*= HOURSPERDAY
;
327 result
+= newp
->tm_hour
- oldp
->tm_hour
;
328 result
*= MINSPERHOUR
;
329 result
+= newp
->tm_min
- oldp
->tm_min
;
330 result
*= SECSPERMIN
;
331 result
+= newp
->tm_sec
- oldp
->tm_sec
;
343 (void) printf("%-*s ", (int) longest
, zone
);
345 dumptime(gmtime(&t
));
346 (void) printf(" UTC = ");
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
;
376 register const struct tm
* timeptr
;
378 static const char wday_name
[][3] = {
379 "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
381 static const char mon_name
[][3] = {
382 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
383 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
385 register const char * wn
;
386 register const char * mn
;
389 ** The packaged versions of localtime and gmtime never put out-of-range
390 ** values in tm_wday or tm_mon, but since this code might be compiled
391 ** with other (perhaps experimental) versions, paranoia is in order.
393 if (timeptr
->tm_wday
< 0 || timeptr
->tm_wday
>=
394 (int) (sizeof wday_name
/ sizeof wday_name
[0]))
396 else wn
= wday_name
[timeptr
->tm_wday
];
397 if (timeptr
->tm_mon
< 0 || timeptr
->tm_mon
>=
398 (int) (sizeof mon_name
/ sizeof mon_name
[0]))
400 else mn
= mon_name
[timeptr
->tm_mon
];
401 (void) printf("%.3s %.3s%3d %.2d:%.2d:%.2d %ld",
403 timeptr
->tm_mday
, timeptr
->tm_hour
,
404 timeptr
->tm_min
, timeptr
->tm_sec
,
405 timeptr
->tm_year
+ (long) TM_YEAR_BASE
);