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