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