summaryrefslogtreecommitdiff
path: root/usr/src/lib/libbc/libc/sys/common/gettimeofday.c
blob: d027ceee3663d60782780393bd2dfd99880e42a0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License, Version 1.0 only
 * (the "License").  You may not use this file except in compliance
 * with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */
/*
 * Copyright (c) 1995 Sun Microsystems, Inc. All rights reserved.
 */

#ident	"%Z%%M%	%I%	%E% SMI"

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <tzfile.h>
#include <sys/time.h>

static int get_tzp_info(void);
extern long _timezone, _altzone;	/* from the base libc */


/*
 * The second parameter to gettimeofday() did not work correctly on
 * 4.x, and it was documented that localtime() should be used instead.
 * This is an attempt to provide correctly what 4.x meant to do. There
 * are shortcomings, however. See notes for DST_RUM and DST_AUSTALT.
 */

int
gettimeofday(tp, tzp)
struct timeval *tp;
struct timezone *tzp;
{
	int ret = 0;

	if (tp != NULL)
		if ((ret = _gettimeofday(tp)) == -1)
			maperror();

	/*
	 * We should call localtime() with the current time and
	 * set tz_minuteswest to _altzone/SECSPERMIN if tm_isdst
	 * is set. But we want to be bug-for-bug compatible with
	 * 4.x, which would never adjust for DST. Futher comments
	 * are in get_tzp_info().
	 */
	if (tzp != NULL) {
		_tzset();
		tzp->tz_dsttime = get_tzp_info();
		tzp->tz_minuteswest = _timezone/SECSPERMIN;
	}

	return(ret);
}

static int
get_tzp_info()
{
	char	*zonename = getenv("TZ");

	if ((zonename == NULL) || (*zonename == '\0'))
		return (DST_NONE);

	if ((strncmp(zonename, "US/", 3) == 0) ||
	    (strcmp(zonename, "PST8PDT") == 0) ||
	    (strcmp(zonename, "MST7MDT") == 0) ||
	    (strcmp(zonename, "CST6CDT") == 0) ||
	    (strcmp(zonename, "EST5EDT") == 0) ||
	    (strncmp(zonename, "America/", 8) == 0))
		return (DST_USA);

	if (strncmp(zonename, "Australia/", 10) == 0)
		return (DST_AUST);

	if (strcmp(zonename, "WET") == 0)
		return (DST_WET);

	if (strcmp(zonename, "MET") == 0)
		return (DST_MET);

	if (strcmp(zonename, "EET") == 0)
		return (DST_EET);

	if (strncmp(zonename, "Canada/", 7) == 0)
		return (DST_CAN);

	if ((strcmp(zonename, "GB") == 0) ||
	    (strcmp(zonename, "GB-Eire") == 0))
		return (DST_GB);

	/*
	 * what's the corresponding DST_RUM: Rumanian DST?
	 * There was not Rumanian timezone on 4.x.
	 */

	if (strcmp(zonename, "Turkey") == 0)
		return (DST_TUR);

	/*
	 * How do we differentiate between DST_AUST and DST_AUSTALT?
	 * It seems that all of our current Australia timezones do
	 * not have the 1986 shift, so we never will return DST_AUSTALT.
	 */

	return (DST_NONE);
}