PostgreSQL Source Code  git master
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

◆ DIVISOR

#define DIVISOR   100

Referenced by _yconv().

◆ Locale

#define Locale   (&C_time_locale)

Definition at line 62 of file strftime.c.

Referenced by _fmt().

Enumeration Type Documentation

◆ warn

enum warn
Enumerator
IN_NONE 
IN_SOME 
IN_THIS 
IN_ALL 

Definition at line 109 of file strftime.c.

110 {
112 };

Function Documentation

◆ _add()

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

Definition at line 508 of file strftime.c.

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

509 {
510  while (pt < ptlim && (*pt = *str++) != '\0')
511  ++pt;
512  return pt;
513 }

◆ _conv()

static char * _conv ( int  n,
const char *  format,
char *  pt,
const char *  ptlim 
)
static

Definition at line 499 of file strftime.c.

References _add(), buf, and INT_STRLEN_MAXIMUM.

Referenced by _fmt(), and _yconv().

500 {
501  char buf[INT_STRLEN_MAXIMUM(int) +1];
502 
503  sprintf(buf, format, n);
504  return _add(buf, pt, ptlim);
505 }
#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:508
static char format
Definition: pg_basebackup.c:81

◆ _fmt()

static char * _fmt ( const char *  format,
const struct pg_tm t,
char *  pt,
const char *  ptlim,
enum warn warnp 
)
static

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

