Fix pg_dump bug in the database-level collation patch. "datcollate" and
[PostgreSQL.git] / src / timezone / strftime.c
blob1a18136ea26242e72cd46af1631f69c19bdfba5b
1 /*
2 * Copyright (c) 1989 The Regents of the University of California.
3 * All rights reserved.
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.
17 * IDENTIFICATION
18 * $PostgreSQL$
21 #include "postgres.h"
23 #include <fcntl.h>
24 #include <locale.h>
26 #include "private.h"
27 #include "tzfile.h"
30 struct lc_time_T
32 const char *mon[MONSPERYEAR];
33 const char *month[MONSPERYEAR];
34 const char *wday[DAYSPERWEEK];
35 const char *weekday[DAYSPERWEEK];
36 const char *X_fmt;
37 const char *x_fmt;
38 const char *c_fmt;
39 const char *am;
40 const char *pm;
41 const char *date_fmt;
44 #define Locale (&C_time_locale)
46 static const struct lc_time_T C_time_locale = {
48 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
49 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
50 }, {
51 "January", "February", "March", "April", "May", "June",
52 "July", "August", "September", "October", "November", "December"
53 }, {
54 "Sun", "Mon", "Tue", "Wed",
55 "Thu", "Fri", "Sat"
56 }, {
57 "Sunday", "Monday", "Tuesday", "Wednesday",
58 "Thursday", "Friday", "Saturday"
61 /* X_fmt */
62 "%H:%M:%S",
65 * x_fmt
67 * C99 requires this format. Using just numbers (as here) makes Quakers
68 * happier; it's also compatible with SVR4.
70 "%m/%d/%y",
73 * c_fmt
75 * C99 requires this format. Previously this code used "%D %X", but we now
76 * conform to C99. Note that "%a %b %d %H:%M:%S %Y" is used by Solaris
77 * 2.3.
79 "%a %b %e %T %Y",
81 /* am */
82 "AM",
84 /* pm */
85 "PM",
87 /* date_fmt */
88 "%a %b %e %H:%M:%S %Z %Y"
91 static char *_add(const char *, char *, const char *);
92 static char *_conv(int, const char *, char *, const char *);
93 static char *_fmt(const char *, const struct pg_tm *, char *,
94 const char *, int *);
95 static char * _yconv(const int, const int, const int, const int,
96 char *, const char * const);
98 #define IN_NONE 0
99 #define IN_SOME 1
100 #define IN_THIS 2
101 #define IN_ALL 3
104 size_t
105 pg_strftime(char *s, size_t maxsize, const char *format,
106 const struct pg_tm * t)
108 char *p;
109 int warn;
111 warn = IN_NONE;
112 p = _fmt(((format == NULL) ? "%c" : format), t, s, s + maxsize, &warn);
113 if (p == s + maxsize)
114 return 0;
115 *p = '\0';
116 return p - s;
119 static char *
120 _fmt(const char *format, const struct pg_tm * t, char *pt, const char *ptlim,
121 int *warnp)
123 for (; *format; ++format)
125 if (*format == '%')
127 label:
128 switch (*++format)
130 case '\0':
131 --format;
132 break;
133 case 'A':
134 pt = _add((t->tm_wday < 0 ||
135 t->tm_wday >= DAYSPERWEEK) ?
136 "?" : Locale->weekday[t->tm_wday],
137 pt, ptlim);
138 continue;
139 case 'a':
140 pt = _add((t->tm_wday < 0 ||
141 t->tm_wday >= DAYSPERWEEK) ?
142 "?" : Locale->wday[t->tm_wday],
143 pt, ptlim);
144 continue;
145 case 'B':
146 pt = _add((t->tm_mon < 0 ||
147 t->tm_mon >= MONSPERYEAR) ?
148 "?" : Locale->month[t->tm_mon],
149 pt, ptlim);
150 continue;
151 case 'b':
152 case 'h':
153 pt = _add((t->tm_mon < 0 ||
154 t->tm_mon >= MONSPERYEAR) ?
155 "?" : Locale->mon[t->tm_mon],
156 pt, ptlim);
157 continue;
158 case 'C':
161 * %C used to do a... _fmt("%a %b %e %X %Y", t);
162 * ...whereas now POSIX 1003.2 calls for something
163 * completely different. (ado, 1993-05-24)
165 pt = _yconv(t->tm_year, TM_YEAR_BASE, 1, 0,
166 pt, ptlim);
167 continue;
168 case 'c':
170 int warn2 = IN_SOME;
172 pt = _fmt(Locale->c_fmt, t, pt, ptlim, warnp);
173 if (warn2 == IN_ALL)
174 warn2 = IN_THIS;
175 if (warn2 > *warnp)
176 *warnp = warn2;
178 continue;
179 case 'D':
180 pt = _fmt("%m/%d/%y", t, pt, ptlim, warnp);
181 continue;
182 case 'd':
183 pt = _conv(t->tm_mday, "%02d", pt, ptlim);
184 continue;
185 case 'E':
186 case 'O':
189 * C99 locale modifiers. The sequences %Ec %EC %Ex %EX
190 * %Ey %EY %Od %oe %OH %OI %Om %OM %OS %Ou %OU %OV %Ow
191 * %OW %Oy are supposed to provide alternate
192 * representations.
194 goto label;
195 case 'e':
196 pt = _conv(t->tm_mday, "%2d", pt, ptlim);
197 continue;
198 case 'F':
199 pt = _fmt("%Y-%m-%d", t, pt, ptlim, warnp);
200 continue;
201 case 'H':
202 pt = _conv(t->tm_hour, "%02d", pt, ptlim);
203 continue;
204 case 'I':
205 pt = _conv((t->tm_hour % 12) ?
206 (t->tm_hour % 12) : 12,
207 "%02d", pt, ptlim);
208 continue;
209 case 'j':
210 pt = _conv(t->tm_yday + 1, "%03d", pt, ptlim);
211 continue;
212 case 'k':
215 * This used to be... _conv(t->tm_hour % 12 ? t->tm_hour
216 * % 12 : 12, 2, ' '); ...and has been changed to the
217 * below to match SunOS 4.1.1 and Arnold Robbins' strftime
218 * version 3.0. That is, "%k" and "%l" have been swapped.
219 * (ado, 1993-05-24)
221 pt = _conv(t->tm_hour, "%2d", pt, ptlim);
222 continue;
223 #ifdef KITCHEN_SINK
224 case 'K':
227 * * After all this time, still unclaimed!
229 pt = _add("kitchen sink", pt, ptlim);
230 continue;
231 #endif /* defined KITCHEN_SINK */
232 case 'l':
235 * This used to be... _conv(t->tm_hour, 2, ' '); ...and
236 * has been changed to the below to match SunOS 4.1.1 and
237 * Arnold Robbin's strftime version 3.0. That is, "%k" and
238 * "%l" have been swapped. (ado, 1993-05-24)
240 pt = _conv((t->tm_hour % 12) ?
241 (t->tm_hour % 12) : 12,
242 "%2d", pt, ptlim);
243 continue;
244 case 'M':
245 pt = _conv(t->tm_min, "%02d", pt, ptlim);
246 continue;
247 case 'm':
248 pt = _conv(t->tm_mon + 1, "%02d", pt, ptlim);
249 continue;
250 case 'n':
251 pt = _add("\n", pt, ptlim);
252 continue;
253 case 'p':
254 pt = _add((t->tm_hour >= (HOURSPERDAY / 2)) ?
255 Locale->pm :
256 Locale->am,
257 pt, ptlim);
258 continue;
259 case 'R':
260 pt = _fmt("%H:%M", t, pt, ptlim, warnp);
261 continue;
262 case 'r':
263 pt = _fmt("%I:%M:%S %p", t, pt, ptlim, warnp);
264 continue;
265 case 'S':
266 pt = _conv(t->tm_sec, "%02d", pt, ptlim);
267 continue;
268 case 'T':
269 pt = _fmt("%H:%M:%S", t, pt, ptlim, warnp);
270 continue;
271 case 't':
272 pt = _add("\t", pt, ptlim);
273 continue;
274 case 'U':
275 pt = _conv((t->tm_yday + DAYSPERWEEK -
276 t->tm_wday) / DAYSPERWEEK,
277 "%02d", pt, ptlim);
278 continue;
279 case 'u':
282 * From Arnold Robbins' strftime version 3.0: "ISO 8601:
283 * Weekday as a decimal number [1 (Monday) - 7]" (ado,
284 * 1993-05-24)
286 pt = _conv((t->tm_wday == 0) ?
287 DAYSPERWEEK : t->tm_wday,
288 "%d", pt, ptlim);
289 continue;
290 case 'V': /* ISO 8601 week number */
291 case 'G': /* ISO 8601 year (four digits) */
292 case 'g': /* ISO 8601 year (two digits) */
294 * From Arnold Robbins' strftime version 3.0: "the week number of the
295 * year (the first Monday as the first day of week 1) as a decimal number
296 * (01-53)."
297 * (ado, 1993-05-24)
299 * From "http://www.ft.uni-erlangen.de/~mskuhn/iso-time.html" by Markus Kuhn:
300 * "Week 01 of a year is per definition the first week which has the
301 * Thursday in this year, which is equivalent to the week which contains
302 * the fourth day of January. In other words, the first week of a new year
303 * is the week which has the majority of its days in the new year. Week 01
304 * might also contain days from the previous year and the week before week
305 * 01 of a year is the last week (52 or 53) of the previous year even if
306 * it contains days from the new year. A week starts with Monday (day 1)
307 * and ends with Sunday (day 7). For example, the first week of the year
308 * 1997 lasts from 1996-12-30 to 1997-01-05..."
309 * (ado, 1996-01-02)
312 int year;
313 int base;
314 int yday;
315 int wday;
316 int w;
318 year = t->tm_year;
319 base = TM_YEAR_BASE;
320 yday = t->tm_yday;
321 wday = t->tm_wday;
322 for (;;)
324 int len;
325 int bot;
326 int top;
328 len = isleap_sum(year, base) ?
329 DAYSPERLYEAR :
330 DAYSPERNYEAR;
333 * What yday (-3 ... 3) does the ISO year begin
334 * on?
336 bot = ((yday + 11 - wday) %
337 DAYSPERWEEK) - 3;
340 * What yday does the NEXT ISO year begin on?
342 top = bot -
343 (len % DAYSPERWEEK);
344 if (top < -3)
345 top += DAYSPERWEEK;
346 top += len;
347 if (yday >= top)
349 ++base;
350 w = 1;
351 break;
353 if (yday >= bot)
355 w = 1 + ((yday - bot) /
356 DAYSPERWEEK);
357 break;
359 --base;
360 yday += isleap_sum(year, base) ?
361 DAYSPERLYEAR :
362 DAYSPERNYEAR;
364 if (*format == 'V')
365 pt = _conv(w, "%02d",
366 pt, ptlim);
367 else if (*format == 'g')
369 *warnp = IN_ALL;
370 pt = _yconv(year, base, 0, 1,
371 pt, ptlim);
373 else
374 pt = _yconv(year, base, 1, 1,
375 pt, ptlim);
377 continue;
378 case 'v':
381 * From Arnold Robbins' strftime version 3.0: "date as
382 * dd-bbb-YYYY" (ado, 1993-05-24)
384 pt = _fmt("%e-%b-%Y", t, pt, ptlim, warnp);
385 continue;
386 case 'W':
387 pt = _conv((t->tm_yday + DAYSPERWEEK -
388 (t->tm_wday ?
389 (t->tm_wday - 1) :
390 (DAYSPERWEEK - 1))) / DAYSPERWEEK,
391 "%02d", pt, ptlim);
392 continue;
393 case 'w':
394 pt = _conv(t->tm_wday, "%d", pt, ptlim);
395 continue;
396 case 'X':
397 pt = _fmt(Locale->X_fmt, t, pt, ptlim, warnp);
398 continue;
399 case 'x':
401 int warn2 = IN_SOME;
403 pt = _fmt(Locale->x_fmt, t, pt, ptlim, &warn2);
404 if (warn2 == IN_ALL)
405 warn2 = IN_THIS;
406 if (warn2 > *warnp)
407 *warnp = warn2;
409 continue;
410 case 'y':
411 *warnp = IN_ALL;
412 pt = _yconv(t->tm_year, TM_YEAR_BASE, 0, 1,
413 pt, ptlim);
414 continue;
415 case 'Y':
416 pt = _yconv(t->tm_year, TM_YEAR_BASE, 1, 1,
417 pt, ptlim);
418 continue;
419 case 'Z':
420 if (t->tm_zone != NULL)
421 pt = _add(t->tm_zone, pt, ptlim);
424 * C99 says that %Z must be replaced by the empty string
425 * if the time zone is not determinable.
427 continue;
428 case 'z':
430 int diff;
431 char const *sign;
433 if (t->tm_isdst < 0)
434 continue;
435 diff = t->tm_gmtoff;
436 if (diff < 0)
438 sign = "-";
439 diff = -diff;
441 else
442 sign = "+";
443 pt = _add(sign, pt, ptlim);
444 diff /= 60;
445 pt = _conv((diff / 60) * 100 + diff % 60,
446 "%04d", pt, ptlim);
448 continue;
449 case '+':
450 pt = _fmt(Locale->date_fmt, t, pt, ptlim,
451 warnp);
452 continue;
453 case '%':
456 * X311J/88-090 (4.12.3.5): if conversion char is
457 * undefined, behavior is undefined. Print out the
458 * character itself as printf(3) also does.
460 default:
461 break;
464 if (pt == ptlim)
465 break;
466 *pt++ = *format;
468 return pt;
471 static char *
472 _conv(int n, const char *format, char *pt, const char *ptlim)
474 char buf[INT_STRLEN_MAXIMUM(int) +1];
476 (void) sprintf(buf, format, n);
477 return _add(buf, pt, ptlim);
480 static char *
481 _add(const char *str, char *pt, const char *ptlim)
483 while (pt < ptlim && (*pt = *str++) != '\0')
484 ++pt;
485 return pt;
489 * POSIX and the C Standard are unclear or inconsistent about
490 * what %C and %y do if the year is negative or exceeds 9999.
491 * Use the convention that %C concatenated with %y yields the
492 * same output as %Y, and that %Y contains at least 4 bytes,
493 * with more only if necessary.
495 static char *
496 _yconv(const int a, const int b, const int convert_top,
497 const int convert_yy, char *pt, const char * const ptlim)
499 int lead;
500 int trail;
502 #define DIVISOR 100
503 trail = a % DIVISOR + b % DIVISOR;
504 lead = a / DIVISOR + b / DIVISOR + trail / DIVISOR;
505 trail %= DIVISOR;
506 if (trail < 0 && lead > 0)
508 trail += DIVISOR;
509 --lead;
511 else if (lead < 0 && trail > 0)
513 trail -= DIVISOR;
514 ++lead;
516 if (convert_top)
518 if (lead == 0 && trail < 0)
519 pt = _add("-0", pt, ptlim);
520 else pt = _conv(lead, "%02d", pt, ptlim);
522 if (convert_yy)
523 pt = _conv(((trail < 0) ? -trail : trail), "%02d", pt, ptlim);
524 return pt;