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 : #define Locale (&C_time_locale)
63 :
64 : static const struct lc_time_T C_time_locale = {
65 : {
66 : "Jan", "Feb", "Mar", "Apr", "May", "Jun",
67 : "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
68 : }, {
69 : "January", "February", "March", "April", "May", "June",
70 : "July", "August", "September", "October", "November", "December"
71 : }, {
72 : "Sun", "Mon", "Tue", "Wed",
73 : "Thu", "Fri", "Sat"
74 : }, {
75 : "Sunday", "Monday", "Tuesday", "Wednesday",
76 : "Thursday", "Friday", "Saturday"
77 : },
78 :
79 : /* X_fmt */
80 : "%H:%M:%S",
81 :
82 : /*
83 : * x_fmt
84 : *
85 : * C99 requires this format. Using just numbers (as here) makes Quakers
86 : * happier; it's also compatible with SVR4.
87 : */
88 : "%m/%d/%y",
89 :
90 : /*
91 : * c_fmt
92 : *
93 : * C99 requires this format. Previously this code used "%D %X", but we now
94 : * conform to C99. Note that "%a %b %d %H:%M:%S %Y" is used by Solaris
95 : * 2.3.
96 : */
97 : "%a %b %e %T %Y",
98 :
99 : /* am */
100 : "AM",
101 :
102 : /* pm */
103 : "PM",
104 :
105 : /* date_fmt */
106 : "%a %b %e %H:%M:%S %Z %Y"
107 : };
108 :
109 : static char *_add(const char *, char *, const char *);
110 : static char *_conv(int, const char *, char *, const char *);
111 : static char *_fmt(const char *, const struct pg_tm *, char *,
112 : const char *, int *);
113 : static char *_yconv(int, int, bool, bool, char *, const char *);
114 :
115 : #define IN_NONE 0
116 : #define IN_SOME 1
117 : #define IN_THIS 2
118 : #define IN_ALL 3
119 :
120 :
121 : size_t
122 8585 : pg_strftime(char *s, size_t maxsize, const char *format,
123 : const struct pg_tm *t)
124 : {
125 : char *p;
126 : int warn;
127 :
128 8585 : warn = IN_NONE;
129 8585 : p = _fmt(format, t, s, s + maxsize, &warn);
130 8585 : if (p == s + maxsize)
131 0 : return 0;
132 8585 : *p = '\0';
133 8585 : return p - s;
134 : }
135 :
136 : static char *
137 8585 : _fmt(const char *format, const struct pg_tm *t, char *pt, const char *ptlim,
138 : int *warnp)
139 : {
140 154526 : for (; *format; ++format)
141 : {
142 145941 : if (*format == '%')
143 : {
144 : label:
145 60095 : switch (*++format)
146 : {
147 : case '\0':
148 0 : --format;
149 0 : break;
150 : case 'A':
151 0 : pt = _add((t->tm_wday < 0 ||
152 0 : t->tm_wday >= DAYSPERWEEK) ?
153 0 : "?" : Locale->weekday[t->tm_wday],
154 : pt, ptlim);
155 0 : continue;
156 : case 'a':
157 0 : pt = _add((t->tm_wday < 0 ||
158 0 : t->tm_wday >= DAYSPERWEEK) ?
159 0 : "?" : Locale->wday[t->tm_wday],
160 : pt, ptlim);
161 0 : continue;
162 : case 'B':
163 0 : pt = _add((t->tm_mon < 0 ||
164 0 : t->tm_mon >= MONSPERYEAR) ?
165 0 : "?" : Locale->month[t->tm_mon],
166 : pt, ptlim);
167 0 : continue;
168 : case 'b':
169 : case 'h':
170 0 : pt = _add((t->tm_mon < 0 ||
171 0 : t->tm_mon >= MONSPERYEAR) ?
172 0 : "?" : Locale->mon[t->tm_mon],
173 : pt, ptlim);
174 0 : continue;
175 : case 'C':
176 :
177 : /*
178 : * %C used to do a... _fmt("%a %b %e %X %Y", t);
179 : * ...whereas now POSIX 1003.2 calls for something
180 : * completely different. (ado, 1993-05-24)
181 : */
182 0 : pt = _yconv(t->tm_year, TM_YEAR_BASE,
183 : true, false, pt, ptlim);
184 0 : continue;
185 : case 'c':
186 : {
187 0 : int warn2 = IN_SOME;
188 :
189 0 : pt = _fmt(Locale->c_fmt, t, pt, ptlim, &warn2);
190 0 : if (warn2 == IN_ALL)
191 0 : warn2 = IN_THIS;
192 0 : if (warn2 > *warnp)
193 0 : *warnp = warn2;
194 : }
195 0 : continue;
196 : case 'D':
197 0 : pt = _fmt("%m/%d/%y", t, pt, ptlim, warnp);
198 0 : continue;
199 : case 'd':
200 8585 : pt = _conv(t->tm_mday, "%02d", pt, ptlim);
201 8585 : continue;
202 : case 'E':
203 : case 'O':
204 :
205 : /*
206 : * C99 locale modifiers. The sequences %Ec %EC %Ex %EX
207 : * %Ey %EY %Od %oe %OH %OI %Om %OM %OS %Ou %OU %OV %Ow
208 : * %OW %Oy are supposed to provide alternate
209 : * representations.
210 : */
211 0 : goto label;
212 : case 'e':
213 0 : pt = _conv(t->tm_mday, "%2d", pt, ptlim);
214 0 : continue;
215 : case 'F':
216 0 : pt = _fmt("%Y-%m-%d", t, pt, ptlim, warnp);
217 0 : continue;
218 : case 'H':
219 8585 : pt = _conv(t->tm_hour, "%02d", pt, ptlim);
220 8585 : continue;
221 : case 'I':
222 0 : pt = _conv((t->tm_hour % 12) ?
223 0 : (t->tm_hour % 12) : 12,
224 : "%02d", pt, ptlim);
225 0 : continue;
226 : case 'j':
227 0 : pt = _conv(t->tm_yday + 1, "%03d", pt, ptlim);
228 0 : continue;
229 : case 'k':
230 :
231 : /*
232 : * This used to be... _conv(t->tm_hour % 12 ? t->tm_hour
233 : * % 12 : 12, 2, ' '); ...and has been changed to the
234 : * below to match SunOS 4.1.1 and Arnold Robbins' strftime
235 : * version 3.0. That is, "%k" and "%l" have been swapped.
236 : * (ado, 1993-05-24)
237 : */
238 0 : pt = _conv(t->tm_hour, "%2d", pt, ptlim);
239 0 : continue;
240 : #ifdef KITCHEN_SINK
241 : case 'K':
242 :
243 : /*
244 : * After all this time, still unclaimed!
245 : */
246 : pt = _add("kitchen sink", pt, ptlim);
247 : continue;
248 : #endif /* defined KITCHEN_SINK */
249 : case 'l':
250 :
251 : /*
252 : * This used to be... _conv(t->tm_hour, 2, ' '); ...and
253 : * has been changed to the below to match SunOS 4.1.1 and
254 : * Arnold Robbin's strftime version 3.0. That is, "%k" and
255 : * "%l" have been swapped. (ado, 1993-05-24)
256 : */
257 0 : pt = _conv((t->tm_hour % 12) ?
258 0 : (t->tm_hour % 12) : 12,
259 : "%2d", pt, ptlim);
260 0 : continue;
261 : case 'M':
262 8585 : pt = _conv(t->tm_min, "%02d", pt, ptlim);
263 8585 : continue;
264 : case 'm':
265 8585 : pt = _conv(t->tm_mon + 1, "%02d", pt, ptlim);
266 8585 : continue;
267 : case 'n':
268 0 : pt = _add("\n", pt, ptlim);
269 0 : continue;
270 : case 'p':
271 0 : pt = _add((t->tm_hour >= (HOURSPERDAY / 2)) ?
272 : Locale->pm :
273 : Locale->am,
274 : pt, ptlim);
275 0 : continue;
276 : case 'R':
277 0 : pt = _fmt("%H:%M", t, pt, ptlim, warnp);
278 0 : continue;
279 : case 'r':
280 0 : pt = _fmt("%I:%M:%S %p", t, pt, ptlim, warnp);
281 0 : continue;
282 : case 'S':
283 8585 : pt = _conv(t->tm_sec, "%02d", pt, ptlim);
284 8585 : continue;
285 : case 'T':
286 0 : pt = _fmt("%H:%M:%S", t, pt, ptlim, warnp);
287 0 : continue;
288 : case 't':
289 0 : pt = _add("\t", pt, ptlim);
290 0 : continue;
291 : case 'U':
292 0 : pt = _conv((t->tm_yday + DAYSPERWEEK -
293 0 : t->tm_wday) / DAYSPERWEEK,
294 : "%02d", pt, ptlim);
295 0 : continue;
296 : case 'u':
297 :
298 : /*
299 : * From Arnold Robbins' strftime version 3.0: "ISO 8601:
300 : * Weekday as a decimal number [1 (Monday) - 7]" (ado,
301 : * 1993-05-24)
302 : */
303 0 : pt = _conv((t->tm_wday == 0) ?
304 : DAYSPERWEEK : t->tm_wday,
305 : "%d", pt, ptlim);
306 0 : continue;
307 : case 'V': /* ISO 8601 week number */
308 : case 'G': /* ISO 8601 year (four digits) */
309 : case 'g': /* ISO 8601 year (two digits) */
310 : /*
311 : * From Arnold Robbins' strftime version 3.0: "the week number of the
312 : * year (the first Monday as the first day of week 1) as a decimal number
313 : * (01-53)."
314 : * (ado, 1993-05-24)
315 : *
316 : * From <http://www.ft.uni-erlangen.de/~mskuhn/iso-time.html> by Markus Kuhn:
317 : * "Week 01 of a year is per definition the first week which has the
318 : * Thursday in this year, which is equivalent to the week which contains
319 : * the fourth day of January. In other words, the first week of a new year
320 : * is the week which has the majority of its days in the new year. Week 01
321 : * might also contain days from the previous year and the week before week
322 : * 01 of a year is the last week (52 or 53) of the previous year even if
323 : * it contains days from the new year. A week starts with Monday (day 1)
324 : * and ends with Sunday (day 7). For example, the first week of the year
325 : * 1997 lasts from 1996-12-30 to 1997-01-05..."
326 : * (ado, 1996-01-02)
327 : */
328 : {
329 : int year;
330 : int base;
331 : int yday;
332 : int wday;
333 : int w;
334 :
335 0 : year = t->tm_year;
336 0 : base = TM_YEAR_BASE;
337 0 : yday = t->tm_yday;
338 0 : wday = t->tm_wday;
339 : for (;;)
340 : {
341 : int len;
342 : int bot;
343 : int top;
344 :
345 0 : len = isleap_sum(year, base) ?
346 0 : DAYSPERLYEAR :
347 : DAYSPERNYEAR;
348 :
349 : /*
350 : * What yday (-3 ... 3) does the ISO year begin
351 : * on?
352 : */
353 0 : bot = ((yday + 11 - wday) %
354 : DAYSPERWEEK) - 3;
355 :
356 : /*
357 : * What yday does the NEXT ISO year begin on?
358 : */
359 0 : top = bot -
360 0 : (len % DAYSPERWEEK);
361 0 : if (top < -3)
362 0 : top += DAYSPERWEEK;
363 0 : top += len;
364 0 : if (yday >= top)
365 : {
366 0 : ++base;
367 0 : w = 1;
368 0 : break;
369 : }
370 0 : if (yday >= bot)
371 : {
372 0 : w = 1 + ((yday - bot) /
373 : DAYSPERWEEK);
374 0 : break;
375 : }
376 0 : --base;
377 0 : yday += isleap_sum(year, base) ?
378 0 : DAYSPERLYEAR :
379 : DAYSPERNYEAR;
380 0 : }
381 0 : if (*format == 'V')
382 0 : pt = _conv(w, "%02d",
383 : pt, ptlim);
384 0 : else if (*format == 'g')
385 : {
386 0 : *warnp = IN_ALL;
387 0 : pt = _yconv(year, base,
388 : false, true,
389 : pt, ptlim);
390 : }
391 : else
392 0 : pt = _yconv(year, base,
393 : true, true,
394 : pt, ptlim);
395 : }
396 0 : continue;
397 : case 'v':
398 :
399 : /*
400 : * From Arnold Robbins' strftime version 3.0: "date as
401 : * dd-bbb-YYYY" (ado, 1993-05-24)
402 : */
403 0 : pt = _fmt("%e-%b-%Y", t, pt, ptlim, warnp);
404 0 : continue;
405 : case 'W':
406 0 : pt = _conv((t->tm_yday + DAYSPERWEEK -
407 0 : (t->tm_wday ?
408 0 : (t->tm_wday - 1) :
409 : (DAYSPERWEEK - 1))) / DAYSPERWEEK,
410 : "%02d", pt, ptlim);
411 0 : continue;
412 : case 'w':
413 0 : pt = _conv(t->tm_wday, "%d", pt, ptlim);
414 0 : continue;
415 : case 'X':
416 0 : pt = _fmt(Locale->X_fmt, t, pt, ptlim, warnp);
417 0 : continue;
418 : case 'x':
419 : {
420 0 : int warn2 = IN_SOME;
421 :
422 0 : pt = _fmt(Locale->x_fmt, t, pt, ptlim, &warn2);
423 0 : if (warn2 == IN_ALL)
424 0 : warn2 = IN_THIS;
425 0 : if (warn2 > *warnp)
426 0 : *warnp = warn2;
427 : }
428 0 : continue;
429 : case 'y':
430 0 : *warnp = IN_ALL;
431 0 : pt = _yconv(t->tm_year, TM_YEAR_BASE,
432 : false, true,
433 : pt, ptlim);
434 0 : continue;
435 : case 'Y':
436 8585 : pt = _yconv(t->tm_year, TM_YEAR_BASE,
437 : true, true,
438 : pt, ptlim);
439 8585 : continue;
440 : case 'Z':
441 8585 : if (t->tm_zone != NULL)
442 8585 : pt = _add(t->tm_zone, pt, ptlim);
443 :
444 : /*
445 : * C99 says that %Z must be replaced by the empty string
446 : * if the time zone is not determinable.
447 : */
448 8585 : continue;
449 : case 'z':
450 : {
451 : long diff;
452 : char const *sign;
453 : bool negative;
454 :
455 0 : if (t->tm_isdst < 0)
456 0 : continue;
457 0 : diff = t->tm_gmtoff;
458 0 : negative = diff < 0;
459 0 : if (diff == 0)
460 : {
461 0 : if (t->tm_zone != NULL)
462 0 : negative = t->tm_zone[0] == '-';
463 : }
464 0 : if (negative)
465 : {
466 0 : sign = "-";
467 0 : diff = -diff;
468 : }
469 : else
470 0 : sign = "+";
471 0 : pt = _add(sign, pt, ptlim);
472 0 : diff /= SECSPERMIN;
473 0 : diff = (diff / MINSPERHOUR) * 100 +
474 0 : (diff % MINSPERHOUR);
475 0 : pt = _conv(diff, "%04d", pt, ptlim);
476 : }
477 0 : continue;
478 : case '+':
479 0 : pt = _fmt(Locale->date_fmt, t, pt, ptlim,
480 : warnp);
481 0 : continue;
482 : case '%':
483 :
484 : /*
485 : * X311J/88-090 (4.12.3.5): if conversion char is
486 : * undefined, behavior is undefined. Print out the
487 : * character itself as printf(3) also does.
488 : */
489 : default:
490 0 : break;
491 : }
492 : }
493 85846 : if (pt == ptlim)
494 0 : break;
495 85846 : *pt++ = *format;
496 : }
497 8585 : return pt;
498 : }
499 :
500 : static char *
501 60095 : _conv(int n, const char *format, char *pt, const char *ptlim)
502 : {
503 : char buf[INT_STRLEN_MAXIMUM(int) +1];
504 :
505 60095 : sprintf(buf, format, n);
506 60095 : return _add(buf, pt, ptlim);
507 : }
508 :
509 : static char *
510 68680 : _add(const char *str, char *pt, const char *ptlim)
511 : {
512 283305 : while (pt < ptlim && (*pt = *str++) != '\0')
513 145945 : ++pt;
514 68680 : return pt;
515 : }
516 :
517 : /*
518 : * POSIX and the C Standard are unclear or inconsistent about
519 : * what %C and %y do if the year is negative or exceeds 9999.
520 : * Use the convention that %C concatenated with %y yields the
521 : * same output as %Y, and that %Y contains at least 4 bytes,
522 : * with more only if necessary.
523 : */
524 : static char *
525 8585 : _yconv(int a, int b, bool convert_top, bool convert_yy,
526 : char *pt, const char *ptlim)
527 : {
528 : int lead;
529 : int trail;
530 :
531 : #define DIVISOR 100
532 8585 : trail = a % DIVISOR + b % DIVISOR;
533 8585 : lead = a / DIVISOR + b / DIVISOR + trail / DIVISOR;
534 8585 : trail %= DIVISOR;
535 8585 : if (trail < 0 && lead > 0)
536 : {
537 0 : trail += DIVISOR;
538 0 : --lead;
539 : }
540 8585 : else if (lead < 0 && trail > 0)
541 : {
542 0 : trail -= DIVISOR;
543 0 : ++lead;
544 : }
545 8585 : if (convert_top)
546 : {
547 8585 : if (lead == 0 && trail < 0)
548 0 : pt = _add("-0", pt, ptlim);
549 : else
550 8585 : pt = _conv(lead, "%02d", pt, ptlim);
551 : }
552 8585 : if (convert_yy)
553 8585 : pt = _conv(((trail < 0) ? -trail : trail), "%02d", pt, ptlim);
554 8585 : return pt;
555 : }
|