75ebca62944987676ac712afba3e2f015e2cf8e5
[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 #include "time_impl.h"
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 const char *__tm_to_tzname(const struct tm *);
48 size_t __strftime_l(char *restrict, size_t, const char *restrict, const struct tm *restrict, locale_t);
49
50 const char *__strftime_fmt_1(char (*s)[100], size_t *l, int f, const struct tm *tm, locale_t loc)
51 {
52         nl_item item;
53         long long val;
54         const char *fmt;
55         int width = 2;
56
57         switch (f) {
58         case 'a':
59                 item = ABDAY_1 + tm->tm_wday;
60                 goto nl_strcat;
61         case 'A':
62                 item = DAY_1 + tm->tm_wday;
63                 goto nl_strcat;
64         case 'h':
65         case 'b':
66                 item = ABMON_1 + tm->tm_mon;
67                 goto nl_strcat;
68         case 'B':
69                 item = MON_1 + tm->tm_mon;
70                 goto nl_strcat;
71         case 'c':
72                 item = D_T_FMT;
73                 goto nl_strftime;
74         case 'C':
75                 val = (1900LL+tm->tm_year) / 100;
76                 goto number;
77         case 'd':
78                 val = tm->tm_mday;
79                 goto number;
80         case 'D':
81                 fmt = "%m/%d/%y";
82                 goto recu_strftime;
83         case 'e':
84                 *l = snprintf(*s, sizeof *s, "%2d", tm->tm_mday);
85                 return *s;
86         case 'F':
87                 fmt = "%Y-%m-%d";
88                 goto recu_strftime;
89         case 'g':
90         case 'G':
91                 val = tm->tm_year + 1900LL;
92                 if (tm->tm_yday < 3 && week_num(tm) != 1) val--;
93                 else if (tm->tm_yday > 360 && week_num(tm) == 1) val++;
94                 if (f=='g') val %= 100;
95                 else width = 4;
96                 goto number;
97         case 'H':
98                 val = tm->tm_hour;
99                 goto number;
100         case 'I':
101                 val = tm->tm_hour;
102                 if (!val) val = 12;
103                 else if (val > 12) val -= 12;
104                 goto number;
105         case 'j':
106                 val = tm->tm_yday+1;
107                 width = 3;
108                 goto number;
109         case 'm':
110                 val = tm->tm_mon+1;
111                 goto number;
112         case 'M':
113                 val = tm->tm_min;
114                 goto number;
115         case 'n':
116                 *l = 1;
117                 return "\n";
118         case 'p':
119                 item = tm->tm_hour >= 12 ? PM_STR : AM_STR;
120                 goto nl_strcat;
121         case 'r':
122                 item = T_FMT_AMPM;
123                 goto nl_strftime;
124         case 'R':
125                 fmt = "%H:%M";
126                 goto recu_strftime;
127         case 's':
128                 val = __tm_to_secs(tm) + tm->__tm_gmtoff;
129                 width = 1;
130                 goto number;
131         case 'S':
132                 val = tm->tm_sec;
133                 goto number;
134         case 't':
135                 *l = 1;
136                 return "\t";
137         case 'T':
138                 fmt = "%H:%M:%S";
139                 goto recu_strftime;
140         case 'u':
141                 val = tm->tm_wday ? tm->tm_wday : 7;
142                 width = 1;
143                 goto number;
144         case 'U':
145                 val = (tm->tm_yday + 7 - tm->tm_wday) / 7;
146                 goto number;
147         case 'W':
148                 val = (tm->tm_yday + 7 - (tm->tm_wday+6)%7) / 7;
149                 goto number;
150         case 'V':
151                 val = week_num(tm);
152                 goto number;
153         case 'w':
154                 val = tm->tm_wday;
155                 width = 1;
156                 goto number;
157         case 'x':
158                 item = D_FMT;
159                 goto nl_strftime;
160         case 'X':
161                 item = T_FMT;
162                 goto nl_strftime;
163         case 'y':
164                 val = tm->tm_year % 100;
165                 goto number;
166         case 'Y':
167                 val = tm->tm_year + 1900;
168                 if (val >= 10000) {
169                         *l = snprintf(*s, sizeof *s, "+%lld", val);
170                         return *s;
171                 }
172                 width = 4;
173                 goto number;
174         case 'z':
175                 if (tm->tm_isdst < 0) {
176                         *l = 0;
177                         return "";
178                 }
179                 *l = snprintf(*s, sizeof *s, "%+.2d%.2d",
180                         (-tm->__tm_gmtoff)/3600,
181                         abs(tm->__tm_gmtoff%3600)/60);
182                 return *s;
183         case 'Z':
184                 if (tm->tm_isdst < 0) {
185                         *l = 0;
186                         return "";
187                 }
188                 fmt = __tm_to_tzname(tm);
189                 goto string;
190         case '%':
191                 *l = 1;
192                 return "%";
193         default:
194                 return 0;
195         }
196 number:
197         *l = snprintf(*s, sizeof *s, "%0*lld", width, val);
198         return *s;
199 nl_strcat:
200         fmt = __nl_langinfo_l(item, loc);
201 string:
202         *l = strlen(fmt);
203         return fmt;
204 nl_strftime:
205         fmt = __nl_langinfo_l(item, loc);
206 recu_strftime:
207         *l = __strftime_l(*s, sizeof *s, fmt, tm, loc);
208         if (!*l) return 0;
209         return *s;
210 }
211
212 size_t __strftime_l(char *restrict s, size_t n, const char *restrict f, const struct tm *restrict tm, locale_t loc)
213 {
214         size_t l, k;
215         char buf[100];
216         char *p;
217         const char *t;
218         int plus;
219         unsigned long width;
220         for (l=0; l<n; f++) {
221                 if (!*f) {
222                         s[l] = 0;
223                         return l;
224                 }
225                 if (*f != '%') {
226                         s[l++] = *f;
227                         continue;
228                 }
229                 f++;
230                 if ((plus = (*f == '+'))) f++;
231                 width = strtoul(f, &p, 10);
232                 if (*p == 'C' || *p == 'F' || *p == 'G' || *p == 'Y') {
233                         if (!width && p!=f) width = 1;
234                 } else {
235                         width = 0;
236                 }
237                 f = p;
238                 if (*f == 'E' || *f == 'O') f++;
239                 t = __strftime_fmt_1(&buf, &k, *f, tm, loc);
240                 if (!t) break;
241                 if (width) {
242                         for (; *t=='+' || *t=='-' || (*t=='0'&&t[1]); t++, k--);
243                         width--;
244                         if (plus && tm->tm_year >= 10000-1900)
245                                 s[l++] = '+';
246                         else if (tm->tm_year < -1900)
247                                 s[l++] = '-';
248                         else
249                                 width++;
250                         for (; width > k && l < n; width--)
251                                 s[l++] = '0';
252                 }
253                 if (k > n-l) k = n-l;
254                 memcpy(s+l, t, k);
255                 l += k;
256         }
257         if (n) {
258                 if (l==n) l=n-1;
259                 s[l] = 0;
260         }
261         return 0;
262 }
263
264 size_t strftime(char *restrict s, size_t n, const char *restrict f, const struct tm *restrict tm)
265 {
266         return __strftime_l(s, n, f, tm, 0);
267 }
268
269 weak_alias(__strftime_l, strftime_l);