PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
strftime.c
Go to the documentation of this file.
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 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  warn = IN_NONE;
129  p = _fmt(format, t, s, s + maxsize, &warn);
130  if (p == s + maxsize)
131  return 0;
132  *p = '\0';
133  return p - s;
134 }
135 
136 static char *
137 _fmt(const char *format, const struct pg_tm *t, char *pt, const char *ptlim,
138  int *warnp)
139 {
140  for (; *format; ++format)
141  {
142  if (*format == '%')
143  {
144  label:
145  switch (*++format)
146  {
147  case '\0':
148  --format;
149  break;
150  case 'A':
151  pt = _add((t->tm_wday < 0 ||
152  t->tm_wday >= DAYSPERWEEK) ?
153  "?" : Locale->weekday[t->tm_wday],
154  pt, ptlim);
155  continue;
156  case 'a':
157  pt = _add((t->tm_wday < 0 ||
158  t->tm_wday >= DAYSPERWEEK) ?
159  "?" : Locale->wday[t->tm_wday],
160  pt, ptlim);
161  continue;
162  case 'B':
163  pt = _add((t->tm_mon < 0 ||
164  t->tm_mon >= MONSPERYEAR) ?
165  "?" : Locale->month[t->tm_mon],
166  pt, ptlim);
167  continue;
168  case 'b':
169  case 'h':
170  pt = _add((t->tm_mon < 0 ||
171  t->tm_mon >= MONSPERYEAR) ?
172  "?" : Locale->mon[t->tm_mon],
173  pt, ptlim);
174  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  pt = _yconv(t->tm_year, TM_YEAR_BASE,
183  true, false, pt, ptlim);
184  continue;
185  case 'c':
186  {
187  int warn2 = IN_SOME;
188 
189  pt = _fmt(Locale->c_fmt, t, pt, ptlim, &warn2);
190  if (warn2 == IN_ALL)
191  warn2 = IN_THIS;
192  if (warn2 > *warnp)
193  *warnp = warn2;
194  }
195  continue;
196  case 'D':
197  pt = _fmt("%m/%d/%y", t, pt, ptlim, warnp);
198  continue;
199  case 'd':
200  pt = _conv(t->tm_mday, "%02d", pt, ptlim);
201  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  goto label;
212  case 'e':
213  pt = _conv(t->tm_mday, "%2d", pt, ptlim);
214  continue;
215  case 'F':
216  pt = _fmt("%Y-%m-%d", t, pt, ptlim, warnp);
217  continue;
218  case 'H':
219  pt = _conv(t->tm_hour, "%02d", pt, ptlim);
220  continue;
221  case 'I':
222  pt = _conv((t->tm_hour % 12) ?
223  (t->tm_hour % 12) : 12,
224  "%02d", pt, ptlim);
225  continue;
226  case 'j':
227  pt = _conv(t->tm_yday + 1, "%03d", pt, ptlim);
228  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  pt = _conv(t->tm_hour, "%2d", pt, ptlim);
239  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  pt = _conv((t->tm_hour % 12) ?
258  (t->tm_hour % 12) : 12,
259  "%2d", pt, ptlim);
260  continue;
261  case 'M':
262  pt = _conv(t->tm_min, "%02d", pt, ptlim);
263  continue;
264  case 'm':
265  pt = _conv(t->tm_mon + 1, "%02d", pt, ptlim);
266  continue;
267  case 'n':
268  pt = _add("\n", pt, ptlim);
269  continue;
270  case 'p':
271  pt = _add((t->tm_hour >= (HOURSPERDAY / 2)) ?
272  Locale->pm :
273  Locale->am,
274  pt, ptlim);
275  continue;
276  case 'R':
277  pt = _fmt("%H:%M", t, pt, ptlim, warnp);
278  continue;
279  case 'r':
280  pt = _fmt("%I:%M:%S %p", t, pt, ptlim, warnp);
281  continue;
282  case 'S':
283  pt = _conv(t->tm_sec, "%02d", pt, ptlim);
284  continue;
285  case 'T':
286  pt = _fmt("%H:%M:%S", t, pt, ptlim, warnp);
287  continue;
288  case 't':
289  pt = _add("\t", pt, ptlim);
290  continue;
291  case 'U':
292  pt = _conv((t->tm_yday + DAYSPERWEEK -
293  t->tm_wday) / DAYSPERWEEK,
294  "%02d", pt, ptlim);
295  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  pt = _conv((t->tm_wday == 0) ?
304  DAYSPERWEEK : t->tm_wday,
305  "%d", pt, ptlim);
306  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  year = t->tm_year;
336  base = TM_YEAR_BASE;
337  yday = t->tm_yday;
338  wday = t->tm_wday;
339  for (;;)
340  {
341  int len;
342  int bot;
343  int top;
344 
345  len = isleap_sum(year, base) ?
346  DAYSPERLYEAR :
347  DAYSPERNYEAR;
348 
349  /*
350  * What yday (-3 ... 3) does the ISO year begin
351  * on?
352  */
353  bot = ((yday + 11 - wday) %
354  DAYSPERWEEK) - 3;
355 
356  /*
357  * What yday does the NEXT ISO year begin on?
358  */
359  top = bot -
360  (len % DAYSPERWEEK);
361  if (top < -3)
362  top += DAYSPERWEEK;
363  top += len;
364  if (yday >= top)
365  {
366  ++base;
367  w = 1;
368  break;
369  }
370  if (yday >= bot)
371  {
372  w = 1 + ((yday - bot) /
373  DAYSPERWEEK);
374  break;
375  }
376  --base;
377  yday += isleap_sum(year, base) ?
378  DAYSPERLYEAR :
379  DAYSPERNYEAR;
380  }
381  if (*format == 'V')
382  pt = _conv(w, "%02d",
383  pt, ptlim);
384  else if (*format == 'g')
385  {
386  *warnp = IN_ALL;
387  pt = _yconv(year, base,
388  false, true,
389  pt, ptlim);
390  }
391  else
392  pt = _yconv(year, base,
393  true, true,
394  pt, ptlim);
395  }
396  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  pt = _fmt("%e-%b-%Y", t, pt, ptlim, warnp);
404  continue;
405  case 'W':
406  pt = _conv((t->tm_yday + DAYSPERWEEK -
407  (t->tm_wday ?
408  (t->tm_wday - 1) :
409  (DAYSPERWEEK - 1))) / DAYSPERWEEK,
410  "%02d", pt, ptlim);
411  continue;
412  case 'w':
413  pt = _conv(t->tm_wday, "%d", pt, ptlim);
414  continue;
415  case 'X':
416  pt = _fmt(Locale->X_fmt, t, pt, ptlim, warnp);
417  continue;
418  case 'x':
419  {
420  int warn2 = IN_SOME;
421 
422  pt = _fmt(Locale->x_fmt, t, pt, ptlim, &warn2);
423  if (warn2 == IN_ALL)
424  warn2 = IN_THIS;
425  if (warn2 > *warnp)
426  *warnp = warn2;
427  }
428  continue;
429  case 'y':
430  *warnp = IN_ALL;
431  pt = _yconv(t->tm_year, TM_YEAR_BASE,
432  false, true,
433  pt, ptlim);
434  continue;
435  case 'Y':
436  pt = _yconv(t->tm_year, TM_YEAR_BASE,
437  true, true,
438  pt, ptlim);
439  continue;
440  case 'Z':
441  if (t->tm_zone != NULL)
442  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  continue;
449  case 'z':
450  {
451  long diff;
452  char const *sign;
453  bool negative;
454 
455  if (t->tm_isdst < 0)
456  continue;
457  diff = t->tm_gmtoff;
458  negative = diff < 0;
459  if (diff == 0)
460  {
461  if (t->tm_zone != NULL)
462  negative = t->tm_zone[0] == '-';
463  }
464  if (negative)
465  {
466  sign = "-";
467  diff = -diff;
468  }
469  else
470  sign = "+";
471  pt = _add(sign, pt, ptlim);
472  diff /= SECSPERMIN;
473  diff = (diff / MINSPERHOUR) * 100 +
474  (diff % MINSPERHOUR);
475  pt = _conv(diff, "%04d", pt, ptlim);
476  }
477  continue;
478  case '+':
479  pt = _fmt(Locale->date_fmt, t, pt, ptlim,
480  warnp);
481  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  break;
491  }
492  }
493  if (pt == ptlim)
494  break;
495  *pt++ = *format;
496  }
497  return pt;
498 }
499 
500 static char *
501 _conv(int n, const char *format, char *pt, const char *ptlim)
502 {
503  char buf[INT_STRLEN_MAXIMUM(int) +1];
504 
505  sprintf(buf, format, n);
506  return _add(buf, pt, ptlim);
507 }
508 
509 static char *
510 _add(const char *str, char *pt, const char *ptlim)
511 {
512  while (pt < ptlim && (*pt = *str++) != '\0')
513  ++pt;
514  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 _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  trail = a % DIVISOR + b % DIVISOR;
533  lead = a / DIVISOR + b / DIVISOR + trail / DIVISOR;
534  trail %= DIVISOR;
535  if (trail < 0 && lead > 0)
536  {
537  trail += DIVISOR;
538  --lead;
539  }
540  else if (lead < 0 && trail > 0)
541  {
542  trail -= DIVISOR;
543  ++lead;
544  }
545  if (convert_top)
546  {
547  if (lead == 0 && trail < 0)
548  pt = _add("-0", pt, ptlim);
549  else
550  pt = _conv(lead, "%02d", pt, ptlim);
551  }
552  if (convert_yy)
553  pt = _conv(((trail < 0) ? -trail : trail), "%02d", pt, ptlim);
554  return pt;
555 }
const char * am
Definition: strftime.c:57
const char * date_fmt
Definition: strftime.c:59
#define SECSPERMIN
Definition: private.h:107
int tm_wday
Definition: pgtime.h:33
int tm_isdst
Definition: pgtime.h:35
#define INT_STRLEN_MAXIMUM(type)
Definition: private.h:91
int tm_hour
Definition: pgtime.h:29
const char * x_fmt
Definition: strftime.c:55
const char * pm
Definition: strftime.c:58
#define MONSPERYEAR
Definition: private.h:115
#define HOURSPERDAY
Definition: private.h:109
#define DAYSPERNYEAR
Definition: private.h:111
#define IN_SOME
Definition: strftime.c:116
long int tm_gmtoff
Definition: pgtime.h:36
Definition: pgtime.h:25
static char * _yconv(int, int, bool, bool, char *, const char *)
Definition: strftime.c:525
const char * X_fmt
Definition: strftime.c:54
static char * _fmt(const char *, const struct pg_tm *, char *, const char *, int *)
Definition: strftime.c:137
#define DAYSPERLYEAR
Definition: private.h:112
#define TM_YEAR_BASE
Definition: private.h:138
#define IN_ALL
Definition: strftime.c:118
#define DAYSPERWEEK
Definition: private.h:110
char sign
Definition: informix.c:693
const char * wday[DAYSPERWEEK]
Definition: strftime.c:52
int tm_mday
Definition: pgtime.h:30
static char * buf
Definition: pg_test_fsync.c:66
int tm_mon
Definition: pgtime.h:31
const char * mon[MONSPERYEAR]
Definition: strftime.c:50
const char * tm_zone
Definition: pgtime.h:37
#define IN_THIS
Definition: strftime.c:117
#define DIVISOR
static char * label
Definition: pg_basebackup.c:81
static char * _add(const char *, char *, const char *)
Definition: strftime.c:510
#define NULL
Definition: c.h:229
#define isleap_sum(a, b)
Definition: private.h:157
int tm_year
Definition: pgtime.h:32
const char * month[MONSPERYEAR]
Definition: strftime.c:51
static const struct lc_time_T C_time_locale
Definition: strftime.c:64
size_t pg_strftime(char *s, size_t maxsize, const char *format, const struct pg_tm *t)
Definition: strftime.c:122
int tm_yday
Definition: pgtime.h:34
static char format
Definition: pg_basebackup.c:80
#define IN_NONE
Definition: strftime.c:115
static void warn(void)
int tm_sec
Definition: pgtime.h:27
#define Locale
Definition: strftime.c:62
const char * weekday[DAYSPERWEEK]
Definition: strftime.c:53
int tm_min
Definition: pgtime.h:28
#define MINSPERHOUR
Definition: private.h:108
const char * c_fmt
Definition: strftime.c:56
static char * _conv(int, const char *, char *, const char *)
Definition: strftime.c:501