Age Owner Branch data TLA Line data Source code
1 : : /* Convert a broken-down timestamp to a string. */
2 : :
3 : : /*
4 : : * Copyright 1989 The Regents of the University of California.
5 : : * All rights reserved.
6 : : *
7 : : * Redistribution and use in source and binary forms, with or without
8 : : * modification, are permitted provided that the following conditions
9 : : * are met:
10 : : * 1. Redistributions of source code must retain the above copyright
11 : : * notice, this list of conditions and the following disclaimer.
12 : : * 2. Redistributions in binary form must reproduce the above copyright
13 : : * notice, this list of conditions and the following disclaimer in the
14 : : * documentation and/or other materials provided with the distribution.
15 : : * 3. Neither the name of the University nor the names of its contributors
16 : : * may be used to endorse or promote products derived from this software
17 : : * without specific prior written permission.
18 : : *
19 : : * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS "AS IS" AND
20 : : * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 : : * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 : : * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 : : * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 : : * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 : : * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 : : * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 : : * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 : : * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 : : * SUCH DAMAGE.
30 : : */
31 : :
32 : : /*
33 : : * Based on the UCB version with the copyright notice appearing above.
34 : : *
35 : : * This is ANSIish only when "multibyte character == plain character".
36 : : *
37 : : * IDENTIFICATION
38 : : * src/timezone/strftime.c
39 : : */
40 : :
41 : : #include "postgres.h"
42 : :
43 : : #include <fcntl.h>
44 : :
45 : : #include "private.h"
46 : :
47 : :
48 : : struct lc_time_T
49 : : {
50 : : const char *mon[MONSPERYEAR];
51 : : const char *month[MONSPERYEAR];
52 : : const char *wday[DAYSPERWEEK];
53 : : const char *weekday[DAYSPERWEEK];
54 : : const char *X_fmt;
55 : : const char *x_fmt;
56 : : const char *c_fmt;
57 : : const char *am;
58 : : const char *pm;
59 : : const char *date_fmt;
60 : : };
61 : :
62 : : static const struct lc_time_T C_time_locale = {
63 : : {
64 : : "Jan", "Feb", "Mar", "Apr", "May", "Jun",
65 : : "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
66 : : }, {
67 : : "January", "February", "March", "April", "May", "June",
68 : : "July", "August", "September", "October", "November", "December"
69 : : }, {
70 : : "Sun", "Mon", "Tue", "Wed",
71 : : "Thu", "Fri", "Sat"
72 : : }, {
73 : : "Sunday", "Monday", "Tuesday", "Wednesday",
74 : : "Thursday", "Friday", "Saturday"
75 : : },
76 : :
77 : : /* X_fmt */
78 : : "%H:%M:%S",
79 : :
80 : : /*
81 : : * x_fmt C99 and later require this format. Using just numbers (as here)
82 : : * makes Quakers happier; it's also compatible with SVR4.
83 : : */
84 : : "%m/%d/%y",
85 : :
86 : : /*
87 : : * c_fmt C99 and later require this format. Previously this code used "%D
88 : : * %X", but we now conform to C99. Note that "%a %b %d %H:%M:%S %Y" is
89 : : * used by Solaris 2.3.
90 : : */
91 : : "%a %b %e %T %Y",
92 : :
93 : : /* am */
94 : : "AM",
95 : :
96 : : /* pm */
97 : : "PM",
98 : :
99 : : /* date_fmt */
100 : : "%a %b %e %H:%M:%S %Z %Y"
101 : : };
102 : :
103 : : enum warn
104 : : {
105 : : IN_NONE, IN_SOME, IN_THIS, IN_ALL
106 : : };
107 : :
108 : : static char *_add(const char *str, char *pt, const char *ptlim);
109 : : static char *_conv(int n, const char *format, char *pt, const char *ptlim);
110 : : static char *_fmt(const char *format, const struct pg_tm *t, char *pt, const char *ptlim,
111 : : enum warn *warnp);
112 : : static char *_yconv(int a, int b, bool convert_top, bool convert_yy, char *pt, char const *ptlim);
113 : :
114 : :
115 : : /*
116 : : * Convert timestamp t to string s, a caller-allocated buffer of size maxsize,
117 : : * using the given format pattern.
118 : : *
119 : : * Unlike standard strftime(), we guarantee to provide a null-terminated
120 : : * result even on failure, so long as maxsize > 0. If we overrun the buffer,
121 : : * return an empty string rather than risking mis-encoded multibyte output.
122 : : * (Since this module only supports C locale, you might think multibyte
123 : : * characters are impossible --- but the time zone name printed by %Z comes
124 : : * from outside and could contain such.)
125 : : *
126 : : * Also unlike standard strftime(), we will not crash on a null pg_tm pointer,
127 : : * and we set errno on failures. Surviving a null pointer is useful because
128 : : * many callers pass the result of pg_localtime() with no further check.
129 : : *
130 : : * See also timestamptz_to_str.
131 : : */
132 : : size_t
3323 tgl@sss.pgh.pa.us 133 :CBC 583291 : pg_strftime(char *s, size_t maxsize, const char *format, const struct pg_tm *t)
134 : : {
135 : : char *p;
2234 136 : 583291 : int saved_errno = errno;
3354 137 : 583291 : enum warn warn = IN_NONE;
138 : :
4 139 [ - + ]: 583291 : if (!t)
140 : : {
4 tgl@sss.pgh.pa.us 141 :UBC 0 : errno = EINVAL;
142 [ # # ]: 0 : if (maxsize > 0)
143 : 0 : *s = '\0';
144 : 0 : return 0;
145 : : }
3692 tgl@sss.pgh.pa.us 146 :CBC 583291 : p = _fmt(format, t, s, s + maxsize, &warn);
2234 147 [ - + ]: 583291 : if (!p)
148 : : {
2234 tgl@sss.pgh.pa.us 149 :UBC 0 : errno = EOVERFLOW;
201 150 [ # # ]: 0 : if (maxsize > 0)
151 : 0 : *s = '\0';
2234 152 : 0 : return 0;
153 : : }
8226 tgl@sss.pgh.pa.us 154 [ - + ]:CBC 583291 : if (p == s + maxsize)
155 : : {
2234 tgl@sss.pgh.pa.us 156 :UBC 0 : errno = ERANGE;
201 157 [ # # ]: 0 : if (maxsize > 0)
158 : 0 : *s = '\0';
8226 159 : 0 : return 0;
160 : : }
8226 tgl@sss.pgh.pa.us 161 :CBC 583291 : *p = '\0';
2234 162 : 583291 : errno = saved_errno;
8226 163 : 583291 : return p - s;
164 : : }
165 : :
166 : : static char *
3354 167 : 583291 : _fmt(const char *format, const struct pg_tm *t, char *pt,
168 : : const char *ptlim, enum warn *warnp)
169 : : {
150 tgl@sss.pgh.pa.us 170 :GNC 583291 : struct lc_time_T const *Locale = &C_time_locale;
171 : :
8226 bruce@momjian.us 172 [ + + ]:CBC 9702713 : for (; *format; ++format)
173 : : {
174 [ + + ]: 9119422 : if (*format == '%')
175 : : {
176 : 4080628 : label:
177 [ - - + - : 4080628 : switch (*++format)
+ - - - +
- - - + -
- - - + +
- - - - +
- - - - -
- - - - -
- + + - -
- ]
178 : : {
150 tgl@sss.pgh.pa.us 179 :UNC 0 : default:
180 : :
181 : : /*
182 : : * Output unknown conversion specifiers as-is, to aid
183 : : * debugging. This includes '%' at format end. This
184 : : * conforms to C23 section 7.29.3.5 paragraph 6, which
185 : : * says behavior is undefined here.
186 : : */
8226 bruce@momjian.us 187 :UBC 0 : --format;
188 : 0 : break;
189 : 0 : case 'A':
190 [ # # ]: 0 : pt = _add((t->tm_wday < 0 ||
191 [ # # ]: 0 : t->tm_wday >= DAYSPERWEEK) ?
192 : 0 : "?" : Locale->weekday[t->tm_wday],
193 : : pt, ptlim);
194 : 0 : continue;
8226 bruce@momjian.us 195 :CBC 400 : case 'a':
196 [ + - ]: 800 : pt = _add((t->tm_wday < 0 ||
197 [ + - ]: 400 : t->tm_wday >= DAYSPERWEEK) ?
198 : 400 : "?" : Locale->wday[t->tm_wday],
199 : : pt, ptlim);
200 : 400 : continue;
8226 bruce@momjian.us 201 :UBC 0 : case 'B':
202 [ # # ]: 0 : pt = _add((t->tm_mon < 0 ||
203 [ # # ]: 0 : t->tm_mon >= MONSPERYEAR) ?
204 : 0 : "?" : Locale->month[t->tm_mon],
205 : : pt, ptlim);
206 : 0 : continue;
8226 bruce@momjian.us 207 :CBC 400 : case 'b':
208 : : case 'h':
209 [ + - ]: 800 : pt = _add((t->tm_mon < 0 ||
210 [ + - ]: 400 : t->tm_mon >= MONSPERYEAR) ?
211 : 400 : "?" : Locale->mon[t->tm_mon],
212 : : pt, ptlim);
213 : 400 : continue;
8226 bruce@momjian.us 214 :UBC 0 : case 'C':
215 : :
216 : : /*
217 : : * %C used to do a... _fmt("%a %b %e %X %Y", t);
218 : : * ...whereas now POSIX 1003.2 calls for something
219 : : * completely different. (ado, 1993-05-24)
220 : : */
3897 tgl@sss.pgh.pa.us 221 : 0 : pt = _yconv(t->tm_year, TM_YEAR_BASE,
222 : : true, false, pt, ptlim);
8226 bruce@momjian.us 223 : 0 : continue;
224 : 0 : case 'c':
225 : : {
3354 tgl@sss.pgh.pa.us 226 : 0 : enum warn warn2 = IN_SOME;
227 : :
6106 228 : 0 : pt = _fmt(Locale->c_fmt, t, pt, ptlim, &warn2);
8226 bruce@momjian.us 229 [ # # ]: 0 : if (warn2 == IN_ALL)
230 : 0 : warn2 = IN_THIS;
231 [ # # ]: 0 : if (warn2 > *warnp)
232 : 0 : *warnp = warn2;
233 : : }
234 : 0 : continue;
235 : 0 : case 'D':
236 : 0 : pt = _fmt("%m/%d/%y", t, pt, ptlim, warnp);
237 : 0 : continue;
8226 bruce@momjian.us 238 :CBC 582891 : case 'd':
239 : 582891 : pt = _conv(t->tm_mday, "%02d", pt, ptlim);
240 : 582891 : continue;
8226 bruce@momjian.us 241 :UBC 0 : case 'E':
242 : : case 'O':
243 : :
244 : : /*
245 : : * Locale modifiers of C99 and later. The sequences %Ec
246 : : * %EC %Ex %EX %Ey %EY %Od %oe %OH %OI %Om %OM %OS %Ou %OU
247 : : * %OV %Ow %OW %Oy are supposed to provide alternative
248 : : * representations.
249 : : */
250 : 0 : goto label;
251 : 0 : case 'e':
252 : 0 : pt = _conv(t->tm_mday, "%2d", pt, ptlim);
253 : 0 : continue;
254 : 0 : case 'F':
255 : 0 : pt = _fmt("%Y-%m-%d", t, pt, ptlim, warnp);
256 : 0 : continue;
8226 bruce@momjian.us 257 :CBC 582891 : case 'H':
258 : 582891 : pt = _conv(t->tm_hour, "%02d", pt, ptlim);
259 : 582891 : continue;
8226 bruce@momjian.us 260 :UBC 0 : case 'I':
261 [ # # ]: 0 : pt = _conv((t->tm_hour % 12) ?
262 : 0 : (t->tm_hour % 12) : 12,
263 : : "%02d", pt, ptlim);
264 : 0 : continue;
265 : 0 : case 'j':
266 : 0 : pt = _conv(t->tm_yday + 1, "%03d", pt, ptlim);
267 : 0 : continue;
268 : 0 : case 'k':
269 : :
270 : : /*
271 : : * This used to be... _conv(t->tm_hour % 12 ? t->tm_hour %
272 : : * 12 : 12, 2, ' '); ...and has been changed to the below
273 : : * to match SunOS 4.1.1 and Arnold Robbins' strftime
274 : : * version 3.0. That is, "%k" and "%l" have been swapped.
275 : : * (ado, 1993-05-24)
276 : : */
277 : 0 : pt = _conv(t->tm_hour, "%2d", pt, ptlim);
278 : 0 : continue;
279 : : #ifdef KITCHEN_SINK
280 : : case 'K':
281 : :
282 : : /*
283 : : * After all this time, still unclaimed!
284 : : */
285 : : pt = _add("kitchen sink", pt, ptlim);
286 : : continue;
287 : : #endif /* defined KITCHEN_SINK */
288 : 0 : case 'l':
289 : :
290 : : /*
291 : : * This used to be... _conv(t->tm_hour, 2, ' '); ...and
292 : : * has been changed to the below to match SunOS 4.1.1 and
293 : : * Arnold Robbin's strftime version 3.0. That is, "%k" and
294 : : * "%l" have been swapped. (ado, 1993-05-24)
295 : : */
296 [ # # ]: 0 : pt = _conv((t->tm_hour % 12) ?
297 : 0 : (t->tm_hour % 12) : 12,
298 : : "%2d", pt, ptlim);
299 : 0 : continue;
8226 bruce@momjian.us 300 :CBC 582891 : case 'M':
301 : 582891 : pt = _conv(t->tm_min, "%02d", pt, ptlim);
302 : 582891 : continue;
303 : 582491 : case 'm':
304 : 582491 : pt = _conv(t->tm_mon + 1, "%02d", pt, ptlim);
305 : 582491 : continue;
8226 bruce@momjian.us 306 :UBC 0 : case 'n':
307 : 0 : pt = _add("\n", pt, ptlim);
308 : 0 : continue;
309 : 0 : case 'p':
310 [ # # ]: 0 : pt = _add((t->tm_hour >= (HOURSPERDAY / 2)) ?
311 : : Locale->pm :
312 : : Locale->am,
313 : : pt, ptlim);
314 : 0 : continue;
315 : 0 : case 'R':
316 : 0 : pt = _fmt("%H:%M", t, pt, ptlim, warnp);
317 : 0 : continue;
318 : 0 : case 'r':
319 : 0 : pt = _fmt("%I:%M:%S %p", t, pt, ptlim, warnp);
320 : 0 : continue;
8226 bruce@momjian.us 321 :CBC 582891 : case 'S':
322 : 582891 : pt = _conv(t->tm_sec, "%02d", pt, ptlim);
323 : 582891 : continue;
8226 bruce@momjian.us 324 :UBC 0 : case 'T':
325 : 0 : pt = _fmt("%H:%M:%S", t, pt, ptlim, warnp);
326 : 0 : continue;
327 : 0 : case 't':
328 : 0 : pt = _add("\t", pt, ptlim);
329 : 0 : continue;
330 : 0 : case 'U':
331 : 0 : pt = _conv((t->tm_yday + DAYSPERWEEK -
332 : 0 : t->tm_wday) / DAYSPERWEEK,
333 : : "%02d", pt, ptlim);
334 : 0 : continue;
335 : 0 : case 'u':
336 : :
337 : : /*
338 : : * From Arnold Robbins' strftime version 3.0: "ISO 8601:
339 : : * Weekday as a decimal number [1 (Monday) - 7]" (ado,
340 : : * 1993-05-24)
341 : : */
342 [ # # ]: 0 : pt = _conv((t->tm_wday == 0) ?
343 : : DAYSPERWEEK : t->tm_wday,
344 : : "%d", pt, ptlim);
345 : 0 : continue;
346 : 0 : case 'V': /* ISO 8601 week number */
347 : : case 'G': /* ISO 8601 year (four digits) */
348 : : case 'g': /* ISO 8601 year (two digits) */
349 : : /*
350 : : * From Arnold Robbins' strftime version 3.0: "the week number of the
351 : : * year (the first Monday as the first day of week 1) as a decimal number
352 : : * (01-53)."
353 : : * (ado, 1993-05-24)
354 : : *
355 : : * From <https://www.cl.cam.ac.uk/~mgk25/iso-time.html> by Markus Kuhn:
356 : : * "Week 01 of a year is per definition the first week which has the
357 : : * Thursday in this year, which is equivalent to the week which contains
358 : : * the fourth day of January. In other words, the first week of a new year
359 : : * is the week which has the majority of its days in the new year. Week 01
360 : : * might also contain days from the previous year and the week before week
361 : : * 01 of a year is the last week (52 or 53) of the previous year even if
362 : : * it contains days from the new year. A week starts with Monday (day 1)
363 : : * and ends with Sunday (day 7). For example, the first week of the year
364 : : * 1997 lasts from 1996-12-30 to 1997-01-05..."
365 : : * (ado, 1996-01-02)
366 : : */
367 : : {
368 : : int year;
369 : : int base;
370 : : int yday;
371 : : int wday;
372 : : int w;
373 : :
6860 tgl@sss.pgh.pa.us 374 : 0 : year = t->tm_year;
375 : 0 : base = TM_YEAR_BASE;
8226 bruce@momjian.us 376 : 0 : yday = t->tm_yday;
377 : 0 : wday = t->tm_wday;
378 : : for (;;)
379 : 0 : {
380 : : int len;
381 : : int bot;
382 : : int top;
383 : :
6860 tgl@sss.pgh.pa.us 384 [ # # # # ]: 0 : len = isleap_sum(year, base) ?
8226 bruce@momjian.us 385 [ # # ]: 0 : DAYSPERLYEAR :
386 : : DAYSPERNYEAR;
387 : :
388 : : /*
389 : : * What yday (-3 ... 3) does the ISO year begin
390 : : * on?
391 : : */
392 : 0 : bot = ((yday + 11 - wday) %
393 : : DAYSPERWEEK) - 3;
394 : :
395 : : /*
396 : : * What yday does the NEXT ISO year begin on?
397 : : */
398 : 0 : top = bot -
399 : 0 : (len % DAYSPERWEEK);
400 [ # # ]: 0 : if (top < -3)
401 : 0 : top += DAYSPERWEEK;
402 : 0 : top += len;
403 [ # # ]: 0 : if (yday >= top)
404 : : {
6860 tgl@sss.pgh.pa.us 405 : 0 : ++base;
8226 bruce@momjian.us 406 : 0 : w = 1;
407 : 0 : break;
408 : : }
409 [ # # ]: 0 : if (yday >= bot)
410 : : {
411 : 0 : w = 1 + ((yday - bot) /
412 : : DAYSPERWEEK);
413 : 0 : break;
414 : : }
6860 tgl@sss.pgh.pa.us 415 : 0 : --base;
416 [ # # # # ]: 0 : yday += isleap_sum(year, base) ?
8226 bruce@momjian.us 417 [ # # ]: 0 : DAYSPERLYEAR :
418 : : DAYSPERNYEAR;
419 : : }
420 [ # # ]: 0 : if (*format == 'V')
421 : 0 : pt = _conv(w, "%02d",
422 : : pt, ptlim);
423 [ # # ]: 0 : else if (*format == 'g')
424 : : {
425 : 0 : *warnp = IN_ALL;
3897 tgl@sss.pgh.pa.us 426 : 0 : pt = _yconv(year, base,
427 : : false, true,
428 : : pt, ptlim);
429 : : }
430 : : else
431 : 0 : pt = _yconv(year, base,
432 : : true, true,
433 : : pt, ptlim);
434 : : }
8226 bruce@momjian.us 435 : 0 : continue;
436 : 0 : case 'v':
437 : :
438 : : /*
439 : : * From Arnold Robbins' strftime version 3.0: "date as
440 : : * dd-bbb-YYYY" (ado, 1993-05-24)
441 : : */
442 : 0 : pt = _fmt("%e-%b-%Y", t, pt, ptlim, warnp);
443 : 0 : continue;
444 : 0 : case 'W':
445 : 0 : pt = _conv((t->tm_yday + DAYSPERWEEK -
446 : 0 : (t->tm_wday ?
447 [ # # ]: 0 : (t->tm_wday - 1) :
448 : : (DAYSPERWEEK - 1))) / DAYSPERWEEK,
449 : : "%02d", pt, ptlim);
450 : 0 : continue;
451 : 0 : case 'w':
452 : 0 : pt = _conv(t->tm_wday, "%d", pt, ptlim);
453 : 0 : continue;
454 : 0 : case 'X':
455 : 0 : pt = _fmt(Locale->X_fmt, t, pt, ptlim, warnp);
456 : 0 : continue;
457 : 0 : case 'x':
458 : : {
3354 tgl@sss.pgh.pa.us 459 : 0 : enum warn warn2 = IN_SOME;
460 : :
8226 bruce@momjian.us 461 : 0 : pt = _fmt(Locale->x_fmt, t, pt, ptlim, &warn2);
462 [ # # ]: 0 : if (warn2 == IN_ALL)
463 : 0 : warn2 = IN_THIS;
464 [ # # ]: 0 : if (warn2 > *warnp)
465 : 0 : *warnp = warn2;
466 : : }
467 : 0 : continue;
468 : 0 : case 'y':
469 : 0 : *warnp = IN_ALL;
3897 tgl@sss.pgh.pa.us 470 : 0 : pt = _yconv(t->tm_year, TM_YEAR_BASE,
471 : : false, true,
472 : : pt, ptlim);
8226 bruce@momjian.us 473 : 0 : continue;
8226 bruce@momjian.us 474 :CBC 582891 : case 'Y':
3897 tgl@sss.pgh.pa.us 475 : 582891 : pt = _yconv(t->tm_year, TM_YEAR_BASE,
476 : : true, true,
477 : : pt, ptlim);
8226 bruce@momjian.us 478 : 582891 : continue;
479 : 582882 : case 'Z':
480 [ + - ]: 582882 : if (t->tm_zone != NULL)
481 : 582882 : pt = _add(t->tm_zone, pt, ptlim);
482 : :
483 : : /*
484 : : * C99 and later say that %Z must be replaced by the empty
485 : : * string if the time zone abbreviation is not
486 : : * determinable.
487 : : */
488 : 582882 : continue;
8226 bruce@momjian.us 489 :UBC 0 : case 'z':
490 : : {
491 : : long diff;
492 : : char const *sign;
493 : : bool negative;
494 : :
150 tgl@sss.pgh.pa.us 495 :UNC 0 : diff = t->TM_GMTOFF;
3499 tgl@sss.pgh.pa.us 496 :UBC 0 : negative = diff < 0;
497 [ # # ]: 0 : if (diff == 0)
498 : : {
3492 499 [ # # ]: 0 : if (t->tm_zone != NULL)
500 : 0 : negative = t->tm_zone[0] == '-';
501 : : }
3499 502 [ # # ]: 0 : if (negative)
503 : : {
8226 bruce@momjian.us 504 : 0 : sign = "-";
505 : 0 : diff = -diff;
506 : : }
507 : : else
508 : 0 : sign = "+";
509 : 0 : pt = _add(sign, pt, ptlim);
3897 tgl@sss.pgh.pa.us 510 : 0 : diff /= SECSPERMIN;
511 : 0 : diff = (diff / MINSPERHOUR) * 100 +
512 : 0 : (diff % MINSPERHOUR);
513 : 0 : pt = _conv(diff, "%04d", pt, ptlim);
514 : : }
8226 bruce@momjian.us 515 : 0 : continue;
516 : 0 : case '+':
517 : 0 : pt = _fmt(Locale->date_fmt, t, pt, ptlim,
518 : : warnp);
519 : 0 : continue;
520 : 0 : case '%':
521 : 0 : break;
522 : : }
523 : : }
8226 tgl@sss.pgh.pa.us 524 [ - + ]:CBC 5038794 : if (pt == ptlim)
8226 tgl@sss.pgh.pa.us 525 :UBC 0 : break;
8226 tgl@sss.pgh.pa.us 526 :CBC 5038794 : *pt++ = *format;
527 : : }
528 : 583291 : return pt;
529 : : }
530 : :
531 : : static char *
7831 neilc@samurai.com 532 : 4079837 : _conv(int n, const char *format, char *pt, const char *ptlim)
533 : : {
534 : : char buf[INT_STRLEN_MAXIMUM(int) + 1];
535 : :
3897 tgl@sss.pgh.pa.us 536 : 4079837 : sprintf(buf, format, n);
8226 537 : 4079837 : return _add(buf, pt, ptlim);
538 : : }
539 : :
540 : : static char *
bruce@momjian.us 541 : 4663519 : _add(const char *str, char *pt, const char *ptlim)
542 : : {
tgl@sss.pgh.pa.us 543 [ + - + + ]: 14574239 : while (pt < ptlim && (*pt = *str++) != '\0')
544 : 9910720 : ++pt;
545 : 4663519 : return pt;
546 : : }
547 : :
548 : : /*
549 : : * POSIX and the C Standard are unclear or inconsistent about
550 : : * what %C and %y do if the year is negative or exceeds 9999.
551 : : * Use the convention that %C concatenated with %y yields the
552 : : * same output as %Y, and that %Y contains at least 4 bytes,
553 : : * with more only if necessary.
554 : : */
555 : :
556 : : static char *
3897 557 : 582891 : _yconv(int a, int b, bool convert_top, bool convert_yy,
558 : : char *pt, const char *ptlim)
559 : : {
560 : : int lead;
561 : : int trail;
562 : :
150 tgl@sss.pgh.pa.us 563 :GNC 582891 : int DIVISOR = 100;
564 : :
6860 tgl@sss.pgh.pa.us 565 :CBC 582891 : trail = a % DIVISOR + b % DIVISOR;
566 : 582891 : lead = a / DIVISOR + b / DIVISOR + trail / DIVISOR;
567 : 582891 : trail %= DIVISOR;
568 [ - + - - ]: 582891 : if (trail < 0 && lead > 0)
569 : : {
6860 tgl@sss.pgh.pa.us 570 :UBC 0 : trail += DIVISOR;
571 : 0 : --lead;
572 : : }
6860 tgl@sss.pgh.pa.us 573 [ - + - - ]:CBC 582891 : else if (lead < 0 && trail > 0)
574 : : {
6860 tgl@sss.pgh.pa.us 575 :UBC 0 : trail -= DIVISOR;
576 : 0 : ++lead;
577 : : }
6860 tgl@sss.pgh.pa.us 578 [ + - ]:CBC 582891 : if (convert_top)
579 : : {
580 [ - + - - ]: 582891 : if (lead == 0 && trail < 0)
6860 tgl@sss.pgh.pa.us 581 :UBC 0 : pt = _add("-0", pt, ptlim);
582 : : else
6379 bruce@momjian.us 583 :CBC 582891 : pt = _conv(lead, "%02d", pt, ptlim);
584 : : }
6860 tgl@sss.pgh.pa.us 585 [ + - ]: 582891 : if (convert_yy)
586 : 582891 : pt = _conv(((trail < 0) ? -trail : trail), "%02d", pt, ptlim);
587 : 582891 : return pt;
588 : : }
|