xref: /titanic_41/usr/src/lib/libast/common/tm/tmxmake.c (revision 3fb517f786391b507780c78aabb8d98bfea9efe9)
1 /***********************************************************************
2 *                                                                      *
3 *               This software is part of the ast package               *
4 *          Copyright (c) 1985-2010 AT&T Intellectual Property          *
5 *                      and is licensed under the                       *
6 *                  Common Public License, Version 1.0                  *
7 *                    by AT&T Intellectual Property                     *
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 tmxtm(register Tm_t* tm, Time_t t, Tm_zone_t* zone)
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 
55 	tmset(tm_info.zone);
56 	leapsec = 0;
57 	if ((tm_info.flags & (TM_ADJUST|TM_LEAP)) == (TM_ADJUST|TM_LEAP) && (n = tmxsec(t)))
58 	{
59 		for (lp = &tm_data.leap[0]; n < lp->time; lp++);
60 		if (lp->total)
61 		{
62 			if (n == lp->time && (leapsec = (lp->total - (lp+1)->total)) < 0)
63 				leapsec = 0;
64 			t = tmxsns(n - lp->total, tmxnsec(t));
65 		}
66 	}
67 	x = tmxsec(t);
68 	if (!(tm->tm_zone = zone))
69 	{
70 		if (tm_info.flags & TM_UTC)
71 			tm->tm_zone = &tm_data.zone[2];
72 		else
73 			tm->tm_zone = tm_info.zone;
74 	}
75 	if ((o = 60 * tm->tm_zone->west) && x > o)
76 	{
77 		x -= o;
78 		o = 0;
79 	}
80 #if TMX_FLOAT
81 	i = x / (24 * 60 * 60);
82 	z = i;
83 	n = x - z * (24 * 60 * 60);
84 	tm->tm_sec = n % 60 + leapsec;
85 	n /= 60;
86 	tm->tm_min = n % 60;
87 	n /= 60;
88 	tm->tm_hour = n % 24;
89 #define x	i
90 #else
91 	tm->tm_sec = x % 60 + leapsec;
92 	x /= 60;
93 	tm->tm_min = x % 60;
94 	x /= 60;
95 	tm->tm_hour = x % 24;
96 	x /= 24;
97 #endif
98 	tm->tm_wday = (x + 4) % 7;
99 	tm->tm_year = (400 * (x + 25202)) / 146097 + 1;
100 	n = tm->tm_year - 1;
101 	x -= n * 365 + n / 4 - n / 100 + (n + (1900 - 1600)) / 400 - (1970 - 1901) * 365 - (1970 - 1901) / 4;
102 	tm->tm_mon = 0;
103 	tm->tm_mday = x + 1;
104 	tm->tm_nsec = tmxnsec(t);
105 	tmfix(tm);
106 	n += 1900;
107 	tm->tm_isdst = 0;
108 	if (tm->tm_zone->daylight)
109 	{
110 		if ((y = tmequiv(tm) - 1900) == tm->tm_year)
111 			now = tmxsec(t);
112 		else
113 		{
114 			Tm_t	te;
115 
116 			te = *tm;
117 			te.tm_year = y;
118 			now = tmxsec(tmxtime(&te, tm->tm_zone->west));
119 		}
120 		if ((tp = tmlocaltime(&now)) && ((tm->tm_isdst = tp->tm_isdst) || o))
121 		{
122 			tm->tm_min -= o / 60 + (tm->tm_isdst ? tm->tm_zone->dst : 0);
123 			tmfix(tm);
124 		}
125 	}
126 	return tm;
127 }
128 
129 /*
130  * return Tm_t for t
131  * time zone and leap seconds accounted for in return value
132  */
133 
134 Tm_t*
135 tmxmake(Time_t t)
136 {
137 	static Tm_t		ts;
138 
139 	return tmxtm(&ts, t, NiL);
140 }
141