LCOV - code coverage report
Current view: top level - src/timezone - strftime.c (source / functions) Hit Total Coverage
Test: PostgreSQL 13devel Lines: 56 185 30.3 %
Date: 2019-11-15 23:07:02 Functions: 5 5 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       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       96472 : pg_strftime(char *s, size_t maxsize, const char *format, const struct pg_tm *t)
     123             : {
     124             :     char       *p;
     125       96472 :     enum warn   warn = IN_NONE;
     126             : 
     127       96472 :     p = _fmt(format, t, s, s + maxsize, &warn);
     128       96472 :     if (p == s + maxsize)
     129           0 :         return 0;
     130       96472 :     *p = '\0';
     131       96472 :     return p - s;
     132             : }
     133             : 
     134             : static char *
     135       96472 : _fmt(const char *format, const struct pg_tm *t, char *pt,
     136             :      const char *ptlim, enum warn *warnp)
     137             : {
     138     1738386 :     for (; *format; ++format)
     139             :     {
     140     1641914 :         if (*format == '%')
     141             :         {
     142             :     label:
     143      678498 :             switch (*++format)
     144             :             {
     145             :                 case '\0':
     146           0 :                     --format;
     147           0 :                     break;
     148             :                 case 'A':
     149           0 :                     pt = _add((t->tm_wday < 0 ||
     150           0 :                                t->tm_wday >= DAYSPERWEEK) ?
     151           0 :                               "?" : Locale->weekday[t->tm_wday],
     152             :                               pt, ptlim);
     153           0 :                     continue;
     154             :                 case 'a':
     155        4800 :                     pt = _add((t->tm_wday < 0 ||
     156        1600 :                                t->tm_wday >= DAYSPERWEEK) ?
     157        1600 :                               "?" : Locale->wday[t->tm_wday],
     158             :                               pt, ptlim);
     159        1600 :                     continue;
     160             :                 case 'B':
     161           0 :                     pt = _add((t->tm_mon < 0 ||
     162           0 :                                t->tm_mon >= MONSPERYEAR) ?
     163           0 :                               "?" : Locale->month[t->tm_mon],
     164             :                               pt, ptlim);
     165           0 :                     continue;
     166             :                 case 'b':
     167             :                 case 'h':
     168        4800 :                     pt = _add((t->tm_mon < 0 ||
     169        1600 :                                t->tm_mon >= MONSPERYEAR) ?
     170        1600 :                               "?" : Locale->mon[t->tm_mon],
     171             :                               pt, ptlim);
     172        1600 :                     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           0 :                     pt = _yconv(t->tm_year, TM_YEAR_BASE,
     181             :                                 true, false, pt, ptlim);
     182           0 :                     continue;
     183             :                 case 'c':
     184             :                     {
     185           0 :                         enum warn   warn2 = IN_SOME;
     186             : 
     187           0 :                         pt = _fmt(Locale->c_fmt, t, pt, ptlim, &warn2);
     188           0 :                         if (warn2 == IN_ALL)
     189           0 :                             warn2 = IN_THIS;
     190           0 :                         if (warn2 > *warnp)
     191           0 :                             *warnp = warn2;
     192             :                     }
     193           0 :                     continue;
     194             :                 case 'D':
     195           0 :                     pt = _fmt("%m/%d/%y", t, pt, ptlim, warnp);
     196           0 :                     continue;
     197             :                 case 'd':
     198       96472 :                     pt = _conv(t->tm_mday, "%02d", pt, ptlim);
     199       96472 :                     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           0 :                     goto label;
     210             :                 case 'e':
     211           0 :                     pt = _conv(t->tm_mday, "%2d", pt, ptlim);
     212           0 :                     continue;
     213             :                 case 'F':
     214           0 :                     pt = _fmt("%Y-%m-%d", t, pt, ptlim, warnp);
     215           0 :                     continue;
     216             :                 case 'H':
     217       96472 :                     pt = _conv(t->tm_hour, "%02d", pt, ptlim);
     218       96472 :                     continue;
     219             :                 case 'I':
     220           0 :                     pt = _conv((t->tm_hour % 12) ?
     221           0 :                                (t->tm_hour % 12) : 12,
     222             :                                "%02d", pt, ptlim);
     223           0 :                     continue;
     224             :                 case 'j':
     225           0 :                     pt = _conv(t->tm_yday + 1, "%03d", pt, ptlim);
     226           0 :                     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           0 :                     pt = _conv(t->tm_hour, "%2d", pt, ptlim);
     237           0 :                     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           0 :                     pt = _conv((t->tm_hour % 12) ?
     256           0 :                                (t->tm_hour % 12) : 12,
     257             :                                "%2d", pt, ptlim);
     258           0 :                     continue;
     259             :                 case 'M':
     260       96472 :                     pt = _conv(t->tm_min, "%02d", pt, ptlim);
     261       96472 :                     continue;
     262             :                 case 'm':
     263       94872 :                     pt = _conv(t->tm_mon + 1, "%02d", pt, ptlim);
     264       94872 :                     continue;
     265             :                 case 'n':
     266           0 :                     pt = _add("\n", pt, ptlim);
     267           0 :                     continue;
     268             :                 case 'p':
     269           0 :                     pt = _add((t->tm_hour >= (HOURSPERDAY / 2)) ?
     270             :                               Locale->pm :
     271             :                               Locale->am,
     272             :                               pt, ptlim);
     273           0 :                     continue;
     274             :                 case 'R':
     275           0 :                     pt = _fmt("%H:%M", t, pt, ptlim, warnp);
     276           0 :                     continue;
     277             :                 case 'r':
     278           0 :                     pt = _fmt("%I:%M:%S %p", t, pt, ptlim, warnp);
     279           0 :                     continue;
     280             :                 case 'S':
     281       96472 :                     pt = _conv(t->tm_sec, "%02d", pt, ptlim);
     282       96472 :                     continue;
     283             :                 case 'T':
     284           0 :                     pt = _fmt("%H:%M:%S", t, pt, ptlim, warnp);
     285           0 :                     continue;
     286             :                 case 't':
     287           0 :                     pt = _add("\t", pt, ptlim);
     288           0 :                     continue;
     289             :                 case 'U':
     290           0 :                     pt = _conv((t->tm_yday + DAYSPERWEEK -
     291           0 :                                 t->tm_wday) / DAYSPERWEEK,
     292             :                                "%02d", pt, ptlim);
     293           0 :                     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           0 :                     pt = _conv((t->tm_wday == 0) ?
     302             :                                DAYSPERWEEK : t->tm_wday,
     303             :                                "%d", pt, ptlim);
     304           0 :                     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           0 :                         year = t->tm_year;
     334           0 :                         base = TM_YEAR_BASE;
     335           0 :                         yday = t->tm_yday;
     336           0 :                         wday = t->tm_wday;
     337             :                         for (;;)
     338           0 :                         {
     339             :                             int         len;
     340             :                             int         bot;
     341             :                             int         top;
     342             : 
     343           0 :                             len = isleap_sum(year, base) ?
     344           0 :                                 DAYSPERLYEAR :
     345             :                                 DAYSPERNYEAR;
     346             : 
     347             :                             /*
     348             :                              * What yday (-3 ... 3) does the ISO year begin
     349             :                              * on?
     350             :                              */
     351           0 :                             bot = ((yday + 11 - wday) %
     352             :                                    DAYSPERWEEK) - 3;
     353             : 
     354             :                             /*
     355             :                              * What yday does the NEXT ISO year begin on?
     356             :                              */
     357           0 :                             top = bot -
     358           0 :                                 (len % DAYSPERWEEK);
     359           0 :                             if (top < -3)
     360           0 :                                 top += DAYSPERWEEK;
     361           0 :                             top += len;
     362           0 :                             if (yday >= top)
     363             :                             {
     364           0 :                                 ++base;
     365           0 :                                 w = 1;
     366           0 :                                 break;
     367             :                             }
     368           0 :                             if (yday >= bot)
     369             :                             {
     370           0 :                                 w = 1 + ((yday - bot) /
     371             :                                          DAYSPERWEEK);
     372           0 :                                 break;
     373             :                             }
     374           0 :                             --base;
     375           0 :                             yday += isleap_sum(year, base) ?
     376           0 :                                 DAYSPERLYEAR :
     377             :                                 DAYSPERNYEAR;
     378             :                         }
     379           0 :                         if (*format == 'V')
     380           0 :                             pt = _conv(w, "%02d",
     381             :                                        pt, ptlim);
     382           0 :                         else if (*format == 'g')
     383             :                         {
     384           0 :                             *warnp = IN_ALL;
     385           0 :                             pt = _yconv(year, base,
     386             :                                         false, true,
     387             :                                         pt, ptlim);
     388             :                         }
     389             :                         else
     390           0 :                             pt = _yconv(year, base,
     391             :                                         true, true,
     392             :                                         pt, ptlim);
     393             :                     }
     394           0 :                     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           0 :                     pt = _fmt("%e-%b-%Y", t, pt, ptlim, warnp);
     402           0 :                     continue;
     403             :                 case 'W':
     404           0 :                     pt = _conv((t->tm_yday + DAYSPERWEEK -
     405           0 :                                 (t->tm_wday ?
     406           0 :                                  (t->tm_wday - 1) :
     407             :                                  (DAYSPERWEEK - 1))) / DAYSPERWEEK,
     408             :                                "%02d", pt, ptlim);
     409           0 :                     continue;
     410             :                 case 'w':
     411           0 :                     pt = _conv(t->tm_wday, "%d", pt, ptlim);
     412           0 :                     continue;
     413             :                 case 'X':
     414           0 :                     pt = _fmt(Locale->X_fmt, t, pt, ptlim, warnp);
     415           0 :                     continue;
     416             :                 case 'x':
     417             :                     {
     418           0 :                         enum warn   warn2 = IN_SOME;
     419             : 
     420           0 :                         pt = _fmt(Locale->x_fmt, t, pt, ptlim, &warn2);
     421           0 :                         if (warn2 == IN_ALL)
     422           0 :                             warn2 = IN_THIS;
     423           0 :                         if (warn2 > *warnp)
     424           0 :                             *warnp = warn2;
     425             :                     }
     426           0 :                     continue;
     427             :                 case 'y':
     428           0 :                     *warnp = IN_ALL;
     429           0 :                     pt = _yconv(t->tm_year, TM_YEAR_BASE,
     430             :                                 false, true,
     431             :                                 pt, ptlim);
     432           0 :                     continue;
     433             :                 case 'Y':
     434       96472 :                     pt = _yconv(t->tm_year, TM_YEAR_BASE,
     435             :                                 true, true,
     436             :                                 pt, ptlim);
     437       96472 :                     continue;
     438             :                 case 'Z':
     439       96466 :                     if (t->tm_zone != NULL)
     440       96466 :                         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       96466 :                     continue;
     448             :                 case 'z':
     449             :                     {
     450             :                         long        diff;
     451             :                         char const *sign;
     452             :                         bool        negative;
     453             : 
     454           0 :                         if (t->tm_isdst < 0)
     455           0 :                             continue;
     456           0 :                         diff = t->tm_gmtoff;
     457           0 :                         negative = diff < 0;
     458           0 :                         if (diff == 0)
     459             :                         {
     460           0 :                             if (t->tm_zone != NULL)
     461           0 :                                 negative = t->tm_zone[0] == '-';
     462             :                         }
     463           0 :                         if (negative)
     464             :                         {
     465           0 :                             sign = "-";
     466           0 :                             diff = -diff;
     467             :                         }
     468             :                         else
     469           0 :                             sign = "+";
     470           0 :                         pt = _add(sign, pt, ptlim);
     471           0 :                         diff /= SECSPERMIN;
     472           0 :                         diff = (diff / MINSPERHOUR) * 100 +
     473           0 :                             (diff % MINSPERHOUR);
     474           0 :                         pt = _conv(diff, "%04d", pt, ptlim);
     475             :                     }
     476           0 :                     continue;
     477             :                 case '+':
     478           0 :                     pt = _fmt(Locale->date_fmt, t, pt, ptlim,
     479             :                               warnp);
     480           0 :                     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        1600 :                     break;
     490             :             }
     491             :         }
     492      965016 :         if (pt == ptlim)
     493           0 :             break;
     494      965016 :         *pt++ = *format;
     495             :     }
     496       96472 :     return pt;
     497             : }
     498             : 
     499             : static char *
     500      673704 : _conv(int n, const char *format, char *pt, const char *ptlim)
     501             : {
     502             :     char        buf[INT_STRLEN_MAXIMUM(int) +1];
     503             : 
     504      673704 :     sprintf(buf, format, n);
     505      673704 :     return _add(buf, pt, ptlim);
     506             : }
     507             : 
     508             : static char *
     509      773370 : _add(const char *str, char *pt, const char *ptlim)
     510             : {
     511     3193146 :     while (pt < ptlim && (*pt = *str++) != '\0')
     512     1646406 :         ++pt;
     513      773370 :     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       96472 : _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       96472 :     trail = a % DIVISOR + b % DIVISOR;
     533       96472 :     lead = a / DIVISOR + b / DIVISOR + trail / DIVISOR;
     534       96472 :     trail %= DIVISOR;
     535       96472 :     if (trail < 0 && lead > 0)
     536             :     {
     537           0 :         trail += DIVISOR;
     538           0 :         --lead;
     539             :     }
     540       96472 :     else if (lead < 0 && trail > 0)
     541             :     {
     542           0 :         trail -= DIVISOR;
     543           0 :         ++lead;
     544             :     }
     545       96472 :     if (convert_top)
     546             :     {
     547       96472 :         if (lead == 0 && trail < 0)
     548           0 :             pt = _add("-0", pt, ptlim);
     549             :         else
     550       96472 :             pt = _conv(lead, "%02d", pt, ptlim);
     551             :     }
     552       96472 :     if (convert_yy)
     553       96472 :         pt = _conv(((trail < 0) ? -trail : trail), "%02d", pt, ptlim);
     554       96472 :     return pt;
     555             : }

Generated by: LCOV version 1.13