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