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 DIVISOR   100
 

Enumerations

enum  warn { IN_NONE, IN_SOME, IN_THIS, IN_ALL }
 

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 *, enum warn *)
 
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 Locale   (&C_time_locale)

Definition at line 62 of file strftime.c.

Referenced by _fmt().

Enumeration Type Documentation

enum warn
Enumerator
IN_NONE 
IN_SOME 
IN_THIS 
IN_ALL 

Definition at line 109 of file strftime.c.

110 {
112 };

Function Documentation

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

Definition at line 509 of file strftime.c.

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

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

Definition at line 500 of file strftime.c.

References _add(), buf, and INT_STRLEN_MAXIMUM.

Referenced by _fmt(), and _yconv().

501 {
502  char buf[INT_STRLEN_MAXIMUM(int) +1];
503 
504  sprintf(buf, format, n);
505  return _add(buf, pt, ptlim);
506 }
#define INT_STRLEN_MAXIMUM(type)
Definition: private.h:74
static char * buf
Definition: pg_test_fsync.c:67
static char * _add(const char *, char *, const char *)
Definition: strftime.c:509
static char format
Definition: pg_basebackup.c:81
static char * _fmt ( const char *  format,
const struct pg_tm t,
char *  pt,
const char *  ptlim,
enum warn warnp 
)
static

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

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 }
static char * _fmt(const char *, const struct pg_tm *, char *, const char *, enum warn *)
Definition: strftime.c:136
#define SECSPERMIN
Definition: private.h:90
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:98
#define HOURSPERDAY
Definition: private.h:92
#define DAYSPERNYEAR
Definition: private.h:94
long int tm_gmtoff
Definition: pgtime.h:36
static char * _yconv(int, int, bool, bool, char *, const char *)
Definition: strftime.c:524
#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
int tm_mday
Definition: pgtime.h:30
int tm_mon
Definition: pgtime.h:31
const char * tm_zone
Definition: pgtime.h:37
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
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
int tm_min
Definition: pgtime.h:28
#define MINSPERHOUR
Definition: private.h:91
static char * _conv(int, const char *, char *, const char *)
Definition: strftime.c:500
static char * _yconv ( int  a,
int  b,
bool  convert_top,
bool  convert_yy,
char *  pt,
const char *  ptlim 
)
static

Definition at line 524 of file strftime.c.

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

Referenced by _fmt().

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 }
#define DIVISOR
static char * _add(const char *, char *, const char *)
Definition: strftime.c:509
static char * _conv(int, const char *, char *, const char *)
Definition: strftime.c:500
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(), and IN_NONE.

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  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 }
static char * _fmt(const char *, const struct pg_tm *, char *, const char *, enum warn *)
Definition: strftime.c:136
warn
Definition: strftime.c:109
static char format
Definition: pg_basebackup.c:81

Variable Documentation

const struct lc_time_T C_time_locale
static

Definition at line 64 of file strftime.c.