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