137 {
138  for (; *format; ++format)
139  {
140  if (*format == '%')
141  {
142  label:
143  switch (*++format)
144  {
145  case '\0':
146  --format;
147  break;
148  case 'A':
149  pt = _add((t->tm_wday < 0 ||
150  t->tm_wday >= DAYSPERWEEK) ?
151  "?" : Locale->weekday[t->tm_wday],
152  pt, ptlim);
153  continue;
154  case 'a':
155  pt = _add((t->tm_wday < 0 ||
156  t->tm_wday >= DAYSPERWEEK) ?
157  "?" : Locale->wday[t->tm_wday],
158  pt, ptlim);
159  continue;
160  case 'B':
161  pt = _add((t->tm_mon < 0 ||
162  t->tm_mon >= MONSPERYEAR) ?
163  "?" : Locale->month[t->tm_mon],
164  pt, ptlim);
165  continue;
166  case 'b':
167  case 'h':
168  pt = _add((t->tm_mon < 0 ||
169  t->tm_mon >= MONSPERYEAR) ?
170  "?" : Locale->mon[t->tm_mon],
171  pt, ptlim);
172  continue;
173  case 'C':
174 
175  /*
176  * %C used to do a... _fmt("%a %b %e %X %Y", t);
177  * ...whereas now POSIX 1003.2 calls for something
178  * completely different. (ado, 1993-05-24)
179  */
180  pt = _yconv(t->tm_year, TM_YEAR_BASE,
181  true, false, pt, ptlim);
182  continue;
183  case 'c':
184  {
185  enum warn warn2 = IN_SOME;
186 
187  pt = _fmt(Locale->c_fmt, t, pt, ptlim, &warn2);
188  if (warn2 == IN_ALL)
189  warn2 = IN_THIS;
190  if (warn2 > *warnp)
191  *warnp = warn2;
192  }
193  continue;
194  case 'D':
195  pt = _fmt("%m/%d/%y", t, pt, ptlim, warnp);
196  continue;
197  case 'd':
198  pt = _conv(t->tm_mday, "%02d", pt, ptlim);
199  continue;
200  case 'E':
201  case 'O':
202 
203  /*
204  * Locale modifiers of C99 and later. The sequences %Ec
205  * %EC %Ex %EX %Ey %EY %Od %oe %OH %OI %Om %OM %OS %Ou %OU
206  * %OV %Ow %OW %Oy are supposed to provide alternate
207  * representations.
208  */
209  goto label;
210  case 'e':
211  pt = _conv(t->tm_mday, "%2d", pt, ptlim);
212  continue;
213  case 'F':
214  pt = _fmt("%Y-%m-%d", t, pt, ptlim, warnp);
215  continue;
216  case 'H':
217  pt = _conv(t->tm_hour, "%02d", pt, ptlim);
218  continue;
219  case 'I':
220  pt = _conv((t->tm_hour % 12) ?
221  (t->tm_hour % 12) : 12,
222  "%02d", pt, ptlim);
223  continue;
224  case 'j':
225  pt = _conv(t->tm_yday + 1, "%03d", pt, ptlim);
226  continue;
227  case 'k':
228 
229  /*
230  * This used to be... _conv(t->tm_hour % 12 ? t->tm_hour %
231  * 12 : 12, 2, ' '); ...and has been changed to the below
232  * to match SunOS 4.1.1 and Arnold Robbins' strftime
233  * version 3.0. That is, "%k" and "%l" have been swapped.
234  * (ado, 1993-05-24)
235  */
236  pt = _conv(t->tm_hour, "%2d", pt, ptlim);
237  continue;
238 #ifdef KITCHEN_SINK
239  case 'K':
240 
241  /*
242  * After all this time, still unclaimed!
243  */
244  pt = _add("kitchen sink", pt, ptlim);
245  continue;
246 #endif /* defined KITCHEN_SINK */
247  case 'l':
248 
249  /*
250  * This used to be... _conv(t->tm_hour, 2, ' '); ...and
251  * has been changed to the below to match SunOS 4.1.1 and
252  * Arnold Robbin's strftime version 3.0. That is, "%k" and
253  * "%l" have been swapped. (ado, 1993-05-24)
254  */
255  pt = _conv((t->tm_hour % 12) ?
256  (t->tm_hour % 12) : 12,
257  "%2d", pt, ptlim);
258  continue;
259  case 'M':
260  pt = _conv(t->tm_min, "%02d", pt, ptlim);
261  continue;
262  case 'm':
263  pt = _conv(t->tm_mon + 1, "%02d", pt, ptlim);
264  continue;
265  case 'n':
266  pt = _add("\n", pt, ptlim);
267  continue;
268  case 'p':
269  pt = _add((t->tm_hour >= (HOURSPERDAY / 2)) ?
270  Locale->pm :
271  Locale->am,
272  pt, ptlim);
273  continue;
274  case 'R':
275  pt = _fmt("%H:%M", t, pt, ptlim, warnp);
276  continue;
277  case 'r':
278  pt = _fmt("%I:%M:%S %p", t, pt, ptlim, warnp);
279  continue;
280  case 'S':
281  pt = _conv(t->tm_sec, "%02d", pt, ptlim);
282  continue;
283  case 'T':
284  pt = _fmt("%H:%M:%S", t, pt, ptlim, warnp);
285  continue;
286  case 't':
287  pt = _add("\t", pt, ptlim);
288  continue;
289  case 'U':
290  pt = _conv((t->tm_yday + DAYSPERWEEK -
291  t->tm_wday) / DAYSPERWEEK,
292  "%02d", pt, ptlim);
293  continue;
294  case 'u':
295 
296  /*
297  * From Arnold Robbins' strftime version 3.0: "ISO 8601:
298  * Weekday as a decimal number [1 (Monday) - 7]" (ado,
299  * 1993-05-24)
300  */
301  pt = _conv((t->tm_wday == 0) ?
302  DAYSPERWEEK : t->tm_wday,
303  "%d", pt, ptlim);
304  continue;
305  case 'V': /* ISO 8601 week number */
306  case 'G': /* ISO 8601 year (four digits) */
307  case 'g': /* ISO 8601 year (two digits) */
308 /*
309  * From Arnold Robbins' strftime version 3.0: "the week number of the
310  * year (the first Monday as the first day of week 1) as a decimal number
311  * (01-53)."
312  * (ado, 1993-05-24)
313  *
314  * From <https://www.cl.cam.ac.uk/~mgk25/iso-time.html> by Markus Kuhn:
315  * "Week 01 of a year is per definition the first week which has the
316  * Thursday in this year, which is equivalent to the week which contains
317  * the fourth day of January. In other words, the first week of a new year
318  * is the week which has the majority of its days in the new year. Week 01
319  * might also contain days from the previous year and the week before week
320  * 01 of a year is the last week (52 or 53) of the previous year even if
321  * it contains days from the new year. A week starts with Monday (day 1)
322  * and ends with Sunday (day 7). For example, the first week of the year
323  * 1997 lasts from 1996-12-30 to 1997-01-05..."
324  * (ado, 1996-01-02)
325  */
326  {
327  int year;
328  int base;
329  int yday;
330  int wday;
331  int w;
332 
333  year = t->tm_year;
334  base = TM_YEAR_BASE;
335  yday = t->tm_yday;
336  wday = t->tm_wday;
337  for (;;)
338  {
339  int len;
340  int bot;
341  int top;
342 
343  len = isleap_sum(year, base) ?
344  DAYSPERLYEAR :
345  DAYSPERNYEAR;
346 
347  /*
348  * What yday (-3 ... 3) does the ISO year begin
349  * on?
350  */
351  bot = ((yday + 11 - wday) %
352  DAYSPERWEEK) - 3;
353 
354  /*
355  * What yday does the NEXT ISO year begin on?
356  */
357  top = bot -
358  (len % DAYSPERWEEK);
359  if (top < -3)
360  top += DAYSPERWEEK;
361  top += len;
362  if (yday >= top)
363  {
364  ++base;
365  w = 1;
366  break;
367  }
368  if (yday >= bot)
369  {
370  w = 1 + ((yday - bot) /
371  DAYSPERWEEK);
372  break;
373  }
374  --base;
375  yday += isleap_sum(year, base) ?
376  DAYSPERLYEAR :
377  DAYSPERNYEAR;
378  }
379  if (*format == 'V')
380  pt = _conv(w, "%02d",
381  pt, ptlim);
382  else if (*format == 'g')
383  {
384  *warnp = IN_ALL;
385  pt = _yconv(year, base,
386  false, true,
387  pt, ptlim);
388  }
389  else
390  pt = _yconv(year, base,
391  true, true,
392  pt, ptlim);
393  }
394  continue;
395  case 'v':
396 
397  /*
398  * From Arnold Robbins' strftime version 3.0: "date as
399  * dd-bbb-YYYY" (ado, 1993-05-24)
400  */
401  pt = _fmt("%e-%b-%Y", t, pt, ptlim, warnp);
402  continue;
403  case 'W':
404  pt = _conv((t->tm_yday + DAYSPERWEEK -
405  (t->tm_wday ?
406  (t->tm_wday - 1) :
407  (DAYSPERWEEK - 1))) / DAYSPERWEEK,
408  "%02d", pt, ptlim);
409  continue;
410  case 'w':
411  pt = _conv(t->tm_wday, "%d", pt, ptlim);
412  continue;
413  case 'X':
414  pt = _fmt(Locale->X_fmt, t, pt, ptlim, warnp);
415  continue;
416  case 'x':
417  {
418  enum warn warn2 = IN_SOME;
419 
420  pt = _fmt(Locale->x_fmt, t, pt, ptlim, &warn2);
421  if (warn2 == IN_ALL)
422  warn2 = IN_THIS;
423  if (warn2 > *warnp)
424  *warnp = warn2;
425  }
426  continue;
427  case 'y':
428  *warnp = IN_ALL;
429  pt = _yconv(t->tm_year, TM_YEAR_BASE,
430  false, true,
431  pt, ptlim);
432  continue;
433  case 'Y':
434  pt = _yconv(t->tm_year, TM_YEAR_BASE,
435  true, true,
436  pt, ptlim);
437  continue;
438  case 'Z':
439  if (t->tm_zone != NULL)
440  pt = _add(t->tm_zone, pt, ptlim);
441 
442  /*
443  * C99 and later say that %Z must be replaced by the empty
444  * string if the time zone is not determinable.
445  */
446  continue;
447  case 'z':
448  {
449  long diff;
450  char const *sign;
451  bool negative;
452 
453  if (t->tm_isdst < 0)
454  continue;
455  diff = t->tm_gmtoff;
456  negative = diff < 0;
457  if (diff == 0)
458  {
459  if (t->tm_zone != NULL)
460  negative = t->tm_zone[0] == '-';
461  }
462  if (negative)
463  {
464  sign = "-";
465  diff = -diff;
466  }
467  else
468  sign = "+";
469  pt = _add(sign, pt, ptlim);
470  diff /= SECSPERMIN;
471  diff = (diff / MINSPERHOUR) * 100 +
472  (diff % MINSPERHOUR);
473  pt = _conv(diff, "%04d", pt, ptlim);
474  }
475  continue;
476  case '+':
477  pt = _fmt(Locale->date_fmt, t, pt, ptlim,
478  warnp);
479  continue;
480  case '%':
481 
482  /*
483  * X311J/88-090 (4.12.3.5): if conversion char is
484  * undefined, behavior is undefined. Print out the
485  * character itself as printf(3) also does.
486  */
487  default:
488  break;
489  }
490  }
491  if (pt == ptlim)
492  break;
493  *pt++ = *format;
494  }
495  return pt;
496 }
static char * _fmt(const char *, const struct pg_tm *, char *, const char *, enum warn *)
Definition: strftime.c:135
#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:523
#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:508
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:499

