"Fossies" - the Fresh Open Source Software Archive

Member "tin-2.4.1/src/strftime.c" (12 Oct 2016, 7215 Bytes) of archive /linux/misc/tin-2.4.1.tar.gz:


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.0_vs_2.4.1.

    1 /*
    2  *  Project   : tin - a Usenet reader
    3  *  Module    : strftime.c
    4  *  Author    : A. Robbins & I. Lea
    5  *  Created   : 1991-02-01
    6  *  Updated   : 1993-08-15
    7  *  Notes     : Relatively quick-and-dirty implemenation 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-2017 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 = s + maxsize;
   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     for (; *format && s < endp - 1; format++) {
  112         tbuf[0] = '\0';
  113         if (*format != '%') {
  114             *s++ = *format;
  115             continue;
  116         }
  117         switch (*++format) {
  118         case '\0':
  119             *s++ = '%';
  120             goto out;
  121 
  122         case '%':
  123             *s++ = '%';
  124             continue;
  125 
  126         case 'a':   /* abbreviated weekday name */
  127             strcpy(tbuf, days_a[timeptr->tm_wday]);
  128             break;
  129 
  130         case 'A':   /* full weekday name */
  131             strcpy(tbuf, days_l[timeptr->tm_wday]);
  132             break;
  133 
  134 #   ifdef SYSV_EXT
  135         case 'h':   /* abbreviated month name */
  136 #   endif /* SYSV_EXT */
  137         case 'b':   /* abbreviated month name */
  138             strcpy(tbuf, months_a[timeptr->tm_mon]);
  139             break;
  140 
  141         case 'B':   /* full month name */
  142             strcpy(tbuf, months_l[timeptr->tm_mon]);
  143             break;
  144 
  145         case 'c':   /* appropriate date and time representation */
  146             snprintf(tbuf, sizeof(tbuf), "%s %s %2d %02d:%02d:%02d %d",
  147                 days_a[timeptr->tm_wday],
  148                 months_a[timeptr->tm_mon],
  149                 timeptr->tm_mday,
  150                 timeptr->tm_hour,
  151                 timeptr->tm_min,
  152                 timeptr->tm_sec,
  153                 timeptr->tm_year + 1900);
  154             break;
  155 
  156         case 'd':   /* day of the month, 01 - 31 */
  157             snprintf(tbuf, sizeof(tbuf), "%02d", timeptr->tm_mday);
  158             break;
  159 
  160         case 'H':   /* hour, 24-hour clock, 00 - 23 */
  161             snprintf(tbuf, sizeof(tbuf), "%02d", timeptr->tm_hour);
  162             break;
  163 
  164         case 'I':   /* hour, 12-hour clock, 01 - 12 */
  165             i = timeptr->tm_hour;
  166             if (i == 0)
  167                 i = 12;
  168             else if (i > 12)
  169                 i -= 12;
  170             snprintf(tbuf, sizeof(tbuf), "%02d", i);
  171             break;
  172 
  173         case 'j':   /* day of the year, 001 - 366 */
  174             snprintf(tbuf, sizeof(tbuf), "%03d", timeptr->tm_yday + 1);
  175             break;
  176 
  177         case 'm':   /* month, 01 - 12 */
  178             snprintf(tbuf, sizeof(tbuf), "%02d", timeptr->tm_mon + 1);
  179             break;
  180 
  181         case 'M':   /* minute, 00 - 59 */
  182             snprintf(tbuf, sizeof(tbuf), "%02d", timeptr->tm_min);
  183             break;
  184 
  185         case 'p':   /* am or pm based on 12-hour clock */
  186             strcpy(tbuf, ampm[((timeptr->tm_hour < 12) ? 0 : 1)]);
  187             break;
  188 
  189         case 'S':   /* second, 00 - 61 */
  190             snprintf(tbuf, sizeof(tbuf), "%02d", timeptr->tm_sec);
  191             break;
  192 
  193         case 'w':   /* weekday, Sunday == 0, 0 - 6 */
  194             snprintf(tbuf, sizeof(tbuf), "%d", timeptr->tm_wday);
  195             break;
  196 
  197         case 'x':   /* appropriate date representation */
  198             snprintf(tbuf, sizeof(tbuf), "%s %s %2d %d",
  199                 days_a[timeptr->tm_wday],
  200                 months_a[timeptr->tm_mon],
  201                 timeptr->tm_mday,
  202                 timeptr->tm_year + 1900);
  203             break;
  204 
  205         case 'X':   /* appropriate time representation */
  206             snprintf(tbuf, sizeof(tbuf), "%02d:%02d:%02d",
  207                 timeptr->tm_hour,
  208                 timeptr->tm_min,
  209                 timeptr->tm_sec);
  210             break;
  211 
  212         case 'y':   /* year without a century, 00 - 99 */
  213             i = timeptr->tm_year % 100;
  214             snprintf(tbuf, sizeof(tbuf), "%d", i);
  215             break;
  216 
  217         case 'Y':   /* year with century */
  218             snprintf(tbuf, sizeof(tbuf), "%d", timeptr->tm_year + 1900);
  219             break;
  220 
  221 #   ifdef SYSV_EXT
  222         case 'n':   /* same as \n */
  223             tbuf[0] = '\n';
  224             tbuf[1] = '\0';
  225             break;
  226 
  227         case 't':   /* same as \t */
  228             tbuf[0] = '\t';
  229             tbuf[1] = '\0';
  230             break;
  231 
  232         case 'D':   /* date as %m/%d/%y */
  233             my_strftime(tbuf, sizeof(tbuf), "%m/%d/%y", timeptr);
  234             break;
  235 
  236         case 'e':   /* day of month, blank padded */
  237             snprintf(tbuf, sizeof(tbuf), "%2d", timeptr->tm_mday);
  238             break;
  239 
  240         case 'r':   /* time as %I:%M:%S %p */
  241             my_strftime(tbuf, sizeof(tbuf), "%I:%M:%S %p", timeptr);
  242             break;
  243 
  244         case 'R':   /* time as %H:%M */
  245             my_strftime(tbuf, sizeof(tbuf), "%H:%M", timeptr);
  246             break;
  247 
  248         case 'T':   /* time as %H:%M:%S */
  249             my_strftime(tbuf, sizeof(tbuf), "%H:%M:%S", timeptr);
  250             break;
  251 #   endif /* SYSV_EXT */
  252 
  253         default:
  254             tbuf[0] = '%';
  255             tbuf[1] = *format;
  256             tbuf[2] = '\0';
  257             break;
  258         }
  259         if ((i = strlen(tbuf))) {
  260             if (s + i < endp - 1) {
  261                 strcpy(s, tbuf);
  262                 s += i;
  263             } else
  264                 return 0;
  265         }
  266     }
  267 out:
  268     if (s < endp && *format == '\0') {
  269         *s = '\0';
  270         return (size_t) (s - start);
  271     } else
  272         return 0;
  273 
  274 #endif /* HAVE_STRFTIME */
  275 }