]>
git.saurik.com Git - apple/system_cmds.git/blob - zdump.tproj/zdump.c
2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
23 * @APPLE_LICENSE_HEADER_END@
25 #if defined(LIBC_SCCS) && !defined(lint)
27 static char elsieid
[] = "@(#)zdump.c 7.24";
29 static char rcsid
[] = "$OpenBSD: zdump.c,v 1.5 1997/01/21 04:52:45 millert Exp $";
31 #endif /* LIBC_SCCS and not lint */
34 ** This code has been made independent of the rest of the time
35 ** conversion package to increase confidence in the verification it provides.
36 ** You can use this code to help in verifying other implementations.
39 #include "stdio.h" /* for stdout, stderr, perror */
40 #include "string.h" /* for strcpy */
41 #include "sys/types.h" /* for time_t */
42 #include "time.h" /* for struct tm */
43 #include "stdlib.h" /* for exit, malloc, atoi */
45 #ifndef MAX_STRING_LENGTH
46 #define MAX_STRING_LENGTH 1024
47 #endif /* !defined MAX_STRING_LENGTH */
51 #endif /* !defined TRUE */
55 #endif /* !defined FALSE */
58 #define EXIT_SUCCESS 0
59 #endif /* !defined EXIT_SUCCESS */
62 #define EXIT_FAILURE 1
63 #endif /* !defined EXIT_FAILURE */
67 #endif /* !defined SECSPERMIN */
70 #define MINSPERHOUR 60
71 #endif /* !defined MINSPERHOUR */
74 #define SECSPERHOUR (SECSPERMIN * MINSPERHOUR)
75 #endif /* !defined SECSPERHOUR */
78 #define HOURSPERDAY 24
79 #endif /* !defined HOURSPERDAY */
82 #define EPOCH_YEAR 1970
83 #endif /* !defined EPOCH_YEAR */
86 #define TM_YEAR_BASE 1900
87 #endif /* !defined TM_YEAR_BASE */
90 #define DAYSPERNYEAR 365
91 #endif /* !defined DAYSPERNYEAR */
94 #define isleap(y) ((((y) % 4) == 0 && ((y) % 100) != 0) || ((y) % 400) == 0)
95 #endif /* !defined isleap */
98 #include "locale.h" /* for setlocale */
100 #endif /* HAVE_GETTEXT - 0 */
105 #endif /* defined lint */
109 #endif /* defined __GNUC__ */
110 #endif /* !defined lint */
111 #endif /* !defined GNUC_or_lint */
115 #define INITIALIZE(x) ((x) = 0)
116 #endif /* defined GNUC_or_lint */
118 #define INITIALIZE(x)
119 #endif /* !defined GNUC_or_lint */
120 #endif /* !defined INITIALIZE */
123 ** For the benefit of GNU folk...
124 ** `_(MSGID)' uses the current locale's message library string for MSGID.
125 ** The default is to use gettext if available, and use MSGID otherwise.
130 #define _(msgid) gettext(msgid)
131 #else /* !(HAVE_GETTEXT - 0) */
132 #define _(msgid) msgid
133 #endif /* !(HAVE_GETTEXT - 0) */
134 #endif /* !defined _ */
137 #define TZ_DOMAIN "tz"
138 #endif /* !defined TZ_DOMAIN */
140 extern char ** environ
;
142 extern char * optarg
;
144 extern time_t time();
145 extern char * tzname
[2];
147 static char * abbr();
149 static time_t hunt();
151 static char * progname
;
162 register char * cutoff
;
163 register int cutyear
;
164 register long cuttime
;
175 (void) setlocale(LC_MESSAGES
, "");
177 (void) bindtextdomain(TZ_DOMAIN
, TZ_DOMAINDIR
);
178 #endif /* defined(TEXTDOMAINDIR) */
179 (void) textdomain(TZ_DOMAIN
);
180 #endif /* HAVE_GETTEXT - 0 */
184 while ((c
= getopt(argc
, argv
, "c:v")) == 'c' || c
== 'v')
187 else cutoff
= optarg
;
189 (optind
== argc
- 1 && strcmp(argv
[optind
], "=") == 0)) {
190 (void) fprintf(stderr
,
191 _("%s: usage is %s [ -v ] [ -c cutoff ] zonename ...\n"),
193 (void) exit(EXIT_FAILURE
);
195 if (cutoff
!= NULL
) {
198 cutyear
= atoi(cutoff
);
200 for (y
= EPOCH_YEAR
; y
< cutyear
; ++y
)
201 cuttime
+= DAYSPERNYEAR
+ isleap(y
);
202 cuttime
*= SECSPERHOUR
* HOURSPERDAY
;
206 for (i
= optind
; i
< argc
; ++i
)
207 if (strlen(argv
[i
]) > longest
)
208 longest
= strlen(argv
[i
]);
209 for (hibit
= 1; (hibit
<< 1) != 0; hibit
<<= 1)
215 for (i
= 0; environ
[i
] != NULL
; ++i
)
217 fakeenv
= (char **) malloc((size_t) ((i
+ 2) *
219 if (fakeenv
== NULL
||
220 (fakeenv
[0] = (char *) malloc((size_t) (longest
+
222 (void) perror(progname
);
223 (void) exit(EXIT_FAILURE
);
226 (void) strcpy(fakeenv
[to
++], "TZ=");
227 for (from
= 0; environ
[from
] != NULL
; ++from
)
228 if (strncmp(environ
[from
], "TZ=", 3) != 0)
229 fakeenv
[to
++] = environ
[from
];
233 for (i
= optind
; i
< argc
; ++i
) {
234 static char buf
[MAX_STRING_LENGTH
];
236 (void) strcpy(&fakeenv
[0][3], argv
[i
]);
237 show(argv
[i
], now
, FALSE
);
241 ** Get lowest value of t.
244 if (t
> 0) /* time_t is unsigned */
246 show(argv
[i
], t
, TRUE
);
247 t
+= SECSPERHOUR
* HOURSPERDAY
;
248 show(argv
[i
], t
, TRUE
);
250 (void) strncpy(buf
, abbr(&tm
), (sizeof buf
) - 1);
252 if (cutoff
!= NULL
&& t
>= cuttime
)
254 newt
= t
+ SECSPERHOUR
* 12;
255 if (cutoff
!= NULL
&& newt
>= cuttime
)
259 newtm
= *localtime(&newt
);
260 if (delta(&newtm
, &tm
) != (newt
- t
) ||
261 newtm
.tm_isdst
!= tm
.tm_isdst
||
262 strcmp(abbr(&newtm
), buf
) != 0) {
263 newt
= hunt(argv
[i
], t
, newt
);
264 newtm
= *localtime(&newt
);
265 (void) strncpy(buf
, abbr(&newtm
),
272 ** Get highest value of t.
275 if (t
< 0) /* time_t is signed */
277 t
-= SECSPERHOUR
* HOURSPERDAY
;
278 show(argv
[i
], t
, TRUE
);
279 t
+= SECSPERHOUR
* HOURSPERDAY
;
280 show(argv
[i
], t
, TRUE
);
282 if (fflush(stdout
) || ferror(stdout
)) {
283 (void) fprintf(stderr
, _("%s: Error writing standard output "),
285 (void) perror(_("standard output"));
286 (void) exit(EXIT_FAILURE
);
290 /* gcc -Wall pacifier */
304 static char loab
[MAX_STRING_LENGTH
];
306 lotm
= *localtime(&lot
);
307 (void) strncpy(loab
, abbr(&lotm
), (sizeof loab
) - 1);
308 loab
[(sizeof loab
) - 1] = '\0';
309 while ((hit
- lot
) >= 2) {
310 t
= lot
/ 2 + hit
/ 2;
316 if (delta(&tm
, &lotm
) == (t
- lot
) &&
317 tm
.tm_isdst
== lotm
.tm_isdst
&&
318 strcmp(abbr(&tm
), loab
) == 0) {
323 show(name
, lot
, TRUE
);
324 show(name
, hit
, TRUE
);
329 ** Thanks to Paul Eggert (eggert@twinsun.com) for logic used in delta.
340 if (newp
->tm_year
< oldp
->tm_year
)
341 return -delta(oldp
, newp
);
343 for (tmy
= oldp
->tm_year
; tmy
< newp
->tm_year
; ++tmy
)
344 result
+= DAYSPERNYEAR
+ isleap(tmy
+ TM_YEAR_BASE
);
345 result
+= newp
->tm_yday
- oldp
->tm_yday
;
346 result
*= HOURSPERDAY
;
347 result
+= newp
->tm_hour
- oldp
->tm_hour
;
348 result
*= MINSPERHOUR
;
349 result
+= newp
->tm_min
- oldp
->tm_min
;
350 result
*= SECSPERMIN
;
351 result
+= newp
->tm_sec
- oldp
->tm_sec
;
355 extern struct tm
* localtime();
365 (void) printf("%-*s ", longest
, zone
);
367 (void) printf("%.24s GMT = ", asctime(gmtime(&t
)));
369 (void) printf("%.24s", asctime(tmp
));
370 if (*abbr(tmp
) != '\0')
371 (void) printf(" %s", abbr(tmp
));
373 (void) printf(" isdst=%d", tmp
->tm_isdst
);
375 (void) printf(" gmtoff=%ld", tmp
->TM_GMTOFF
);
376 #endif /* defined TM_GMTOFF */
385 register char * result
;
388 if (tmp
->tm_isdst
!= 0 && tmp
->tm_isdst
!= 1)
390 result
= tzname
[tmp
->tm_isdst
];
391 return (result
== NULL
) ? &nada
: result
;