home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume26 / strftime-3.0 / part01 / strftime.c < prev   
Encoding:
C/C++ Source or Header  |  1993-05-02  |  13.3 KB  |  592 lines

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