sd: remove 'ssd' driver support
[unleashed/tickless.git] / usr / src / lib / libast / common / tm / tmfix.c
blob6095b7ec6d5eb5b7098ff86978c0631bf309bee8
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
24 * Glenn Fowler
25 * AT&T Research
27 * time conversion support
30 #include <ast.h>
31 #include <tmx.h>
33 #define DAYS(p) (tm_data.days[(p)->tm_mon]+((p)->tm_mon==1&&LEAP(p)))
34 #define LEAP(p) (tmisleapyear((p)->tm_year))
37 * correct out of bounds fields in tm
39 * tm_isdst is not changed -- call tmxtm() to get that
41 * tm is the return value
44 Tm_t*
45 tmfix(register Tm_t* tm)
47 register int n;
48 register int w;
49 Tm_t* p;
50 time_t t;
53 * check for special case that adjusts tm_wday at the end
54 * this happens during
55 * nl_langinfo() => strftime() => tmfmt()
58 if (w = !tm->tm_sec && !tm->tm_min && !tm->tm_mday && !tm->tm_year && !tm->tm_yday && !tm->tm_isdst)
60 tm->tm_year = 99;
61 tm->tm_mday = 2;
65 * adjust from shortest to longest units
68 if ((n = tm->tm_nsec) < 0)
70 tm->tm_sec -= (TMX_RESOLUTION - n) / TMX_RESOLUTION;
71 tm->tm_nsec = TMX_RESOLUTION - (-n) % TMX_RESOLUTION;
73 else if (n >= TMX_RESOLUTION)
75 tm->tm_sec += n / TMX_RESOLUTION;
76 tm->tm_nsec %= TMX_RESOLUTION;
78 if ((n = tm->tm_sec) < 0)
80 tm->tm_min -= (60 - n) / 60;
81 tm->tm_sec = 60 - (-n) % 60;
83 else if (n > (59 + TM_MAXLEAP))
85 tm->tm_min += n / 60;
86 tm->tm_sec %= 60;
88 if ((n = tm->tm_min) < 0)
90 tm->tm_hour -= (60 - n) / 60;
91 n = tm->tm_min = 60 - (-n) % 60;
93 if (n > 59)
95 tm->tm_hour += n / 60;
96 tm->tm_min %= 60;
98 if ((n = tm->tm_hour) < 0)
100 tm->tm_mday -= (23 - n) / 24;
101 tm->tm_hour = 24 - (-n) % 24;
103 else if (n >= 24)
105 tm->tm_mday += n / 24;
106 tm->tm_hour %= 24;
108 if (tm->tm_mon >= 12)
110 tm->tm_year += tm->tm_mon / 12;
111 tm->tm_mon %= 12;
113 else if (tm->tm_mon < 0)
115 tm->tm_year--;
116 if ((tm->tm_mon += 12) < 0)
118 tm->tm_year += tm->tm_mon / 12;
119 tm->tm_mon = (-tm->tm_mon) % 12;
122 while (tm->tm_mday < -365)
124 tm->tm_year--;
125 tm->tm_mday += 365 + LEAP(tm);
127 while (tm->tm_mday > 365)
129 tm->tm_mday -= 365 + LEAP(tm);
130 tm->tm_year++;
132 while (tm->tm_mday < 1)
134 if (--tm->tm_mon < 0)
136 tm->tm_mon = 11;
137 tm->tm_year--;
139 tm->tm_mday += DAYS(tm);
141 while (tm->tm_mday > (n = DAYS(tm)))
143 tm->tm_mday -= n;
144 if (++tm->tm_mon > 11)
146 tm->tm_mon = 0;
147 tm->tm_year++;
150 if (w)
152 w = tm->tm_wday;
153 t = tmtime(tm, TM_LOCALZONE);
154 p = tmmake(&t);
155 if (w = (w - p->tm_wday))
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;
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;
169 * tm_isdst is adjusted by tmtime()
172 return tm;