]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * Copyright (c) 1989 The Regents of the University of California. | |
3 | * All rights reserved. | |
4 | * | |
5 | * Redistribution and use in source and binary forms are permitted | |
6 | * provided that the above copyright notice and this paragraph are | |
7 | * duplicated in all such forms and that any documentation, | |
8 | * advertising materials, and other materials related to such | |
9 | * distribution and use acknowledge that the software was developed | |
10 | * by the University of California, Berkeley. The name of the | |
11 | * University may not be used to endorse or promote products derived | |
12 | * from this software without specific prior written permission. | |
13 | * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR | |
14 | * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED | |
15 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. | |
16 | */ | |
17 | ||
18 | #pragma clang diagnostic push | |
19 | #pragma clang diagnostic ignored "-Wunreachable-code" | |
20 | ||
21 | #if 0 | |
22 | static const char elsieid[] = "@(#)strftime.3 8.3"; | |
23 | /* | |
24 | ** Based on the UCB version with the ID appearing below. | |
25 | ** This is ANSIish only when "multibyte character == plain character". | |
26 | */ | |
27 | #endif | |
28 | ||
29 | #include "xlocale_private.h" | |
30 | ||
31 | #include "namespace.h" | |
32 | #include "private.h" | |
33 | ||
34 | #if defined(LIBC_SCCS) && !defined(lint) | |
35 | static const char sccsid[] = "@(#)strftime.c 5.4 (Berkeley) 3/14/89"; | |
36 | #endif /* LIBC_SCCS and not lint */ | |
37 | #include <sys/cdefs.h> | |
38 | __FBSDID("$FreeBSD: src/lib/libc/stdtime/strftime.c,v 1.44 2009/06/09 09:02:58 delphij Exp $"); | |
39 | ||
40 | #include "tzfile.h" | |
41 | #include <time.h> | |
42 | #include <fcntl.h> | |
43 | #include <sys/stat.h> | |
44 | #include "un-namespace.h" | |
45 | #include "timelocal.h" | |
46 | ||
47 | #if !BUILDING_VARIANT | |
48 | static char * _add(const char *, char *, const char *); | |
49 | static char * _conv(int, const char *, char *, const char *, locale_t); | |
50 | static char * _yconv(int, int, int, int, char *, const char *, locale_t); | |
51 | #endif | |
52 | #define _fmt _st_fmt | |
53 | __private_extern__ char * _fmt(const char *, const struct tm *, char *, const char *, | |
54 | int *, struct lc_time_T *, locale_t); | |
55 | ||
56 | extern char * tzname[]; | |
57 | extern long __darwin_altzone; /* DST timezone offset */ | |
58 | #define altzone __darwin_altzone | |
59 | __private_extern__ long _st_get_timezone(void); | |
60 | ||
61 | #ifndef YEAR_2000_NAME | |
62 | #define YEAR_2000_NAME "CHECK_STRFTIME_FORMATS_FOR_TWO_DIGIT_YEARS" | |
63 | #endif /* !defined YEAR_2000_NAME */ | |
64 | ||
65 | #define IN_NONE 0 | |
66 | #define IN_SOME 1 | |
67 | #define IN_THIS 2 | |
68 | #define IN_ALL 3 | |
69 | ||
70 | #define PAD_DEFAULT 0 | |
71 | #define PAD_LESS 1 | |
72 | #define PAD_SPACE 2 | |
73 | #define PAD_ZERO 3 | |
74 | ||
75 | #ifndef BUILDING_VARIANT | |
76 | static const char * const fmt_padding[][4] = { | |
77 | /* DEFAULT, LESS, SPACE, ZERO */ | |
78 | #define PAD_FMT_MONTHDAY 0 | |
79 | #define PAD_FMT_HMS 0 | |
80 | #define PAD_FMT_CENTURY 0 | |
81 | #define PAD_FMT_SHORTYEAR 0 | |
82 | #define PAD_FMT_MONTH 0 | |
83 | #define PAD_FMT_WEEKOFYEAR 0 | |
84 | #define PAD_FMT_DAYOFMONTH 0 | |
85 | { "%02d", "%d", "%2d", "%02d" }, | |
86 | #define PAD_FMT_SDAYOFMONTH 1 | |
87 | #define PAD_FMT_SHMS 1 | |
88 | { "%2d", "%d", "%2d", "%02d" }, | |
89 | #define PAD_FMT_DAYOFYEAR 2 | |
90 | { "%03d", "%d", "%3d", "%03d" }, | |
91 | #define PAD_FMT_YEAR 3 | |
92 | { "%04d", "%d", "%4d", "%04d" } | |
93 | }; | |
94 | #endif | |
95 | ||
96 | #define USG_COMPAT | |
97 | #define ALTZONE | |
98 | ||
99 | size_t | |
100 | strftime_l(char * __restrict s, size_t maxsize, const char * __restrict format, | |
101 | const struct tm * __restrict t, locale_t loc) | |
102 | { | |
103 | char * p; | |
104 | int warn; | |
105 | ||
106 | NORMALIZE_LOCALE(loc); | |
107 | tzset(); | |
108 | warn = IN_NONE; | |
109 | p = _fmt(((format == NULL) ? "%c" : format), t, s, s + maxsize, &warn, __get_current_time_locale(loc), loc); | |
110 | #ifndef NO_RUN_TIME_WARNINGS_ABOUT_YEAR_2000_PROBLEMS_THANK_YOU | |
111 | if (warn != IN_NONE && getenv(YEAR_2000_NAME) != NULL) { | |
112 | (void) fputs("\n", stderr); | |
113 | if (format == NULL) | |
114 | (void) fputs("NULL strftime format ", stderr); | |
115 | else (void) fprintf_l(stderr, loc, "strftime format \"%s\" ", | |
116 | format); | |
117 | (void) fputs("yields only two digits of years in ", stderr); | |
118 | if (warn == IN_SOME) | |
119 | (void) fputs("some locales", stderr); | |
120 | else if (warn == IN_THIS) | |
121 | (void) fputs("the current locale", stderr); | |
122 | else (void) fputs("all locales", stderr); | |
123 | (void) fputs("\n", stderr); | |
124 | } | |
125 | #endif /* !defined NO_RUN_TIME_WARNINGS_ABOUT_YEAR_2000_PROBLEMS_THANK_YOU */ | |
126 | if (p == s + maxsize) | |
127 | return 0; | |
128 | *p = '\0'; | |
129 | return p - s; | |
130 | } | |
131 | ||
132 | size_t | |
133 | strftime(char * __restrict s, size_t maxsize, const char * __restrict format, | |
134 | const struct tm * __restrict t) | |
135 | { | |
136 | return strftime_l(s, maxsize, format, t, __current_locale()); | |
137 | } | |
138 | ||
139 | #ifndef BUILDING_VARIANT | |
140 | __private_extern__ char * | |
141 | _fmt(format, t, pt, ptlim, warnp, tptr, loc) | |
142 | const char * format; | |
143 | const struct tm * const t; | |
144 | char * pt; | |
145 | const char * const ptlim; | |
146 | int * warnp; | |
147 | struct lc_time_T * tptr; | |
148 | locale_t loc; | |
149 | { | |
150 | int Ealternative, Oalternative, PadIndex; | |
151 | ||
152 | for ( ; *format; ++format) { | |
153 | if (*format == '%') { | |
154 | Ealternative = 0; | |
155 | Oalternative = 0; | |
156 | PadIndex = PAD_DEFAULT; | |
157 | label: | |
158 | switch (*++format) { | |
159 | case '\0': | |
160 | --format; | |
161 | break; | |
162 | case 'A': | |
163 | pt = _add((t->tm_wday < 0 || | |
164 | t->tm_wday >= DAYSPERWEEK) ? | |
165 | "?" : tptr->weekday[t->tm_wday], | |
166 | pt, ptlim); | |
167 | continue; | |
168 | case 'a': | |
169 | pt = _add((t->tm_wday < 0 || | |
170 | t->tm_wday >= DAYSPERWEEK) ? | |
171 | "?" : tptr->wday[t->tm_wday], | |
172 | pt, ptlim); | |
173 | continue; | |
174 | case 'B': | |
175 | pt = _add((t->tm_mon < 0 || | |
176 | t->tm_mon >= MONSPERYEAR) ? | |
177 | "?" : (Oalternative ? tptr->alt_month : | |
178 | tptr->month)[t->tm_mon], | |
179 | pt, ptlim); | |
180 | continue; | |
181 | case 'b': | |
182 | case 'h': | |
183 | pt = _add((t->tm_mon < 0 || | |
184 | t->tm_mon >= MONSPERYEAR) ? | |
185 | "?" : tptr->mon[t->tm_mon], | |
186 | pt, ptlim); | |
187 | continue; | |
188 | case 'C': | |
189 | /* | |
190 | ** %C used to do a... | |
191 | ** _fmt("%a %b %e %X %Y", t, tptr, loc); | |
192 | ** ...whereas now POSIX 1003.2 calls for | |
193 | ** something completely different. | |
194 | ** (ado, 1993-05-24) | |
195 | */ | |
196 | pt = _yconv(t->tm_year, TM_YEAR_BASE, 1, 0, | |
197 | pt, ptlim, loc); | |
198 | continue; | |
199 | case 'c': | |
200 | { | |
201 | int warn2 = IN_SOME; | |
202 | ||
203 | pt = _fmt(tptr->c_fmt, t, pt, ptlim, &warn2, tptr, loc); | |
204 | if (warn2 == IN_ALL) | |
205 | warn2 = IN_THIS; | |
206 | if (warn2 > *warnp) | |
207 | *warnp = warn2; | |
208 | } | |
209 | continue; | |
210 | case 'D': | |
211 | pt = _fmt("%m/%d/%y", t, pt, ptlim, warnp, tptr, loc); | |
212 | continue; | |
213 | case 'd': | |
214 | pt = _conv(t->tm_mday, fmt_padding[PAD_FMT_DAYOFMONTH][PadIndex], | |
215 | pt, ptlim, loc); | |
216 | continue; | |
217 | case 'E': | |
218 | if (Ealternative || Oalternative) | |
219 | break; | |
220 | Ealternative++; | |
221 | goto label; | |
222 | case 'O': | |
223 | /* | |
224 | ** C99 locale modifiers. | |
225 | ** The sequences | |
226 | ** %Ec %EC %Ex %EX %Ey %EY | |
227 | ** %Od %oe %OH %OI %Om %OM | |
228 | ** %OS %Ou %OU %OV %Ow %OW %Oy | |
229 | ** are supposed to provide alternate | |
230 | ** representations. | |
231 | ** | |
232 | ** FreeBSD extension | |
233 | ** %OB | |
234 | */ | |
235 | if (Ealternative || Oalternative) | |
236 | break; | |
237 | Oalternative++; | |
238 | goto label; | |
239 | case 'e': | |
240 | pt = _conv(t->tm_mday, | |
241 | fmt_padding[PAD_FMT_SDAYOFMONTH][PadIndex], pt, ptlim, loc); | |
242 | continue; | |
243 | case 'F': | |
244 | pt = _fmt("%Y-%m-%d", t, pt, ptlim, warnp, tptr, loc); | |
245 | continue; | |
246 | case 'H': | |
247 | pt = _conv(t->tm_hour, fmt_padding[PAD_FMT_HMS][PadIndex], | |
248 | pt, ptlim, loc); | |
249 | continue; | |
250 | case 'I': | |
251 | pt = _conv((t->tm_hour % 12) ? | |
252 | (t->tm_hour % 12) : 12, | |
253 | fmt_padding[PAD_FMT_HMS][PadIndex], pt, ptlim, loc); | |
254 | continue; | |
255 | case 'j': | |
256 | pt = _conv(t->tm_yday + 1, | |
257 | fmt_padding[PAD_FMT_DAYOFYEAR][PadIndex], pt, ptlim, loc); | |
258 | continue; | |
259 | case 'k': | |
260 | /* | |
261 | ** This used to be... | |
262 | ** _conv(t->tm_hour % 12 ? | |
263 | ** t->tm_hour % 12 : 12, 2, ' ', loc); | |
264 | ** ...and has been changed to the below to | |
265 | ** match SunOS 4.1.1 and Arnold Robbins' | |
266 | ** strftime version 3.0. That is, "%k" and | |
267 | ** "%l" have been swapped. | |
268 | ** (ado, 1993-05-24) | |
269 | */ | |
270 | pt = _conv(t->tm_hour, fmt_padding[PAD_FMT_SHMS][PadIndex], | |
271 | pt, ptlim, loc); | |
272 | continue; | |
273 | #ifdef KITCHEN_SINK | |
274 | case 'K': | |
275 | /* | |
276 | ** After all this time, still unclaimed! | |
277 | */ | |
278 | pt = _add("kitchen sink", pt, ptlim); | |
279 | continue; | |
280 | #endif /* defined KITCHEN_SINK */ | |
281 | case 'l': | |
282 | /* | |
283 | ** This used to be... | |
284 | ** _conv(t->tm_hour, 2, ' '); | |
285 | ** ...and has been changed to the below to | |
286 | ** match SunOS 4.1.1 and Arnold Robbin's | |
287 | ** strftime version 3.0. That is, "%k" and | |
288 | ** "%l" have been swapped. | |
289 | ** (ado, 1993-05-24) | |
290 | */ | |
291 | pt = _conv((t->tm_hour % 12) ? | |
292 | (t->tm_hour % 12) : 12, | |
293 | fmt_padding[PAD_FMT_SHMS][PadIndex], pt, ptlim, loc); | |
294 | continue; | |
295 | case 'M': | |
296 | pt = _conv(t->tm_min, fmt_padding[PAD_FMT_HMS][PadIndex], | |
297 | pt, ptlim, loc); | |
298 | continue; | |
299 | case 'm': | |
300 | pt = _conv(t->tm_mon + 1, | |
301 | fmt_padding[PAD_FMT_MONTH][PadIndex], pt, ptlim, loc); | |
302 | continue; | |
303 | case 'n': | |
304 | pt = _add("\n", pt, ptlim); | |
305 | continue; | |
306 | case 'p': | |
307 | pt = _add((t->tm_hour >= (HOURSPERDAY / 2)) ? | |
308 | tptr->pm : | |
309 | tptr->am, | |
310 | pt, ptlim); | |
311 | continue; | |
312 | case 'R': | |
313 | pt = _fmt("%H:%M", t, pt, ptlim, warnp, tptr, loc); | |
314 | continue; | |
315 | case 'r': | |
316 | pt = _fmt(tptr->ampm_fmt, t, pt, ptlim, | |
317 | warnp, tptr, loc); | |
318 | continue; | |
319 | case 'S': | |
320 | pt = _conv(t->tm_sec, fmt_padding[PAD_FMT_HMS][PadIndex], | |
321 | pt, ptlim, loc); | |
322 | continue; | |
323 | case 's': | |
324 | { | |
325 | struct tm tm; | |
326 | char buf[INT_STRLEN_MAXIMUM( | |
327 | time_t) + 1]; | |
328 | time_t mkt; | |
329 | ||
330 | tm = *t; | |
331 | mkt = mktime(&tm); | |
332 | if (TYPE_SIGNED(time_t)) | |
333 | (void) sprintf_l(buf, loc, "%ld", | |
334 | (long) mkt); | |
335 | else (void) sprintf_l(buf, loc, "%lu", | |
336 | (unsigned long) mkt); | |
337 | pt = _add(buf, pt, ptlim); | |
338 | } | |
339 | continue; | |
340 | case 'T': | |
341 | pt = _fmt("%H:%M:%S", t, pt, ptlim, warnp, tptr, loc); | |
342 | continue; | |
343 | case 't': | |
344 | pt = _add("\t", pt, ptlim); | |
345 | continue; | |
346 | case 'U': | |
347 | pt = _conv((t->tm_yday + DAYSPERWEEK - | |
348 | t->tm_wday) / DAYSPERWEEK, | |
349 | fmt_padding[PAD_FMT_WEEKOFYEAR][PadIndex], pt, ptlim, loc); | |
350 | continue; | |
351 | case 'u': | |
352 | /* | |
353 | ** From Arnold Robbins' strftime version 3.0: | |
354 | ** "ISO 8601: Weekday as a decimal number | |
355 | ** [1 (Monday) - 7]" | |
356 | ** (ado, 1993-05-24) | |
357 | */ | |
358 | pt = _conv((t->tm_wday == 0) ? | |
359 | DAYSPERWEEK : t->tm_wday, | |
360 | "%d", pt, ptlim, loc); | |
361 | continue; | |
362 | case 'V': /* ISO 8601 week number */ | |
363 | case 'G': /* ISO 8601 year (four digits) */ | |
364 | case 'g': /* ISO 8601 year (two digits) */ | |
365 | /* | |
366 | ** From Arnold Robbins' strftime version 3.0: "the week number of the | |
367 | ** year (the first Monday as the first day of week 1) as a decimal number | |
368 | ** (01-53)." | |
369 | ** (ado, 1993-05-24) | |
370 | ** | |
371 | ** From "http://www.ft.uni-erlangen.de/~mskuhn/iso-time.html" by Markus Kuhn: | |
372 | ** "Week 01 of a year is per definition the first week which has the | |
373 | ** Thursday in this year, which is equivalent to the week which contains | |
374 | ** the fourth day of January. In other words, the first week of a new year | |
375 | ** is the week which has the majority of its days in the new year. Week 01 | |
376 | ** might also contain days from the previous year and the week before week | |
377 | ** 01 of a year is the last week (52 or 53) of the previous year even if | |
378 | ** it contains days from the new year. A week starts with Monday (day 1) | |
379 | ** and ends with Sunday (day 7). For example, the first week of the year | |
380 | ** 1997 lasts from 1996-12-30 to 1997-01-05..." | |
381 | ** (ado, 1996-01-02) | |
382 | */ | |
383 | { | |
384 | int year; | |
385 | int base; | |
386 | int yday; | |
387 | int wday; | |
388 | int w; | |
389 | ||
390 | year = t->tm_year; | |
391 | base = TM_YEAR_BASE; | |
392 | yday = t->tm_yday; | |
393 | wday = t->tm_wday; | |
394 | for ( ; ; ) { | |
395 | int len; | |
396 | int bot; | |
397 | int top; | |
398 | ||
399 | len = isleap_sum(year, base) ? | |
400 | DAYSPERLYEAR : | |
401 | DAYSPERNYEAR; | |
402 | /* | |
403 | ** What yday (-3 ... 3) does | |
404 | ** the ISO year begin on? | |
405 | */ | |
406 | bot = ((yday + 11 - wday) % | |
407 | DAYSPERWEEK) - 3; | |
408 | /* | |
409 | ** What yday does the NEXT | |
410 | ** ISO year begin on? | |
411 | */ | |
412 | top = bot - | |
413 | (len % DAYSPERWEEK); | |
414 | if (top < -3) | |
415 | top += DAYSPERWEEK; | |
416 | top += len; | |
417 | if (yday >= top) { | |
418 | ++base; | |
419 | w = 1; | |
420 | break; | |
421 | } | |
422 | if (yday >= bot) { | |
423 | w = 1 + ((yday - bot) / | |
424 | DAYSPERWEEK); | |
425 | break; | |
426 | } | |
427 | --base; | |
428 | yday += isleap_sum(year, base) ? | |
429 | DAYSPERLYEAR : | |
430 | DAYSPERNYEAR; | |
431 | } | |
432 | #ifdef XPG4_1994_04_09 | |
433 | if ((w == 52 && | |
434 | t->tm_mon == TM_JANUARY) || | |
435 | (w == 1 && | |
436 | t->tm_mon == TM_DECEMBER)) | |
437 | w = 53; | |
438 | #endif /* defined XPG4_1994_04_09 */ | |
439 | if (*format == 'V') | |
440 | pt = _conv(w, fmt_padding[PAD_FMT_WEEKOFYEAR][PadIndex], | |
441 | pt, ptlim, loc); | |
442 | else if (*format == 'g') { | |
443 | *warnp = IN_ALL; | |
444 | pt = _yconv(year, base, 0, 1, | |
445 | pt, ptlim, loc); | |
446 | } else pt = _yconv(year, base, 1, 1, | |
447 | pt, ptlim, loc); | |
448 | } | |
449 | continue; | |
450 | case 'v': | |
451 | /* | |
452 | ** From Arnold Robbins' strftime version 3.0: | |
453 | ** "date as dd-bbb-YYYY" | |
454 | ** (ado, 1993-05-24) | |
455 | */ | |
456 | pt = _fmt("%e-%b-%Y", t, pt, ptlim, warnp, tptr, loc); | |
457 | continue; | |
458 | case 'W': | |
459 | pt = _conv((t->tm_yday + DAYSPERWEEK - | |
460 | (t->tm_wday ? | |
461 | (t->tm_wday - 1) : | |
462 | (DAYSPERWEEK - 1))) / DAYSPERWEEK, | |
463 | fmt_padding[PAD_FMT_WEEKOFYEAR][PadIndex], pt, ptlim, loc); | |
464 | continue; | |
465 | case 'w': | |
466 | pt = _conv(t->tm_wday, "%d", pt, ptlim, loc); | |
467 | continue; | |
468 | case 'X': | |
469 | pt = _fmt(tptr->X_fmt, t, pt, ptlim, warnp, tptr, loc); | |
470 | continue; | |
471 | case 'x': | |
472 | { | |
473 | int warn2 = IN_SOME; | |
474 | ||
475 | pt = _fmt(tptr->x_fmt, t, pt, ptlim, &warn2, tptr, loc); | |
476 | if (warn2 == IN_ALL) | |
477 | warn2 = IN_THIS; | |
478 | if (warn2 > *warnp) | |
479 | *warnp = warn2; | |
480 | } | |
481 | continue; | |
482 | case 'y': | |
483 | *warnp = IN_ALL; | |
484 | pt = _yconv(t->tm_year, TM_YEAR_BASE, 0, 1, | |
485 | pt, ptlim, loc); | |
486 | continue; | |
487 | case 'Y': | |
488 | pt = _yconv(t->tm_year, TM_YEAR_BASE, 1, 1, | |
489 | pt, ptlim, loc); | |
490 | continue; | |
491 | case 'Z': | |
492 | #ifdef TM_ZONE | |
493 | if (t->TM_ZONE != NULL) | |
494 | pt = _add(t->TM_ZONE, pt, ptlim); | |
495 | else | |
496 | #endif /* defined TM_ZONE */ | |
497 | if (t->tm_isdst >= 0) | |
498 | pt = _add(tzname[t->tm_isdst != 0], | |
499 | pt, ptlim); | |
500 | /* | |
501 | ** C99 says that %Z must be replaced by the | |
502 | ** empty string if the time zone is not | |
503 | ** determinable. | |
504 | */ | |
505 | continue; | |
506 | case 'z': | |
507 | { | |
508 | int diff; | |
509 | char const * sign; | |
510 | ||
511 | if (t->tm_isdst < 0) | |
512 | continue; | |
513 | #if defined(TM_GMTOFF) && !__DARWIN_UNIX03 | |
514 | diff = t->TM_GMTOFF; | |
515 | #else /* !defined TM_GMTOFF || __DARWIN_UNIX03 */ | |
516 | /* | |
517 | ** C99 says that the UTC offset must | |
518 | ** be computed by looking only at | |
519 | ** tm_isdst. This requirement is | |
520 | ** incorrect, since it means the code | |
521 | ** must rely on magic (in this case | |
522 | ** altzone and timezone), and the | |
523 | ** magic might not have the correct | |
524 | ** offset. Doing things correctly is | |
525 | ** tricky and requires disobeying C99; | |
526 | ** see GNU C strftime for details. | |
527 | ** For now, punt and conform to the | |
528 | ** standard, even though it's incorrect. | |
529 | ** | |
530 | ** C99 says that %z must be replaced by the | |
531 | ** empty string if the time zone is not | |
532 | ** determinable, so output nothing if the | |
533 | ** appropriate variables are not available. | |
534 | */ | |
535 | if (t->tm_isdst == 0) | |
536 | #ifdef USG_COMPAT | |
537 | diff = -_st_get_timezone(); | |
538 | #else /* !defined USG_COMPAT */ | |
539 | continue; | |
540 | #endif /* !defined USG_COMPAT */ | |
541 | else | |
542 | #ifdef ALTZONE | |
543 | diff = -altzone; | |
544 | #else /* !defined ALTZONE */ | |
545 | continue; | |
546 | #endif /* !defined ALTZONE */ | |
547 | #endif /* !defined TM_GMTOFF || __DARWIN_UNIX03 */ | |
548 | if (diff < 0) { | |
549 | sign = "-"; | |
550 | diff = -diff; | |
551 | } else sign = "+"; | |
552 | pt = _add(sign, pt, ptlim); | |
553 | diff /= SECSPERMIN; | |
554 | diff = (diff / MINSPERHOUR) * 100 + | |
555 | (diff % MINSPERHOUR); | |
556 | pt = _conv(diff, | |
557 | fmt_padding[PAD_FMT_YEAR][PadIndex], pt, ptlim, loc); | |
558 | } | |
559 | continue; | |
560 | case '+': | |
561 | pt = _fmt(tptr->date_fmt, t, pt, ptlim, | |
562 | warnp, tptr, loc); | |
563 | continue; | |
564 | case '-': | |
565 | if (PadIndex != PAD_DEFAULT) | |
566 | break; | |
567 | PadIndex = PAD_LESS; | |
568 | goto label; | |
569 | case '_': | |
570 | if (PadIndex != PAD_DEFAULT) | |
571 | break; | |
572 | PadIndex = PAD_SPACE; | |
573 | goto label; | |
574 | case '0': | |
575 | if (PadIndex != PAD_DEFAULT) | |
576 | break; | |
577 | PadIndex = PAD_ZERO; | |
578 | goto label; | |
579 | case '%': | |
580 | /* | |
581 | ** X311J/88-090 (4.12.3.5): if conversion char is | |
582 | ** undefined, behavior is undefined. Print out the | |
583 | ** character itself as printf(3) also does. | |
584 | */ | |
585 | default: | |
586 | break; | |
587 | } | |
588 | } | |
589 | if (pt == ptlim) | |
590 | break; | |
591 | *pt++ = *format; | |
592 | } | |
593 | return pt; | |
594 | } | |
595 | ||
596 | static char * | |
597 | _conv(n, format, pt, ptlim, loc) | |
598 | const int n; | |
599 | const char * const format; | |
600 | char * const pt; | |
601 | const char * const ptlim; | |
602 | locale_t loc; | |
603 | { | |
604 | char buf[INT_STRLEN_MAXIMUM(int) + 1]; | |
605 | ||
606 | (void) sprintf_l(buf, loc, format, n); | |
607 | return _add(buf, pt, ptlim); | |
608 | } | |
609 | ||
610 | static char * | |
611 | _add(str, pt, ptlim) | |
612 | const char * str; | |
613 | char * pt; | |
614 | const char * const ptlim; | |
615 | { | |
616 | while (pt < ptlim && (*pt = *str++) != '\0') | |
617 | ++pt; | |
618 | return pt; | |
619 | } | |
620 | ||
621 | /* | |
622 | ** POSIX and the C Standard are unclear or inconsistent about | |
623 | ** what %C and %y do if the year is negative or exceeds 9999. | |
624 | ** Use the convention that %C concatenated with %y yields the | |
625 | ** same output as %Y, and that %Y contains at least 4 bytes, | |
626 | ** with more only if necessary. | |
627 | */ | |
628 | ||
629 | static char * | |
630 | _yconv(a, b, convert_top, convert_yy, pt, ptlim, loc) | |
631 | const int a; | |
632 | const int b; | |
633 | const int convert_top; | |
634 | const int convert_yy; | |
635 | char * pt; | |
636 | const char * const ptlim; | |
637 | locale_t loc; | |
638 | { | |
639 | register int lead; | |
640 | register int trail; | |
641 | ||
642 | #define DIVISOR 100 | |
643 | trail = a % DIVISOR + b % DIVISOR; | |
644 | lead = a / DIVISOR + b / DIVISOR + trail / DIVISOR; | |
645 | trail %= DIVISOR; | |
646 | if (trail < 0 && lead > 0) { | |
647 | trail += DIVISOR; | |
648 | --lead; | |
649 | } else if (lead < 0 && trail > 0) { | |
650 | trail -= DIVISOR; | |
651 | ++lead; | |
652 | } | |
653 | if (convert_top) { | |
654 | if (lead == 0 && trail < 0) | |
655 | pt = _add("-0", pt, ptlim); | |
656 | else pt = _conv(lead, "%02d", pt, ptlim, loc); | |
657 | } | |
658 | if (convert_yy) | |
659 | pt = _conv(((trail < 0) ? -trail : trail), "%02d", pt, ptlim, loc); | |
660 | return pt; | |
661 | } | |
662 | #endif /* !BUILDING_VARIANT */ | |
663 | #pragma clang diagnostic pop |