"Fossies" - the Fresh Open Source Software Archive

Member "tin-2.4.3/src/strftime.c" (23 Nov 2018, 7223 Bytes) of package /linux/misc/tin-2.4.3.tar.xz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "strftime.c" see the Fossies "Dox" file reference documentation and the latest Fossies "Diffs" side-by-side code changes report: 2.4.2_vs_2.4.3.

    1 /*
    2  *  Project   : tin - a Usenet reader
    3  *  Module    : strftime.c
    4  *  Author    : A. Robbins & I. Lea
    5  *  Created   : 1991-02-01
    6  *  Updated   : 2018-04-04
    7  *  Notes     : Relatively quick-and-dirty implementation of ANSI library
    8  *              routine for System V Unix systems.
    9  *              If target system already has strftime() call the #define
   10  *              HAVE_STRFTIME can be set to use it.
   11  *  Example   : time(&secs);
   12  *              tm = localtime(&secs);
   13  *              num = strftime(buf, sizeof(buf), "%a %d-%m-%y %H:%M:%S", tm);
   14  *
   15  * Copyright (c) 1991-2019 Arnold Robbins <arnold@skeeve.com>
   16  * All rights reserved.
   17  *
   18  * Redistribution and use in source and binary forms, with or without
   19  * modification, are permitted provided that the following conditions
   20  * are met:
   21  * 1. Redistributions of source code must retain the above copyright
   22  *    notice, this list of conditions and the following disclaimer.
   23  * 2. Redistributions in binary form must reproduce the above copyright
   24  *    notice, this list of conditions and the following disclaimer in the
   25  *    documentation and/or other materials provided with the distribution.
   26  * 3. The name of the author may not be used to endorse or promote
   27  *    products derived from this software without specific prior written
   28  *    permission.
   29  *
   30  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
   31  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
   32  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   33  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
   34  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   35  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
   36  * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
   37  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
   38  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
   39  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
   40  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
   41  */
   42 
   43 
   44 #ifndef TIN_H
   45 #   include "tin.h"
   46 #endif /* !TIN_H */
   47 
   48 #ifdef SYSV
   49 extern int daylight;
   50 #endif /* SYSV */
   51 
   52 #ifndef HAVE_STRFTIME
   53 #   define SYSV_EXT 1   /* stuff in System V ascftime routine */
   54 #endif /* !HAVE_STRFTIME */
   55 
   56 /*
   57  * strftime --- produce formatted time
   58  */
   59 
   60 size_t
   61 my_strftime(
   62     char *s,
   63     size_t maxsize,
   64     const char *format,
   65     struct tm *timeptr)
   66 {
   67 #ifdef HAVE_STRFTIME
   68     return strftime(s, maxsize, format, timeptr);
   69 #else
   70     char *endp;
   71     char *start = s;
   72     char tbuf[100];
   73     int i;
   74 
   75     /*
   76      * various tables, useful in North America
   77      */
   78     static const char *days_a[] = {
   79         "Sun", "Mon", "Tue", "Wed",
   80         "Thu", "Fri", "Sat",
   81     };
   82     static const char *days_l[] = {
   83         "Sunday", "Monday", "Tuesday", "Wednesday",
   84         "Thursday", "Friday", "Saturday",
   85     };
   86     static const char *months_a[] = {
   87         "Jan", "Feb", "Mar", "Apr", "May", "Jun",
   88         "Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
   89     };
   90     static const char *months_l[] = {
   91         "January", "February", "March", "April",
   92         "May", "June", "July", "August", "September",
   93         "October", "November", "December",
   94     };
   95     static const char *ampm[] = { "AM", "PM", };
   96 
   97     if (s == NULL || format == NULL || timeptr == NULL || maxsize == 0)
   98         return 0;
   99 
  100     if (strchr(format, '%') == NULL && strlen(format) + 1 >= maxsize)
  101         return 0;
  102 
  103 #ifdef HAVE_TZSET
  104     tzset();
  105 #else
  106 #   ifdef HAVE_SETTZ
  107     settz();
  108 #   endif /* HAVE_SETTZ */
  109 #endif /* HAVE_TZSET */
  110 
  111     endp = s + maxsize;
  112     for (; *format && s < endp - 1; format++) {
  113         tbuf[0] = '\0';
  114         if (*format != '%') {
  115             *s++ = *format;
  116             continue;
  117         }
  118         switch (*++format) {
  119         case '\0':
  120             *s++ = '%';
  121             goto out;
  122 
  123         case '%':
  124             *s++ = '%';
  125             continue;
  126 
  127         case 'a':   /* abbreviated weekday name */
  128             strcpy(tbuf, days_a[timeptr->tm_wday]);
  129             break;
  130 
  131         case 'A':   /* full weekday name */
  132             strcpy(tbuf, days_l[timeptr->tm_wday]);
  133             break;
  134 
  135 #   ifdef SYSV_EXT
  136         case 'h':   /* abbreviated month name */
  137 #   endif /* SYSV_EXT */
  138         case 'b':   /* abbreviated month name */
  139             strcpy(tbuf, months_a[timeptr->tm_mon]);
  140             break;
  141 
  142         case 'B':   /* full month name */
  143             strcpy(tbuf, months_l[timeptr->tm_mon]);
  144             break;
  145 
  146         case 'c':   /* appropriate date and time representation */
  147             snprintf(tbuf, sizeof(tbuf), "%s %s %2d %02d:%02d:%02d %d",
  148                 days_a[timeptr->tm_wday],
  149                 months_a[timeptr->tm_mon],
  150                 timeptr->tm_mday,
  151                 timeptr->tm_hour,
  152                 timeptr->tm_min,
  153                 timeptr->tm_sec,
  154                 timeptr->tm_year + 1900);
  155             break;
  156 
  157         case 'd':   /* day of the month, 01 - 31 */
  158             snprintf(tbuf, sizeof(tbuf), "%02d", timeptr->tm_mday);
  159             break;
  160 
  161         case 'H':   /* hour, 24-hour clock, 00 - 23 */
  162             snprintf(tbuf, sizeof(tbuf), "%02d", timeptr->tm_hour);
  163             break;
  164 
  165         case 'I':   /* hour, 12-hour clock, 01 - 12 */
  166             i = timeptr->tm_hour;
  167             if (i == 0)
  168                 i = 12;
  169             else if (i > 12)
  170                 i -= 12;
  171             snprintf(tbuf, sizeof(tbuf), "%02d", i);
  172             break;
  173 
  174         case 'j':   /* day of the year, 001 - 366 */
  175             snprintf(tbuf, sizeof(tbuf), "%03d", timeptr->tm_yday + 1);
  176             break;
  177 
  178         case 'm':   /* month, 01 - 12 */
  179             snprintf(tbuf, sizeof(tbuf), "%02d", timeptr->tm_mon + 1);
  180             break;
  181 
  182         case 'M':   /* minute, 00 - 59 */
  183             snprintf(tbuf, sizeof(tbuf), "%02d", timeptr->tm_min);
  184             break;
  185 
  186         case 'p':   /* am or pm based on 12-hour clock */
  187             strcpy(tbuf, ampm[((timeptr->tm_hour < 12) ? 0 : 1)]);
  188             break;
  189 
  190         case 'S':   /* second, 00 - 61 */
  191             snprintf(tbuf, sizeof(tbuf), "%02d", timeptr->tm_sec);
  192             break;
  193 
  194         case 'w':   /* weekday, Sunday == 0, 0 - 6 */
  195             snprintf(tbuf, sizeof(tbuf), "%d", timeptr->tm_wday);
  196             break;
  197 
  198         case 'x':   /* appropriate date representation */
  199             snprintf(tbuf, sizeof(tbuf), "%s %s %2d %d",
  200                 days_a[timeptr->tm_wday],
  201                 months_a[timeptr->tm_mon],
  202                 timeptr->tm_mday,
  203                 timeptr->tm_year + 1900);
  204             break;
  205 
  206         case 'X':   /* appropriate time representation */
  207             snprintf(tbuf, sizeof(tbuf), "%02d:%02d:%02d",
  208                 timeptr->tm_hour,
  209                 timeptr->tm_min,
  210                 timeptr->tm_sec);
  211             break;
  212 
  213         case 'y':   /* year without a century, 00 - 99 */
  214             i = timeptr->tm_year % 100;
  215             snprintf(tbuf, sizeof(tbuf), "%d", i);
  216             break;
  217 
  218         case 'Y':   /* year with century */
  219             snprintf(tbuf, sizeof(tbuf), "%d", timeptr->tm_year + 1900);
  220             break;
  221 
  222 #   ifdef SYSV_EXT
  223         case 'n':   /* same as \n */
  224             tbuf[0] = '\n';
  225             tbuf[1] = '\0';
  226             break;
  227 
  228         case 't':   /* same as \t */
  229             tbuf[0] = '\t';
  230             tbuf[1] = '\0';
  231             break;
  232 
  233         case 'D':   /* date as %m/%d/%y */
  234             my_strftime(tbuf, sizeof(tbuf), "%m/%d/%y", timeptr);
  235             break;
  236 
  237         case 'e':   /* day of month, blank padded */
  238             snprintf(tbuf, sizeof(tbuf), "%2d", timeptr->tm_mday);
  239             break;
  240 
  241         case 'r':   /* time as %I:%M:%S %p */
  242             my_strftime(tbuf, sizeof(tbuf), "%I:%M:%S %p", timeptr);
  243             break;
  244 
  245         case 'R':   /* time as %H:%M */
  246             my_strftime(tbuf, sizeof(tbuf), "%H:%M", timeptr);
  247             break;
  248 
  249         case 'T':   /* time as %H:%M:%S */
  250             my_strftime(tbuf, sizeof(tbuf), "%H:%M:%S", timeptr);
  251             break;
  252 #   endif /* SYSV_EXT */
  253 
  254         default:
  255             tbuf[0] = '%';
  256             tbuf[1] = *format;
  257             tbuf[2] = '\0';
  258             break;
  259         }
  260         if ((i = strlen(tbuf))) {
  261             if (s + i < endp - 1) {
  262                 strcpy(s, tbuf);
  263                 s += i;
  264             } else
  265                 return 0;
  266         }
  267     }
  268 out:
  269     if (s < endp && *format == '\0') {
  270         *s = '\0';
  271         return (size_t) (s - start);
  272     } else
  273         return 0;
  274 
  275 #endif /* HAVE_STRFTIME */
  276 }