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