1 /* Convert a broken-down timestamp to a string. */
4 * Copyright 1989 The Regents of the University of California.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS "AS IS" AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * Based on the UCB version with the copyright notice appearing above.
35 * This is ANSIish only when "multibyte character == plain character".
38 * src/timezone/strftime.c
50 const char *mon
[MONSPERYEAR
];
51 const char *month
[MONSPERYEAR
];
52 const char *wday
[DAYSPERWEEK
];
53 const char *weekday
[DAYSPERWEEK
];
62 #define Locale (&C_time_locale)
64 static const struct lc_time_T C_time_locale
= {
66 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
67 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
69 "January", "February", "March", "April", "May", "June",
70 "July", "August", "September", "October", "November", "December"
72 "Sun", "Mon", "Tue", "Wed",
75 "Sunday", "Monday", "Tuesday", "Wednesday",
76 "Thursday", "Friday", "Saturday"
85 * C99 and later require this format. Using just numbers (as here) makes
86 * Quakers happier; it's also compatible with SVR4.
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
106 "%a %b %e %H:%M:%S %Z %Y"
111 IN_NONE
, IN_SOME
, IN_THIS
, IN_ALL
114 static char *_add(const char *, char *, const char *);
115 static char *_conv(int, const char *, char *, const char *);
116 static char *_fmt(const char *, const struct pg_tm
*, char *, const char *,
118 static char *_yconv(int, int, bool, bool, char *, char const *);
122 * Convert timestamp t to string s, a caller-allocated buffer of size maxsize,
123 * using the given format pattern.
125 * See also timestamptz_to_str.
128 pg_strftime(char *s
, size_t maxsize
, const char *format
, const struct pg_tm
*t
)
131 int saved_errno
= errno
;
132 enum warn warn
= IN_NONE
;
134 p
= _fmt(format
, t
, s
, s
+ maxsize
, &warn
);
140 if (p
== s
+ maxsize
)
151 _fmt(const char *format
, const struct pg_tm
*t
, char *pt
,
152 const char *ptlim
, enum warn
*warnp
)
154 for (; *format
; ++format
)
165 pt
= _add((t
->tm_wday
< 0 ||
166 t
->tm_wday
>= DAYSPERWEEK
) ?
167 "?" : Locale
->weekday
[t
->tm_wday
],
171 pt
= _add((t
->tm_wday
< 0 ||
172 t
->tm_wday
>= DAYSPERWEEK
) ?
173 "?" : Locale
->wday
[t
->tm_wday
],
177 pt
= _add((t
->tm_mon
< 0 ||
178 t
->tm_mon
>= MONSPERYEAR
) ?
179 "?" : Locale
->month
[t
->tm_mon
],
184 pt
= _add((t
->tm_mon
< 0 ||
185 t
->tm_mon
>= MONSPERYEAR
) ?
186 "?" : Locale
->mon
[t
->tm_mon
],
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)
196 pt
= _yconv(t
->tm_year
, TM_YEAR_BASE
,
197 true, false, pt
, ptlim
);
201 enum warn warn2
= IN_SOME
;
203 pt
= _fmt(Locale
->c_fmt
, t
, pt
, ptlim
, &warn2
);
211 pt
= _fmt("%m/%d/%y", t
, pt
, ptlim
, warnp
);
214 pt
= _conv(t
->tm_mday
, "%02d", pt
, ptlim
);
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
227 pt
= _conv(t
->tm_mday
, "%2d", pt
, ptlim
);
230 pt
= _fmt("%Y-%m-%d", t
, pt
, ptlim
, warnp
);
233 pt
= _conv(t
->tm_hour
, "%02d", pt
, ptlim
);
236 pt
= _conv((t
->tm_hour
% 12) ?
237 (t
->tm_hour
% 12) : 12,
241 pt
= _conv(t
->tm_yday
+ 1, "%03d", pt
, ptlim
);
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.
252 pt
= _conv(t
->tm_hour
, "%2d", pt
, ptlim
);
258 * After all this time, still unclaimed!
260 pt
= _add("kitchen sink", pt
, ptlim
);
262 #endif /* defined KITCHEN_SINK */
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)
271 pt
= _conv((t
->tm_hour
% 12) ?
272 (t
->tm_hour
% 12) : 12,
276 pt
= _conv(t
->tm_min
, "%02d", pt
, ptlim
);
279 pt
= _conv(t
->tm_mon
+ 1, "%02d", pt
, ptlim
);
282 pt
= _add("\n", pt
, ptlim
);
285 pt
= _add((t
->tm_hour
>= (HOURSPERDAY
/ 2)) ?
291 pt
= _fmt("%H:%M", t
, pt
, ptlim
, warnp
);
294 pt
= _fmt("%I:%M:%S %p", t
, pt
, ptlim
, warnp
);
297 pt
= _conv(t
->tm_sec
, "%02d", pt
, ptlim
);
300 pt
= _fmt("%H:%M:%S", t
, pt
, ptlim
, warnp
);
303 pt
= _add("\t", pt
, ptlim
);
306 pt
= _conv((t
->tm_yday
+ DAYSPERWEEK
-
307 t
->tm_wday
) / DAYSPERWEEK
,
313 * From Arnold Robbins' strftime version 3.0: "ISO 8601:
314 * Weekday as a decimal number [1 (Monday) - 7]" (ado,
317 pt
= _conv((t
->tm_wday
== 0) ?
318 DAYSPERWEEK
: t
->tm_wday
,
321 case 'V': /* ISO 8601 week number */
322 case 'G': /* ISO 8601 year (four digits) */
323 case 'g': /* ISO 8601 year (two digits) */
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
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..."
359 len
= isleap_sum(year
, base
) ?
364 * What yday (-3 ... 3) does the ISO year begin
367 bot
= ((yday
+ 11 - wday
) %
371 * What yday does the NEXT ISO year begin on?
386 w
= 1 + ((yday
- bot
) /
391 yday
+= isleap_sum(year
, base
) ?
396 pt
= _conv(w
, "%02d",
398 else if (*format
== 'g')
401 pt
= _yconv(year
, base
,
406 pt
= _yconv(year
, base
,
414 * From Arnold Robbins' strftime version 3.0: "date as
415 * dd-bbb-YYYY" (ado, 1993-05-24)
417 pt
= _fmt("%e-%b-%Y", t
, pt
, ptlim
, warnp
);
420 pt
= _conv((t
->tm_yday
+ DAYSPERWEEK
-
423 (DAYSPERWEEK
- 1))) / DAYSPERWEEK
,
427 pt
= _conv(t
->tm_wday
, "%d", pt
, ptlim
);
430 pt
= _fmt(Locale
->X_fmt
, t
, pt
, ptlim
, warnp
);
434 enum warn warn2
= IN_SOME
;
436 pt
= _fmt(Locale
->x_fmt
, t
, pt
, ptlim
, &warn2
);
445 pt
= _yconv(t
->tm_year
, TM_YEAR_BASE
,
450 pt
= _yconv(t
->tm_year
, TM_YEAR_BASE
,
455 if (t
->tm_zone
!= NULL
)
456 pt
= _add(t
->tm_zone
, pt
, ptlim
);
459 * C99 and later say that %Z must be replaced by the empty
460 * string if the time zone abbreviation is not
476 if (t
->tm_zone
!= NULL
)
477 negative
= t
->tm_zone
[0] == '-';
486 pt
= _add(sign
, pt
, ptlim
);
488 diff
= (diff
/ MINSPERHOUR
) * 100 +
489 (diff
% MINSPERHOUR
);
490 pt
= _conv(diff
, "%04d", pt
, ptlim
);
494 pt
= _fmt(Locale
->date_fmt
, t
, pt
, ptlim
,
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.
516 _conv(int n
, const char *format
, char *pt
, const char *ptlim
)
518 char buf
[INT_STRLEN_MAXIMUM(int) + 1];
520 sprintf(buf
, format
, n
);
521 return _add(buf
, pt
, ptlim
);
525 _add(const char *str
, char *pt
, const char *ptlim
)
527 while (pt
< ptlim
&& (*pt
= *str
++) != '\0')
533 * POSIX and the C Standard are unclear or inconsistent about
534 * what %C and %y do if the year is negative or exceeds 9999.
535 * Use the convention that %C concatenated with %y yields the
536 * same output as %Y, and that %Y contains at least 4 bytes,
537 * with more only if necessary.
541 _yconv(int a
, int b
, bool convert_top
, bool convert_yy
,
542 char *pt
, const char *ptlim
)
548 trail
= a
% DIVISOR
+ b
% DIVISOR
;
549 lead
= a
/ DIVISOR
+ b
/ DIVISOR
+ trail
/ DIVISOR
;
551 if (trail
< 0 && lead
> 0)
556 else if (lead
< 0 && trail
> 0)
563 if (lead
== 0 && trail
< 0)
564 pt
= _add("-0", pt
, ptlim
);
566 pt
= _conv(lead
, "%02d", pt
, ptlim
);
569 pt
= _conv(((trail
< 0) ? -trail
: trail
), "%02d", pt
, ptlim
);