xref: /titanic_50/usr/src/lib/libast/common/tm/tmfix.c (revision 67e3a03ed4a2813074d36330f062ed6e593a4937)
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 conversion support
28  */
29 
30 #include <ast.h>
31 #include <tm.h>
32 
33 #define DAYS(p)	(tm_data.days[(p)->tm_mon]+((p)->tm_mon==1&&LEAP(p)))
34 #define LEAP(p)	(tmisleapyear((p)->tm_year))
35 
36 /*
37  * correct out of bounds fields in tm
38  *
39  * tm_isdst is not changed -- call tmxmake() to get that
40  *
41  * tm is the return value
42  */
43 
44 Tm_t*
45 tmfix(register Tm_t* tm)
46 {
47 	register int	n;
48 	register int	w;
49 	Tm_t*		p;
50 	time_t		t;
51 
52 	/*
53 	 * check for special case that adjusts tm_wday at the end
54 	 * this happens during
55 	 *	nl_langinfo() => strftime() => tmfmt()
56 	 */
57 
58 	if (w = !tm->tm_sec && !tm->tm_min && !tm->tm_mday && !tm->tm_year && !tm->tm_yday && !tm->tm_isdst)
59 	{
60 		tm->tm_year = 99;
61 		tm->tm_mday = 2;
62 	}
63 
64 	/*
65 	 * adjust from shortest to longest units
66 	 */
67 
68 	if ((n = tm->tm_sec) < 0)
69 	{
70 		tm->tm_min -= (60 - n) / 60;
71 		tm->tm_sec = 60 - (-n) % 60;
72 	}
73 	else if (n > (59 + TM_MAXLEAP))
74 	{
75 		tm->tm_min += n / 60;
76 		tm->tm_sec %= 60;
77 	}
78 	if ((n = tm->tm_min) < 0)
79 	{
80 		tm->tm_hour -= (60 - n) / 60;
81 		n = tm->tm_min = 60 - (-n) % 60;
82 	}
83 	if (n > 59)
84 	{
85 		tm->tm_hour += n / 60;
86 		tm->tm_min %= 60;
87 	}
88 	if ((n = tm->tm_hour) < 0)
89 	{
90 		tm->tm_mday -= (23 - n) / 24;
91 		tm->tm_hour = 24 - (-n) % 24;
92 	}
93 	else if (n >= 24)
94 	{
95 		tm->tm_mday += n / 24;
96 		tm->tm_hour %= 24;
97 	}
98 	if (tm->tm_mon >= 12)
99 	{
100 		tm->tm_year += tm->tm_mon / 12;
101 		tm->tm_mon %= 12;
102 	}
103 	else if (tm->tm_mon < 0)
104 	{
105 		tm->tm_year--;
106 		if ((tm->tm_mon += 12) < 0)
107 		{
108 			tm->tm_year += tm->tm_mon / 12;
109 			tm->tm_mon = (-tm->tm_mon) % 12;
110 		}
111 	}
112 	while (tm->tm_mday < -365)
113 	{
114 		tm->tm_year--;
115 		tm->tm_mday += 365 + LEAP(tm);
116 	}
117 	while (tm->tm_mday > 365)
118 	{
119 		tm->tm_mday -= 365 + LEAP(tm);
120 		tm->tm_year++;
121 	}
122 	while (tm->tm_mday < 1)
123 	{
124 		if (--tm->tm_mon < 0)
125 		{
126 			tm->tm_mon = 11;
127 			tm->tm_year--;
128 		}
129 		tm->tm_mday += DAYS(tm);
130 	}
131 	while (tm->tm_mday > (n = DAYS(tm)))
132 	{
133 		tm->tm_mday -= n;
134 		if (++tm->tm_mon > 11)
135 		{
136 			tm->tm_mon = 0;
137 			tm->tm_year++;
138 		}
139 	}
140 	if (w)
141 	{
142 		w = tm->tm_wday;
143 		t = tmtime(tm, TM_LOCALZONE);
144 		p = tmmake(&t);
145 		if (w = (w - p->tm_wday))
146 		{
147 			if (w < 0)
148 				w += 7;
149 			tm->tm_wday += w;
150 			if ((tm->tm_mday += w) > DAYS(tm))
151 				tm->tm_mday -= 7;
152 		}
153 	}
154 	tm->tm_yday = tm_data.sum[tm->tm_mon] + (tm->tm_mon > 1 && LEAP(tm)) + tm->tm_mday - 1;
155 	n = tm->tm_year + 1900 - 1;
156 	tm->tm_wday = (n + n / 4 - n / 100 + n / 400 + tm->tm_yday + 1) % 7;
157 
158 	/*
159 	 * tm_isdst is adjusted by tmtime()
160 	 */
161 
162 	return tm;
163 }
164