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