blob: cc53d5369ca717900ef5816ed7bdc400d149dc6b [file] [log] [blame]
Rich Felker0b44a032011-02-12 00:22:29 -05001#include <stdio.h>
2#include <stdlib.h>
Rich Felker8b491f12013-08-23 08:11:43 -04003#include <string.h>
Rich Felker0b44a032011-02-12 00:22:29 -05004#include <langinfo.h>
Rich Felker0a37d992013-07-24 17:58:31 -04005#include <locale.h>
Rich Felker0b44a032011-02-12 00:22:29 -05006#include <time.h>
Rich Felker062446a2013-06-28 12:12:55 -04007#include <limits.h>
Rich Felker4c485012014-07-02 21:46:41 -04008#include "locale_impl.h"
Rich Felker242a4bb2013-08-25 02:02:15 -04009#include "time_impl.h"
Rich Felker0b44a032011-02-12 00:22:29 -050010
Rich Felkerc5faf1b2013-06-28 12:03:58 -040011static 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
Rich Felkeraea79192013-06-28 12:38:42 -040019static int week_num(const struct tm *tm)
20{
Rich Felker8d93cb52015-10-14 18:58:15 -040021 int val = (tm->tm_yday + 7U - (tm->tm_wday+6U)%7) / 7;
Rich Felkeraea79192013-06-28 12:38:42 -040022 /* If 1 Jan is just 1-3 days past Monday,
23 * the previous week is also in this year. */
Rich Felker8d93cb52015-10-14 18:58:15 -040024 if ((tm->tm_wday + 371U - tm->tm_yday - 2) % 7 <= 2)
Rich Felkeraea79192013-06-28 12:38:42 -040025 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. */
Rich Felker8d93cb52015-10-14 18:58:15 -040031 int dec31 = (tm->tm_wday + 7U - tm->tm_yday - 1) % 7;
Rich Felkeraea79192013-06-28 12:38:42 -040032 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. */
Rich Felker8d93cb52015-10-14 18:58:15 -040038 int jan1 = (tm->tm_wday + 371U - tm->tm_yday) % 7;
Rich Felkeraea79192013-06-28 12:38:42 -040039 if (jan1 != 4 && (jan1 != 3 || !is_leap(tm->tm_year)))
40 val = 1;
41 }
42 return val;
43}
44
Timo Teräs8a6bd732016-11-22 10:29:08 +020045const char *__strftime_fmt_1(char (*s)[100], size_t *l, int f, const struct tm *tm, locale_t loc, int pad)
Rich Felker0b44a032011-02-12 00:22:29 -050046{
47 nl_item item;
Rich Felker33413cd2013-08-22 19:44:02 -040048 long long val;
Rich Felker8d93cb52015-10-14 18:58:15 -040049 const char *fmt = "-";
Timo Teräs8a6bd732016-11-22 10:29:08 +020050 int width = 2, def_pad = '0';
Rich Felkerf5e4efc2013-08-22 19:02:52 -040051
Rich Felkerf5e4efc2013-08-22 19:02:52 -040052 switch (f) {
53 case 'a':
Rich Felker8d93cb52015-10-14 18:58:15 -040054 if (tm->tm_wday > 6U) goto string;
Rich Felkerf5e4efc2013-08-22 19:02:52 -040055 item = ABDAY_1 + tm->tm_wday;
56 goto nl_strcat;
57 case 'A':
Rich Felker8d93cb52015-10-14 18:58:15 -040058 if (tm->tm_wday > 6U) goto string;
Rich Felkerf5e4efc2013-08-22 19:02:52 -040059 item = DAY_1 + tm->tm_wday;
60 goto nl_strcat;
61 case 'h':
62 case 'b':
Rich Felker8d93cb52015-10-14 18:58:15 -040063 if (tm->tm_mon > 11U) goto string;
Rich Felkerf5e4efc2013-08-22 19:02:52 -040064 item = ABMON_1 + tm->tm_mon;
65 goto nl_strcat;
66 case 'B':
Rich Felker8d93cb52015-10-14 18:58:15 -040067 if (tm->tm_mon > 11U) goto string;
Rich Felkerf5e4efc2013-08-22 19:02:52 -040068 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':
Rich Felker33413cd2013-08-22 19:44:02 -040074 val = (1900LL+tm->tm_year) / 100;
Rich Felkerf5e4efc2013-08-22 19:02:52 -040075 goto number;
Timo Teräs8a6bd732016-11-22 10:29:08 +020076 case 'e':
77 def_pad = '_';
Rich Felkerf5e4efc2013-08-22 19:02:52 -040078 case 'd':
79 val = tm->tm_mday;
Rich Felkerf5e4efc2013-08-22 19:02:52 -040080 goto number;
81 case 'D':
82 fmt = "%m/%d/%y";
83 goto recu_strftime;
Rich Felkerf5e4efc2013-08-22 19:02:52 -040084 case 'F':
85 fmt = "%Y-%m-%d";
86 goto recu_strftime;
87 case 'g':
88 case 'G':
Rich Felker33413cd2013-08-22 19:44:02 -040089 val = tm->tm_year + 1900LL;
Rich Felkerf5e4efc2013-08-22 19:02:52 -040090 if (tm->tm_yday < 3 && week_num(tm) != 1) val--;
91 else if (tm->tm_yday > 360 && week_num(tm) == 1) val++;
Rich Felker33413cd2013-08-22 19:44:02 -040092 if (f=='g') val %= 100;
93 else width = 4;
Rich Felkerf5e4efc2013-08-22 19:02:52 -040094 goto number;
95 case 'H':
96 val = tm->tm_hour;
Rich Felkerf5e4efc2013-08-22 19:02:52 -040097 goto number;
98 case 'I':
99 val = tm->tm_hour;
100 if (!val) val = 12;
101 else if (val > 12) val -= 12;
Rich Felkerf5e4efc2013-08-22 19:02:52 -0400102 goto number;
103 case 'j':
104 val = tm->tm_yday+1;
Rich Felker33413cd2013-08-22 19:44:02 -0400105 width = 3;
Rich Felkerf5e4efc2013-08-22 19:02:52 -0400106 goto number;
107 case 'm':
108 val = tm->tm_mon+1;
Rich Felkerf5e4efc2013-08-22 19:02:52 -0400109 goto number;
110 case 'M':
111 val = tm->tm_min;
Rich Felkerf5e4efc2013-08-22 19:02:52 -0400112 goto number;
113 case 'n':
Rich Felker87e133b2013-08-22 19:36:30 -0400114 *l = 1;
Rich Felker45849d32013-08-22 19:27:36 -0400115 return "\n";
Rich Felkerf5e4efc2013-08-22 19:02:52 -0400116 case 'p':
117 item = tm->tm_hour >= 12 ? PM_STR : AM_STR;
118 goto nl_strcat;
119 case 'r':
120 item = T_FMT_AMPM;
121 goto nl_strftime;
122 case 'R':
123 fmt = "%H:%M";
124 goto recu_strftime;
Rich Felker242a4bb2013-08-25 02:02:15 -0400125 case 's':
Natanael Copac13f2af2015-08-13 17:28:39 +0200126 val = __tm_to_secs(tm) - tm->__tm_gmtoff;
Szabolcs Nagyac0acd52014-05-08 19:04:48 +0200127 width = 1;
Rich Felker242a4bb2013-08-25 02:02:15 -0400128 goto number;
Rich Felkerf5e4efc2013-08-22 19:02:52 -0400129 case 'S':
130 val = tm->tm_sec;
Rich Felkerf5e4efc2013-08-22 19:02:52 -0400131 goto number;
132 case 't':
Rich Felker87e133b2013-08-22 19:36:30 -0400133 *l = 1;
Rich Felker45849d32013-08-22 19:27:36 -0400134 return "\t";
Rich Felkerf5e4efc2013-08-22 19:02:52 -0400135 case 'T':
136 fmt = "%H:%M:%S";
137 goto recu_strftime;
138 case 'u':
139 val = tm->tm_wday ? tm->tm_wday : 7;
Rich Felker33413cd2013-08-22 19:44:02 -0400140 width = 1;
Rich Felkerf5e4efc2013-08-22 19:02:52 -0400141 goto number;
142 case 'U':
Rich Felker8d93cb52015-10-14 18:58:15 -0400143 val = (tm->tm_yday + 7U - tm->tm_wday) / 7;
Rich Felkerf5e4efc2013-08-22 19:02:52 -0400144 goto number;
145 case 'W':
Rich Felker8d93cb52015-10-14 18:58:15 -0400146 val = (tm->tm_yday + 7U - (tm->tm_wday+6U)%7) / 7;
Rich Felkerf5e4efc2013-08-22 19:02:52 -0400147 goto number;
148 case 'V':
149 val = week_num(tm);
Rich Felkerf5e4efc2013-08-22 19:02:52 -0400150 goto number;
151 case 'w':
152 val = tm->tm_wday;
Rich Felker33413cd2013-08-22 19:44:02 -0400153 width = 1;
Rich Felkerf5e4efc2013-08-22 19:02:52 -0400154 goto number;
155 case 'x':
156 item = D_FMT;
157 goto nl_strftime;
158 case 'X':
159 item = T_FMT;
160 goto nl_strftime;
161 case 'y':
Rich Felker61fb81e2017-01-02 17:30:40 -0500162 val = (tm->tm_year + 1900LL) % 100;
163 if (val < 0) val = -val;
Rich Felkerf5e4efc2013-08-22 19:02:52 -0400164 goto number;
165 case 'Y':
Rich Felker8d93cb52015-10-14 18:58:15 -0400166 val = tm->tm_year + 1900LL;
Rich Felkerfc48cee2013-08-22 22:36:19 -0400167 if (val >= 10000) {
168 *l = snprintf(*s, sizeof *s, "+%lld", val);
169 return *s;
170 }
Rich Felker33413cd2013-08-22 19:44:02 -0400171 width = 4;
Rich Felkerf5e4efc2013-08-22 19:02:52 -0400172 goto number;
173 case 'z':
Rich Felkerd78be392013-08-24 12:59:02 -0400174 if (tm->tm_isdst < 0) {
175 *l = 0;
176 return "";
177 }
Rich Felker1ad81382018-08-07 22:15:04 -0400178 *l = snprintf(*s, sizeof *s, "%+.4ld",
179 tm->__tm_gmtoff/3600*100 + tm->__tm_gmtoff%3600/60);
Rich Felker45849d32013-08-22 19:27:36 -0400180 return *s;
Rich Felkerf5e4efc2013-08-22 19:02:52 -0400181 case 'Z':
Rich Felkerd78be392013-08-24 12:59:02 -0400182 if (tm->tm_isdst < 0) {
183 *l = 0;
184 return "";
185 }
186 fmt = __tm_to_tzname(tm);
Rich Felker87e133b2013-08-22 19:36:30 -0400187 goto string;
Rich Felkerf5e4efc2013-08-22 19:02:52 -0400188 case '%':
Rich Felker87e133b2013-08-22 19:36:30 -0400189 *l = 1;
Rich Felker45849d32013-08-22 19:27:36 -0400190 return "%";
Rich Felkerf5e4efc2013-08-22 19:02:52 -0400191 default:
192 return 0;
193 }
194number:
Timo Teräs8a6bd732016-11-22 10:29:08 +0200195 switch (pad ? pad : def_pad) {
196 case '-': *l = snprintf(*s, sizeof *s, "%lld", val); break;
197 case '_': *l = snprintf(*s, sizeof *s, "%*lld", width, val); break;
198 case '0':
199 default: *l = snprintf(*s, sizeof *s, "%0*lld", width, val); break;
200 }
Rich Felker45849d32013-08-22 19:27:36 -0400201 return *s;
Rich Felkerf5e4efc2013-08-22 19:02:52 -0400202nl_strcat:
Rich Felker87e133b2013-08-22 19:36:30 -0400203 fmt = __nl_langinfo_l(item, loc);
204string:
205 *l = strlen(fmt);
206 return fmt;
Rich Felkerf5e4efc2013-08-22 19:02:52 -0400207nl_strftime:
208 fmt = __nl_langinfo_l(item, loc);
209recu_strftime:
Rich Felker87e133b2013-08-22 19:36:30 -0400210 *l = __strftime_l(*s, sizeof *s, fmt, tm, loc);
211 if (!*l) return 0;
Rich Felker45849d32013-08-22 19:27:36 -0400212 return *s;
Rich Felkerf5e4efc2013-08-22 19:02:52 -0400213}
214
215size_t __strftime_l(char *restrict s, size_t n, const char *restrict f, const struct tm *restrict tm, locale_t loc)
216{
217 size_t l, k;
Rich Felker45849d32013-08-22 19:27:36 -0400218 char buf[100];
Rich Felkerfc48cee2013-08-22 22:36:19 -0400219 char *p;
Rich Felker45849d32013-08-22 19:27:36 -0400220 const char *t;
Timo Teräs8a6bd732016-11-22 10:29:08 +0200221 int pad, plus;
Rich Felkerfc48cee2013-08-22 22:36:19 -0400222 unsigned long width;
Rich Felkerf63b8c82013-11-26 20:01:21 -0500223 for (l=0; l<n; f++) {
Rich Felker45849d32013-08-22 19:27:36 -0400224 if (!*f) {
225 s[l] = 0;
226 return l;
227 }
Rich Felkerd53b1f82013-07-27 17:47:03 -0400228 if (*f != '%') {
Rich Felkerd53b1f82013-07-27 17:47:03 -0400229 s[l++] = *f;
230 continue;
231 }
Rich Felkerf5e4efc2013-08-22 19:02:52 -0400232 f++;
Timo Teräs8a6bd732016-11-22 10:29:08 +0200233 pad = 0;
234 if (*f == '-' || *f == '_' || *f == '0') pad = *f++;
Rich Felkerfc48cee2013-08-22 22:36:19 -0400235 if ((plus = (*f == '+'))) f++;
236 width = strtoul(f, &p, 10);
237 if (*p == 'C' || *p == 'F' || *p == 'G' || *p == 'Y') {
238 if (!width && p!=f) width = 1;
Rich Felkerfc48cee2013-08-22 22:36:19 -0400239 } else {
240 width = 0;
241 }
242 f = p;
Rich Felkerf5e4efc2013-08-22 19:02:52 -0400243 if (*f == 'E' || *f == 'O') f++;
Timo Teräs8a6bd732016-11-22 10:29:08 +0200244 t = __strftime_fmt_1(&buf, &k, *f, tm, loc, pad);
Rich Felkerf63b8c82013-11-26 20:01:21 -0500245 if (!t) break;
Rich Felkerfc48cee2013-08-22 22:36:19 -0400246 if (width) {
Rich Felkerc7f0da42018-02-06 12:31:06 -0500247 /* Trim off any sign and leading zeros, then
248 * count remaining digits to determine behavior
249 * for the + flag. */
Rich Felker596207a2018-02-05 13:36:04 -0500250 if (*t=='+' || *t=='-') t++, k--;
251 for (; *t=='0' && t[1]-'0'<10U; t++, k--);
Rich Felkerc7f0da42018-02-06 12:31:06 -0500252 if (width < k) width = k;
253 size_t d;
254 for (d=0; t[d]-'0'<10U; d++);
255 if (tm->tm_year < -1900) {
Rich Felkerfc48cee2013-08-22 22:36:19 -0400256 s[l++] = '-';
Rich Felkerc7f0da42018-02-06 12:31:06 -0500257 width--;
258 } else if (plus && d+(width-k) >= (*p=='C'?3:5)) {
259 s[l++] = '+';
260 width--;
261 }
Rich Felkerf63b8c82013-11-26 20:01:21 -0500262 for (; width > k && l < n; width--)
Rich Felkerfc48cee2013-08-22 22:36:19 -0400263 s[l++] = '0';
264 }
Rich Felkerf63b8c82013-11-26 20:01:21 -0500265 if (k > n-l) k = n-l;
Rich Felker45849d32013-08-22 19:27:36 -0400266 memcpy(s+l, t, k);
Rich Felkerf5e4efc2013-08-22 19:02:52 -0400267 l += k;
Rich Felker0b44a032011-02-12 00:22:29 -0500268 }
Rich Felkerf63b8c82013-11-26 20:01:21 -0500269 if (n) {
270 if (l==n) l=n-1;
271 s[l] = 0;
272 }
Rich Felker45849d32013-08-22 19:27:36 -0400273 return 0;
Rich Felker0b44a032011-02-12 00:22:29 -0500274}
Rich Felker0a37d992013-07-24 17:58:31 -0400275
276size_t strftime(char *restrict s, size_t n, const char *restrict f, const struct tm *restrict tm)
277{
Rich Felker4c485012014-07-02 21:46:41 -0400278 return __strftime_l(s, n, f, tm, CURRENT_LOCALE);
Rich Felker0a37d992013-07-24 17:58:31 -0400279}
280
281weak_alias(__strftime_l, strftime_l);