2 * Copyright (c) 1989 The Regents of the University of California.
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.
18 * $PostgreSQL: pgsql/src/timezone/strftime.c,v 1.14 2009/06/11 14:49:15 momjian Exp $
32 const char *mon[MONSPERYEAR];
33 const char *month[MONSPERYEAR];
34 const char *wday[DAYSPERWEEK];
35 const char *weekday[DAYSPERWEEK];
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"
51 "January", "February", "March", "April", "May", "June",
52 "July", "August", "September", "October", "November", "December"
54 "Sun", "Mon", "Tue", "Wed",
57 "Sunday", "Monday", "Tuesday", "Wednesday",
58 "Thursday", "Friday", "Saturday"
67 * C99 requires this format. Using just numbers (as here) makes Quakers
68 * happier; it's also compatible with SVR4.
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
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 *,
95 static char *_yconv(const int, const int, const int, const int,
96 char *, const char *const);
105 pg_strftime(char *s, size_t maxsize, const char *format,
106 const struct pg_tm * t)
112 p = _fmt(((format == NULL) ? "%c" : format), t, s, s + maxsize, &warn);
113 if (p == s + maxsize)
120 _fmt(const char *format, const struct pg_tm * t, char *pt, const char *ptlim,
123 for (; *format; ++format)
134 pt = _add((t->tm_wday < 0 ||
135 t->tm_wday >= DAYSPERWEEK) ?
136 "?" : Locale->weekday[t->tm_wday],
140 pt = _add((t->tm_wday < 0 ||
141 t->tm_wday >= DAYSPERWEEK) ?
142 "?" : Locale->wday[t->tm_wday],
146 pt = _add((t->tm_mon < 0 ||
147 t->tm_mon >= MONSPERYEAR) ?
148 "?" : Locale->month[t->tm_mon],
153 pt = _add((t->tm_mon < 0 ||
154 t->tm_mon >= MONSPERYEAR) ?
155 "?" : Locale->mon[t->tm_mon],
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,
172 pt = _fmt(Locale->c_fmt, t, pt, ptlim, warnp);
180 pt = _fmt("%m/%d/%y", t, pt, ptlim, warnp);
183 pt = _conv(t->tm_mday, "%02d", pt, ptlim);
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
196 pt = _conv(t->tm_mday, "%2d", pt, ptlim);
199 pt = _fmt("%Y-%m-%d", t, pt, ptlim, warnp);
202 pt = _conv(t->tm_hour, "%02d", pt, ptlim);
205 pt = _conv((t->tm_hour % 12) ?
206 (t->tm_hour % 12) : 12,
210 pt = _conv(t->tm_yday + 1, "%03d", pt, ptlim);
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.
221 pt = _conv(t->tm_hour, "%2d", pt, ptlim);
227 * * After all this time, still unclaimed!
229 pt = _add("kitchen sink", pt, ptlim);
231 #endif /* defined KITCHEN_SINK */
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,
245 pt = _conv(t->tm_min, "%02d", pt, ptlim);
248 pt = _conv(t->tm_mon + 1, "%02d", pt, ptlim);
251 pt = _add("\n", pt, ptlim);
254 pt = _add((t->tm_hour >= (HOURSPERDAY / 2)) ?
260 pt = _fmt("%H:%M", t, pt, ptlim, warnp);
263 pt = _fmt("%I:%M:%S %p", t, pt, ptlim, warnp);
266 pt = _conv(t->tm_sec, "%02d", pt, ptlim);
269 pt = _fmt("%H:%M:%S", t, pt, ptlim, warnp);
272 pt = _add("\t", pt, ptlim);
275 pt = _conv((t->tm_yday + DAYSPERWEEK -
276 t->tm_wday) / DAYSPERWEEK,
282 * From Arnold Robbins' strftime version 3.0: "ISO 8601:
283 * Weekday as a decimal number [1 (Monday) - 7]" (ado,
286 pt = _conv((t->tm_wday == 0) ?
287 DAYSPERWEEK : t->tm_wday,
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
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..."
328 len = isleap_sum(year, base) ?
333 * What yday (-3 ... 3) does the ISO year begin
336 bot = ((yday + 11 - wday) %
340 * What yday does the NEXT ISO year begin on?
355 w = 1 + ((yday - bot) /
360 yday += isleap_sum(year, base) ?
365 pt = _conv(w, "%02d",
367 else if (*format == 'g')
370 pt = _yconv(year, base, 0, 1,
374 pt = _yconv(year, base, 1, 1,
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);
387 pt = _conv((t->tm_yday + DAYSPERWEEK -
390 (DAYSPERWEEK - 1))) / DAYSPERWEEK,
394 pt = _conv(t->tm_wday, "%d", pt, ptlim);
397 pt = _fmt(Locale->X_fmt, t, pt, ptlim, warnp);
403 pt = _fmt(Locale->x_fmt, t, pt, ptlim, &warn2);
412 pt = _yconv(t->tm_year, TM_YEAR_BASE, 0, 1,
416 pt = _yconv(t->tm_year, TM_YEAR_BASE, 1, 1,
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.
443 pt = _add(sign, pt, ptlim);
445 pt = _conv((diff / 60) * 100 + diff % 60,
450 pt = _fmt(Locale->date_fmt, t, pt, ptlim,
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.
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);
481 _add(const char *str, char *pt, const char *ptlim)
483 while (pt < ptlim && (*pt = *str++) != '\0')
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.
496 _yconv(const int a, const int b, const int convert_top,
497 const int convert_yy, char *pt, const char *const ptlim)
503 trail = a % DIVISOR + b % DIVISOR;
504 lead = a / DIVISOR + b / DIVISOR + trail / DIVISOR;
506 if (trail < 0 && lead > 0)
511 else if (lead < 0 && trail > 0)
518 if (lead == 0 && trail < 0)
519 pt = _add("-0", pt, ptlim);
521 pt = _conv(lead, "%02d", pt, ptlim);
524 pt = _conv(((trail < 0) ? -trail : trail), "%02d", pt, ptlim);