◆ _yconv()

static char * _yconv ( int  a,
int  b,
bool  convert_top,
bool  convert_yy,
char *  pt,
const char *  ptlim 
)
static

Definition at line 523 of file strftime.c.

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

Referenced by _fmt().

525 {
526  int lead;
527  int trail;
528 
529 #define DIVISOR 100
530  trail = a % DIVISOR + b % DIVISOR;
531  lead = a / DIVISOR + b / DIVISOR + trail / DIVISOR;
532  trail %= DIVISOR;
533  if (trail < 0 && lead > 0)
534  {
535  trail += DIVISOR;
536  --lead;
537  }
538  else if (lead < 0 && trail > 0)
539  {
540  trail -= DIVISOR;
541  ++lead;
542  }
543  if (convert_top)
544  {
545  if (lead == 0 && trail < 0)
546  pt = _add("-0", pt, ptlim);
547  else
548  pt = _conv(lead, "%02d", pt, ptlim);
549  }
550  if (convert_yy)
551  pt = _conv(((trail < 0) ? -trail : trail), "%02d", pt, ptlim);
552  return pt;
553 }
#define DIVISOR
static char * _add(const char *, char *, const char *)
Definition: strftime.c:508
static char * _conv(int, const char *, char *, const char *)
Definition: strftime.c:499

◆ pg_strftime()

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().

123 {
124  char *p;
125  enum warn warn = IN_NONE;
126 
127  p = _fmt(format, t, s, s + maxsize, &warn);
128  if (p == s + maxsize)
129  return 0;
130  *p = '\0';
131  return p - s;
132 }
static char * _fmt(const char *, const struct pg_tm *, char *, const char *, enum warn *)
Definition: strftime.c:135
warn
Definition: strftime.c:109
static char format
Definition: pg_basebackup.c:81

Variable Documentation

◆ C_time_locale

const struct lc_time_T C_time_locale
static

Definition at line 64 of file strftime.c.