home *** CD-ROM | disk | FTP | other *** search
/ RISC DISC 2 / RISC_DISC_2.iso / pd_share / program / gnu / gawk / missing / c / strftime < prev    next >
Encoding:
Text File  |  1994-01-04  |  16.7 KB  |  708 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 April, 1993
  27.  *
  28.  * Fixes from ado@elsie.nci.nih.gov
  29.  * February 1991, May 1992
  30.  * Fixes from Tor Lillqvist tor@tik.vtt.fi
  31.  * May, 1993
  32.  */
  33.  
  34. #ifndef GAWK
  35. #include <stdio.h>
  36. #include <ctype.h>
  37. #include <string.h>
  38. #include <time.h>
  39. #include <sys/types.h>
  40. #endif
  41.  
  42. /* defaults: season to taste */
  43. #define SYSV_EXT    1    /* stuff in System V ascftime routine */
  44. #define SUNOS_EXT    1    /* stuff in SunOS strftime routine */
  45. #define POSIX2_DATE    1    /* stuff in Posix 1003.2 date command */
  46. #define VMS_EXT        1    /* include %v for VMS date format */
  47. #ifndef GAWK
  48. #define POSIX_SEMANTICS    1    /* call tzset() if TZ changes */
  49. #endif
  50.  
  51. #if defined(POSIX2_DATE)
  52. #if ! defined(SYSV_EXT)
  53. #define SYSV_EXT    1
  54. #endif
  55. #if ! defined(SUNOS_EXT)
  56. #define SUNOS_EXT    1
  57. #endif
  58. #endif
  59.  
  60. #if defined(POSIX2_DATE)
  61. #define adddecl(stuff)    stuff
  62. #else
  63. #define adddecl(stuff)
  64. #endif
  65.  
  66. #undef strchr    /* avoid AIX weirdness */
  67.  
  68. #ifndef __STDC__
  69. #define const    /**/
  70. extern void *malloc();
  71. extern void *realloc();
  72. extern void tzset();
  73. extern char *strchr();
  74. extern char *getenv();
  75. static int weeknumber();
  76. adddecl(static int iso8601wknum();)
  77. #else
  78. extern void *malloc(unsigned count);
  79. extern void *realloc(void *ptr, unsigned count);
  80. extern void tzset(void);
  81. extern char *strchr(const char *str, int ch);
  82. extern char *getenv(const char *v);
  83. static int weeknumber(const struct tm *timeptr, int firstweekday);
  84. adddecl(static int iso8601wknum(const struct tm *timeptr);)
  85. #endif
  86.  
  87. #ifdef __GNUC__
  88. #define inline    __inline__
  89. #else
  90. #define inline    /**/
  91. #endif
  92.  
  93. #define range(low, item, hi)    max(low, min(item, hi))
  94.  
  95. #if !defined(OS2) && !defined(MSDOS) && !defined(TZNAME_MISSING)
  96. extern char *tzname[2];
  97. extern int daylight;
  98. #endif
  99.  
  100. /* min --- return minimum of two numbers */
  101.  
  102. #ifndef __STDC__
  103. static inline int
  104. min(a, b)
  105. int a, b;
  106. #else
  107. static inline int
  108. min(int a, int b)
  109. #endif
  110. {
  111.     return (a < b ? a : b);
  112. }
  113.  
  114. /* max --- return maximum of two numbers */
  115.  
  116. #ifndef __STDC__
  117. static inline int
  118. max(a, b)
  119. int a, b;
  120. #else
  121. static inline int
  122. max(int a, int b)
  123. #endif
  124. {
  125.     return (a > b ? a : b);
  126. }
  127.  
  128. /* strftime --- produce formatted time */
  129.  
  130. #ifndef __STDC__
  131. size_t
  132. strftime(s, maxsize, format, timeptr)
  133. char *s;
  134. size_t maxsize;
  135. const char *format;
  136. const struct tm *timeptr;
  137. #else
  138. size_t
  139. strftime(char *s, size_t maxsize, const char *format, const struct tm *timeptr)
  140. #endif
  141. {
  142.     char *endp = s + maxsize;
  143.     char *start = s;
  144.     char tbuf[100];
  145.     int i;
  146.     static short first = 1;
  147. #ifdef POSIX_SEMANTICS
  148.     static char *savetz = NULL;
  149.     static int savetzlen = 0;
  150.     char *tz;
  151. #endif /* POSIX_SEMANTICS */
  152.  
  153.     /* various tables, useful in North America */
  154.     static const char *days_a[] = {
  155.         "Sun", "Mon", "Tue", "Wed",
  156.         "Thu", "Fri", "Sat",
  157.     };
  158.     static const char *days_l[] = {
  159.         "Sunday", "Monday", "Tuesday", "Wednesday",
  160.         "Thursday", "Friday", "Saturday",
  161.     };
  162.     static const char *months_a[] = {
  163.         "Jan", "Feb", "Mar", "Apr", "May", "Jun",
  164.         "Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
  165.     };
  166.     static const char *months_l[] = {
  167.         "January", "February", "March", "April",
  168.         "May", "June", "July", "August", "September",
  169.         "October", "November", "December",
  170.     };
  171.     static const char *ampm[] = { "AM", "PM", };
  172.  
  173.     if (s == NULL || format == NULL || timeptr == NULL || maxsize == 0)
  174.         return 0;
  175.  
  176.     if (strchr(format, '%') == NULL && strlen(format) + 1 >= maxsize)
  177.         return 0;
  178.  
  179. #ifndef POSIX_SEMANTICS
  180.     if (first) {
  181.         tzset();
  182.         first = 0;
  183.     }
  184. #else    /* POSIX_SEMANTICS */
  185.     tz = getenv("TZ");
  186.     if (first) {
  187.         if (tz != NULL) {
  188.             int tzlen = strlen(tz);
  189.  
  190.             savetz = (char *) malloc(tzlen + 1);
  191.             if (savetz != NULL) {
  192.                 savetzlen = tzlen + 1;
  193.                 strcpy(savetz, tz);
  194.             }
  195.         }
  196.         tzset();
  197.         first = 0;
  198.     }
  199.     /* if we have a saved TZ, and it is different, recapture and reset */
  200.     if (tz && savetz && (tz[0] != savetz[0] || strcmp(tz, savetz) != 0)) {
  201.         i = strlen(tz) + 1;
  202.         if (i > savetzlen) {
  203.             savetz = (char *) realloc(savetz, i);
  204.             if (savetz) {
  205.                 savetzlen = i;
  206.                 strcpy(savetz, tz);
  207.             }
  208.         } else
  209.             strcpy(savetz, tz);
  210.         tzset();
  211.     }
  212. #endif    /* POSIX_SEMANTICS */
  213.  
  214.     for (; *format && s < endp - 1; format++) {
  215.         tbuf[0] = '\0';
  216.         if (*format != '%') {
  217.             *s++ = *format;
  218.             continue;
  219.         }
  220.     again:
  221.         switch (*++format) {
  222.         case '\0':
  223.             *s++ = '%';
  224.             goto out;
  225.  
  226.         case '%':
  227.             *s++ = '%';
  228.             continue;
  229.  
  230.         case 'a':    /* abbreviated weekday name */
  231.             if (timeptr->tm_wday < 0 || timeptr->tm_wday > 6)
  232.                 strcpy(tbuf, "?");
  233.             else
  234.                 strcpy(tbuf, days_a[timeptr->tm_wday]);
  235.             break;
  236.  
  237.         case 'A':    /* full weekday name */
  238.             if (timeptr->tm_wday < 0 || timeptr->tm_wday > 6)
  239.                 strcpy(tbuf, "?");
  240.             else
  241.                 strcpy(tbuf, days_l[timeptr->tm_wday]);
  242.             break;
  243.  
  244. #ifdef SYSV_EXT
  245.         case 'h':    /* abbreviated month name */
  246. #endif
  247.         case 'b':    /* abbreviated month name */
  248.             if (timeptr->tm_mon < 0 || timeptr->tm_mon > 11)
  249.                 strcpy(tbuf, "?");
  250.             else
  251.                 strcpy(tbuf, months_a[timeptr->tm_mon]);
  252.             break;
  253.  
  254.         case 'B':    /* full month name */
  255.             if (timeptr->tm_mon < 0 || timeptr->tm_mon > 11)
  256.                 strcpy(tbuf, "?");
  257.             else
  258.                 strcpy(tbuf, months_l[timeptr->tm_mon]);
  259.             break;
  260.  
  261.         case 'c':    /* appropriate date and time representation */
  262.             sprintf(tbuf, "%s %s %2d %02d:%02d:%02d %d",
  263.                 days_a[range(0, timeptr->tm_wday, 6)],
  264.                 months_a[range(0, timeptr->tm_mon, 11)],
  265.                 range(1, timeptr->tm_mday, 31),
  266.                 range(0, timeptr->tm_hour, 23),
  267.                 range(0, timeptr->tm_min, 59),
  268.                 range(0, timeptr->tm_sec, 61),
  269.                 timeptr->tm_year + 1900);
  270.             break;
  271.  
  272.         case 'd':    /* day of the month, 01 - 31 */
  273.             i = range(1, timeptr->tm_mday, 31);
  274.             sprintf(tbuf, "%02d", i);
  275.             break;
  276.  
  277.         case 'H':    /* hour, 24-hour clock, 00 - 23 */
  278.             i = range(0, timeptr->tm_hour, 23);
  279.             sprintf(tbuf, "%02d", i);
  280.             break;
  281.  
  282.         case 'I':    /* hour, 12-hour clock, 01 - 12 */
  283.             i = range(0, timeptr->tm_hour, 23);
  284.             if (i == 0)
  285.                 i = 12;
  286.             else if (i > 12)
  287.                 i -= 12;
  288.             sprintf(tbuf, "%02d", i);
  289.             break;
  290.  
  291.         case 'j':    /* day of the year, 001 - 366 */
  292.             sprintf(tbuf, "%03d", timeptr->tm_yday + 1);
  293.             break;
  294.  
  295.         case 'm':    /* month, 01 - 12 */
  296.             i = range(0, timeptr->tm_mon, 11);
  297.             sprintf(tbuf, "%02d", i + 1);
  298.             break;
  299.  
  300.         case 'M':    /* minute, 00 - 59 */
  301.             i = range(0, timeptr->tm_min, 59);
  302.             sprintf(tbuf, "%02d", i);
  303.             break;
  304.  
  305.         case 'p':    /* am or pm based on 12-hour clock */
  306.             i = range(0, timeptr->tm_hour, 23);
  307.             if (i < 12)
  308.                 strcpy(tbuf, ampm[0]);
  309.             else
  310.                 strcpy(tbuf, ampm[1]);
  311.             break;
  312.  
  313.         case 'S':    /* second, 00 - 61 */
  314.             i = range(0, timeptr->tm_sec, 61);
  315.             sprintf(tbuf, "%02d", i);
  316.             break;
  317.  
  318.         case 'U':    /* week of year, Sunday is first day of week */
  319.             sprintf(tbuf, "%d", weeknumber(timeptr, 0));
  320.             break;
  321.  
  322.         case 'w':    /* weekday, Sunday == 0, 0 - 6 */
  323.             i = range(0, timeptr->tm_wday, 6);
  324.             sprintf(tbuf, "%d", i);
  325.             break;
  326.  
  327.         case 'W':    /* week of year, Monday is first day of week */
  328.             sprintf(tbuf, "%d", weeknumber(timeptr, 1));
  329.             break;
  330.  
  331.         case 'x':    /* appropriate date representation */
  332.             sprintf(tbuf, "%s %s %2d %d",
  333.                 days_a[range(0, timeptr->tm_wday, 6)],
  334.                 months_a[range(0, timeptr->tm_mon, 11)],
  335.                 range(1, timeptr->tm_mday, 31),
  336.                 timeptr->tm_year + 1900);
  337.             break;
  338.  
  339.         case 'X':    /* appropriate time representation */
  340.             sprintf(tbuf, "%02d:%02d:%02d",
  341.                 range(0, timeptr->tm_hour, 23),
  342.                 range(0, timeptr->tm_min, 59),
  343.                 range(0, timeptr->tm_sec, 61));
  344.             break;
  345.  
  346.         case 'y':    /* year without a century, 00 - 99 */
  347.             i = timeptr->tm_year % 100;
  348.             sprintf(tbuf, "%d", i);
  349.             break;
  350.  
  351.         case 'Y':    /* year with century */
  352.             sprintf(tbuf, "%d", 1900 + timeptr->tm_year);
  353.             break;
  354.  
  355.         case 'Z':    /* time zone name or abbrevation */
  356.             i = 0;
  357.             if (
  358. #ifndef TZNAME_MISSING
  359.                 daylight &&
  360. #endif
  361.                 timeptr->tm_isdst)
  362.                 i = 1;
  363. #ifdef TZNAME_MISSING
  364.             strcpy(tbuf, timeptr->tm_zone);
  365. #else
  366.             strcpy(tbuf, tzname[i]);
  367. #endif
  368.             break;
  369.  
  370. #ifdef SYSV_EXT
  371.         case 'n':    /* same as \n */
  372.             tbuf[0] = '\n';
  373.             tbuf[1] = '\0';
  374.             break;
  375.  
  376.         case 't':    /* same as \t */
  377.             tbuf[0] = '\t';
  378.             tbuf[1] = '\0';
  379.             break;
  380.  
  381.         case 'D':    /* date as %m/%d/%y */
  382.             strftime(tbuf, sizeof tbuf, "%m/%d/%y", timeptr);
  383.             break;
  384.  
  385.         case 'e':    /* day of month, blank padded */
  386.             sprintf(tbuf, "%2d", range(1, timeptr->tm_mday, 31));
  387.             break;
  388.  
  389.         case 'r':    /* time as %I:%M:%S %p */
  390.             strftime(tbuf, sizeof tbuf, "%I:%M:%S %p", timeptr);
  391.             break;
  392.  
  393.         case 'R':    /* time as %H:%M */
  394.             strftime(tbuf, sizeof tbuf, "%H:%M", timeptr);
  395.             break;
  396.  
  397.         case 'T':    /* time as %H:%M:%S */
  398.             strftime(tbuf, sizeof tbuf, "%H:%M:%S", timeptr);
  399.             break;
  400. #endif
  401.  
  402. #ifdef SUNOS_EXT
  403.         case 'k':    /* hour, 24-hour clock, blank pad */
  404.             sprintf(tbuf, "%2d", range(0, timeptr->tm_hour, 23));
  405.             break;
  406.  
  407.         case 'l':    /* hour, 12-hour clock, 1 - 12, blank pad */
  408.             i = range(0, timeptr->tm_hour, 23);
  409.             if (i == 0)
  410.                 i = 12;
  411.             else if (i > 12)
  412.                 i -= 12;
  413.             sprintf(tbuf, "%2d", i);
  414.             break;
  415. #endif
  416.  
  417.  
  418. #ifdef VMS_EXT
  419.         case 'v':    /* date as dd-bbb-YYYY */
  420.             sprintf(tbuf, "%2d-%3.3s-%4d",
  421.                 range(1, timeptr->tm_mday, 31),
  422.                 months_a[range(0, timeptr->tm_mon, 11)],
  423.                 timeptr->tm_year + 1900);
  424.             for (i = 3; i < 6; i++)
  425.                 if (islower(tbuf[i]))
  426.                     tbuf[i] = toupper(tbuf[i]);
  427.             break;
  428. #endif
  429.  
  430.  
  431. #ifdef POSIX2_DATE
  432.         case 'C':
  433.             sprintf(tbuf, "%02d", (timeptr->tm_year + 1900) / 100);
  434.             break;
  435.  
  436.  
  437.         case 'E':
  438.         case 'O':
  439.             /* POSIX locale extensions, ignored for now */
  440.             goto again;
  441.  
  442.         case 'V':    /* week of year according ISO 8601 */
  443. #if defined(GAWK) && defined(VMS_EXT)
  444.         {
  445.             extern int do_lint;
  446.             extern void warning();
  447.             static int warned = 0;
  448.  
  449.             if (! warned && do_lint) {
  450.                 warned = 1;
  451.                 warning(
  452.     "conversion %%V added in P1003.2/11.3; for VMS style date, use %%v");
  453.             }
  454.         }
  455. #endif
  456.             sprintf(tbuf, "%d", iso8601wknum(timeptr));
  457.             break;
  458.  
  459.         case 'u':
  460.         /* ISO 8601: Weekday as a decimal number [1 (Monday) - 7] */
  461.             sprintf(tbuf, "%d", timeptr->tm_wday == 0 ? 7 :
  462.                     timeptr->tm_wday);
  463.             break;
  464. #endif    /* POSIX2_DATE */
  465.         default:
  466.             tbuf[0] = '%';
  467.             tbuf[1] = *format;
  468.             tbuf[2] = '\0';
  469.             break;
  470.         }
  471.         i = strlen(tbuf);
  472.         if (i)
  473.             if (s + i < endp - 1) {
  474.                 strcpy(s, tbuf);
  475.                 s += i;
  476.             } else
  477.                 return 0;
  478.     }
  479. out:
  480.     if (s < endp && *format == '\0') {
  481.         *s = '\0';
  482.         return (s - start);
  483.     } else
  484.         return 0;
  485. }
  486.  
  487. #ifdef POSIX2_DATE
  488. /* iso8601wknum --- compute week number according to ISO 8601 */
  489.  
  490. #ifndef __STDC__
  491. static int
  492. iso8601wknum(timeptr)
  493. const struct tm *timeptr;
  494. #else
  495. static int
  496. iso8601wknum(const struct tm *timeptr)
  497. #endif
  498. {
  499.     /*
  500.      * From 1003.2 D11.3:
  501.      *    If the week (Monday to Sunday) containing January 1
  502.      *    has four or more days in the new year, then it is week 1;
  503.      *    otherwise it is week 53 of the previous year, and the
  504.      *    next week is week 1.
  505.      *
  506.      * ADR: This means if Jan 1 was Monday through Thursday,
  507.      *    it was week 1, otherwise week 53.
  508.      */
  509.  
  510.     int simple_wknum, jan1day, diff, ret;
  511.  
  512.     /* get week number, Monday as first day of the week */
  513.     simple_wknum = weeknumber(timeptr, 1) + 1;
  514.  
  515.     /*
  516.      * With thanks and tip of the hatlo to tml@tik.vtt.fi
  517.      *
  518.      * What day of the week does January 1 fall on?
  519.      * We know that
  520.      *    (timeptr->tm_yday - jan1.tm_yday) MOD 7 ==
  521.      *        (timeptr->tm_wday - jan1.tm_wday) MOD 7
  522.      * and that
  523.      *     jan1.tm_yday == 0
  524.      * and that
  525.      *     timeptr->tm_wday MOD 7 == timeptr->tm_wday
  526.      * from which it follows that. . .
  527.       */
  528.     jan1day = timeptr->tm_wday - (timeptr->tm_yday % 7);
  529.     if (jan1day < 0)
  530.         jan1day += 7;
  531.  
  532.     /*
  533.      * If Jan 1 was a Monday through Thursday, it was in
  534.      * week 1.  Otherwise it was last year's week 53, which is
  535.      * this year's week 0.
  536.      */
  537.     if (jan1day >= 1 && jan1day <= 4)
  538.         diff = 0;
  539.     else
  540.         diff = 1;
  541.     ret = simple_wknum - diff;
  542.     if (ret == 0)    /* we're in the first week of the year */
  543.         ret = 53;
  544.     return ret;
  545. }
  546. #endif
  547.  
  548. /* weeknumber --- figure how many weeks into the year */
  549.  
  550. /* With thanks and tip of the hatlo to ado@elsie.nci.nih.gov */
  551.  
  552. #ifndef __STDC__
  553. static int
  554. weeknumber(timeptr, firstweekday)
  555. const struct tm *timeptr;
  556. int firstweekday;
  557. #else
  558. static int
  559. weeknumber(const struct tm *timeptr, int firstweekday)
  560. #endif
  561. {
  562.     if (firstweekday == 0)
  563.         return (timeptr->tm_yday + 7 - timeptr->tm_wday) / 7;
  564.     else
  565.         return (timeptr->tm_yday + 7 -
  566.             (timeptr->tm_wday ? (timeptr->tm_wday - 1) : 6)) / 7;
  567. }
  568.  
  569. #if 0
  570. /* ADR --- I'm loathe to mess with ado's code ... */
  571.  
  572. Date:         Wed, 24 Apr 91 20:54:08 MDT
  573. From: Michal Jaegermann <audfax!emory!vm.ucs.UAlberta.CA!NTOMCZAK>
  574. To: arnold@audiofax.com
  575.  
  576. Hi Arnold,
  577. in a process of fixing of strftime() in libraries on Atari ST I grabbed
  578. some pieces of code from your own strftime.  When doing that it came
  579. to mind that your weeknumber() function compiles a little bit nicer
  580. in the following form:
  581. /*
  582.  * firstweekday is 0 if starting in Sunday, non-zero if in Monday
  583.  */
  584. {
  585.     return (timeptr->tm_yday - timeptr->tm_wday +
  586.         (firstweekday ? (timeptr->tm_wday ? 8 : 1) : 7)) / 7;
  587. }
  588. How nicer it depends on a compiler, of course, but always a tiny bit.
  589.  
  590.    Cheers,
  591.    Michal
  592.    ntomczak@vm.ucs.ualberta.ca
  593. #endif
  594.  
  595. #ifdef    TEST_STRFTIME
  596.  
  597. /*
  598.  * NAME:
  599.  *    tst
  600.  *
  601.  * SYNOPSIS:
  602.  *    tst
  603.  *
  604.  * DESCRIPTION:
  605.  *    "tst" is a test driver for the function "strftime".
  606.  *
  607.  * OPTIONS:
  608.  *    None.
  609.  *
  610.  * AUTHOR:
  611.  *    Karl Vogel
  612.  *    Control Data Systems, Inc.
  613.  *    vogelke@c-17igp.wpafb.af.mil
  614.  *
  615.  * BUGS:
  616.  *    None noticed yet.
  617.  *
  618.  * COMPILE:
  619.  *    cc -o tst -DTEST_STRFTIME strftime.c
  620.  */
  621.  
  622. /* ADR: I reformatted this to my liking, and deleted some unneeded code. */
  623.  
  624. #ifndef NULL
  625. #include    <stdio.h>
  626. #endif
  627. #include    <sys/time.h>
  628. #include    <string.h>
  629.  
  630. #define        MAXTIME        132
  631.  
  632. /*
  633.  * Array of time formats.
  634.  */
  635.  
  636. static char *array[] =
  637. {
  638.     "(%%A)      full weekday name, var length (Sunday..Saturday)  %A",
  639.     "(%%B)       full month name, var length (January..December)  %B",
  640.     "(%%C)                                               Century  %C",
  641.     "(%%D)                                       date (%%m/%%d/%%y)  %D",
  642.     "(%%E)                           Locale extensions (ignored)  %E",
  643.     "(%%H)                          hour (24-hour clock, 00..23)  %H",
  644.     "(%%I)                          hour (12-hour clock, 01..12)  %I",
  645.     "(%%M)                                       minute (00..59)  %M",
  646.     "(%%O)                           Locale extensions (ignored)  %O",
  647.     "(%%R)                                 time, 24-hour (%%H:%%M)  %R",
  648.     "(%%S)                                       second (00..61)  %S",
  649.     "(%%T)                              time, 24-hour (%%H:%%M:%%S)  %T",
  650.     "(%%U)    week of year, Sunday as first day of week (00..53)  %U",
  651.     "(%%V)                    week of year according to ISO 8601  %V",
  652.     "(%%W)    week of year, Monday as first day of week (00..53)  %W",
  653.     "(%%X)     appropriate locale time representation (%H:%M:%S)  %X",
  654.     "(%%Y)                           year with century (1970...)  %Y",
  655.     "(%%Z) timezone (EDT), or blank if timezone not determinable  %Z",
  656.     "(%%a)          locale's abbreviated weekday name (Sun..Sat)  %a",
  657.     "(%%b)            locale's abbreviated month name (Jan..Dec)  %b",
  658.     "(%%c)           full date (Sat Nov  4 12:02:33 1989)%n%t%t%t  %c",
  659.     "(%%d)                             day of the month (01..31)  %d",
  660.     "(%%e)               day of the month, blank-padded ( 1..31)  %e",
  661.     "(%%h)                                should be same as (%%b)  %h",
  662.     "(%%j)                            day of the year (001..366)  %j",
  663.     "(%%k)               hour, 24-hour clock, blank pad ( 0..23)  %k",
  664.     "(%%l)               hour, 12-hour clock, blank pad ( 0..12)  %l",
  665.     "(%%m)                                        month (01..12)  %m",
  666.     "(%%p)              locale's AM or PM based on 12-hour clock  %p",
  667.     "(%%r)                   time, 12-hour (same as %%I:%%M:%%S %%p)  %r",
  668.     "(%%u) ISO 8601: Weekday as decimal number [1 (Monday) - 7]   %u",
  669.     "(%%v)                                VAX date (dd-bbb-YYYY)  %v",
  670.     "(%%w)                       day of week (0..6, Sunday == 0)  %w",
  671.     "(%%x)                appropriate locale date representation  %x",
  672.     "(%%y)                      last two digits of year (00..99)  %y",
  673.     (char *) NULL
  674. };
  675.  
  676. /* Main routine. */
  677.  
  678. int
  679. main(argc, argv)
  680. int argc;
  681. char **argv;
  682. {
  683.     long time();
  684.  
  685.     char *next;
  686.     char string[MAXTIME];
  687.  
  688.     int k;
  689.     int length;
  690.  
  691.     struct tm *tm;
  692.  
  693.     long clock;
  694.  
  695.     /* Call the function. */
  696.  
  697.     clock = time((long *) 0);
  698.     tm = localtime(&clock);
  699.  
  700.     for (k = 0; next = array[k]; k++) {
  701.         length = strftime(string, MAXTIME, next, tm);
  702.         printf("%s\n", string);
  703.     }
  704.  
  705.     exit(0);
  706. }
  707. #endif    /* TEST_STRFTIME */
  708.