fix strftime handling of time zone data
[musl] / src / time / strftime.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <langinfo.h>
5 #include <locale.h>
6 #include <time.h>
7 #include <limits.h>
8 #include "libc.h"
9
10 const char *__nl_langinfo_l(nl_item, locale_t);
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 static int week_num(const struct tm *tm)
21 {
22         int val = (tm->tm_yday + 7 - (tm->tm_wday+6)%7) / 7;
23         /* If 1 Jan is just 1-3 days past Monday,
24          * the previous week is also in this year. */
25         if ((tm->tm_wday - tm->tm_yday - 2 + 371) % 7 <= 2)
26                 val++;
27         if (!val) {
28                 val = 52;
29                 /* If 31 December of prev year a Thursday,
30                  * or Friday of a leap year, then the
31                  * prev year has 53 weeks. */
32                 int dec31 = (tm->tm_wday - tm->tm_yday - 1 + 7) % 7;
33                 if (dec31 == 4 || (dec31 == 5 && is_leap(tm->tm_year%400-1)))
34                         val++;
35         } else if (val == 53) {
36                 /* If 1 January is not a Thursday, and not
37                  * a Wednesday of a leap year, then this
38                  * year has only 52 weeks. */
39                 int jan1 = (tm->tm_wday - tm->tm_yday + 371) % 7;
40                 if (jan1 != 4 && (jan1 != 3 || !is_leap(tm->tm_year)))
41                         val = 1;
42         }
43         return val;
44 }
45
46 const char *__tm_to_tzname(const struct tm *);
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], size_t *l, int f, const struct tm *tm, locale_t loc)
50 {
51         nl_item item;
52         long long val;
53         const char *fmt;
54         int width = 2;
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 = (1900LL+tm->tm_year) / 100;
75                 goto number;
76         case 'd':
77                 val = tm->tm_mday;
78                 goto number;
79         case 'D':
80                 fmt = "%m/%d/%y";
81                 goto recu_strftime;
82         case 'e':
83                 val = tm->tm_mday;
84                 goto number;
85         case 'F':
86                 fmt = "%Y-%m-%d";
87                 goto recu_strftime;
88         case 'g':
89         case 'G':
90                 val = tm->tm_year + 1900LL;
91                 if (tm->tm_yday < 3 && week_num(tm) != 1) val--;
92                 else if (tm->tm_yday > 360 && week_num(tm) == 1) val++;
93                 if (f=='g') val %= 100;
94                 else width = 4;
95                 goto number;
96         case 'H':
97                 val = tm->tm_hour;
98                 goto number;
99         case 'I':
100                 val = tm->tm_hour;
101                 if (!val) val = 12;
102                 else if (val > 12) val -= 12;
103                 goto number;
104         case 'j':
105                 val = tm->tm_yday+1;
106                 width = 3;
107                 goto number;
108         case 'm':
109                 val = tm->tm_mon+1;
110                 goto number;
111         case 'M':
112                 val = tm->tm_min;
113                 goto number;
114         case 'n':
115                 *l = 1;
116                 return "\n";
117         case 'p':
118                 item = tm->tm_hour >= 12 ? PM_STR : AM_STR;
119                 goto nl_strcat;
120         case 'r':
121                 item = T_FMT_AMPM;
122                 goto nl_strftime;
123         case 'R':
124                 fmt = "%H:%M";
125                 goto recu_strftime;
126         case 'S':
127                 val = tm->tm_sec;
128                 goto number;
129         case 't':
130                 *l = 1;
131                 return "\t";
132         case 'T':
133                 fmt = "%H:%M:%S";
134                 goto recu_strftime;
135         case 'u':
136                 val = tm->tm_wday ? tm->tm_wday : 7;
137                 width = 1;
138                 goto number;
139         case 'U':
140                 val = (tm->tm_yday + 7 - tm->tm_wday) / 7;
141                 goto number;
142         case 'W':
143                 val = (tm->tm_yday + 7 - (tm->tm_wday+6)%7) / 7;
144                 goto number;
145         case 'V':
146                 val = week_num(tm);
147                 goto number;
148         case 'w':
149                 val = tm->tm_wday;
150                 width = 1;
151                 goto number;
152         case 'x':
153                 item = D_FMT;
154                 goto nl_strftime;
155         case 'X':
156                 item = T_FMT;
157                 goto nl_strftime;
158         case 'y':
159                 val = tm->tm_year % 100;
160                 goto number;
161         case 'Y':
162                 val = tm->tm_year + 1900;
163                 if (val >= 10000) {
164                         *l = snprintf(*s, sizeof *s, "+%lld", val);
165                         return *s;
166                 }
167                 width = 4;
168                 goto number;
169         case 'z':
170                 if (tm->tm_isdst < 0) {
171                         *l = 0;
172                         return "";
173                 }
174                 *l = snprintf(*s, sizeof *s, "%+.2d%.2d",
175                         (-tm->__tm_gmtoff)/3600,
176                         abs(tm->__tm_gmtoff%3600)/60);
177                 return *s;
178         case 'Z':
179                 if (tm->tm_isdst < 0) {
180                         *l = 0;
181                         return "";
182                 }
183                 fmt = __tm_to_tzname(tm);
184                 goto string;
185         case '%':
186                 *l = 1;
187                 return "%";
188         default:
189                 return 0;
190         }
191 number:
192         *l = snprintf(*s, sizeof *s, "%0*lld", width, val);
193         return *s;
194 nl_strcat:
195         fmt = __nl_langinfo_l(item, loc);
196 string:
197         *l = strlen(fmt);
198         return fmt;
199 nl_strftime:
200         fmt = __nl_langinfo_l(item, loc);
201 recu_strftime:
202         *l = __strftime_l(*s, sizeof *s, fmt, tm, loc);
203         if (!*l) return 0;
204         return *s;
205 }
206
207 size_t __strftime_l(char *restrict s, size_t n, const char *restrict f, const struct tm *restrict tm, locale_t loc)
208 {
209         size_t l, k;
210         char buf[100];
211         char *p;
212         const char *t;
213         int plus;
214         unsigned long width;
215         for (l=0; l+1<n; f++) {
216                 if (!*f) {
217                         s[l] = 0;
218                         return l;
219                 }
220                 if (*f != '%') {
221                         s[l++] = *f;
222                         continue;
223                 }
224                 f++;
225                 if ((plus = (*f == '+'))) f++;
226                 width = strtoul(f, &p, 10);
227                 if (*p == 'C' || *p == 'F' || *p == 'G' || *p == 'Y') {
228                         if (!width && p!=f) width = 1;
229                         if (width >= n-l) return 0;
230                 } else {
231                         width = 0;
232                 }
233                 f = p;
234                 if (*f == 'E' || *f == 'O') f++;
235                 t = __strftime_fmt_1(&buf, &k, *f, tm, loc);
236                 if (!t) return 0;
237                 if (width) {
238                         for (; *t=='+' || *t=='-' || (*t=='0'&&t[1]); t++, k--);
239                         width--;
240                         if (plus && tm->tm_year >= 10000-1900)
241                                 s[l++] = '+';
242                         else if (tm->tm_year < -1900)
243                                 s[l++] = '-';
244                         else
245                                 width++;
246                         if (width >= n-l) return 0;
247                         for (; width > k; width--)
248                                 s[l++] = '0';
249                 }
250                 if (k >= n-l) return 0;
251                 memcpy(s+l, t, k);
252                 l += k;
253         }
254         return 0;
255 }
256
257 size_t strftime(char *restrict s, size_t n, const char *restrict f, const struct tm *restrict tm)
258 {
259         return __strftime_l(s, n, f, tm, 0);
260 }
261
262 weak_alias(__strftime_l, strftime_l);