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 "tzfile.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 119 of file strftime.c.

Referenced by _fmt().

#define IN_NONE   0

Definition at line 116 of file strftime.c.

Referenced by pg_strftime().

#define IN_SOME   1

Definition at line 117 of file strftime.c.

Referenced by _fmt().

#define IN_THIS   2

Definition at line 118 of file strftime.c.

Referenced by _fmt().

#define Locale   (&C_time_locale)

Definition at line 63 of file strftime.c.

Referenced by _fmt().

Function Documentation

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

Definition at line 504 of file strftime.c.

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

505 {
506  while (pt < ptlim && (*pt = *str++) != '\0')
507  ++pt;
508  return pt;
509 }
static char * _conv ( int  n,
const char *  format,
char *  pt,
const char *  ptlim 
)
static

Definition at line 495 of file strftime.c.

References _add(), buf, and INT_STRLEN_MAXIMUM.

Referenced by _fmt(), and _yconv().

496 {
497  char buf[INT_STRLEN_MAXIMUM(int) +1];
498 
499  sprintf(buf, format, n);
500  return _add(buf, pt, ptlim);
501 }
#define INT_STRLEN_MAXIMUM(type)
Definition: private.h:98
static char * buf
Definition: pg_test_fsync.c:65
static char * _add(const char *, char *, const char *)
Definition: strftime.c:504
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 138 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().

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 }
#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
int tm_hour
Definition: pgtime.h:29
#define IN_SOME
Definition: strftime.c:117
long int tm_gmtoff
Definition: pgtime.h:36
static char * _yconv(int, int, bool, bool, char *, const char *)
Definition: strftime.c:519
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
int tm_mday
Definition: pgtime.h:30
int tm_mon
Definition: pgtime.h:31
#define MONSPERYEAR
Definition: tzfile.h:111
const char * tm_zone
Definition: pgtime.h:37
#define IN_THIS
Definition: strftime.c:118
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
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:63
int tm_min
Definition: pgtime.h:28
static char * _conv(int, const char *, char *, const char *)
Definition: strftime.c:495
static char * _yconv ( int  a,
int  b,
bool  convert_top,
bool  convert_yy,
char *  pt,
const char *  ptlim 
)
static

Definition at line 519 of file strftime.c.

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

Referenced by _fmt().

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

Definition at line 123 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().

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

Variable Documentation

const struct lc_time_T C_time_locale
static

Definition at line 65 of file strftime.c.