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 *str, char *pt, const char *ptlim)
 
static char * _conv (int n, const char *format, char *pt, const char *ptlim)
 
static char * _fmt (const char *format, const struct pg_tm *t, char *pt, const char *ptlim, enum warn *warnp)
 
static char * _yconv (int a, int b, bool convert_top, bool convert_yy, char *pt, char const *ptlim)
 
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

◆ Locale

#define Locale   (&C_time_locale)

Definition at line 62 of file strftime.c.

Enumeration Type Documentation

◆ warn

enum warn ( void  )
Enumerator
IN_NONE 
IN_SOME 
IN_THIS 
IN_ALL 

Definition at line 109 of file strftime.c.

110 {
112 };
@ IN_ALL
Definition: strftime.c:111
@ IN_NONE
Definition: strftime.c:111
@ IN_SOME
Definition: strftime.c:111
@ IN_THIS
Definition: strftime.c:111

Function Documentation

◆ _add()

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

Definition at line 525 of file strftime.c.

526 {
527  while (pt < ptlim && (*pt = *str++) != '\0')
528  ++pt;
529  return pt;
530 }
const char * str

References str.

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

◆ _conv()

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

Definition at line 516 of file strftime.c.

517 {
518  char buf[INT_STRLEN_MAXIMUM(int) + 1];
519 
520  sprintf(buf, format, n);
521  return _add(buf, pt, ptlim);
522 }
static char format
static char * buf
Definition: pg_test_fsync.c:73
#define sprintf
Definition: port.h:240
#define INT_STRLEN_MAXIMUM(type)
Definition: private.h:81
static char * _add(const char *str, char *pt, const char *ptlim)
Definition: strftime.c:525

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

Referenced by _fmt(), and _yconv().

◆ _fmt()

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

Definition at line 151 of file strftime.c.

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

References _add(), _conv(), _yconv(), DAYSPERLYEAR, DAYSPERNYEAR, DAYSPERWEEK, format, HOURSPERDAY, IN_ALL, IN_SOME, IN_THIS, isleap_sum, label, len, 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().

◆ _yconv()

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

Definition at line 541 of file strftime.c.

543 {
544  int lead;
545  int trail;
546 
547 #define DIVISOR 100
548  trail = a % DIVISOR + b % DIVISOR;
549  lead = a / DIVISOR + b / DIVISOR + trail / DIVISOR;
550  trail %= DIVISOR;
551  if (trail < 0 && lead > 0)
552  {
553  trail += DIVISOR;
554  --lead;
555  }
556  else if (lead < 0 && trail > 0)
557  {
558  trail -= DIVISOR;
559  ++lead;
560  }
561  if (convert_top)
562  {
563  if (lead == 0 && trail < 0)
564  pt = _add("-0", pt, ptlim);
565  else
566  pt = _conv(lead, "%02d", pt, ptlim);
567  }
568  if (convert_yy)
569  pt = _conv(((trail < 0) ? -trail : trail), "%02d", pt, ptlim);
570  return pt;
571 }
int b
Definition: isn.c:70
int a
Definition: isn.c:69
#define DIVISOR

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

Referenced by _fmt().

◆ 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.

129 {
130  char *p;
131  int saved_errno = errno;
132  enum warn warn = IN_NONE;
133 
134  p = _fmt(format, t, s, s + maxsize, &warn);
135  if (!p)
136  {
137  errno = EOVERFLOW;
138  return 0;
139  }
140  if (p == s + maxsize)
141  {
142  errno = ERANGE;
143  return 0;
144  }
145  *p = '\0';
146  errno = saved_errno;
147  return p - s;
148 }
#define EOVERFLOW
Definition: private.h:41

References _fmt(), EOVERFLOW, format, and IN_NONE.

Referenced by AddFileToBackupManifest(), build_backup_content(), get_formatted_log_time(), get_formatted_start_time(), log_status_format(), logfile_getname(), str_time(), and timeofday().

Variable Documentation

◆ C_time_locale

const struct lc_time_T C_time_locale
static

Definition at line 1 of file strftime.c.