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