xref: /onnv-gate/usr/src/lib/libast/common/tm/tmxmake.c (revision 8462:6e341f5569ba)
14887Schin /***********************************************************************
24887Schin *                                                                      *
34887Schin *               This software is part of the ast package               *
4*8462SApril.Chin@Sun.COM *          Copyright (c) 1985-2008 AT&T Intellectual Property          *
54887Schin *                      and is licensed under the                       *
64887Schin *                  Common Public License, Version 1.0                  *
7*8462SApril.Chin@Sun.COM *                    by AT&T Intellectual Property                     *
84887Schin *                                                                      *
94887Schin *                A copy of the License is available at                 *
104887Schin *            http://www.opensource.org/licenses/cpl1.0.txt             *
114887Schin *         (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9)         *
124887Schin *                                                                      *
134887Schin *              Information and Software Systems Research               *
144887Schin *                            AT&T Research                             *
154887Schin *                           Florham Park NJ                            *
164887Schin *                                                                      *
174887Schin *                 Glenn Fowler <gsf@research.att.com>                  *
184887Schin *                  David Korn <dgk@research.att.com>                   *
194887Schin *                   Phong Vo <kpv@research.att.com>                    *
204887Schin *                                                                      *
214887Schin ***********************************************************************/
224887Schin #pragma prototyped
234887Schin /*
244887Schin  * Glenn Fowler
254887Schin  * AT&T Research
264887Schin  *
274887Schin  * Time_t conversion support
284887Schin  */
294887Schin 
304887Schin #include <tmx.h>
314887Schin 
324887Schin #include "FEATURE/tmlib"
334887Schin 
344887Schin /*
354887Schin  * return Tm_t for t
364887Schin  * time zone and leap seconds accounted for in return value
374887Schin  */
384887Schin 
394887Schin Tm_t*
404887Schin tmxmake(Time_t t)
414887Schin {
424887Schin 	register struct tm*	tp;
434887Schin 	register Tm_leap_t*	lp;
444887Schin 	Time_t			x;
454887Schin 	time_t			now;
464887Schin 	int			leapsec;
474887Schin 	int			y;
484887Schin 	uint32_t		n;
494887Schin 	int32_t			o;
504887Schin #if TMX_FLOAT
514887Schin 	Time_t			z;
524887Schin 	uint32_t		i;
534887Schin #endif
544887Schin 	Tm_t			tm;
554887Schin 
564887Schin 	static Tm_t		ts;
574887Schin 
584887Schin 	tmset(tm_info.zone);
594887Schin 	leapsec = 0;
604887Schin 	if ((tm_info.flags & (TM_ADJUST|TM_LEAP)) == (TM_ADJUST|TM_LEAP) && (n = tmxsec(t)))
614887Schin 	{
624887Schin 		for (lp = &tm_data.leap[0]; n < lp->time; lp++);
634887Schin 		if (lp->total)
644887Schin 		{
654887Schin 			if (n == lp->time && (leapsec = (lp->total - (lp+1)->total)) < 0)
664887Schin 				leapsec = 0;
674887Schin 			t = tmxsns(n - lp->total, tmxnsec(t));
684887Schin 		}
694887Schin 	}
704887Schin 	x = tmxsec(t);
714887Schin 	if (tm_info.flags & TM_UTC)
724887Schin 	{
734887Schin 		tm.tm_zone = &tm_data.zone[2];
744887Schin 		o = 0;
754887Schin 	}
764887Schin 	else
774887Schin 	{
784887Schin 		tm.tm_zone = tm_info.zone;
794887Schin 		o = 60 * tm.tm_zone->west;
804887Schin 		if (x > o)
814887Schin 		{
824887Schin 			x -= o;
834887Schin 			o = 0;
844887Schin 		}
854887Schin 	}
864887Schin #if TMX_FLOAT
874887Schin 	i = x / (24 * 60 * 60);
884887Schin 	z = i;
894887Schin 	n = x - z * (24 * 60 * 60);
904887Schin 	tm.tm_sec = n % 60 + leapsec;
914887Schin 	n /= 60;
924887Schin 	tm.tm_min = n % 60;
934887Schin 	n /= 60;
944887Schin 	tm.tm_hour = n % 24;
954887Schin #define x	i
964887Schin #else
974887Schin 	tm.tm_sec = x % 60 + leapsec;
984887Schin 	x /= 60;
994887Schin 	tm.tm_min = x % 60;
1004887Schin 	x /= 60;
1014887Schin 	tm.tm_hour = x % 24;
1024887Schin 	x /= 24;
1034887Schin #endif
1044887Schin 	tm.tm_wday = (x + 4) % 7;
1054887Schin 	tm.tm_year = (400 * (x + 25202)) / 146097 + 1;
1064887Schin 	n = tm.tm_year - 1;
1074887Schin 	x -= n * 365 + n / 4 - n / 100 + (n + (1900 - 1600)) / 400 - (1970 - 1901) * 365 - (1970 - 1901) / 4;
1084887Schin 	tm.tm_mon = 0;
1094887Schin 	tm.tm_mday = x + 1;
1104887Schin 	tmfix(&tm);
1114887Schin 	n += 1900;
1124887Schin 	tm.tm_isdst = 0;
1134887Schin 	if (tm.tm_zone->daylight)
1144887Schin 	{
1154887Schin 		if ((y = tmequiv(&tm) - 1900) == tm.tm_year)
1164887Schin 			now = tmxsec(t);
1174887Schin 		else
1184887Schin 		{
1194887Schin 			Tm_t	te;
1204887Schin 
1214887Schin 			te = tm;
1224887Schin 			te.tm_year = y;
1234887Schin 			now = tmxsec(tmxtime(&te, tm.tm_zone->west));
1244887Schin 		}
1254887Schin 		if ((tp = tmlocaltime(&now)) && ((tm.tm_isdst = tp->tm_isdst) || o))
1264887Schin 		{
1274887Schin 			tm.tm_min -= o / 60 + (tm.tm_isdst ? tm.tm_zone->dst : 0);
1284887Schin 			tmfix(&tm);
1294887Schin 		}
1304887Schin 	}
1314887Schin 	tm.tm_nsec = tmxnsec(t);
1324887Schin 	ts = tm;
1334887Schin 	return &ts;
1344887Schin }
135