Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(99)

Side by Side Diff: fusl/src/time/strftime.c

Issue 1714623002: [fusl] clang-format fusl (Closed) Base URL: git@github.com:domokit/mojo.git@master
Patch Set: headers too Created 4 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 #include <stdio.h> 1 #include <stdio.h>
2 #include <stdlib.h> 2 #include <stdlib.h>
3 #include <string.h> 3 #include <string.h>
4 #include <langinfo.h> 4 #include <langinfo.h>
5 #include <locale.h> 5 #include <locale.h>
6 #include <time.h> 6 #include <time.h>
7 #include <limits.h> 7 #include <limits.h>
8 #include "locale_impl.h" 8 #include "locale_impl.h"
9 #include "libc.h" 9 #include "libc.h"
10 #include "time_impl.h" 10 #include "time_impl.h"
11 11
12 const char *__nl_langinfo_l(nl_item, locale_t); 12 const char* __nl_langinfo_l(nl_item, locale_t);
13 13
14 static int is_leap(int y) 14 static int is_leap(int y) {
15 { 15 /* Avoid overflow */
16 » /* Avoid overflow */ 16 if (y > INT_MAX - 1900)
17 » if (y>INT_MAX-1900) y -= 2000; 17 y -= 2000;
18 » y += 1900; 18 y += 1900;
19 » return !(y%4) && ((y%100) || !(y%400)); 19 return !(y % 4) && ((y % 100) || !(y % 400));
20 } 20 }
21 21
22 static int week_num(const struct tm *tm) 22 static int week_num(const struct tm* tm) {
23 { 23 int val = (tm->tm_yday + 7U - (tm->tm_wday + 6U) % 7) / 7;
24 » int val = (tm->tm_yday + 7U - (tm->tm_wday+6U)%7) / 7; 24 /* If 1 Jan is just 1-3 days past Monday,
25 » /* If 1 Jan is just 1-3 days past Monday, 25 * the previous week is also in this year. */
26 » * the previous week is also in this year. */ 26 if ((tm->tm_wday + 371U - tm->tm_yday - 2) % 7 <= 2)
27 » if ((tm->tm_wday + 371U - tm->tm_yday - 2) % 7 <= 2) 27 val++;
28 » » val++; 28 if (!val) {
29 » if (!val) { 29 val = 52;
30 » » val = 52; 30 /* If 31 December of prev year a Thursday,
31 » » /* If 31 December of prev year a Thursday, 31 * or Friday of a leap year, then the
32 » » * or Friday of a leap year, then the 32 * prev year has 53 weeks. */
33 » » * prev year has 53 weeks. */ 33 int dec31 = (tm->tm_wday + 7U - tm->tm_yday - 1) % 7;
34 » » int dec31 = (tm->tm_wday + 7U - tm->tm_yday - 1) % 7; 34 if (dec31 == 4 || (dec31 == 5 && is_leap(tm->tm_year % 400 - 1)))
35 » » if (dec31 == 4 || (dec31 == 5 && is_leap(tm->tm_year%400-1))) 35 val++;
36 » » » val++; 36 } else if (val == 53) {
37 » } else if (val == 53) { 37 /* If 1 January is not a Thursday, and not
38 » » /* If 1 January is not a Thursday, and not 38 * a Wednesday of a leap year, then this
39 » » * a Wednesday of a leap year, then this 39 * year has only 52 weeks. */
40 » » * year has only 52 weeks. */ 40 int jan1 = (tm->tm_wday + 371U - tm->tm_yday) % 7;
41 » » int jan1 = (tm->tm_wday + 371U - tm->tm_yday) % 7; 41 if (jan1 != 4 && (jan1 != 3 || !is_leap(tm->tm_year)))
42 » » if (jan1 != 4 && (jan1 != 3 || !is_leap(tm->tm_year))) 42 val = 1;
43 » » » val = 1; 43 }
44 » } 44 return val;
45 » return val; 45 }
46 } 46
47 47 const char* __tm_to_tzname(const struct tm*);
48 const char *__tm_to_tzname(const struct tm *); 48 size_t __strftime_l(char* restrict,
49 size_t __strftime_l(char *restrict, size_t, const char *restrict, const struct t m *restrict, locale_t); 49 size_t,
50 50 const char* restrict,
51 const char *__strftime_fmt_1(char (*s)[100], size_t *l, int f, const struct tm * tm, locale_t loc) 51 const struct tm* restrict,
52 { 52 locale_t);
53 » nl_item item; 53
54 » long long val; 54 const char* __strftime_fmt_1(char (*s)[100],
55 » const char *fmt = "-"; 55 size_t* l,
56 » int width = 2; 56 int f,
57 57 const struct tm* tm,
58 » switch (f) { 58 locale_t loc) {
59 » case 'a': 59 nl_item item;
60 » » if (tm->tm_wday > 6U) goto string; 60 long long val;
61 » » item = ABDAY_1 + tm->tm_wday; 61 const char* fmt = "-";
62 » » goto nl_strcat; 62 int width = 2;
63 » case 'A': 63
64 » » if (tm->tm_wday > 6U) goto string; 64 switch (f) {
65 » » item = DAY_1 + tm->tm_wday; 65 case 'a':
66 » » goto nl_strcat; 66 if (tm->tm_wday > 6U)
67 » case 'h': 67 goto string;
68 » case 'b': 68 item = ABDAY_1 + tm->tm_wday;
69 » » if (tm->tm_mon > 11U) goto string; 69 goto nl_strcat;
70 » » item = ABMON_1 + tm->tm_mon; 70 case 'A':
71 » » goto nl_strcat; 71 if (tm->tm_wday > 6U)
72 » case 'B': 72 goto string;
73 » » if (tm->tm_mon > 11U) goto string; 73 item = DAY_1 + tm->tm_wday;
74 » » item = MON_1 + tm->tm_mon; 74 goto nl_strcat;
75 » » goto nl_strcat; 75 case 'h':
76 » case 'c': 76 case 'b':
77 » » item = D_T_FMT; 77 if (tm->tm_mon > 11U)
78 » » goto nl_strftime; 78 goto string;
79 » case 'C': 79 item = ABMON_1 + tm->tm_mon;
80 » » val = (1900LL+tm->tm_year) / 100; 80 goto nl_strcat;
81 » » goto number; 81 case 'B':
82 » case 'd': 82 if (tm->tm_mon > 11U)
83 » » val = tm->tm_mday; 83 goto string;
84 » » goto number; 84 item = MON_1 + tm->tm_mon;
85 » case 'D': 85 goto nl_strcat;
86 » » fmt = "%m/%d/%y"; 86 case 'c':
87 » » goto recu_strftime; 87 item = D_T_FMT;
88 » case 'e': 88 goto nl_strftime;
89 » » *l = snprintf(*s, sizeof *s, "%2d", tm->tm_mday); 89 case 'C':
90 » » return *s; 90 val = (1900LL + tm->tm_year) / 100;
91 » case 'F': 91 goto number;
92 » » fmt = "%Y-%m-%d"; 92 case 'd':
93 » » goto recu_strftime; 93 val = tm->tm_mday;
94 » case 'g': 94 goto number;
95 » case 'G': 95 case 'D':
96 » » val = tm->tm_year + 1900LL; 96 fmt = "%m/%d/%y";
97 » » if (tm->tm_yday < 3 && week_num(tm) != 1) val--; 97 goto recu_strftime;
98 » » else if (tm->tm_yday > 360 && week_num(tm) == 1) val++; 98 case 'e':
99 » » if (f=='g') val %= 100; 99 *l = snprintf(*s, sizeof *s, "%2d", tm->tm_mday);
100 » » else width = 4; 100 return *s;
101 » » goto number; 101 case 'F':
102 » case 'H': 102 fmt = "%Y-%m-%d";
103 » » val = tm->tm_hour; 103 goto recu_strftime;
104 » » goto number; 104 case 'g':
105 » case 'I': 105 case 'G':
106 » » val = tm->tm_hour; 106 val = tm->tm_year + 1900LL;
107 » » if (!val) val = 12; 107 if (tm->tm_yday < 3 && week_num(tm) != 1)
108 » » else if (val > 12) val -= 12; 108 val--;
109 » » goto number; 109 else if (tm->tm_yday > 360 && week_num(tm) == 1)
110 » case 'j': 110 val++;
111 » » val = tm->tm_yday+1; 111 if (f == 'g')
112 » » width = 3; 112 val %= 100;
113 » » goto number; 113 else
114 » case 'm': 114 width = 4;
115 » » val = tm->tm_mon+1; 115 goto number;
116 » » goto number; 116 case 'H':
117 » case 'M': 117 val = tm->tm_hour;
118 » » val = tm->tm_min; 118 goto number;
119 » » goto number; 119 case 'I':
120 » case 'n': 120 val = tm->tm_hour;
121 » » *l = 1; 121 if (!val)
122 » » return "\n"; 122 val = 12;
123 » case 'p': 123 else if (val > 12)
124 » » item = tm->tm_hour >= 12 ? PM_STR : AM_STR; 124 val -= 12;
125 » » goto nl_strcat; 125 goto number;
126 » case 'r': 126 case 'j':
127 » » item = T_FMT_AMPM; 127 val = tm->tm_yday + 1;
128 » » goto nl_strftime; 128 width = 3;
129 » case 'R': 129 goto number;
130 » » fmt = "%H:%M"; 130 case 'm':
131 » » goto recu_strftime; 131 val = tm->tm_mon + 1;
132 » case 's': 132 goto number;
133 » » val = __tm_to_secs(tm) - tm->__tm_gmtoff; 133 case 'M':
134 » » width = 1; 134 val = tm->tm_min;
135 » » goto number; 135 goto number;
136 » case 'S': 136 case 'n':
137 » » val = tm->tm_sec; 137 *l = 1;
138 » » goto number; 138 return "\n";
139 » case 't': 139 case 'p':
140 » » *l = 1; 140 item = tm->tm_hour >= 12 ? PM_STR : AM_STR;
141 » » return "\t"; 141 goto nl_strcat;
142 » case 'T': 142 case 'r':
143 » » fmt = "%H:%M:%S"; 143 item = T_FMT_AMPM;
144 » » goto recu_strftime; 144 goto nl_strftime;
145 » case 'u': 145 case 'R':
146 » » val = tm->tm_wday ? tm->tm_wday : 7; 146 fmt = "%H:%M";
147 » » width = 1; 147 goto recu_strftime;
148 » » goto number; 148 case 's':
149 » case 'U': 149 val = __tm_to_secs(tm) - tm->__tm_gmtoff;
150 » » val = (tm->tm_yday + 7U - tm->tm_wday) / 7; 150 width = 1;
151 » » goto number; 151 goto number;
152 » case 'W': 152 case 'S':
153 » » val = (tm->tm_yday + 7U - (tm->tm_wday+6U)%7) / 7; 153 val = tm->tm_sec;
154 » » goto number; 154 goto number;
155 » case 'V': 155 case 't':
156 » » val = week_num(tm); 156 *l = 1;
157 » » goto number; 157 return "\t";
158 » case 'w': 158 case 'T':
159 » » val = tm->tm_wday; 159 fmt = "%H:%M:%S";
160 » » width = 1; 160 goto recu_strftime;
161 » » goto number; 161 case 'u':
162 » case 'x': 162 val = tm->tm_wday ? tm->tm_wday : 7;
163 » » item = D_FMT; 163 width = 1;
164 » » goto nl_strftime; 164 goto number;
165 » case 'X': 165 case 'U':
166 » » item = T_FMT; 166 val = (tm->tm_yday + 7U - tm->tm_wday) / 7;
167 » » goto nl_strftime; 167 goto number;
168 » case 'y': 168 case 'W':
169 » » val = tm->tm_year % 100; 169 val = (tm->tm_yday + 7U - (tm->tm_wday + 6U) % 7) / 7;
170 » » goto number; 170 goto number;
171 » case 'Y': 171 case 'V':
172 » » val = tm->tm_year + 1900LL; 172 val = week_num(tm);
173 » » if (val >= 10000) { 173 goto number;
174 » » » *l = snprintf(*s, sizeof *s, "+%lld", val); 174 case 'w':
175 » » » return *s; 175 val = tm->tm_wday;
176 » » } 176 width = 1;
177 » » width = 4; 177 goto number;
178 » » goto number; 178 case 'x':
179 » case 'z': 179 item = D_FMT;
180 » » if (tm->tm_isdst < 0) { 180 goto nl_strftime;
181 » » » *l = 0; 181 case 'X':
182 » » » return ""; 182 item = T_FMT;
183 » » } 183 goto nl_strftime;
184 » » *l = snprintf(*s, sizeof *s, "%+.2d%.2d", 184 case 'y':
185 » » » (tm->__tm_gmtoff)/3600, 185 val = tm->tm_year % 100;
186 » » » abs(tm->__tm_gmtoff%3600)/60); 186 goto number;
187 » » return *s; 187 case 'Y':
188 » case 'Z': 188 val = tm->tm_year + 1900LL;
189 » » if (tm->tm_isdst < 0) { 189 if (val >= 10000) {
190 » » » *l = 0; 190 *l = snprintf(*s, sizeof *s, "+%lld", val);
191 » » » return ""; 191 return *s;
192 » » } 192 }
193 » » fmt = __tm_to_tzname(tm); 193 width = 4;
194 » » goto string; 194 goto number;
195 » case '%': 195 case 'z':
196 » » *l = 1; 196 if (tm->tm_isdst < 0) {
197 » » return "%"; 197 *l = 0;
198 » default: 198 return "";
199 » » return 0; 199 }
200 » } 200 *l = snprintf(*s, sizeof *s, "%+.2d%.2d", (tm->__tm_gmtoff) / 3600,
201 abs(tm->__tm_gmtoff % 3600) / 60);
202 return *s;
203 case 'Z':
204 if (tm->tm_isdst < 0) {
205 *l = 0;
206 return "";
207 }
208 fmt = __tm_to_tzname(tm);
209 goto string;
210 case '%':
211 *l = 1;
212 return "%";
213 default:
214 return 0;
215 }
201 number: 216 number:
202 » *l = snprintf(*s, sizeof *s, "%0*lld", width, val); 217 *l = snprintf(*s, sizeof *s, "%0*lld", width, val);
203 » return *s; 218 return *s;
204 nl_strcat: 219 nl_strcat:
205 » fmt = __nl_langinfo_l(item, loc); 220 fmt = __nl_langinfo_l(item, loc);
206 string: 221 string:
207 » *l = strlen(fmt); 222 *l = strlen(fmt);
208 » return fmt; 223 return fmt;
209 nl_strftime: 224 nl_strftime:
210 » fmt = __nl_langinfo_l(item, loc); 225 fmt = __nl_langinfo_l(item, loc);
211 recu_strftime: 226 recu_strftime:
212 » *l = __strftime_l(*s, sizeof *s, fmt, tm, loc); 227 *l = __strftime_l(*s, sizeof *s, fmt, tm, loc);
213 » if (!*l) return 0; 228 if (!*l)
214 » return *s; 229 return 0;
215 } 230 return *s;
216 231 }
217 size_t __strftime_l(char *restrict s, size_t n, const char *restrict f, const st ruct tm *restrict tm, locale_t loc) 232
218 { 233 size_t __strftime_l(char* restrict s,
219 » size_t l, k; 234 size_t n,
220 » char buf[100]; 235 const char* restrict f,
221 » char *p; 236 const struct tm* restrict tm,
222 » const char *t; 237 locale_t loc) {
223 » int plus; 238 size_t l, k;
224 » unsigned long width; 239 char buf[100];
225 » for (l=0; l<n; f++) { 240 char* p;
226 » » if (!*f) { 241 const char* t;
227 » » » s[l] = 0; 242 int plus;
228 » » » return l; 243 unsigned long width;
229 » » } 244 for (l = 0; l < n; f++) {
230 » » if (*f != '%') { 245 if (!*f) {
231 » » » s[l++] = *f; 246 s[l] = 0;
232 » » » continue; 247 return l;
233 » » } 248 }
234 » » f++; 249 if (*f != '%') {
235 » » if ((plus = (*f == '+'))) f++; 250 s[l++] = *f;
236 » » width = strtoul(f, &p, 10); 251 continue;
237 » » if (*p == 'C' || *p == 'F' || *p == 'G' || *p == 'Y') { 252 }
238 » » » if (!width && p!=f) width = 1; 253 f++;
239 » » } else { 254 if ((plus = (*f == '+')))
240 » » » width = 0; 255 f++;
241 » » } 256 width = strtoul(f, &p, 10);
242 » » f = p; 257 if (*p == 'C' || *p == 'F' || *p == 'G' || *p == 'Y') {
243 » » if (*f == 'E' || *f == 'O') f++; 258 if (!width && p != f)
244 » » t = __strftime_fmt_1(&buf, &k, *f, tm, loc); 259 width = 1;
245 » » if (!t) break; 260 } else {
246 » » if (width) { 261 width = 0;
247 » » » for (; *t=='+' || *t=='-' || (*t=='0'&&t[1]); t++, k--); 262 }
248 » » » width--; 263 f = p;
249 » » » if (plus && tm->tm_year >= 10000-1900) 264 if (*f == 'E' || *f == 'O')
250 » » » » s[l++] = '+'; 265 f++;
251 » » » else if (tm->tm_year < -1900) 266 t = __strftime_fmt_1(&buf, &k, *f, tm, loc);
252 » » » » s[l++] = '-'; 267 if (!t)
253 » » » else 268 break;
254 » » » » width++; 269 if (width) {
255 » » » for (; width > k && l < n; width--) 270 for (; *t == '+' || *t == '-' || (*t == '0' && t[1]); t++, k--)
256 » » » » s[l++] = '0'; 271 ;
257 » » } 272 width--;
258 » » if (k > n-l) k = n-l; 273 if (plus && tm->tm_year >= 10000 - 1900)
259 » » memcpy(s+l, t, k); 274 s[l++] = '+';
260 » » l += k; 275 else if (tm->tm_year < -1900)
261 » } 276 s[l++] = '-';
262 » if (n) { 277 else
263 » » if (l==n) l=n-1; 278 width++;
264 » » s[l] = 0; 279 for (; width > k && l < n; width--)
265 » } 280 s[l++] = '0';
266 » return 0; 281 }
267 } 282 if (k > n - l)
268 283 k = n - l;
269 size_t strftime(char *restrict s, size_t n, const char *restrict f, const struct tm *restrict tm) 284 memcpy(s + l, t, k);
270 { 285 l += k;
271 » return __strftime_l(s, n, f, tm, CURRENT_LOCALE); 286 }
287 if (n) {
288 if (l == n)
289 l = n - 1;
290 s[l] = 0;
291 }
292 return 0;
293 }
294
295 size_t strftime(char* restrict s,
296 size_t n,
297 const char* restrict f,
298 const struct tm* restrict tm) {
299 return __strftime_l(s, n, f, tm, CURRENT_LOCALE);
272 } 300 }
273 301
274 weak_alias(__strftime_l, strftime_l); 302 weak_alias(__strftime_l, strftime_l);
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698