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