"Fossies" - the Fresh Open Source Software Archive

Member "tin-2.6.2/src/strftime.c" (9 Dec 2022, 7330 Bytes) of package /linux/misc/tin-2.6.2.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.6.1_vs_2.6.2.

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