rework langinfo code for ABI compat and for use by time code
[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 do_fmt:
56                 switch (*++f) {
57                 case '%':
58                         goto literal;
59                 case 'E':
60                 case 'O':
61                         goto do_fmt;
62                 case 'a':
63                         item = ABDAY_1 + tm->tm_wday;
64                         goto nl_strcat;
65                 case 'A':
66                         item = DAY_1 + tm->tm_wday;
67                         goto nl_strcat;
68                 case 'h':
69                 case 'b':
70                         item = ABMON_1 + tm->tm_mon;
71                         goto nl_strcat;
72                 case 'B':
73                         item = MON_1 + tm->tm_mon;
74                         goto nl_strcat;
75                 case 'c':
76                         item = D_T_FMT;
77                         goto nl_strftime;
78                 case 'C':
79                         val = (1900+tm->tm_year) / 100;
80                         fmt = "%02d";
81                         goto number;
82                 case 'd':
83                         val = tm->tm_mday;
84                         fmt = "%02d";
85                         goto number;
86                 case 'D':
87                         fmt = "%m/%d/%y";
88                         goto recu_strftime;
89                 case 'e':
90                         val = tm->tm_mday;
91                         fmt = "%2d";
92                         goto number;
93                 case 'F':
94                         fmt = "%Y-%m-%d";
95                         goto recu_strftime;
96                 case 'g':
97                 case 'G':
98                         fmt = "%04d";
99                         val = tm->tm_year + 1900;
100                         if (tm->tm_yday < 3 && week_num(tm) != 1) val--;
101                         else if (tm->tm_yday > 360 && week_num(tm) == 1) val++;
102                         if (*f=='g') {
103                                 fmt = "%02d";
104                                 val %= 100;
105                         }
106                         goto number;
107                 case 'H':
108                         val = tm->tm_hour;
109                         fmt = "%02d";
110                         goto number;
111                 case 'I':
112                         val = tm->tm_hour;
113                         if (!val) val = 12;
114                         else if (val > 12) val -= 12;
115                         fmt = "%02d";
116                         goto number;
117                 case 'j':
118                         val = tm->tm_yday+1;
119                         fmt = "%03d";
120                         goto number;
121                 case 'm':
122                         val = tm->tm_mon+1;
123                         fmt = "%02d";
124                         goto number;
125                 case 'M':
126                         val = tm->tm_min;
127                         fmt = "%02d";
128                         goto number;
129                 case 'n':
130                         s[l++] = '\n';
131                         continue;
132                 case 'p':
133                         item = tm->tm_hour >= 12 ? PM_STR : AM_STR;
134                         goto nl_strcat;
135                 case 'r':
136                         item = T_FMT_AMPM;
137                         goto nl_strftime;
138                 case 'R':
139                         fmt = "%H:%M";
140                         goto recu_strftime;
141                 case 'S':
142                         val = tm->tm_sec;
143                         fmt = "%02d";
144                         goto number;
145                 case 't':
146                         s[l++] = '\t';
147                         continue;
148                 case 'T':
149                         fmt = "%H:%M:%S";
150                         goto recu_strftime;
151                 case 'u':
152                         val = tm->tm_wday ? tm->tm_wday : 7;
153                         fmt = "%d";
154                         goto number;
155                 case 'U':
156                         val = (tm->tm_yday + 7 - tm->tm_wday) / 7;
157                         fmt = "%02d";
158                         goto number;
159                 case 'W':
160                         val = (tm->tm_yday + 7 - (tm->tm_wday+6)%7) / 7;
161                         fmt = "%02d";
162                         goto number;
163                 case 'V':
164                         val = week_num(tm);
165                         fmt = "%02d";
166                         goto number;
167                 case 'w':
168                         val = tm->tm_wday;
169                         fmt = "%d";
170                         goto number;
171                 case 'x':
172                         item = D_FMT;
173                         goto nl_strftime;
174                 case 'X':
175                         item = T_FMT;
176                         goto nl_strftime;
177                 case 'y':
178                         val = tm->tm_year % 100;
179                         fmt = "%02d";
180                         goto number;
181                 case 'Y':
182                         val = tm->tm_year + 1900;
183                         fmt = "%04d";
184                         goto number;
185                 case 'z':
186                         val = -tm->__tm_gmtoff;
187                         l += snprintf(s+l, n-l, "%+.2d%.2d", val/3600, abs(val%3600)/60);
188                         continue;
189                 case 'Z':
190                         l += snprintf(s+l, n-l, "%s", tm->__tm_zone);
191                         continue;
192                 default:
193                         return 0;
194                 }
195                 }
196 literal:
197                 s[l++] = *f;
198                 continue;
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, LC_GLOBAL_LOCALE);
218 }
219
220 weak_alias(__strftime_l, strftime_l);