fix aliasing violations in tsearch functions
[musl] / src / time / strftime.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <langinfo.h>
4 #include <locale.h>
5 #include <time.h>
6 #include <limits.h>
7 #include "libc.h"
8
9 // FIXME: integer overflows
10
11 const char *__nl_langinfo_l(nl_item, locale_t);
12
13 static int is_leap(int y)
14 {
15         /* Avoid overflow */
16         if (y>INT_MAX-1900) y -= 2000;
17         y += 1900;
18         return !(y%4) && ((y%100) || !(y%400));
19 }
20
21 static int week_num(const struct tm *tm)
22 {
23         int val = (tm->tm_yday + 7 - (tm->tm_wday+6)%7) / 7;
24         /* If 1 Jan is just 1-3 days past Monday,
25          * the previous week is also in this year. */
26         if ((tm->tm_wday - tm->tm_yday - 2 + 371) % 7 <= 2)
27                 val++;
28         if (!val) {
29                 val = 52;
30                 /* If 31 December of prev year a Thursday,
31                  * or Friday of a leap year, then the
32                  * prev year has 53 weeks. */
33                 int dec31 = (tm->tm_wday - tm->tm_yday - 1 + 7) % 7;
34                 if (dec31 == 4 || (dec31 == 5 && is_leap(tm->tm_year%400-1)))
35                         val++;
36         } else if (val == 53) {
37                 /* If 1 January is not a Thursday, and not
38                  * a Wednesday of a leap year, then this
39                  * year has only 52 weeks. */
40                 int jan1 = (tm->tm_wday - tm->tm_yday + 371) % 7;
41                 if (jan1 != 4 && (jan1 != 3 || !is_leap(tm->tm_year)))
42                         val = 1;
43         }
44         return val;
45 }
46
47 size_t __strftime_l(char *restrict s, size_t n, const char *restrict f, const struct tm *restrict tm, locale_t loc)
48 {
49         nl_item item;
50         int val;
51         const char *fmt;
52         size_t l;
53         for (l=0; *f && l<n; f++) {
54                 if (*f != '%') {
55 literal:
56                         s[l++] = *f;
57                         continue;
58                 }
59 do_fmt:
60                 switch (*++f) {
61                 case '%':
62                         goto literal;
63                 case 'E':
64                 case 'O':
65                         goto do_fmt;
66                 case 'a':
67                         item = ABDAY_1 + tm->tm_wday;
68                         goto nl_strcat;
69                 case 'A':
70                         item = DAY_1 + tm->tm_wday;
71                         goto nl_strcat;
72                 case 'h':
73                 case 'b':
74                         item = ABMON_1 + tm->tm_mon;
75                         goto nl_strcat;
76                 case 'B':
77                         item = MON_1 + tm->tm_mon;
78                         goto nl_strcat;
79                 case 'c':
80                         item = D_T_FMT;
81                         goto nl_strftime;
82                 case 'C':
83                         val = (1900+tm->tm_year) / 100;
84                         fmt = "%02d";
85                         goto number;
86                 case 'd':
87                         val = tm->tm_mday;
88                         fmt = "%02d";
89                         goto number;
90                 case 'D':
91                         fmt = "%m/%d/%y";
92                         goto recu_strftime;
93                 case 'e':
94                         val = tm->tm_mday;
95                         fmt = "%2d";
96                         goto number;
97                 case 'F':
98                         fmt = "%Y-%m-%d";
99                         goto recu_strftime;
100                 case 'g':
101                 case 'G':
102                         fmt = "%04d";
103                         val = tm->tm_year + 1900;
104                         if (tm->tm_yday < 3 && week_num(tm) != 1) val--;
105                         else if (tm->tm_yday > 360 && week_num(tm) == 1) val++;
106                         if (*f=='g') {
107                                 fmt = "%02d";
108                                 val %= 100;
109                         }
110                         goto number;
111                 case 'H':
112                         val = tm->tm_hour;
113                         fmt = "%02d";
114                         goto number;
115                 case 'I':
116                         val = tm->tm_hour;
117                         if (!val) val = 12;
118                         else if (val > 12) val -= 12;
119                         fmt = "%02d";
120                         goto number;
121                 case 'j':
122                         val = tm->tm_yday+1;
123                         fmt = "%03d";
124                         goto number;
125                 case 'm':
126                         val = tm->tm_mon+1;
127                         fmt = "%02d";
128                         goto number;
129                 case 'M':
130                         val = tm->tm_min;
131                         fmt = "%02d";
132                         goto number;
133                 case 'n':
134                         s[l++] = '\n';
135                         continue;
136                 case 'p':
137                         item = tm->tm_hour >= 12 ? PM_STR : AM_STR;
138                         goto nl_strcat;
139                 case 'r':
140                         item = T_FMT_AMPM;
141                         goto nl_strftime;
142                 case 'R':
143                         fmt = "%H:%M";
144                         goto recu_strftime;
145                 case 'S':
146                         val = tm->tm_sec;
147                         fmt = "%02d";
148                         goto number;
149                 case 't':
150                         s[l++] = '\t';
151                         continue;
152                 case 'T':
153                         fmt = "%H:%M:%S";
154                         goto recu_strftime;
155                 case 'u':
156                         val = tm->tm_wday ? tm->tm_wday : 7;
157                         fmt = "%d";
158                         goto number;
159                 case 'U':
160                         val = (tm->tm_yday + 7 - tm->tm_wday) / 7;
161                         fmt = "%02d";
162                         goto number;
163                 case 'W':
164                         val = (tm->tm_yday + 7 - (tm->tm_wday+6)%7) / 7;
165                         fmt = "%02d";
166                         goto number;
167                 case 'V':
168                         val = week_num(tm);
169                         fmt = "%02d";
170                         goto number;
171                 case 'w':
172                         val = tm->tm_wday;
173                         fmt = "%d";
174                         goto number;
175                 case 'x':
176                         item = D_FMT;
177                         goto nl_strftime;
178                 case 'X':
179                         item = T_FMT;
180                         goto nl_strftime;
181                 case 'y':
182                         val = tm->tm_year % 100;
183                         fmt = "%02d";
184                         goto number;
185                 case 'Y':
186                         val = tm->tm_year + 1900;
187                         fmt = "%04d";
188                         goto number;
189                 case 'z':
190                         val = -tm->__tm_gmtoff;
191                         l += snprintf(s+l, n-l, "%+.2d%.2d", val/3600, abs(val%3600)/60);
192                         continue;
193                 case 'Z':
194                         l += snprintf(s+l, n-l, "%s", tm->__tm_zone);
195                         continue;
196                 default:
197                         return 0;
198                 }
199 number:
200                 l += snprintf(s+l, n-l, fmt, val);
201                 continue;
202 nl_strcat:
203                 l += snprintf(s+l, n-l, "%s", __nl_langinfo_l(item, loc));
204                 continue;
205 nl_strftime:
206                 fmt = __nl_langinfo_l(item, loc);
207 recu_strftime:
208                 l += __strftime_l(s+l, n-l, fmt, tm, loc);
209         }
210         if (l >= n) return 0;
211         s[l] = 0;
212         return l;
213 }
214
215 size_t strftime(char *restrict s, size_t n, const char *restrict f, const struct tm *restrict tm)
216 {
217         return __strftime_l(s, n, f, tm, 0);
218 }
219
220 weak_alias(__strftime_l, strftime_l);