master
  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
 12static 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
 20static 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
 46const 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 '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_to_secs(tm) - tm->__tm_gmtoff;
128		width = 1;
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 + 7U - tm->tm_wday) / 7;
145		goto number;
146	case 'W':
147		val = (tm->tm_yday + 7U - (tm->tm_wday+6U)%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 + 1900LL) % 100;
164		if (val < 0) val = -val;
165		goto number;
166	case 'Y':
167		val = tm->tm_year + 1900LL;
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#ifdef __wasilibc_unmodified_upstream // wasi-libc's __tm_gmtoff is an int
180		*l = snprintf(*s, sizeof *s, "%+.4ld",
181#else
182		*l = snprintf(*s, sizeof *s, "%+.4d",
183#endif
184			tm->__tm_gmtoff/3600*100 + tm->__tm_gmtoff%3600/60);
185		return *s;
186	case 'Z':
187		if (tm->tm_isdst < 0) {
188			*l = 0;
189			return "";
190		}
191		fmt = __tm_to_tzname(tm);
192		goto string;
193	case '%':
194		*l = 1;
195		return "%";
196	default:
197		return 0;
198	}
199number:
200	switch (pad ? pad : def_pad) {
201	case '-': *l = snprintf(*s, sizeof *s, "%lld", val); break;
202	case '_': *l = snprintf(*s, sizeof *s, "%*lld", width, val); break;
203	case '0':
204	default:  *l = snprintf(*s, sizeof *s, "%0*lld", width, val); break;
205	}
206	return *s;
207nl_strcat:
208	fmt = __nl_langinfo_l(item, loc);
209string:
210	*l = strlen(fmt);
211	return fmt;
212nl_strftime:
213	fmt = __nl_langinfo_l(item, loc);
214recu_strftime:
215	*l = __strftime_l(*s, sizeof *s, fmt, tm, loc);
216	if (!*l) return 0;
217	return *s;
218}
219
220size_t __strftime_l(char *restrict s, size_t n, const char *restrict f, const struct tm *restrict tm, locale_t loc)
221{
222	size_t l, k;
223	char buf[100];
224	char *p;
225	const char *t;
226	int pad, plus;
227	unsigned long width;
228	for (l=0; l<n; f++) {
229		if (!*f) {
230			s[l] = 0;
231			return l;
232		}
233		if (*f != '%') {
234			s[l++] = *f;
235			continue;
236		}
237		f++;
238		pad = 0;
239		if (*f == '-' || *f == '_' || *f == '0') pad = *f++;
240		if ((plus = (*f == '+'))) f++;
241		if (isdigit(*f)) {
242			width = strtoul(f, &p, 10);
243		} else {
244			width = 0;
245			p = (void *)f;
246		}
247		if (*p == 'C' || *p == 'F' || *p == 'G' || *p == 'Y') {
248			if (!width && p!=f) width = 1;
249		} else {
250			width = 0;
251		}
252		f = p;
253		if (*f == 'E' || *f == 'O') f++;
254		t = __strftime_fmt_1(&buf, &k, *f, tm, loc, pad);
255		if (!t) break;
256		if (width) {
257			/* Trim off any sign and leading zeros, then
258			 * count remaining digits to determine behavior
259			 * for the + flag. */
260			if (*t=='+' || *t=='-') t++, k--;
261			for (; *t=='0' && t[1]-'0'<10U; t++, k--);
262			if (width < k) width = k;
263			size_t d;
264			for (d=0; t[d]-'0'<10U; d++);
265			if (tm->tm_year < -1900) {
266				s[l++] = '-';
267				width--;
268			} else if (plus && d+(width-k) >= (*p=='C'?3:5)) {
269				s[l++] = '+';
270				width--;
271			}
272			for (; width > k && l < n; width--)
273				s[l++] = '0';
274		}
275		if (k > n-l) k = n-l;
276		memcpy(s+l, t, k);
277		l += k;
278	}
279	if (n) {
280		if (l==n) l=n-1;
281		s[l] = 0;
282	}
283	return 0;
284}
285
286size_t strftime(char *restrict s, size_t n, const char *restrict f, const struct tm *restrict tm)
287{
288	return __strftime_l(s, n, f, tm, CURRENT_LOCALE);
289}
290
291weak_alias(__strftime_l, strftime_l);