fix type-mismatched declarations of __nl_langinfo_l in source files
[musl.git] / src / time / strftime.c
blob5d2484edcfba4fababdc1d8d599d3a5cbc80bfad
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <langinfo.h>
5 #include <locale.h>
6 #include <time.h>
7 #include <limits.h>
8 #include "locale_impl.h"
9 #include "libc.h"
10 #include "time_impl.h"
12 char *__nl_langinfo_l(nl_item, locale_t);
14 static int is_leap(int y)
16 /* Avoid overflow */
17 if (y>INT_MAX-1900) y -= 2000;
18 y += 1900;
19 return !(y%4) && ((y%100) || !(y%400));
22 static int week_num(const struct tm *tm)
24 int val = (tm->tm_yday + 7U - (tm->tm_wday+6U)%7) / 7;
25 /* If 1 Jan is just 1-3 days past Monday,
26 * the previous week is also in this year. */
27 if ((tm->tm_wday + 371U - tm->tm_yday - 2) % 7 <= 2)
28 val++;
29 if (!val) {
30 val = 52;
31 /* If 31 December of prev year a Thursday,
32 * or Friday of a leap year, then the
33 * prev year has 53 weeks. */
34 int dec31 = (tm->tm_wday + 7U - tm->tm_yday - 1) % 7;
35 if (dec31 == 4 || (dec31 == 5 && is_leap(tm->tm_year%400-1)))
36 val++;
37 } else if (val == 53) {
38 /* If 1 January is not a Thursday, and not
39 * a Wednesday of a leap year, then this
40 * year has only 52 weeks. */
41 int jan1 = (tm->tm_wday + 371U - tm->tm_yday) % 7;
42 if (jan1 != 4 && (jan1 != 3 || !is_leap(tm->tm_year)))
43 val = 1;
45 return val;
48 const char *__tm_to_tzname(const struct tm *);
49 size_t __strftime_l(char *restrict, size_t, const char *restrict, const struct tm *restrict, locale_t);
51 const char *__strftime_fmt_1(char (*s)[100], size_t *l, int f, const struct tm *tm, locale_t loc, int pad)
53 nl_item item;
54 long long val;
55 const char *fmt = "-";
56 int width = 2, def_pad = '0';
58 switch (f) {
59 case 'a':
60 if (tm->tm_wday > 6U) goto string;
61 item = ABDAY_1 + tm->tm_wday;
62 goto nl_strcat;
63 case 'A':
64 if (tm->tm_wday > 6U) goto string;
65 item = DAY_1 + tm->tm_wday;
66 goto nl_strcat;
67 case 'h':
68 case 'b':
69 if (tm->tm_mon > 11U) goto string;
70 item = ABMON_1 + tm->tm_mon;
71 goto nl_strcat;
72 case 'B':
73 if (tm->tm_mon > 11U) goto string;
74 item = MON_1 + tm->tm_mon;
75 goto nl_strcat;
76 case 'c':
77 item = D_T_FMT;
78 goto nl_strftime;
79 case 'C':
80 val = (1900LL+tm->tm_year) / 100;
81 goto number;
82 case 'e':
83 def_pad = '_';
84 case 'd':
85 val = tm->tm_mday;
86 goto number;
87 case 'D':
88 fmt = "%m/%d/%y";
89 goto recu_strftime;
90 case 'F':
91 fmt = "%Y-%m-%d";
92 goto recu_strftime;
93 case 'g':
94 case 'G':
95 val = tm->tm_year + 1900LL;
96 if (tm->tm_yday < 3 && week_num(tm) != 1) val--;
97 else if (tm->tm_yday > 360 && week_num(tm) == 1) val++;
98 if (f=='g') val %= 100;
99 else width = 4;
100 goto number;
101 case 'H':
102 val = tm->tm_hour;
103 goto number;
104 case 'I':
105 val = tm->tm_hour;
106 if (!val) val = 12;
107 else if (val > 12) val -= 12;
108 goto number;
109 case 'j':
110 val = tm->tm_yday+1;
111 width = 3;
112 goto number;
113 case 'm':
114 val = tm->tm_mon+1;
115 goto number;
116 case 'M':
117 val = tm->tm_min;
118 goto number;
119 case 'n':
120 *l = 1;
121 return "\n";
122 case 'p':
123 item = tm->tm_hour >= 12 ? PM_STR : AM_STR;
124 goto nl_strcat;
125 case 'r':
126 item = T_FMT_AMPM;
127 goto nl_strftime;
128 case 'R':
129 fmt = "%H:%M";
130 goto recu_strftime;
131 case 's':
132 val = __tm_to_secs(tm) - tm->__tm_gmtoff;
133 width = 1;
134 goto number;
135 case 'S':
136 val = tm->tm_sec;
137 goto number;
138 case 't':
139 *l = 1;
140 return "\t";
141 case 'T':
142 fmt = "%H:%M:%S";
143 goto recu_strftime;
144 case 'u':
145 val = tm->tm_wday ? tm->tm_wday : 7;
146 width = 1;
147 goto number;
148 case 'U':
149 val = (tm->tm_yday + 7U - tm->tm_wday) / 7;
150 goto number;
151 case 'W':
152 val = (tm->tm_yday + 7U - (tm->tm_wday+6U)%7) / 7;
153 goto number;
154 case 'V':
155 val = week_num(tm);
156 goto number;
157 case 'w':
158 val = tm->tm_wday;
159 width = 1;
160 goto number;
161 case 'x':
162 item = D_FMT;
163 goto nl_strftime;
164 case 'X':
165 item = T_FMT;
166 goto nl_strftime;
167 case 'y':
168 val = (tm->tm_year + 1900LL) % 100;
169 if (val < 0) val = -val;
170 goto number;
171 case 'Y':
172 val = tm->tm_year + 1900LL;
173 if (val >= 10000) {
174 *l = snprintf(*s, sizeof *s, "+%lld", val);
175 return *s;
177 width = 4;
178 goto number;
179 case 'z':
180 if (tm->tm_isdst < 0) {
181 *l = 0;
182 return "";
184 *l = snprintf(*s, sizeof *s, "%+.4ld",
185 tm->__tm_gmtoff/3600*100 + tm->__tm_gmtoff%3600/60);
186 return *s;
187 case 'Z':
188 if (tm->tm_isdst < 0) {
189 *l = 0;
190 return "";
192 fmt = __tm_to_tzname(tm);
193 goto string;
194 case '%':
195 *l = 1;
196 return "%";
197 default:
198 return 0;
200 number:
201 switch (pad ? pad : def_pad) {
202 case '-': *l = snprintf(*s, sizeof *s, "%lld", val); break;
203 case '_': *l = snprintf(*s, sizeof *s, "%*lld", width, val); break;
204 case '0':
205 default: *l = snprintf(*s, sizeof *s, "%0*lld", width, val); break;
207 return *s;
208 nl_strcat:
209 fmt = __nl_langinfo_l(item, loc);
210 string:
211 *l = strlen(fmt);
212 return fmt;
213 nl_strftime:
214 fmt = __nl_langinfo_l(item, loc);
215 recu_strftime:
216 *l = __strftime_l(*s, sizeof *s, fmt, tm, loc);
217 if (!*l) return 0;
218 return *s;
221 size_t __strftime_l(char *restrict s, size_t n, const char *restrict f, const struct tm *restrict tm, locale_t loc)
223 size_t l, k;
224 char buf[100];
225 char *p;
226 const char *t;
227 int pad, plus;
228 unsigned long width;
229 for (l=0; l<n; f++) {
230 if (!*f) {
231 s[l] = 0;
232 return l;
234 if (*f != '%') {
235 s[l++] = *f;
236 continue;
238 f++;
239 pad = 0;
240 if (*f == '-' || *f == '_' || *f == '0') pad = *f++;
241 if ((plus = (*f == '+'))) f++;
242 width = strtoul(f, &p, 10);
243 if (*p == 'C' || *p == 'F' || *p == 'G' || *p == 'Y') {
244 if (!width && p!=f) width = 1;
245 } else {
246 width = 0;
248 f = p;
249 if (*f == 'E' || *f == 'O') f++;
250 t = __strftime_fmt_1(&buf, &k, *f, tm, loc, pad);
251 if (!t) break;
252 if (width) {
253 /* Trim off any sign and leading zeros, then
254 * count remaining digits to determine behavior
255 * for the + flag. */
256 if (*t=='+' || *t=='-') t++, k--;
257 for (; *t=='0' && t[1]-'0'<10U; t++, k--);
258 if (width < k) width = k;
259 size_t d;
260 for (d=0; t[d]-'0'<10U; d++);
261 if (tm->tm_year < -1900) {
262 s[l++] = '-';
263 width--;
264 } else if (plus && d+(width-k) >= (*p=='C'?3:5)) {
265 s[l++] = '+';
266 width--;
268 for (; width > k && l < n; width--)
269 s[l++] = '0';
271 if (k > n-l) k = n-l;
272 memcpy(s+l, t, k);
273 l += k;
275 if (n) {
276 if (l==n) l=n-1;
277 s[l] = 0;
279 return 0;
282 size_t strftime(char *restrict s, size_t n, const char *restrict f, const struct tm *restrict tm)
284 return __strftime_l(s, n, f, tm, CURRENT_LOCALE);
287 weak_alias(__strftime_l, strftime_l);