1 | /* Convert a broken-down timestamp to a string. */ |
2 | |
3 | /* |
4 | * Copyright 1989 The Regents of the University of California. |
5 | * All rights reserved. |
6 | * |
7 | * Redistribution and use in source and binary forms, with or without |
8 | * modification, are permitted provided that the following conditions |
9 | * are met: |
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. |
18 | * |
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 |
29 | * SUCH DAMAGE. |
30 | */ |
31 | |
32 | /* |
33 | * Based on the UCB version with the copyright notice appearing above. |
34 | * |
35 | * This is ANSIish only when "multibyte character == plain character". |
36 | * |
37 | * IDENTIFICATION |
38 | * src/timezone/strftime.c |
39 | */ |
40 | |
41 | #include "postgres.h" |
42 | |
43 | #include <fcntl.h> |
44 | |
45 | #include "private.h" |
46 | |
47 | |
48 | struct lc_time_T |
49 | { |
50 | const char *mon[MONSPERYEAR]; |
51 | const char *month[MONSPERYEAR]; |
52 | const char *wday[DAYSPERWEEK]; |
53 | const char *weekday[DAYSPERWEEK]; |
54 | const char *X_fmt; |
55 | const char *x_fmt; |
56 | const char *c_fmt; |
57 | const char *am; |
58 | const char *pm; |
59 | const char *date_fmt; |
60 | }; |
61 | |
62 | #define Locale (&C_time_locale) |
63 | |
64 | static const struct lc_time_T C_time_locale = { |
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 | }; |
108 | |
109 | enum warn |
110 | { |
111 | IN_NONE, IN_SOME, IN_THIS, IN_ALL |
112 | }; |
113 | |
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 *, |
117 | enum warn *); |
118 | static char *_yconv(int, int, bool, bool, char *, char const *); |
119 | |
120 | |
121 | size_t |
122 | pg_strftime(char *s, size_t maxsize, const char *format, const struct pg_tm *t) |
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 | } |
133 | |
134 | static char * |
135 | _fmt(const char *format, const struct pg_tm *t, char *pt, |
136 | const char *ptlim, enum warn *warnp) |
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 alternative |
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 abbreviation is not |
445 | * determinable. |
446 | */ |
447 | continue; |
448 | case 'z': |
449 | { |
450 | long diff; |
451 | char const *sign; |
452 | bool negative; |
453 | |
454 | if (t->tm_isdst < 0) |
455 | continue; |
456 | diff = t->tm_gmtoff; |
457 | negative = diff < 0; |
458 | if (diff == 0) |
459 | { |
460 | if (t->tm_zone != NULL) |
461 | negative = t->tm_zone[0] == '-'; |
462 | } |
463 | if (negative) |
464 | { |
465 | sign = "-" ; |
466 | diff = -diff; |
467 | } |
468 | else |
469 | sign = "+" ; |
470 | pt = _add(sign, pt, ptlim); |
471 | diff /= SECSPERMIN; |
472 | diff = (diff / MINSPERHOUR) * 100 + |
473 | (diff % MINSPERHOUR); |
474 | pt = _conv(diff, "%04d" , pt, ptlim); |
475 | } |
476 | continue; |
477 | case '+': |
478 | pt = _fmt(Locale->date_fmt, t, pt, ptlim, |
479 | warnp); |
480 | continue; |
481 | case '%': |
482 | |
483 | /* |
484 | * X311J/88-090 (4.12.3.5): if conversion char is |
485 | * undefined, behavior is undefined. Print out the |
486 | * character itself as printf(3) also does. |
487 | */ |
488 | default: |
489 | break; |
490 | } |
491 | } |
492 | if (pt == ptlim) |
493 | break; |
494 | *pt++ = *format; |
495 | } |
496 | return pt; |
497 | } |
498 | |
499 | static char * |
500 | _conv(int n, const char *format, char *pt, const char *ptlim) |
501 | { |
502 | char buf[INT_STRLEN_MAXIMUM(int) +1]; |
503 | |
504 | sprintf(buf, format, n); |
505 | return _add(buf, pt, ptlim); |
506 | } |
507 | |
508 | static char * |
509 | _add(const char *str, char *pt, const char *ptlim) |
510 | { |
511 | while (pt < ptlim && (*pt = *str++) != '\0') |
512 | ++pt; |
513 | return pt; |
514 | } |
515 | |
516 | /* |
517 | * POSIX and the C Standard are unclear or inconsistent about |
518 | * what %C and %y do if the year is negative or exceeds 9999. |
519 | * Use the convention that %C concatenated with %y yields the |
520 | * same output as %Y, and that %Y contains at least 4 bytes, |
521 | * with more only if necessary. |
522 | */ |
523 | |
524 | static char * |
525 | _yconv(int a, int b, bool convert_top, bool convert_yy, |
526 | char *pt, const char *ptlim) |
527 | { |
528 | int lead; |
529 | int trail; |
530 | |
531 | #define DIVISOR 100 |
532 | trail = a % DIVISOR + b % DIVISOR; |
533 | lead = a / DIVISOR + b / DIVISOR + trail / DIVISOR; |
534 | trail %= DIVISOR; |
535 | if (trail < 0 && lead > 0) |
536 | { |
537 | trail += DIVISOR; |
538 | --lead; |
539 | } |
540 | else if (lead < 0 && trail > 0) |
541 | { |
542 | trail -= DIVISOR; |
543 | ++lead; |
544 | } |
545 | if (convert_top) |
546 | { |
547 | if (lead == 0 && trail < 0) |
548 | pt = _add("-0" , pt, ptlim); |
549 | else |
550 | pt = _conv(lead, "%02d" , pt, ptlim); |
551 | } |
552 | if (convert_yy) |
553 | pt = _conv(((trail < 0) ? -trail : trail), "%02d" , pt, ptlim); |
554 | return pt; |
555 | } |
556 | |