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