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 *, char const *)
 
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 515 of file strftime.c.

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

516 {
517  while (pt < ptlim && (*pt = *str++) != '\0')
518  ++pt;
519  return pt;
520 }

◆ _conv()

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

Definition at line 506 of file strftime.c.

References _add(), buf, INT_STRLEN_MAXIMUM, and sprintf.

Referenced by _fmt(), and _yconv().

507 {
508  char buf[INT_STRLEN_MAXIMUM(int) + 1];
509 
510  sprintf(buf, format, n);
511  return _add(buf, pt, ptlim);
512 }
#define INT_STRLEN_MAXIMUM(type)
Definition: private.h:85
#define sprintf
Definition: port.h:195
static char * buf
Definition: pg_test_fsync.c:68
static char * _add(const char *, char *, const char *)
Definition: strftime.c:515
static char format

◆ _fmt()

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

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

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

◆ _yconv()

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

Definition at line 531 of file strftime.c.

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

Referenced by _fmt().

533 {
534  int lead;
535  int trail;
536 
537 #define DIVISOR 100
538  trail = a % DIVISOR + b % DIVISOR;
539  lead = a / DIVISOR + b / DIVISOR + trail / DIVISOR;
540  trail %= DIVISOR;
541  if (trail < 0 && lead > 0)
542  {
543  trail += DIVISOR;
544  --lead;
545  }
546  else if (lead < 0 && trail > 0)
547  {
548  trail -= DIVISOR;
549  ++lead;
550  }
551  if (convert_top)
552  {
553  if (lead == 0 && trail < 0)
554  pt = _add("-0", pt, ptlim);
555  else
556  pt = _conv(lead, "%02d", pt, ptlim);
557  }
558  if (convert_yy)
559  pt = _conv(((trail < 0) ? -trail : trail), "%02d", pt, ptlim);
560  return pt;
561 }
#define DIVISOR
static char * _add(const char *, char *, const char *)
Definition: strftime.c:515
static char * _conv(int, const char *, char *, const char *)
Definition: strftime.c:506

◆ pg_strftime()

size_t pg_strftime ( char *  s,
size_t  maxsize,
const char *  format,
const struct pg_tm t 
)

Definition at line 128 of file strftime.c.

References _fmt(), and IN_NONE.

Referenced by AddFileToBackupManifest(), 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().

129 {
130  char *p;
131  enum warn warn = IN_NONE;
132 
133  p = _fmt(format, t, s, s + maxsize, &warn);
134  if (p == s + maxsize)
135  return 0;
136  *p = '\0';
137  return p - s;
138 }
static char * _fmt(const char *, const struct pg_tm *, char *, const char *, enum warn *)
Definition: strftime.c:141
warn
Definition: strftime.c:109
static char format

Variable Documentation

◆ C_time_locale

const struct lc_time_T C_time_locale
static

Definition at line 64 of file strftime.c.