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