xref: /onnv-gate/usr/src/lib/libast/common/tm/tmxmake.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  * return Tm_t for t
36*4887Schin  * time zone and leap seconds accounted for in return value
37*4887Schin  */
38*4887Schin 
39*4887Schin Tm_t*
40*4887Schin tmxmake(Time_t t)
41*4887Schin {
42*4887Schin 	register struct tm*	tp;
43*4887Schin 	register Tm_leap_t*	lp;
44*4887Schin 	Time_t			x;
45*4887Schin 	time_t			now;
46*4887Schin 	int			leapsec;
47*4887Schin 	int			y;
48*4887Schin 	uint32_t		n;
49*4887Schin 	int32_t			o;
50*4887Schin #if TMX_FLOAT
51*4887Schin 	Time_t			z;
52*4887Schin 	uint32_t		i;
53*4887Schin #endif
54*4887Schin 	Tm_t			tm;
55*4887Schin 
56*4887Schin 	static Tm_t		ts;
57*4887Schin 
58*4887Schin 	tmset(tm_info.zone);
59*4887Schin 	leapsec = 0;
60*4887Schin 	if ((tm_info.flags & (TM_ADJUST|TM_LEAP)) == (TM_ADJUST|TM_LEAP) && (n = tmxsec(t)))
61*4887Schin 	{
62*4887Schin 		for (lp = &tm_data.leap[0]; n < lp->time; lp++);
63*4887Schin 		if (lp->total)
64*4887Schin 		{
65*4887Schin 			if (n == lp->time && (leapsec = (lp->total - (lp+1)->total)) < 0)
66*4887Schin 				leapsec = 0;
67*4887Schin 			t = tmxsns(n - lp->total, tmxnsec(t));
68*4887Schin 		}
69*4887Schin 	}
70*4887Schin 	x = tmxsec(t);
71*4887Schin 	if (tm_info.flags & TM_UTC)
72*4887Schin 	{
73*4887Schin 		tm.tm_zone = &tm_data.zone[2];
74*4887Schin 		o = 0;
75*4887Schin 	}
76*4887Schin 	else
77*4887Schin 	{
78*4887Schin 		tm.tm_zone = tm_info.zone;
79*4887Schin 		o = 60 * tm.tm_zone->west;
80*4887Schin 		if (x > o)
81*4887Schin 		{
82*4887Schin 			x -= o;
83*4887Schin 			o = 0;
84*4887Schin 		}
85*4887Schin 	}
86*4887Schin #if TMX_FLOAT
87*4887Schin 	i = x / (24 * 60 * 60);
88*4887Schin 	z = i;
89*4887Schin 	n = x - z * (24 * 60 * 60);
90*4887Schin 	tm.tm_sec = n % 60 + leapsec;
91*4887Schin 	n /= 60;
92*4887Schin 	tm.tm_min = n % 60;
93*4887Schin 	n /= 60;
94*4887Schin 	tm.tm_hour = n % 24;
95*4887Schin #define x	i
96*4887Schin #else
97*4887Schin 	tm.tm_sec = x % 60 + leapsec;
98*4887Schin 	x /= 60;
99*4887Schin 	tm.tm_min = x % 60;
100*4887Schin 	x /= 60;
101*4887Schin 	tm.tm_hour = x % 24;
102*4887Schin 	x /= 24;
103*4887Schin #endif
104*4887Schin 	tm.tm_wday = (x + 4) % 7;
105*4887Schin 	tm.tm_year = (400 * (x + 25202)) / 146097 + 1;
106*4887Schin 	n = tm.tm_year - 1;
107*4887Schin 	x -= n * 365 + n / 4 - n / 100 + (n + (1900 - 1600)) / 400 - (1970 - 1901) * 365 - (1970 - 1901) / 4;
108*4887Schin 	tm.tm_mon = 0;
109*4887Schin 	tm.tm_mday = x + 1;
110*4887Schin 	tmfix(&tm);
111*4887Schin 	n += 1900;
112*4887Schin 	tm.tm_isdst = 0;
113*4887Schin 	if (tm.tm_zone->daylight)
114*4887Schin 	{
115*4887Schin 		if ((y = tmequiv(&tm) - 1900) == tm.tm_year)
116*4887Schin 			now = tmxsec(t);
117*4887Schin 		else
118*4887Schin 		{
119*4887Schin 			Tm_t	te;
120*4887Schin 
121*4887Schin 			te = tm;
122*4887Schin 			te.tm_year = y;
123*4887Schin 			now = tmxsec(tmxtime(&te, tm.tm_zone->west));
124*4887Schin 		}
125*4887Schin 		if ((tp = tmlocaltime(&now)) && ((tm.tm_isdst = tp->tm_isdst) || o))
126*4887Schin 		{
127*4887Schin 			tm.tm_min -= o / 60 + (tm.tm_isdst ? tm.tm_zone->dst : 0);
128*4887Schin 			tmfix(&tm);
129*4887Schin 		}
130*4887Schin 	}
131*4887Schin 	tm.tm_nsec = tmxnsec(t);
132*4887Schin 	ts = tm;
133*4887Schin 	return &ts;
134*4887Schin }
135