xref: /onnv-gate/usr/src/lib/libast/common/tm/tmxtime.c (revision 4887:feebf9260c2e)
1*4887Schin /***********************************************************************
2*4887Schin *                                                                      *
3*4887Schin *               This software is part of the ast package               *
4*4887Schin *           Copyright (c) 1985-2007 AT&T Knowledge Ventures            *
5*4887Schin *                      and is licensed under the                       *
6*4887Schin *                  Common Public License, Version 1.0                  *
7*4887Schin *                      by AT&T Knowledge Ventures                      *
8*4887Schin *                                                                      *
9*4887Schin *                A copy of the License is available at                 *
10*4887Schin *            http://www.opensource.org/licenses/cpl1.0.txt             *
11*4887Schin *         (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9)         *
12*4887Schin *                                                                      *
13*4887Schin *              Information and Software Systems Research               *
14*4887Schin *                            AT&T Research                             *
15*4887Schin *                           Florham Park NJ                            *
16*4887Schin *                                                                      *
17*4887Schin *                 Glenn Fowler <gsf@research.att.com>                  *
18*4887Schin *                  David Korn <dgk@research.att.com>                   *
19*4887Schin *                   Phong Vo <kpv@research.att.com>                    *
20*4887Schin *                                                                      *
21*4887Schin ***********************************************************************/
22*4887Schin #pragma prototyped
23*4887Schin /*
24*4887Schin  * Glenn Fowler
25*4887Schin  * AT&T Research
26*4887Schin  *
27*4887Schin  * Time_t conversion support
28*4887Schin  */
29*4887Schin 
30*4887Schin #include <tmx.h>
31*4887Schin 
32*4887Schin #include "FEATURE/tmlib"
33*4887Schin 
34*4887Schin /*
35*4887Schin  * convert Tm_t to Time_t
36*4887Schin  *
37*4887Schin  * if west==TM_LOCALZONE then the local timezone is used
38*4887Schin  * otherwise west is the number of minutes west
39*4887Schin  * of GMT with DST taken into account
40*4887Schin  *
41*4887Schin  * this routine works with a copy of Tm_t to avoid clashes
42*4887Schin  * with other tm*() that may return static Tm_t*
43*4887Schin  */
44*4887Schin 
45*4887Schin Time_t
46*4887Schin tmxtime(register Tm_t* tm, int west)
47*4887Schin {
48*4887Schin 	register Time_t		t;
49*4887Schin 	register Tm_leap_t*	lp;
50*4887Schin 	register int32_t	y;
51*4887Schin 	int			n;
52*4887Schin 	int			sec;
53*4887Schin 	time_t			now;
54*4887Schin 	struct tm*		tl;
55*4887Schin 	Tm_t*			to;
56*4887Schin 	Tm_t			ts;
57*4887Schin 
58*4887Schin 	ts = *tm;
59*4887Schin 	to = tm;
60*4887Schin 	tm = &ts;
61*4887Schin 	tmset(tm_info.zone);
62*4887Schin 	tmfix(tm);
63*4887Schin 	y = tm->tm_year;
64*4887Schin 	if (y < 69 || y > (TMX_MAXYEAR - 1900))
65*4887Schin 		return TMX_NOTIME;
66*4887Schin 	y--;
67*4887Schin 	t = y * 365 + y / 4 - y / 100 + (y + (1900 - 1600)) / 400 - (1970 - 1901) * 365 - (1970 - 1901) / 4;
68*4887Schin 	if ((n = tm->tm_mon) > 11)
69*4887Schin 		n = 11;
70*4887Schin 	y += 1901;
71*4887Schin 	if (n > 1 && tmisleapyear(y))
72*4887Schin 		t++;
73*4887Schin 	t += tm_data.sum[n] + tm->tm_mday - 1;
74*4887Schin 	t *= 24;
75*4887Schin 	t += tm->tm_hour;
76*4887Schin 	t *= 60;
77*4887Schin 	t += tm->tm_min;
78*4887Schin 	t *= 60;
79*4887Schin 	t += sec = tm->tm_sec;
80*4887Schin 	if (west != TM_UTCZONE && !(tm_info.flags & TM_UTC))
81*4887Schin 	{
82*4887Schin 		/*
83*4887Schin 		 * time zone adjustments
84*4887Schin 		 */
85*4887Schin 
86*4887Schin 		if (west == TM_LOCALZONE)
87*4887Schin 		{
88*4887Schin 			t += tm_info.zone->west * 60;
89*4887Schin 			if (!tm_info.zone->daylight)
90*4887Schin 				tm->tm_isdst = 0;
91*4887Schin 			else
92*4887Schin 			{
93*4887Schin 				y = tm->tm_year;
94*4887Schin 				tm->tm_year = tmequiv(tm) - 1900;
95*4887Schin 				now = tmxsec(tmxtime(tm, tm_info.zone->west));
96*4887Schin 				tm->tm_year = y;
97*4887Schin 				if (!(tl = tmlocaltime(&now)))
98*4887Schin 					return TMX_NOTIME;
99*4887Schin 				if (tm->tm_isdst = tl->tm_isdst)
100*4887Schin 					t += tm_info.zone->dst * 60;
101*4887Schin 			}
102*4887Schin 		}
103*4887Schin 		else
104*4887Schin 		{
105*4887Schin 			t += west * 60;
106*4887Schin 			if (!tm_info.zone->daylight)
107*4887Schin 				tm->tm_isdst = 0;
108*4887Schin 			else if (tm->tm_isdst < 0)
109*4887Schin 			{
110*4887Schin 				y = tm->tm_year;
111*4887Schin 				tm->tm_year = tmequiv(tm) - 1900;
112*4887Schin 				tm->tm_isdst = 0;
113*4887Schin 				now = tmxsec(tmxtime(tm, tm_info.zone->west));
114*4887Schin 				tm->tm_year = y;
115*4887Schin 				if (!(tl = tmlocaltime(&now)))
116*4887Schin 					return TMX_NOTIME;
117*4887Schin 				tm->tm_isdst = tl->tm_isdst;
118*4887Schin 			}
119*4887Schin 		}
120*4887Schin 	}
121*4887Schin 	else if (tm->tm_isdst)
122*4887Schin 		tm->tm_isdst = 0;
123*4887Schin 	*to = *tm;
124*4887Schin 	if (tm_info.flags & TM_LEAP)
125*4887Schin 	{
126*4887Schin 		/*
127*4887Schin 		 * leap second adjustments
128*4887Schin 		 */
129*4887Schin 
130*4887Schin 		for (lp = &tm_data.leap[0]; t < lp->time - (lp+1)->total; lp++);
131*4887Schin 		t += lp->total;
132*4887Schin 		n = lp->total - (lp+1)->total;
133*4887Schin 		if (t <= (lp->time + n) && (n > 0 && sec > 59 || n < 0 && sec > (59 + n) && sec <= 59))
134*4887Schin 			t -= n;
135*4887Schin 	}
136*4887Schin 	return tmxsns(t, tm->tm_nsec);
137*4887Schin }
138