blob: 794fbe1d70727a057a99a4d709bd71a2a66b8a99 [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 Felker0a37d992013-07-24 17:58:31 -04009#include "libc.h"
Rich Felker242a4bb2013-08-25 02:02:15 -040010#include "time_impl.h"
Rich Felker0b44a032011-02-12 00:22:29 -050011
Rich Felker87be54a2013-07-24 18:52:02 -040012const char *__nl_langinfo_l(nl_item, locale_t);
Rich Felker0b44a032011-02-12 00:22:29 -050013
Rich Felkerc5faf1b2013-06-28 12:03:58 -040014static int is_leap(int y)
15{
16 /* Avoid overflow */
17 if (y>INT_MAX-1900) y -= 2000;
18 y += 1900;
19 return !(y%4) && ((y%100) || !(y%400));
20}
21
Rich Felkeraea79192013-06-28 12:38:42 -040022static int week_num(const struct tm *tm)
23{
24 int val = (tm->tm_yday + 7 - (tm->tm_wday+6)%7) / 7;
25 /* If 1 Jan is just 1-3 days past Monday,
26 * the previous week is also in this year. */
27 if ((tm->tm_wday - tm->tm_yday - 2 + 371) % 7 <= 2)
28 val++;
29 if (!val) {
30 val = 52;
31 /* If 31 December of prev year a Thursday,
32 * or Friday of a leap year, then the
33 * prev year has 53 weeks. */
34 int dec31 = (tm->tm_wday - tm->tm_yday - 1 + 7) % 7;
35 if (dec31 == 4 || (dec31 == 5 && is_leap(tm->tm_year%400-1)))
36 val++;
37 } else if (val == 53) {
38 /* If 1 January is not a Thursday, and not
39 * a Wednesday of a leap year, then this
40 * year has only 52 weeks. */
41 int jan1 = (tm->tm_wday - tm->tm_yday + 371) % 7;
42 if (jan1 != 4 && (jan1 != 3 || !is_leap(tm->tm_year)))
43 val = 1;
44 }
45 return val;
46}
47
Rich Felkerd78be392013-08-24 12:59:02 -040048const char *__tm_to_tzname(const struct tm *);
Rich Felkerf5e4efc2013-08-22 19:02:52 -040049size_t __strftime_l(char *restrict, size_t, const char *restrict, const struct tm *restrict, locale_t);
50
Rich Felker87e133b2013-08-22 19:36:30 -040051const char *__strftime_fmt_1(char (*s)[100], size_t *l, int f, const struct tm *tm, locale_t loc)
Rich Felker0b44a032011-02-12 00:22:29 -050052{
53 nl_item item;
Rich Felker33413cd2013-08-22 19:44:02 -040054 long long val;
Rich Felker0b44a032011-02-12 00:22:29 -050055 const char *fmt;
Rich Felker33413cd2013-08-22 19:44:02 -040056 int width = 2;
Rich Felkerf5e4efc2013-08-22 19:02:52 -040057
Rich Felkerf5e4efc2013-08-22 19:02:52 -040058 switch (f) {
59 case 'a':
60 item = ABDAY_1 + tm->tm_wday;
61 goto nl_strcat;
62 case 'A':
63 item = DAY_1 + tm->tm_wday;
64 goto nl_strcat;
65 case 'h':
66 case 'b':
67 item = ABMON_1 + tm->tm_mon;
68 goto nl_strcat;
69 case 'B':
70 item = MON_1 + tm->tm_mon;
71 goto nl_strcat;
72 case 'c':
73 item = D_T_FMT;
74 goto nl_strftime;
75 case 'C':
Rich Felker33413cd2013-08-22 19:44:02 -040076 val = (1900LL+tm->tm_year) / 100;
Rich Felkerf5e4efc2013-08-22 19:02:52 -040077 goto number;
78 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;
84 case 'e':
Rich Felker2828a132013-08-24 14:35:17 -040085 *l = snprintf(*s, sizeof *s, "%2d", tm->tm_mday);
86 return *s;
Rich Felkerf5e4efc2013-08-22 19:02:52 -040087 case 'F':
88 fmt = "%Y-%m-%d";
89 goto recu_strftime;
90 case 'g':
91 case 'G':
Rich Felker33413cd2013-08-22 19:44:02 -040092 val = tm->tm_year + 1900LL;
Rich Felkerf5e4efc2013-08-22 19:02:52 -040093 if (tm->tm_yday < 3 && week_num(tm) != 1) val--;
94 else if (tm->tm_yday > 360 && week_num(tm) == 1) val++;
Rich Felker33413cd2013-08-22 19:44:02 -040095 if (f=='g') val %= 100;
96 else width = 4;
Rich Felkerf5e4efc2013-08-22 19:02:52 -040097 goto number;
98 case 'H':
99 val = tm->tm_hour;
Rich Felkerf5e4efc2013-08-22 19:02:52 -0400100 goto number;
101 case 'I':
102 val = tm->tm_hour;
103 if (!val) val = 12;
104 else if (val > 12) val -= 12;
Rich Felkerf5e4efc2013-08-22 19:02:52 -0400105 goto number;
106 case 'j':
107 val = tm->tm_yday+1;
Rich Felker33413cd2013-08-22 19:44:02 -0400108 width = 3;
Rich Felkerf5e4efc2013-08-22 19:02:52 -0400109 goto number;
110 case 'm':
111 val = tm->tm_mon+1;
Rich Felkerf5e4efc2013-08-22 19:02:52 -0400112 goto number;
113 case 'M':
114 val = tm->tm_min;
Rich Felkerf5e4efc2013-08-22 19:02:52 -0400115 goto number;
116 case 'n':
Rich Felker87e133b2013-08-22 19:36:30 -0400117 *l = 1;
Rich Felker45849d32013-08-22 19:27:36 -0400118 return "\n";
Rich Felkerf5e4efc2013-08-22 19:02:52 -0400119 case 'p':
120 item = tm->tm_hour >= 12 ? PM_STR : AM_STR;
121 goto nl_strcat;
122 case 'r':
123 item = T_FMT_AMPM;
124 goto nl_strftime;
125 case 'R':
126 fmt = "%H:%M";
127 goto recu_strftime;
Rich Felker242a4bb2013-08-25 02:02:15 -0400128 case 's':
129 val = __tm_to_secs(tm) + tm->__tm_gmtoff;
Szabolcs Nagyac0acd52014-05-08 19:04:48 +0200130 width = 1;
Rich Felker242a4bb2013-08-25 02:02:15 -0400131 goto number;
Rich Felkerf5e4efc2013-08-22 19:02:52 -0400132 case 'S':
133 val = tm->tm_sec;
Rich Felkerf5e4efc2013-08-22 19:02:52 -0400134 goto number;
135 case 't':
Rich Felker87e133b2013-08-22 19:36:30 -0400136 *l = 1;
Rich Felker45849d32013-08-22 19:27:36 -0400137 return "\t";
Rich Felkerf5e4efc2013-08-22 19:02:52 -0400138 case 'T':
139 fmt = "%H:%M:%S";
140 goto recu_strftime;
141 case 'u':
142 val = tm->tm_wday ? tm->tm_wday : 7;
Rich Felker33413cd2013-08-22 19:44:02 -0400143 width = 1;
Rich Felkerf5e4efc2013-08-22 19:02:52 -0400144 goto number;
145 case 'U':
146 val = (tm->tm_yday + 7 - tm->tm_wday) / 7;
Rich Felkerf5e4efc2013-08-22 19:02:52 -0400147 goto number;
148 case 'W':
149 val = (tm->tm_yday + 7 - (tm->tm_wday+6)%7) / 7;
Rich Felkerf5e4efc2013-08-22 19:02:52 -0400150 goto number;
151 case 'V':
152 val = week_num(tm);
Rich Felkerf5e4efc2013-08-22 19:02:52 -0400153 goto number;
154 case 'w':
155 val = tm->tm_wday;
Rich Felker33413cd2013-08-22 19:44:02 -0400156 width = 1;
Rich Felkerf5e4efc2013-08-22 19:02:52 -0400157 goto number;
158 case 'x':
159 item = D_FMT;
160 goto nl_strftime;
161 case 'X':
162 item = T_FMT;
163 goto nl_strftime;
164 case 'y':
165 val = tm->tm_year % 100;
Rich Felkerf5e4efc2013-08-22 19:02:52 -0400166 goto number;
167 case 'Y':
Rich Felkerfc48cee2013-08-22 22:36:19 -0400168 val = tm->tm_year + 1900;
169 if (val >= 10000) {
170 *l = snprintf(*s, sizeof *s, "+%lld", val);
171 return *s;
172 }
Rich Felker33413cd2013-08-22 19:44:02 -0400173 width = 4;
Rich Felkerf5e4efc2013-08-22 19:02:52 -0400174 goto number;
175 case 'z':
Rich Felkerd78be392013-08-24 12:59:02 -0400176 if (tm->tm_isdst < 0) {
177 *l = 0;
178 return "";
179 }
180 *l = snprintf(*s, sizeof *s, "%+.2d%.2d",
181 (-tm->__tm_gmtoff)/3600,
182 abs(tm->__tm_gmtoff%3600)/60);
Rich Felker45849d32013-08-22 19:27:36 -0400183 return *s;
Rich Felkerf5e4efc2013-08-22 19:02:52 -0400184 case 'Z':
Rich Felkerd78be392013-08-24 12:59:02 -0400185 if (tm->tm_isdst < 0) {
186 *l = 0;
187 return "";
188 }
189 fmt = __tm_to_tzname(tm);
Rich Felker87e133b2013-08-22 19:36:30 -0400190 goto string;
Rich Felkerf5e4efc2013-08-22 19:02:52 -0400191 case '%':
Rich Felker87e133b2013-08-22 19:36:30 -0400192 *l = 1;
Rich Felker45849d32013-08-22 19:27:36 -0400193 return "%";
Rich Felkerf5e4efc2013-08-22 19:02:52 -0400194 default:
195 return 0;
196 }
197number:
Rich Felker33413cd2013-08-22 19:44:02 -0400198 *l = snprintf(*s, sizeof *s, "%0*lld", width, val);
Rich Felker45849d32013-08-22 19:27:36 -0400199 return *s;
Rich Felkerf5e4efc2013-08-22 19:02:52 -0400200nl_strcat:
Rich Felker87e133b2013-08-22 19:36:30 -0400201 fmt = __nl_langinfo_l(item, loc);
202string:
203 *l = strlen(fmt);
204 return fmt;
Rich Felkerf5e4efc2013-08-22 19:02:52 -0400205nl_strftime:
206 fmt = __nl_langinfo_l(item, loc);
207recu_strftime:
Rich Felker87e133b2013-08-22 19:36:30 -0400208 *l = __strftime_l(*s, sizeof *s, fmt, tm, loc);
209 if (!*l) return 0;
Rich Felker45849d32013-08-22 19:27:36 -0400210 return *s;
Rich Felkerf5e4efc2013-08-22 19:02:52 -0400211}
212
213size_t __strftime_l(char *restrict s, size_t n, const char *restrict f, const struct tm *restrict tm, locale_t loc)
214{
215 size_t l, k;
Rich Felker45849d32013-08-22 19:27:36 -0400216 char buf[100];
Rich Felkerfc48cee2013-08-22 22:36:19 -0400217 char *p;
Rich Felker45849d32013-08-22 19:27:36 -0400218 const char *t;
Rich Felkerfc48cee2013-08-22 22:36:19 -0400219 int plus;
220 unsigned long width;
Rich Felkerf63b8c82013-11-26 20:01:21 -0500221 for (l=0; l<n; f++) {
Rich Felker45849d32013-08-22 19:27:36 -0400222 if (!*f) {
223 s[l] = 0;
224 return l;
225 }
Rich Felkerd53b1f82013-07-27 17:47:03 -0400226 if (*f != '%') {
Rich Felkerd53b1f82013-07-27 17:47:03 -0400227 s[l++] = *f;
228 continue;
229 }
Rich Felkerf5e4efc2013-08-22 19:02:52 -0400230 f++;
Rich Felkerfc48cee2013-08-22 22:36:19 -0400231 if ((plus = (*f == '+'))) f++;
232 width = strtoul(f, &p, 10);
233 if (*p == 'C' || *p == 'F' || *p == 'G' || *p == 'Y') {
234 if (!width && p!=f) width = 1;
Rich Felkerfc48cee2013-08-22 22:36:19 -0400235 } else {
236 width = 0;
237 }
238 f = p;
Rich Felkerf5e4efc2013-08-22 19:02:52 -0400239 if (*f == 'E' || *f == 'O') f++;
Rich Felker87e133b2013-08-22 19:36:30 -0400240 t = __strftime_fmt_1(&buf, &k, *f, tm, loc);
Rich Felkerf63b8c82013-11-26 20:01:21 -0500241 if (!t) break;
Rich Felkerfc48cee2013-08-22 22:36:19 -0400242 if (width) {
243 for (; *t=='+' || *t=='-' || (*t=='0'&&t[1]); t++, k--);
244 width--;
245 if (plus && tm->tm_year >= 10000-1900)
246 s[l++] = '+';
247 else if (tm->tm_year < -1900)
248 s[l++] = '-';
249 else
250 width++;
Rich Felkerf63b8c82013-11-26 20:01:21 -0500251 for (; width > k && l < n; width--)
Rich Felkerfc48cee2013-08-22 22:36:19 -0400252 s[l++] = '0';
253 }
Rich Felkerf63b8c82013-11-26 20:01:21 -0500254 if (k > n-l) k = n-l;
Rich Felker45849d32013-08-22 19:27:36 -0400255 memcpy(s+l, t, k);
Rich Felkerf5e4efc2013-08-22 19:02:52 -0400256 l += k;
Rich Felker0b44a032011-02-12 00:22:29 -0500257 }
Rich Felkerf63b8c82013-11-26 20:01:21 -0500258 if (n) {
259 if (l==n) l=n-1;
260 s[l] = 0;
261 }
Rich Felker45849d32013-08-22 19:27:36 -0400262 return 0;
Rich Felker0b44a032011-02-12 00:22:29 -0500263}
Rich Felker0a37d992013-07-24 17:58:31 -0400264
265size_t strftime(char *restrict s, size_t n, const char *restrict f, const struct tm *restrict tm)
266{
Rich Felker4c485012014-07-02 21:46:41 -0400267 return __strftime_l(s, n, f, tm, CURRENT_LOCALE);
Rich Felker0a37d992013-07-24 17:58:31 -0400268}
269
270weak_alias(__strftime_l, strftime_l);