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