home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume26 / strftime-2.0 / part01 / strftime.c < prev   
Encoding:
C/C++ Source or Header  |  1993-04-09  |  12.8 KB  |  566 lines

  1. /*
  2.  * strftime.c
  3.  *
  4.  * Public-domain relatively quick-and-dirty implemenation of
  5.  * ANSI library routine for System V Unix systems.
  6.  *
  7.  * It's written in old-style C for maximal portability.
  8.  * However, since I'm used to prototypes, I've included them too.
  9.  *
  10.  * If you want stuff in the System V ascftime routine, add the SYSV_EXT define.
  11.  * For stuff needed to implement the P1003.2 date command, add POSIX2_DATE.
  12.  * For complete POSIX semantics, add POSIX_SEMANTICS.
  13.  *
  14.  * The code for %c, %x, and %X is my best guess as to what's "appropriate".
  15.  * This version ignores LOCALE information.
  16.  * It also doesn't worry about multi-byte characters.
  17.  * So there.
  18.  *
  19.  * This file is also shipped with GAWK (GNU Awk), gawk specific bits of
  20.  * code are included if GAWK is defined.
  21.  *
  22.  * Arnold Robbins
  23.  * January, February, March, 1991
  24.  * Updated March, April 1992
  25.  *
  26.  * Fixes from ado@elsie.nci.nih.gov
  27.  * February 1991, May 1992
  28.  */
  29.  
  30. #ifndef GAWK
  31. #include <stdio.h>
  32. #include <ctype.h>
  33. #include <string.h>
  34. #include <time.h>
  35. #include <sys/types.h>
  36. #endif
  37.  
  38. /* defaults: season to taste */
  39. #define SYSV_EXT    1    /* stuff in System V ascftime routine */
  40. #define POSIX2_DATE    1    /* stuff in Posix 1003.2 date command */
  41. #define VMS_EXT        1    /* include %v for VMS date format */
  42. #ifndef GAWK
  43. #define POSIX_SEMANTICS    1    /* call tzset() if TZ changes */
  44. #endif
  45. #define    TZNAME_MISSING    1    /* needed on ultrix and maybe others */
  46.  
  47. #if defined(POSIX2_DATE) && ! defined(SYSV_EXT)
  48. #define SYSV_EXT    1
  49. #endif
  50.  
  51. #if defined(POSIX2_DATE)
  52. #define adddecl(stuff)    stuff
  53. #else
  54. #define adddecl(stuff)
  55. #endif
  56.  
  57. #ifndef __STDC__
  58. #define const    /**/
  59. extern void *malloc();
  60. extern void *realloc();
  61. extern void tzset();
  62. extern char *strchr();
  63. extern char *getenv();
  64. static int weeknumber();
  65. adddecl(static int iso8601wknum();)
  66. #else
  67. extern void *malloc(unsigned count);
  68. extern void *realloc(void *ptr, unsigned count);
  69. extern void tzset(void);
  70. extern char *strchr(const char *str, int ch);
  71. extern char *getenv(const char *v);
  72. static int weeknumber(const struct tm *timeptr, int firstweekday);
  73. adddecl(static int iso8601wknum(const struct tm *timeptr);)
  74. #endif
  75.  
  76. #ifdef __GNUC__
  77. #define inline    __inline__
  78. #else
  79. #define inline    /**/
  80. #endif
  81.  
  82. #define range(low, item, hi)    max(low, min(item, hi))
  83.  
  84. #if !defined(MSDOS) && !defined(TZNAME_MISSING)
  85. extern char *tzname[2];
  86. extern int daylight;
  87. #endif
  88.  
  89. /* min --- return minimum of two numbers */
  90.  
  91. #ifndef __STDC__
  92. static inline int
  93. min(a, b)
  94. int a, b;
  95. #else
  96. static inline int
  97. min(int a, int b)
  98. #endif
  99. {
  100.     return (a < b ? a : b);
  101. }
  102.  
  103. /* max --- return maximum of two numbers */
  104.  
  105. #ifndef __STDC__
  106. static inline int
  107. max(a, b)
  108. int a, b;
  109. #else
  110. static inline int
  111. max(int a, int b)
  112. #endif
  113. {
  114.     return (a > b ? a : b);
  115. }
  116.  
  117. /* strftime --- produce formatted time */
  118.  
  119. #ifndef __STDC__
  120. size_t
  121. strftime(s, maxsize, format, timeptr)
  122. char *s;
  123. size_t maxsize;
  124. const char *format;
  125. const struct tm *timeptr;
  126. #else
  127. size_t
  128. strftime(char *s, size_t maxsize, const char *format, const struct tm *timeptr)
  129. #endif
  130. {
  131.     char *endp = s + maxsize;
  132.     char *start = s;
  133.     char tbuf[100];
  134.     int i;
  135.     static short first = 1;
  136.     static char *savetz = NULL;
  137.     static int savetzlen = 0;
  138.     char *tz;
  139.  
  140.     /* various tables, useful in North America */
  141.     static char *days_a[] = {
  142.         "Sun", "Mon", "Tue", "Wed",
  143.         "Thu", "Fri", "Sat",
  144.     };
  145.     static char *days_l[] = {
  146.         "Sunday", "Monday", "Tuesday", "Wednesday",
  147.         "Thursday", "Friday", "Saturday",
  148.     };
  149.     static char *months_a[] = {
  150.         "Jan", "Feb", "Mar", "Apr", "May", "Jun",
  151.         "Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
  152.     };
  153.     static char *months_l[] = {
  154.         "January", "February", "March", "April",
  155.         "May", "June", "July", "August", "September",
  156.         "October", "November", "December",
  157.     };
  158.     static char *ampm[] = { "AM", "PM", };
  159.  
  160.     if (s == NULL || format == NULL || timeptr == NULL || maxsize == 0)
  161.         return 0;
  162.  
  163.     if (strchr(format, '%') == NULL && strlen(format) + 1 >= maxsize)
  164.         return 0;
  165.  
  166. #ifndef POSIX_SEMANTICS
  167.     if (first) {
  168.         tzset();
  169.         first = 0;
  170.     }
  171. #else    /* POSIX_SEMANTICS */
  172.     tz = getenv("TZ");
  173.     if (first) {
  174.         if (tz != NULL) {
  175.             int tzlen = strlen(tz);
  176.  
  177.             savetz = (char *) malloc(tzlen + 1);
  178.             if (savetz != NULL) {
  179.                 savetzlen = tzlen + 1;
  180.                 strcpy(savetz, tz);
  181.             }
  182.         }
  183.         tzset();
  184.         first = 0;
  185.     }
  186.     /* if we have a saved TZ, and it is different, recapture and reset */
  187.     if (tz && savetz && (tz[0] != savetz[0] || strcmp(tz, savetz) != 0)) {
  188.         i = strlen(tz) + 1;
  189.         if (i > savetzlen) {
  190.             savetz = (char *) realloc(savetz, i);
  191.             if (savetz) {
  192.                 savetzlen = i;
  193.                 strcpy(savetz, tz);
  194.             }
  195.         } else
  196.             strcpy(savetz, tz);
  197.         tzset();
  198.     }
  199. #endif    /* POSIX_SEMANTICS */
  200.  
  201.     for (; *format && s < endp - 1; format++) {
  202.         tbuf[0] = '\0';
  203.         if (*format != '%') {
  204.             *s++ = *format;
  205.             continue;
  206.         }
  207.     again:
  208.         switch (*++format) {
  209.         case '\0':
  210.             *s++ = '%';
  211.             goto out;
  212.  
  213.         case '%':
  214.             *s++ = '%';
  215.             continue;
  216.  
  217.         case 'a':    /* abbreviated weekday name */
  218.             if (timeptr->tm_wday < 0 || timeptr->tm_wday > 6)
  219.                 strcpy(tbuf, "?");
  220.             else
  221.                 strcpy(tbuf, days_a[timeptr->tm_wday]);
  222.             break;
  223.  
  224.         case 'A':    /* full weekday name */
  225.             if (timeptr->tm_wday < 0 || timeptr->tm_wday > 6)
  226.                 strcpy(tbuf, "?");
  227.             else
  228.                 strcpy(tbuf, days_l[timeptr->tm_wday]);
  229.             break;
  230.  
  231. #ifdef SYSV_EXT
  232.         case 'h':    /* abbreviated month name */
  233. #endif
  234.         case 'b':    /* abbreviated month name */
  235.             if (timeptr->tm_mon < 0 || timeptr->tm_mon > 11)
  236.                 strcpy(tbuf, "?");
  237.             else
  238.                 strcpy(tbuf, months_a[timeptr->tm_mon]);
  239.             break;
  240.  
  241.         case 'B':    /* full month name */
  242.             if (timeptr->tm_mon < 0 || timeptr->tm_mon > 11)
  243.                 strcpy(tbuf, "?");
  244.             else
  245.                 strcpy(tbuf, months_l[timeptr->tm_mon]);
  246.             break;
  247.  
  248.         case 'c':    /* appropriate date and time representation */
  249.             sprintf(tbuf, "%s %s %2d %02d:%02d:%02d %d",
  250.                 days_a[range(0, timeptr->tm_wday, 6)],
  251.                 months_a[range(0, timeptr->tm_mon, 11)],
  252.                 range(1, timeptr->tm_mday, 31),
  253.                 range(0, timeptr->tm_hour, 23),
  254.                 range(0, timeptr->tm_min, 59),
  255.                 range(0, timeptr->tm_sec, 61),
  256.                 timeptr->tm_year + 1900);
  257.             break;
  258.  
  259.         case 'd':    /* day of the month, 01 - 31 */
  260.             i = range(1, timeptr->tm_mday, 31);
  261.             sprintf(tbuf, "%02d", i);
  262.             break;
  263.  
  264.         case 'H':    /* hour, 24-hour clock, 00 - 23 */
  265.             i = range(0, timeptr->tm_hour, 23);
  266.             sprintf(tbuf, "%02d", i);
  267.             break;
  268.  
  269.         case 'I':    /* hour, 12-hour clock, 01 - 12 */
  270.             i = range(0, timeptr->tm_hour, 23);
  271.             if (i == 0)
  272.                 i = 12;
  273.             else if (i > 12)
  274.                 i -= 12;
  275.             sprintf(tbuf, "%02d", i);
  276.             break;
  277.  
  278.         case 'j':    /* day of the year, 001 - 366 */
  279.             sprintf(tbuf, "%03d", timeptr->tm_yday + 1);
  280.             break;
  281.  
  282.         case 'm':    /* month, 01 - 12 */
  283.             i = range(0, timeptr->tm_mon, 11);
  284.             sprintf(tbuf, "%02d", i + 1);
  285.             break;
  286.  
  287.         case 'M':    /* minute, 00 - 59 */
  288.             i = range(0, timeptr->tm_min, 59);
  289.             sprintf(tbuf, "%02d", i);
  290.             break;
  291.  
  292.         case 'p':    /* am or pm based on 12-hour clock */
  293.             i = range(0, timeptr->tm_hour, 23);
  294.             if (i < 12)
  295.                 strcpy(tbuf, ampm[0]);
  296.             else
  297.                 strcpy(tbuf, ampm[1]);
  298.             break;
  299.  
  300.         case 'S':    /* second, 00 - 61 */
  301.             i = range(0, timeptr->tm_sec, 61);
  302.             sprintf(tbuf, "%02d", i);
  303.             break;
  304.  
  305.         case 'U':    /* week of year, Sunday is first day of week */
  306.             sprintf(tbuf, "%d", weeknumber(timeptr, 0));
  307.             break;
  308.  
  309.         case 'w':    /* weekday, Sunday == 0, 0 - 6 */
  310.             i = range(0, timeptr->tm_wday, 6);
  311.             sprintf(tbuf, "%d", i);
  312.             break;
  313.  
  314.         case 'W':    /* week of year, Monday is first day of week */
  315.             sprintf(tbuf, "%d", weeknumber(timeptr, 1));
  316.             break;
  317.  
  318.         case 'x':    /* appropriate date representation */
  319.             sprintf(tbuf, "%s %s %2d %d",
  320.                 days_a[range(0, timeptr->tm_wday, 6)],
  321.                 months_a[range(0, timeptr->tm_mon, 11)],
  322.                 range(1, timeptr->tm_mday, 31),
  323.                 timeptr->tm_year + 1900);
  324.             break;
  325.  
  326.         case 'X':    /* appropriate time representation */
  327.             sprintf(tbuf, "%02d:%02d:%02d",
  328.                 range(0, timeptr->tm_hour, 23),
  329.                 range(0, timeptr->tm_min, 59),
  330.                 range(0, timeptr->tm_sec, 61));
  331.             break;
  332.  
  333.         case 'y':    /* year without a century, 00 - 99 */
  334.             i = timeptr->tm_year % 100;
  335.             sprintf(tbuf, "%d", i);
  336.             break;
  337.  
  338.         case 'Y':    /* year with century */
  339.             sprintf(tbuf, "%d", 1900 + timeptr->tm_year);
  340.             break;
  341.  
  342.         case 'Z':    /* time zone name or abbrevation */
  343.             i = 0;
  344.             if (
  345. #ifndef TZNAME_MISSING
  346.                 daylight &&
  347. #endif
  348.                 timeptr->tm_isdst)
  349.                 i = 1;
  350. #ifdef TZNAME_MISSING
  351.             strcpy(tbuf, timeptr->tm_zone);
  352. #else
  353.             strcpy(tbuf, tzname[i]);
  354. #endif
  355.             break;
  356.  
  357. #ifdef SYSV_EXT
  358.         case 'n':    /* same as \n */
  359.             tbuf[0] = '\n';
  360.             tbuf[1] = '\0';
  361.             break;
  362.  
  363.         case 't':    /* same as \t */
  364.             tbuf[0] = '\t';
  365.             tbuf[1] = '\0';
  366.             break;
  367.  
  368.         case 'D':    /* date as %m/%d/%y */
  369.             strftime(tbuf, sizeof tbuf, "%m/%d/%y", timeptr);
  370.             break;
  371.  
  372.         case 'e':    /* day of month, blank padded */
  373.             sprintf(tbuf, "%2d", range(1, timeptr->tm_mday, 31));
  374.             break;
  375.  
  376.         case 'r':    /* time as %I:%M:%S %p */
  377.             strftime(tbuf, sizeof tbuf, "%I:%M:%S %p", timeptr);
  378.             break;
  379.  
  380.         case 'R':    /* time as %H:%M */
  381.             strftime(tbuf, sizeof tbuf, "%H:%M", timeptr);
  382.             break;
  383.  
  384.         case 'T':    /* time as %H:%M:%S */
  385.             strftime(tbuf, sizeof tbuf, "%H:%M:%S", timeptr);
  386.             break;
  387. #endif
  388.  
  389.  
  390. #ifdef VMS_EXT
  391.         case 'v':    /* date as dd-bbb-YYYY */
  392.             sprintf(tbuf, "%2d-%3.3s-%4d",
  393.                 range(1, timeptr->tm_mday, 31),
  394.                 months_a[range(0, timeptr->tm_mon, 11)],
  395.                 timeptr->tm_year + 1900);
  396.             for (i = 3; i < 6; i++)
  397.                 if (islower(tbuf[i]))
  398.                     tbuf[i] = toupper(tbuf[i]);
  399.             break;
  400. #endif
  401.  
  402.  
  403. #ifdef POSIX2_DATE
  404.         case 'C':
  405.             sprintf(tbuf, "%02d", (timeptr->tm_year + 1900) / 100);
  406.             break;
  407.  
  408.  
  409.         case 'E':
  410.         case 'O':
  411.             /* POSIX locale extensions, ignored for now */
  412.             goto again;
  413.  
  414.         case 'V':    /* week of year according ISO 8601 */
  415. #if defined(GAWK) && defined(VMS_EXT)
  416.         {
  417.             extern int do_lint;
  418.             extern void warning();
  419.             static int warned = 0;
  420.  
  421.             if (! warned && do_lint) {
  422.                 warned = 1;
  423.                 warning(
  424.     "conversion %%V added in P1003.2/11.3; for VMS style date, use %%v");
  425.             }
  426.         }
  427. #endif
  428.             sprintf(tbuf, "%d", iso8601wknum(timeptr));
  429.             break;
  430.  
  431.         case 'u':
  432.         /* ISO 8601: Weekday as a decimal number [1 (Monday) - 7] */
  433.             sprintf(tbuf, "%d", timeptr->tm_wday == 0 ? 7 :
  434.                     timeptr->tm_wday);
  435.             break;
  436. #endif    /* POSIX2_DATE */
  437.         default:
  438.             tbuf[0] = '%';
  439.             tbuf[1] = *format;
  440.             tbuf[2] = '\0';
  441.             break;
  442.         }
  443.         i = strlen(tbuf);
  444.         if (i)
  445.             if (s + i < endp - 1) {
  446.                 strcpy(s, tbuf);
  447.                 s += i;
  448.             } else
  449.                 return 0;
  450.     }
  451. out:
  452.     if (s < endp && *format == '\0') {
  453.         *s = '\0';
  454.         return (s - start);
  455.     } else
  456.         return 0;
  457. }
  458.  
  459. #ifdef POSIX2_DATE
  460. /* iso8601wknum --- compute week number according to ISO 8601 */
  461.  
  462. #ifndef __STDC__
  463. static int
  464. iso8601wknum(timeptr)
  465. const struct tm *timeptr;
  466. #else
  467. static int
  468. iso8601wknum(const struct tm *timeptr)
  469. #endif
  470. {
  471.     /*
  472.      * From 1003.2 D11.3:
  473.      *    If the week (Monday to Sunday) containing January 1
  474.      *    has four or more days in the new year, then it is week 1;
  475.      *    otherwise it is week 53 of the previous year, and the
  476.      *    next week is week 1.
  477.      *
  478.      * ADR: This means if Jan 1 was Monday through Thursday,
  479.      *    it was week 1, otherwise week 53.
  480.      */
  481.  
  482.     int simple_wknum, jan1day, diff, ret;
  483.  
  484.     /* get week number, Monday as first day of the week */
  485.     simple_wknum = weeknumber(timeptr, 1) + 1;
  486.  
  487.     /*
  488.      * With thanks and tip of the hatlo to ado@elsie.nci.nih.gov
  489.      *
  490.      * What day of the week does January 1 fall on?
  491.      * We know that
  492.      *    (timeptr->tm_yday - jan1.tm_yday) MOD 7 ==
  493.      *        (timeptr->tm_wday - jan1.tm_wday) MOD 7
  494.      * and that
  495.      *     jan1.tm_yday == 1
  496.      * and that
  497.      *     timeptr->tm_wday MOD 7 == timeptr->tm_wday
  498.      * from which it follows that. . .
  499.       */
  500.     jan1day = (timeptr->tm_yday - 1) % 7 - timeptr->tm_wday;
  501.     if (jan1day < 0)
  502.         jan1day += 7;
  503.  
  504.     /*
  505.      * If Jan 1 was a Monday through Thursday, it was in
  506.      * week 1.  Otherwise it was last year's week 53, which is
  507.      * this year's week 0.
  508.      */
  509.     if (jan1day >= 1 && jan1day <= 4)
  510.         diff = 0;
  511.     else
  512.         diff = 1;
  513.     ret = simple_wknum - diff;
  514.     if (ret == 0)    /* we're in the first week of the year */
  515.         ret = 53;
  516.     return ret;
  517. }
  518. #endif
  519.  
  520. /* weeknumber --- figure how many weeks into the year */
  521.  
  522. /* With thanks and tip of the hatlo to ado@elsie.nci.nih.gov */
  523.  
  524. #ifndef __STDC__
  525. static int
  526. weeknumber(timeptr, firstweekday)
  527. const struct tm *timeptr;
  528. int firstweekday;
  529. #else
  530. static int
  531. weeknumber(const struct tm *timeptr, int firstweekday)
  532. #endif
  533. {
  534.     if (firstweekday == 0)
  535.         return (timeptr->tm_yday + 7 - timeptr->tm_wday) / 7;
  536.     else
  537.         return (timeptr->tm_yday + 7 -
  538.             (timeptr->tm_wday ? (timeptr->tm_wday - 1) : 6)) / 7;
  539. }
  540.  
  541. #if 0
  542. /* ADR --- I'm loathe to mess with ado's code ... */
  543.  
  544. Date:         Wed, 24 Apr 91 20:54:08 MDT
  545. From: Michal Jaegermann <audfax!emory!vm.ucs.UAlberta.CA!NTOMCZAK>
  546. To: arnold@audiofax.com
  547.  
  548. Hi Arnold,
  549. in a process of fixing of strftime() in libraries on Atari ST I grabbed
  550. some pieces of code from your own strftime.  When doing that it came
  551. to mind that your weeknumber() function compiles a little bit nicer
  552. in the following form:
  553. /*
  554.  * firstweekday is 0 if starting in Sunday, non-zero if in Monday
  555.  */
  556. {
  557.     return (timeptr->tm_yday - timeptr->tm_wday +
  558.         (firstweekday ? (timeptr->tm_wday ? 8 : 1) : 7)) / 7;
  559. }
  560. How nicer it depends on a compiler, of course, but always a tiny bit.
  561.  
  562.    Cheers,
  563.    Michal
  564.    ntomczak@vm.ucs.ualberta.ca
  565. #endif
  566.