PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
strftime.c File Reference
#include "postgres.h"
#include <fcntl.h>
#include "private.h"
Include dependency graph for strftime.c:

Go to the source code of this file.

Data Structures

struct  lc_time_T
 

Macros

#define Locale   (&C_time_locale)
 
#define IN_NONE   0
 
#define IN_SOME   1
 
#define IN_THIS   2
 
#define IN_ALL   3
 
#define DIVISOR   100
 

Functions

static char * _add (const char *, char *, const char *)
 
static char * _conv (int, const char *, char *, const char *)
 
static char * _fmt (const char *, const struct pg_tm *, char *, const char *, int *)
 
static char * _yconv (int, int, bool, bool, char *, const char *)
 
size_t pg_strftime (char *s, size_t maxsize, const char *format, const struct pg_tm *t)
 

Variables

static const struct lc_time_T C_time_locale
 

Macro Definition Documentation

#define DIVISOR   100

Referenced by _yconv().

#define IN_ALL   3

Definition at line 118 of file strftime.c.

Referenced by _fmt().

#define IN_NONE   0

Definition at line 115 of file strftime.c.

Referenced by pg_strftime().

#define IN_SOME   1

Definition at line 116 of file strftime.c.

Referenced by _fmt().

#define IN_THIS   2

Definition at line 117 of file strftime.c.

Referenced by _fmt().

#define Locale   (&C_time_locale)

Definition at line 62 of file strftime.c.

Referenced by _fmt().

Function Documentation

static char * _add ( const char *  str,
char *  pt,
const char *  ptlim 
)
static

Definition at line 510 of file strftime.c.

Referenced by _conv(), _fmt(), and _yconv().

511 {
512  while (pt < ptlim && (*pt = *str++) != '\0')
513  ++pt;
514  return pt;
515 }
static char * _conv ( int  n,
const char *  format,
char *  pt,
const char *  ptlim 
)
static

Definition at line 501 of file strftime.c.

References _add(), buf, and INT_STRLEN_MAXIMUM.

Referenced by _fmt(), and _yconv().

502 {
503  char buf[INT_STRLEN_MAXIMUM(int) +1];
504 
505  sprintf(buf, format, n);
506  return _add(buf, pt, ptlim);
507 }
#define INT_STRLEN_MAXIMUM(type)
Definition: private.h:91
static char * buf
Definition: pg_test_fsync.c:66
static char * _add(const char *, char *, const char *)
Definition: strftime.c:510
static char format
Definition: pg_basebackup.c:80
static char * _fmt ( const char *  format,
const struct pg_tm t,
char *  pt,
const char *  ptlim,
int *  warnp 
)
static

Definition at line 137 of file strftime.c.

References _add(), _conv(), _yconv(), DAYSPERLYEAR, DAYSPERNYEAR, DAYSPERWEEK, format, HOURSPERDAY, IN_ALL, IN_SOME, IN_THIS, isleap_sum, label, Locale, MINSPERHOUR, MONSPERYEAR, NULL, SECSPERMIN, sign, pg_tm::tm_gmtoff, pg_tm::tm_hour, pg_tm::tm_isdst, pg_tm::tm_mday, pg_tm::tm_min, pg_tm::tm_mon, pg_tm::tm_sec, pg_tm::tm_wday, pg_tm::tm_yday, pg_tm::tm_year, TM_YEAR_BASE, pg_tm::tm_zone, and lc_time_T::wday.

Referenced by pg_strftime().

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 }
#define SECSPERMIN
Definition: private.h:107
int tm_wday
Definition: pgtime.h:33
int tm_isdst
Definition: pgtime.h:35
int tm_hour
Definition: pgtime.h:29
#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
static char * _yconv(int, int, bool, bool, char *, const char *)
Definition: strftime.c:525
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
int tm_mday
Definition: pgtime.h:30
int tm_mon
Definition: pgtime.h:31
const char * tm_zone
Definition: pgtime.h:37
#define IN_THIS
Definition: strftime.c:117
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
int tm_yday
Definition: pgtime.h:34
static char format
Definition: pg_basebackup.c:80
int tm_sec
Definition: pgtime.h:27
#define Locale
Definition: strftime.c:62
int tm_min
Definition: pgtime.h:28
#define MINSPERHOUR
Definition: private.h:108
static char * _conv(int, const char *, char *, const char *)
Definition: strftime.c:501
static char * _yconv ( int  a,
int  b,
bool  convert_top,
bool  convert_yy,
char *  pt,
const char *  ptlim 
)
static

Definition at line 525 of file strftime.c.

References _add(), _conv(), and DIVISOR.

Referenced by _fmt().

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 }
#define DIVISOR
static char * _add(const char *, char *, const char *)
Definition: strftime.c:510
static char * _conv(int, const char *, char *, const char *)
Definition: strftime.c:501
size_t pg_strftime ( char *  s,
size_t  maxsize,
const char *  format,
const struct pg_tm t 
)

Definition at line 122 of file strftime.c.

References _fmt(), IN_NONE, and warn().

Referenced by do_pg_start_backup(), do_pg_stop_backup(), log_line_prefix(), logfile_getname(), setup_formatted_log_time(), setup_formatted_start_time(), str_time(), and timeofday().

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 }
static char * _fmt(const char *, const struct pg_tm *, char *, const char *, int *)
Definition: strftime.c:137
static char format
Definition: pg_basebackup.c:80
#define IN_NONE
Definition: strftime.c:115
static void warn(void)

Variable Documentation

const struct lc_time_T C_time_locale
static

Definition at line 64 of file strftime.c.