fix breakage in last commit to strftime due to missing INT_MAX
[musl] / src / time / strftime.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <langinfo.h>
4 #include <time.h>
5 #include <limits.h>
6 #include "__time.h"
7
8 // FIXME: integer overflows
9
10 const char *__langinfo(nl_item);
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 size_t strftime(char *restrict s, size_t n, const char *restrict f, const struct tm *restrict tm)
21 {
22         nl_item item;
23         int val;
24         const char *fmt;
25         size_t l;
26         for (l=0; *f && l<n; f++) {
27                 if (*f == '%') {
28 do_fmt:
29                 switch (*++f) {
30                 case '%':
31                         goto literal;
32                 case 'E':
33                 case 'O':
34                         goto do_fmt;
35                 case 'a':
36                         item = ABDAY_1 + tm->tm_wday;
37                         goto nl_strcat;
38                 case 'A':
39                         item = DAY_1 + tm->tm_wday;
40                         goto nl_strcat;
41                 case 'h':
42                 case 'b':
43                         item = ABMON_1 + tm->tm_mon;
44                         goto nl_strcat;
45                 case 'B':
46                         item = MON_1 + tm->tm_mon;
47                         goto nl_strcat;
48                 case 'c':
49                         item = D_T_FMT;
50                         goto nl_strftime;
51                 case 'C':
52                         val = (1900+tm->tm_year) / 100;
53                         fmt = "%02d";
54                         goto number;
55                 case 'd':
56                         val = tm->tm_mday;
57                         fmt = "%02d";
58                         goto number;
59                 case 'D':
60                         fmt = "%m/%d/%y";
61                         goto recu_strftime;
62                 case 'e':
63                         val = tm->tm_mday;
64                         fmt = "%2d";
65                         goto number;
66                 case 'F':
67                         fmt = "%Y-%m-%d";
68                         goto recu_strftime;
69                 case 'g':
70                         // FIXME
71                         val = 0; //week_based_year(tm)%100;
72                         fmt = "%02d";
73                         goto number;
74                 case 'G':
75                         // FIXME
76                         val = 0; //week_based_year(tm);
77                         fmt = "%04d";
78                         goto number;
79                 case 'H':
80                         val = tm->tm_hour;
81                         fmt = "%02d";
82                         goto number;
83                 case 'I':
84                         val = tm->tm_hour;
85                         if (!val) val = 12;
86                         else if (val > 12) val -= 12;
87                         fmt = "%02d";
88                         goto number;
89                 case 'j':
90                         val = tm->tm_yday+1;
91                         fmt = "%03d";
92                         goto number;
93                 case 'm':
94                         val = tm->tm_mon+1;
95                         fmt = "%02d";
96                         goto number;
97                 case 'M':
98                         val = tm->tm_min;
99                         fmt = "%02d";
100                         goto number;
101                 case 'n':
102                         s[l++] = '\n';
103                         continue;
104                 case 'p':
105                         item = tm->tm_hour >= 12 ? PM_STR : AM_STR;
106                         goto nl_strcat;
107                 case 'r':
108                         item = T_FMT_AMPM;
109                         goto nl_strftime;
110                 case 'R':
111                         fmt = "%H:%M";
112                         goto recu_strftime;
113                 case 'S':
114                         val = tm->tm_sec;
115                         fmt = "%02d";
116                         goto number;
117                 case 't':
118                         s[l++] = '\t';
119                         continue;
120                 case 'T':
121                         fmt = "%H:%M:%S";
122                         goto recu_strftime;
123                 case 'u':
124                         val = tm->tm_wday ? tm->tm_wday : 7;
125                         fmt = "%d";
126                         goto number;
127                 case 'U':
128                         val = (tm->tm_yday + 7 - tm->tm_wday) / 7;
129                         fmt = "%02d";
130                         goto number;
131                 case 'W':
132                         val = (tm->tm_yday + 7 - (tm->tm_wday+6)%7) / 7;
133                         fmt = "%02d";
134                         goto number;
135                 case 'V':
136                         val = (tm->tm_yday + 7 - (tm->tm_wday+6)%7) / 7;
137                         /* If 1 Jan is just 1-3 days past Monday,
138                          * the previous week is also in this year. */
139                         if ((tm->tm_wday - tm->tm_yday - 2 + 371) % 7 <= 2)
140                                 val++;
141                         if (!val) {
142                                 val = 52;
143                                 /* If 31 December of prev year a Thursday,
144                                  * or Friday of a leap year, then the
145                                  * prev year has 53 weeks. */
146                                 int dec31 = (tm->tm_wday - tm->tm_yday - 1 + 7) % 7;
147                                 if (dec31 == 4 || (dec31 == 5 && is_leap(tm->tm_year%400-1)))
148                                         val++;
149                         } else if (val == 53) {
150                                 /* If 1 January is not a Thursday, and not
151                                  * a Wednesday of a leap year, then this
152                                  * year has only 52 weeks. */
153                                 int jan1 = (tm->tm_wday - tm->tm_yday + 371) % 7;
154                                 if (jan1 != 4 && (jan1 != 3 || !is_leap(tm->tm_year)))
155                                         val = 1;
156                         }
157                         fmt = "%02d";
158                         goto number;
159                 case 'w':
160                         val = tm->tm_wday;
161                         fmt = "%d";
162                         goto number;
163                 case 'x':
164                         item = D_FMT;
165                         goto nl_strftime;
166                 case 'X':
167                         item = T_FMT;
168                         goto nl_strftime;
169                 case 'y':
170                         val = tm->tm_year % 100;
171                         fmt = "%02d";
172                         goto number;
173                 case 'Y':
174                         val = tm->tm_year + 1900;
175                         fmt = "%04d";
176                         goto number;
177                 case 'z':
178                         if (tm->tm_isdst < 0) continue;
179                         val = -__timezone - (tm->tm_isdst ? __dst_offset : 0);
180                         l += snprintf(s+l, n-l, "%+.2d%.2d", val/3600, abs(val%3600)/60);
181                         continue;
182                 case 'Z':
183                         if (tm->tm_isdst < 0 || !__tzname[0] || !__tzname[0][0])
184                                 continue;
185                         l += snprintf(s+l, n-l, "%s", __tzname[!!tm->tm_isdst]);
186                         continue;
187                 default:
188                         return 0;
189                 }
190                 }
191 literal:
192                 s[l++] = *f;
193                 continue;
194 number:
195                 l += snprintf(s+l, n-l, fmt, val);
196                 continue;
197 nl_strcat:
198                 l += snprintf(s+l, n-l, "%s", __langinfo(item));
199                 continue;
200 nl_strftime:
201                 fmt = __langinfo(item);
202 recu_strftime:
203                 l += strftime(s+l, n-l, fmt, tm);
204         }
205         if (l >= n) return 0;
206         s[l] = 0;
207         return l;
208 }