correct the sysconf value for RTSIG_MAX
[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                 goto number;
130         case 'S':
131                 val = tm->tm_sec;
132                 goto number;
133         case 't':
134                 *l = 1;
135                 return "\t";
136         case 'T':
137                 fmt = "%H:%M:%S";
138                 goto recu_strftime;
139         case 'u':
140                 val = tm->tm_wday ? tm->tm_wday : 7;
141                 width = 1;
142                 goto number;
143         case 'U':
144                 val = (tm->tm_yday + 7 - tm->tm_wday) / 7;
145                 goto number;
146         case 'W':
147                 val = (tm->tm_yday + 7 - (tm->tm_wday+6)%7) / 7;
148                 goto number;
149         case 'V':
150                 val = week_num(tm);
151                 goto number;
152         case 'w':
153                 val = tm->tm_wday;
154                 width = 1;
155                 goto number;
156         case 'x':
157                 item = D_FMT;
158                 goto nl_strftime;
159         case 'X':
160                 item = T_FMT;
161                 goto nl_strftime;
162         case 'y':
163                 val = tm->tm_year % 100;
164                 goto number;
165         case 'Y':
166                 val = tm->tm_year + 1900;
167                 if (val >= 10000) {
168                         *l = snprintf(*s, sizeof *s, "+%lld", val);
169                         return *s;
170                 }
171                 width = 4;
172                 goto number;
173         case 'z':
174                 if (tm->tm_isdst < 0) {
175                         *l = 0;
176                         return "";
177                 }
178                 *l = snprintf(*s, sizeof *s, "%+.2d%.2d",
179                         (-tm->__tm_gmtoff)/3600,
180                         abs(tm->__tm_gmtoff%3600)/60);
181                 return *s;
182         case 'Z':
183                 if (tm->tm_isdst < 0) {
184                         *l = 0;
185                         return "";
186                 }
187                 fmt = __tm_to_tzname(tm);
188                 goto string;
189         case '%':
190                 *l = 1;
191                 return "%";
192         default:
193                 return 0;
194         }
195 number:
196         *l = snprintf(*s, sizeof *s, "%0*lld", width, val);
197         return *s;
198 nl_strcat:
199         fmt = __nl_langinfo_l(item, loc);
200 string:
201         *l = strlen(fmt);
202         return fmt;
203 nl_strftime:
204         fmt = __nl_langinfo_l(item, loc);
205 recu_strftime:
206         *l = __strftime_l(*s, sizeof *s, fmt, tm, loc);
207         if (!*l) return 0;
208         return *s;
209 }
210
211 size_t __strftime_l(char *restrict s, size_t n, const char *restrict f, const struct tm *restrict tm, locale_t loc)
212 {
213         size_t l, k;
214         char buf[100];
215         char *p;
216         const char *t;
217         int plus;
218         unsigned long width;
219         for (l=0; l+1<n; f++) {
220                 if (!*f) {
221                         s[l] = 0;
222                         return l;
223                 }
224                 if (*f != '%') {
225                         s[l++] = *f;
226                         continue;
227                 }
228                 f++;
229                 if ((plus = (*f == '+'))) f++;
230                 width = strtoul(f, &p, 10);
231                 if (*p == 'C' || *p == 'F' || *p == 'G' || *p == 'Y') {
232                         if (!width && p!=f) width = 1;
233                         if (width >= n-l) return 0;
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) return 0;
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                         if (width >= n-l) return 0;
251                         for (; width > k; width--)
252                                 s[l++] = '0';
253                 }
254                 if (k >= n-l) return 0;
255                 memcpy(s+l, t, k);
256                 l += k;
257         }
258         return 0;
259 }
260
261 size_t strftime(char *restrict s, size_t n, const char *restrict f, const struct tm *restrict tm)
262 {
263         return __strftime_l(s, n, f, tm, 0);
264 }
265
266 weak_alias(__strftime_l, strftime_l);