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.5 2004-08-29 05:07:02 momjian Exp $
33
const char *mon[MONSPERYEAR];
34
const char *month[MONSPERYEAR];
35
const char *wday[DAYSPERWEEK];
36
const char *weekday[DAYSPERWEEK];
45
#define Locale (&C_time_locale)
47
static const struct lc_time_T C_time_locale = {
49
"Jan", "Feb", "Mar", "Apr", "May", "Jun",
50
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
52
"January", "February", "March", "April", "May", "June",
53
"July", "August", "September", "October", "November", "December"
55
"Sun", "Mon", "Tue", "Wed",
58
"Sunday", "Monday", "Tuesday", "Wednesday",
59
"Thursday", "Friday", "Saturday"
68
* C99 requires this format. Using just numbers (as here) makes Quakers
69
* happier; it's also compatible with SVR4.
76
* C99 requires this format. Previously this code used "%D %X", but we
77
* now conform to C99. Note that "%a %b %d %H:%M:%S %Y" is used by
89
"%a %b %e %H:%M:%S %Z %Y"
92
static char *_add(const char *, char *, const char *);
93
static char *_conv(int, const char *, char *, const char *);
94
static char *_fmt(const char *, const struct pg_tm *, char *,
104
pg_strftime(char *s, size_t maxsize, const char *format,
105
const struct pg_tm * t)
111
p = _fmt(((format == NULL) ? "%c" : format), t, s, s + maxsize, &warn);
112
if (p == s + maxsize)
119
_fmt(const char *format, const struct pg_tm * t, char *pt, const char *ptlim,
122
for (; *format; ++format)
133
pt = _add((t->tm_wday < 0 ||
134
t->tm_wday >= DAYSPERWEEK) ?
135
"?" : Locale->weekday[t->tm_wday],
139
pt = _add((t->tm_wday < 0 ||
140
t->tm_wday >= DAYSPERWEEK) ?
141
"?" : Locale->wday[t->tm_wday],
145
pt = _add((t->tm_mon < 0 ||
146
t->tm_mon >= MONSPERYEAR) ?
147
"?" : Locale->month[t->tm_mon],
152
pt = _add((t->tm_mon < 0 ||
153
t->tm_mon >= MONSPERYEAR) ?
154
"?" : Locale->mon[t->tm_mon],
160
* %C used to do a... _fmt("%a %b %e %X %Y", t);
161
* ...whereas now POSIX 1003.2 calls for something
162
* completely different. (ado, 1993-05-24)
164
pt = _conv((t->tm_year + TM_YEAR_BASE) / 100,
171
pt = _fmt(Locale->c_fmt, t, pt, ptlim, warnp);
179
pt = _fmt("%m/%d/%y", t, pt, ptlim, warnp);
182
pt = _conv(t->tm_mday, "%02d", pt, ptlim);
188
* C99 locale modifiers. The sequences %Ec %EC %Ex
189
* %EX %Ey %EY %Od %oe %OH %OI %Om %OM %OS %Ou %OU
190
* %OV %Ow %OW %Oy are supposed to provide alternate
195
pt = _conv(t->tm_mday, "%2d", pt, ptlim);
198
pt = _fmt("%Y-%m-%d", t, pt, ptlim, warnp);
201
pt = _conv(t->tm_hour, "%02d", pt, ptlim);
204
pt = _conv((t->tm_hour % 12) ?
205
(t->tm_hour % 12) : 12,
209
pt = _conv(t->tm_yday + 1, "%03d", pt, ptlim);
214
* This used to be... _conv(t->tm_hour % 12 ?
215
* t->tm_hour % 12 : 12, 2, ' '); ...and has been
216
* changed to the below to match SunOS 4.1.1 and
217
* Arnold Robbins' strftime version 3.0. That is,
218
* "%k" and "%l" have been swapped. (ado, 1993-05-24)
220
pt = _conv(t->tm_hour, "%2d", pt, ptlim);
226
* * After all this time, still unclaimed!
228
pt = _add("kitchen sink", pt, ptlim);
230
#endif /* defined KITCHEN_SINK */
234
* This used to be... _conv(t->tm_hour, 2, ' ');
235
* ...and has been changed to the below to match SunOS
236
* 4.1.1 and Arnold Robbin's strftime version 3.0.
237
* That is, "%k" and "%l" have been swapped. (ado,
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
283
* 8601: Weekday as a decimal number [1 (Monday) - 7]"
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..."
317
year = t->tm_year + TM_YEAR_BASE;
331
* What yday (-3 ... 3) does the ISO year
334
bot = ((yday + 11 - wday) %
338
* What yday does the NEXT ISO year begin on?
353
w = 1 + ((yday - bot) /
358
yday += isleap(year) ?
363
pt = _conv(w, "%02d",
365
else if (*format == 'g')
368
pt = _conv(year % 100, "%02d",
372
pt = _conv(year, "%04d",
379
* From Arnold Robbins' strftime version 3.0: "date as
380
* dd-bbb-YYYY" (ado, 1993-05-24)
382
pt = _fmt("%e-%b-%Y", t, pt, ptlim, warnp);
385
pt = _conv((t->tm_yday + DAYSPERWEEK -
388
(DAYSPERWEEK - 1))) / DAYSPERWEEK,
392
pt = _conv(t->tm_wday, "%d", pt, ptlim);
395
pt = _fmt(Locale->X_fmt, t, pt, ptlim, warnp);
401
pt = _fmt(Locale->x_fmt, t, pt, ptlim, &warn2);
410
pt = _conv((t->tm_year + TM_YEAR_BASE) % 100,
414
pt = _conv(t->tm_year + TM_YEAR_BASE, "%04d",
418
if (t->tm_zone != NULL)
419
pt = _add(t->tm_zone, pt, ptlim);
422
* C99 says that %Z must be replaced by the empty
423
* string if the time zone is not determinable.
441
pt = _add(sign, pt, ptlim);
443
pt = _conv((diff / 60) * 100 + diff % 60,
448
pt = _fmt(Locale->date_fmt, t, pt, ptlim,
454
* X311J/88-090 (4.12.3.5): if conversion char is
455
* undefined, behavior is undefined. Print out the
456
* character itself as printf(3) also does.
470
_conv(const int n, const char *format, char *pt, const char *ptlim)
472
char buf[INT_STRLEN_MAXIMUM(int) +1];
474
(void) sprintf(buf, format, n);
475
return _add(buf, pt, ptlim);
479
_add(const char *str, char *pt, const char *ptlim)
481
while (pt < ptlim && (*pt = *str++) != '\0')