PostgreSQL Source Code git master
Loading...
Searching...
No Matches
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 536 of file strftime.c.

537{
538 while (pt < ptlim && (*pt = *str++) != '\0')
539 ++pt;
540 return pt;
541}
const char * str
static int fb(int x)

References fb(), and 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 527 of file strftime.c.

528{
529 char buf[INT_STRLEN_MAXIMUM(int) + 1];
530
531 sprintf(buf, format, n);
532 return _add(buf, pt, ptlim);
533}
static char format
static char buf[DEFAULT_XLOG_SEG_SIZE]
#define sprintf
Definition port.h:263
#define INT_STRLEN_MAXIMUM(type)
Definition private.h:77
static char * _add(const char *str, char *pt, const char *ptlim)
Definition strftime.c:536

References _add(), buf, fb(), 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 162 of file strftime.c.

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

554{
555 int lead;
556 int trail;
557
558#define DIVISOR 100
559 trail = a % DIVISOR + b % DIVISOR;
560 lead = a / DIVISOR + b / DIVISOR + trail / DIVISOR;
561 trail %= DIVISOR;
563 {
564 trail += DIVISOR;
565 --lead;
566 }
567 else if (lead < 0 && trail > 0)
568 {
569 trail -= DIVISOR;
570 ++lead;
571 }
572 if (convert_top)
573 {
574 if (lead == 0 && trail < 0)
575 pt = _add("-0", pt, ptlim);
576 else
577 pt = _conv(lead, "%02d", pt, ptlim);
578 }
579 if (convert_yy)
580 pt = _conv(((trail < 0) ? -trail : trail), "%02d", pt, ptlim);
581 return pt;
582}
int b
Definition isn.c:74
int a
Definition isn.c:73
#define DIVISOR

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

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 135 of file strftime.c.

136{
137 char *p;
138 int saved_errno = errno;
139 enum warn warn = IN_NONE;
140
141 p = _fmt(format, t, s, s + maxsize, &warn);
142 if (!p)
143 {
145 if (maxsize > 0)
146 *s = '\0';
147 return 0;
148 }
149 if (p == s + maxsize)
150 {
151 errno = ERANGE;
152 if (maxsize > 0)
153 *s = '\0';
154 return 0;
155 }
156 *p = '\0';
157 errno = saved_errno;
158 return p - s;
159}
#define EOVERFLOW
Definition private.h:41

References _fmt(), EOVERFLOW, fb(), 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 64 of file strftime.c.

64 {
65 {
66 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
67 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
68 }, {
69 "January", "February", "March", "April", "May", "June",
70 "July", "August", "September", "October", "November", "December"
71 }, {
72 "Sun", "Mon", "Tue", "Wed",
73 "Thu", "Fri", "Sat"
74 }, {
75 "Sunday", "Monday", "Tuesday", "Wednesday",
76 "Thursday", "Friday", "Saturday"
77 },
78
79 /* X_fmt */
80 "%H:%M:%S",
81
82 /*
83 * x_fmt
84 *
85 * C99 and later require this format. Using just numbers (as here) makes
86 * Quakers happier; it's also compatible with SVR4.
87 */
88 "%m/%d/%y",
89
90 /*
91 * c_fmt
92 *
93 * C99 and later require this format. Previously this code used "%D %X",
94 * but we now conform to C99. Note that "%a %b %d %H:%M:%S %Y" is used by
95 * Solaris 2.3.
96 */
97 "%a %b %e %T %Y",
98
99 /* am */
100 "AM",
101
102 /* pm */
103 "PM",
104
105 /* date_fmt */
106 "%a %b %e %H:%M:%S %Z %Y"
107};