xref: /illumos-gate/usr/src/contrib/ast/src/lib/libast/tm/tmfix.c (revision 16b76d3cb933ff92018a2a75594449010192eacb)
1 /***********************************************************************
2 *                                                                      *
3 *               This software is part of the ast package               *
4 *          Copyright (c) 1985-2011 AT&T Intellectual Property          *
5 *                      and is licensed under the                       *
6 *                 Eclipse Public License, Version 1.0                  *
7 *                    by AT&T Intellectual Property                     *
8 *                                                                      *
9 *                A copy of the License is available at                 *
10 *          http://www.eclipse.org/org/documents/epl-v10.html           *
11 *         (with md5 checksum b35adb5213ca9657e911e9befb180842)         *
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 <tmx.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 tmxtm() 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_nsec) < 0)
69 	{
70 		tm->tm_sec -= (TMX_RESOLUTION - n) / TMX_RESOLUTION;
71 		tm->tm_nsec = TMX_RESOLUTION - (-n) % TMX_RESOLUTION;
72 	}
73 	else if (n >= TMX_RESOLUTION)
74 	{
75 		tm->tm_sec += n / TMX_RESOLUTION;
76 		tm->tm_nsec %= TMX_RESOLUTION;
77 	}
78 	if ((n = tm->tm_sec) < 0)
79 	{
80 		tm->tm_min -= (60 - n) / 60;
81 		tm->tm_sec = 60 - (-n) % 60;
82 	}
83 	else if (n > (59 + TM_MAXLEAP))
84 	{
85 		tm->tm_min += n / 60;
86 		tm->tm_sec %= 60;
87 	}
88 	if ((n = tm->tm_min) < 0)
89 	{
90 		tm->tm_hour -= (60 - n) / 60;
91 		n = tm->tm_min = 60 - (-n) % 60;
92 	}
93 	if (n > 59)
94 	{
95 		tm->tm_hour += n / 60;
96 		tm->tm_min %= 60;
97 	}
98 	if ((n = tm->tm_hour) < 0)
99 	{
100 		tm->tm_mday -= (23 - n) / 24;
101 		tm->tm_hour = 24 - (-n) % 24;
102 	}
103 	else if (n >= 24)
104 	{
105 		tm->tm_mday += n / 24;
106 		tm->tm_hour %= 24;
107 	}
108 	if (tm->tm_mon >= 12)
109 	{
110 		tm->tm_year += tm->tm_mon / 12;
111 		tm->tm_mon %= 12;
112 	}
113 	else if (tm->tm_mon < 0)
114 	{
115 		tm->tm_year--;
116 		if ((tm->tm_mon += 12) < 0)
117 		{
118 			tm->tm_year += tm->tm_mon / 12;
119 			tm->tm_mon = (-tm->tm_mon) % 12;
120 		}
121 	}
122 	while (tm->tm_mday < -365)
123 	{
124 		tm->tm_year--;
125 		tm->tm_mday += 365 + LEAP(tm);
126 	}
127 	while (tm->tm_mday > 365)
128 	{
129 		tm->tm_mday -= 365 + LEAP(tm);
130 		tm->tm_year++;
131 	}
132 	while (tm->tm_mday < 1)
133 	{
134 		if (--tm->tm_mon < 0)
135 		{
136 			tm->tm_mon = 11;
137 			tm->tm_year--;
138 		}
139 		tm->tm_mday += DAYS(tm);
140 	}
141 	while (tm->tm_mday > (n = DAYS(tm)))
142 	{
143 		tm->tm_mday -= n;
144 		if (++tm->tm_mon > 11)
145 		{
146 			tm->tm_mon = 0;
147 			tm->tm_year++;
148 		}
149 	}
150 	if (w)
151 	{
152 		w = tm->tm_wday;
153 		t = tmtime(tm, TM_LOCALZONE);
154 		p = tmmake(&t);
155 		if (w = (w - p->tm_wday))
156 		{
157 			if (w < 0)
158 				w += 7;
159 			tm->tm_wday += w;
160 			if ((tm->tm_mday += w) > DAYS(tm))
161 				tm->tm_mday -= 7;
162 		}
163 	}
164 	tm->tm_yday = tm_data.sum[tm->tm_mon] + (tm->tm_mon > 1 && LEAP(tm)) + tm->tm_mday - 1;
165 	n = tm->tm_year + 1900 - 1;
166 	tm->tm_wday = (n + n / 4 - n / 100 + n / 400 + tm->tm_yday + 1) % 7;
167 
168 	/*
169 	 * tm_isdst is adjusted by tmtime()
170 	 */
171 
172 	return tm;
173 }
174