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