xref: /titanic_44/usr/src/lib/libbc/libc/sys/common/gettimeofday.c (revision da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright (c) 1995 Sun Microsystems, Inc. All rights reserved.
24  */
25 
26 #ident	"%Z%%M%	%I%	%E% SMI"
27 
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <time.h>
31 #include <tzfile.h>
32 #include <sys/time.h>
33 
34 static int get_tzp_info(void);
35 extern long _timezone, _altzone;	/* from the base libc */
36 
37 
38 /*
39  * The second parameter to gettimeofday() did not work correctly on
40  * 4.x, and it was documented that localtime() should be used instead.
41  * This is an attempt to provide correctly what 4.x meant to do. There
42  * are shortcomings, however. See notes for DST_RUM and DST_AUSTALT.
43  */
44 
45 int
46 gettimeofday(tp, tzp)
47 struct timeval *tp;
48 struct timezone *tzp;
49 {
50 	int ret = 0;
51 
52 	if (tp != NULL)
53 		if ((ret = _gettimeofday(tp)) == -1)
54 			maperror();
55 
56 	/*
57 	 * We should call localtime() with the current time and
58 	 * set tz_minuteswest to _altzone/SECSPERMIN if tm_isdst
59 	 * is set. But we want to be bug-for-bug compatible with
60 	 * 4.x, which would never adjust for DST. Futher comments
61 	 * are in get_tzp_info().
62 	 */
63 	if (tzp != NULL) {
64 		_tzset();
65 		tzp->tz_dsttime = get_tzp_info();
66 		tzp->tz_minuteswest = _timezone/SECSPERMIN;
67 	}
68 
69 	return(ret);
70 }
71 
72 static int
73 get_tzp_info()
74 {
75 	char	*zonename = getenv("TZ");
76 
77 	if ((zonename == NULL) || (*zonename == '\0'))
78 		return (DST_NONE);
79 
80 	if ((strncmp(zonename, "US/", 3) == 0) ||
81 	    (strcmp(zonename, "PST8PDT") == 0) ||
82 	    (strcmp(zonename, "MST7MDT") == 0) ||
83 	    (strcmp(zonename, "CST6CDT") == 0) ||
84 	    (strcmp(zonename, "EST5EDT") == 0) ||
85 	    (strncmp(zonename, "America/", 8) == 0))
86 		return (DST_USA);
87 
88 	if (strncmp(zonename, "Australia/", 10) == 0)
89 		return (DST_AUST);
90 
91 	if (strcmp(zonename, "WET") == 0)
92 		return (DST_WET);
93 
94 	if (strcmp(zonename, "MET") == 0)
95 		return (DST_MET);
96 
97 	if (strcmp(zonename, "EET") == 0)
98 		return (DST_EET);
99 
100 	if (strncmp(zonename, "Canada/", 7) == 0)
101 		return (DST_CAN);
102 
103 	if ((strcmp(zonename, "GB") == 0) ||
104 	    (strcmp(zonename, "GB-Eire") == 0))
105 		return (DST_GB);
106 
107 	/*
108 	 * what's the corresponding DST_RUM: Rumanian DST?
109 	 * There was not Rumanian timezone on 4.x.
110 	 */
111 
112 	if (strcmp(zonename, "Turkey") == 0)
113 		return (DST_TUR);
114 
115 	/*
116 	 * How do we differentiate between DST_AUST and DST_AUSTALT?
117 	 * It seems that all of our current Australia timezones do
118 	 * not have the 1986 shift, so we never will return DST_AUSTALT.
119 	 */
120 
121 	return (DST_NONE);
122 }
123