Debian Codegraph @

internal/overlay/musl/src/time/strftime.c

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