strftime cleanup: avoid recomputing strlen when it's known
[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, size_t, const char *restrict, const struct tm *restrict, locale_t);
48
49 const char *__strftime_fmt_1(char (*s)[100], size_t *l, int f, const struct tm *tm, locale_t loc)
50 {
51         nl_item item;
52         int val;
53         const char *fmt;
54
55         switch (f) {
56         case 'a':
57                 item = ABDAY_1 + tm->tm_wday;
58                 goto nl_strcat;
59         case 'A':
60                 item = DAY_1 + tm->tm_wday;
61                 goto nl_strcat;
62         case 'h':
63         case 'b':
64                 item = ABMON_1 + tm->tm_mon;
65                 goto nl_strcat;
66         case 'B':
67                 item = MON_1 + tm->tm_mon;
68                 goto nl_strcat;
69         case 'c':
70                 item = D_T_FMT;
71                 goto nl_strftime;
72         case 'C':
73                 val = (1900+tm->tm_year) / 100;
74                 fmt = "%02d";
75                 goto number;
76         case 'd':
77                 val = tm->tm_mday;
78                 fmt = "%02d";
79                 goto number;
80         case 'D':
81                 fmt = "%m/%d/%y";
82                 goto recu_strftime;
83         case 'e':
84                 val = tm->tm_mday;
85                 fmt = "%2d";
86                 goto number;
87         case 'F':
88                 fmt = "%Y-%m-%d";
89                 goto recu_strftime;
90         case 'g':
91         case 'G':
92                 fmt = "%04d";
93                 val = tm->tm_year + 1900;
94                 if (tm->tm_yday < 3 && week_num(tm) != 1) val--;
95                 else if (tm->tm_yday > 360 && week_num(tm) == 1) val++;
96                 if (f=='g') {
97                         fmt = "%02d";
98                         val %= 100;
99                 }
100                 goto number;
101         case 'H':
102                 val = tm->tm_hour;
103                 fmt = "%02d";
104                 goto number;
105         case 'I':
106                 val = tm->tm_hour;
107                 if (!val) val = 12;
108                 else if (val > 12) val -= 12;
109                 fmt = "%02d";
110                 goto number;
111         case 'j':
112                 val = tm->tm_yday+1;
113                 fmt = "%03d";
114                 goto number;
115         case 'm':
116                 val = tm->tm_mon+1;
117                 fmt = "%02d";
118                 goto number;
119         case 'M':
120                 val = tm->tm_min;
121                 fmt = "%02d";
122                 goto number;
123         case 'n':
124                 *l = 1;
125                 return "\n";
126         case 'p':
127                 item = tm->tm_hour >= 12 ? PM_STR : AM_STR;
128                 goto nl_strcat;
129         case 'r':
130                 item = T_FMT_AMPM;
131                 goto nl_strftime;
132         case 'R':
133                 fmt = "%H:%M";
134                 goto recu_strftime;
135         case 'S':
136                 val = tm->tm_sec;
137                 fmt = "%02d";
138                 goto number;
139         case 't':
140                 *l = 1;
141                 return "\t";
142         case 'T':
143                 fmt = "%H:%M:%S";
144                 goto recu_strftime;
145         case 'u':
146                 val = tm->tm_wday ? tm->tm_wday : 7;
147                 fmt = "%d";
148                 goto number;
149         case 'U':
150                 val = (tm->tm_yday + 7 - tm->tm_wday) / 7;
151                 fmt = "%02d";
152                 goto number;
153         case 'W':
154                 val = (tm->tm_yday + 7 - (tm->tm_wday+6)%7) / 7;
155                 fmt = "%02d";
156                 goto number;
157         case 'V':
158                 val = week_num(tm);
159                 fmt = "%02d";
160                 goto number;
161         case 'w':
162                 val = tm->tm_wday;
163                 fmt = "%d";
164                 goto number;
165         case 'x':
166                 item = D_FMT;
167                 goto nl_strftime;
168         case 'X':
169                 item = T_FMT;
170                 goto nl_strftime;
171         case 'y':
172                 val = tm->tm_year % 100;
173                 fmt = "%02d";
174                 goto number;
175         case 'Y':
176                 val = tm->tm_year + 1900;
177                 fmt = "%04d";
178                 goto number;
179         case 'z':
180                 val = -tm->__tm_gmtoff;
181                 *l = snprintf(*s, sizeof *s, "%+.2d%.2d", val/3600, abs(val%3600)/60);
182                 return *s;
183         case 'Z':
184                 fmt = tm->__tm_zone;
185                 goto string;
186         case '%':
187                 *l = 1;
188                 return "%";
189         default:
190                 return 0;
191         }
192 number:
193         *l = snprintf(*s, sizeof *s, fmt, val);
194         return *s;
195 nl_strcat:
196         fmt = __nl_langinfo_l(item, loc);
197 string:
198         *l = strlen(fmt);
199         return fmt;
200 nl_strftime:
201         fmt = __nl_langinfo_l(item, loc);
202 recu_strftime:
203         *l = __strftime_l(*s, sizeof *s, fmt, tm, loc);
204         if (!*l) return 0;
205         return *s;
206 }
207
208 size_t __strftime_l(char *restrict s, size_t n, const char *restrict f, const struct tm *restrict tm, locale_t loc)
209 {
210         size_t l, k;
211         char buf[100];
212         const char *t;
213         for (l=0; l+1<n; f++) {
214                 if (!*f) {
215                         s[l] = 0;
216                         return l;
217                 }
218                 if (*f != '%') {
219                         s[l++] = *f;
220                         continue;
221                 }
222                 f++;
223                 if (*f == 'E' || *f == 'O') f++;
224                 t = __strftime_fmt_1(&buf, &k, *f, tm, loc);
225                 if (!t || k >= n-l)
226                         return 0;
227                 memcpy(s+l, t, k);
228                 l += k;
229         }
230         return 0;
231 }
232
233 size_t strftime(char *restrict s, size_t n, const char *restrict f, const struct tm *restrict tm)
234 {
235         return __strftime_l(s, n, f, tm, 0);
236 }
237
238 weak_alias(__strftime_l, strftime_l);