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