home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / misc / volume41 / rperf / part04 / strftime.c < prev   
Encoding:
C/C++ Source or Header  |  1993-12-19  |  16.8 KB  |  706 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.  * Fixes from Tor Lillqvist tor@tik.vtt.fi
  31.  * May, 1993
  32.  */
  33.  
  34. #include <stdio.h>
  35. #include <ctype.h>
  36. #include <time.h>
  37. #include <sys/types.h>
  38.  
  39. /* defaults: season to taste */
  40. #define SYSV_EXT    1    /* stuff in System V ascftime routine */
  41. #define SUNOS_EXT    1    /* stuff in SunOS strftime routine */
  42. #define POSIX2_DATE    1    /* stuff in Posix 1003.2 date command */
  43. #define VMS_EXT        1    /* include %v for VMS date format */
  44. #ifndef GAWK
  45. #define POSIX_SEMANTICS    1    /* call tzset() if TZ changes */
  46. #endif
  47.  
  48. #if defined(POSIX2_DATE)
  49. #if ! defined(SYSV_EXT)
  50. #define SYSV_EXT    1
  51. #endif
  52. #if ! defined(SUNOS_EXT)
  53. #define SUNOS_EXT    1
  54. #endif
  55. #endif
  56.  
  57. #if defined(POSIX2_DATE)
  58. #define adddecl(stuff)    stuff
  59. #else
  60. #define adddecl(stuff)
  61. #endif
  62.  
  63. #ifdef HAVE_STDLIB_H
  64. #include <stdlib.h>
  65. #else                /* !HAVE_STDLIB_H */
  66. extern char    *getenv();
  67. #endif                /* !HAVE_STDLIB_H */
  68.  
  69. #include <string.h>
  70.  
  71. #undef strchr    /* avoid AIX weirdness */
  72.  
  73. #ifndef __STDC__
  74. #define const    /**/
  75. #endif
  76.  
  77. #ifndef __STDC__
  78. static int weeknumber();
  79. adddecl(static int iso8601wknum();)
  80. #else
  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((unsigned) tzlen + 1);
  189.             if (savetz != NULL) {
  190.                 savetzlen = tzlen + 1;
  191.                 (void) 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, (unsigned) i);
  202.             if (savetz) {
  203.                 savetzlen = i;
  204.                 (void) strcpy(savetz, tz);
  205.             }
  206.         } else
  207.             (void) 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.                 (void) strcpy(tbuf, "?");
  231.             else
  232.                 (void) 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.                 (void) strcpy(tbuf, "?");
  238.             else
  239.                 (void) 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.                 (void) strcpy(tbuf, "?");
  248.             else
  249.                 (void) 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.                 (void) strcpy(tbuf, "?");
  255.             else
  256.                 (void) strcpy(tbuf, months_l[timeptr->tm_mon]);
  257.             break;
  258.  
  259.         case 'c':    /* appropriate date and time representation */
  260.             (void) 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.             (void) 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.             (void) 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.             (void) sprintf(tbuf, "%02d", i);
  287.             break;
  288.  
  289.         case 'j':    /* day of the year, 001 - 366 */
  290.             (void) 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.             (void) sprintf(tbuf, "%02d", i + 1);
  296.             break;
  297.  
  298.         case 'M':    /* minute, 00 - 59 */
  299.             i = range(0, timeptr->tm_min, 59);
  300.             (void) 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.                 (void) strcpy(tbuf, ampm[0]);
  307.             else
  308.                 (void) strcpy(tbuf, ampm[1]);
  309.             break;
  310.  
  311.         case 'S':    /* second, 00 - 61 */
  312.             i = range(0, timeptr->tm_sec, 61);
  313.             (void) sprintf(tbuf, "%02d", i);
  314.             break;
  315.  
  316.         case 'U':    /* week of year, Sunday is first day of week */
  317.             (void) 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.             (void) sprintf(tbuf, "%d", i);
  323.             break;
  324.  
  325.         case 'W':    /* week of year, Monday is first day of week */
  326.             (void) sprintf(tbuf, "%d", weeknumber(timeptr, 1));
  327.             break;
  328.  
  329.         case 'x':    /* appropriate date representation */
  330.             (void) 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.             (void) 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.             (void) sprintf(tbuf, "%d", i);
  347.             break;
  348.  
  349.         case 'Y':    /* year with century */
  350.             (void) 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.             (void) strcpy(tbuf, timeptr->tm_zone);
  363. #else
  364.             (void) 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.             (void) strftime(tbuf, sizeof tbuf, "%m/%d/%y", timeptr);
  381.             break;
  382.  
  383.         case 'e':    /* day of month, blank padded */
  384.             (void) sprintf(tbuf, "%2d", range(1, timeptr->tm_mday, 31));
  385.             break;
  386.  
  387.         case 'r':    /* time as %I:%M:%S %p */
  388.             (void) strftime(tbuf, sizeof tbuf, "%I:%M:%S %p", timeptr);
  389.             break;
  390.  
  391.         case 'R':    /* time as %H:%M */
  392.             (void) strftime(tbuf, sizeof tbuf, "%H:%M", timeptr);
  393.             break;
  394.  
  395.         case 'T':    /* time as %H:%M:%S */
  396.             (void) 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.             (void) 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.             (void) sprintf(tbuf, "%2d", i);
  412.             break;
  413. #endif
  414.  
  415.  
  416. #ifdef VMS_EXT
  417.         case 'v':    /* date as dd-bbb-YYYY */
  418.             (void) 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.             (void) 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.             (void) sprintf(tbuf, "%d", iso8601wknum(timeptr));
  455.             break;
  456.  
  457.         case 'u':
  458.         /* ISO 8601: Weekday as a decimal number [1 (Monday) - 7] */
  459.             (void) 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.                 (void) 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 tml@tik.vtt.fi
  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 == 0
  522.      * and that
  523.      *     timeptr->tm_wday MOD 7 == timeptr->tm_wday
  524.      * from which it follows that. . .
  525.       */
  526.     jan1day = timeptr->tm_wday - (timeptr->tm_yday % 7);
  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.  
  593. #ifdef    TEST_STRFTIME
  594.  
  595. /*
  596.  * NAME:
  597.  *    tst
  598.  *
  599.  * SYNOPSIS:
  600.  *    tst
  601.  *
  602.  * DESCRIPTION:
  603.  *    "tst" is a test driver for the function "strftime".
  604.  *
  605.  * OPTIONS:
  606.  *    None.
  607.  *
  608.  * AUTHOR:
  609.  *    Karl Vogel
  610.  *    Control Data Systems, Inc.
  611.  *    vogelke@c-17igp.wpafb.af.mil
  612.  *
  613.  * BUGS:
  614.  *    None noticed yet.
  615.  *
  616.  * COMPILE:
  617.  *    cc -o tst -DTEST_STRFTIME strftime.c
  618.  */
  619.  
  620. /* ADR: I reformatted this to my liking, and deleted some unneeded code. */
  621.  
  622. #ifndef NULL
  623. #include    <stdio.h>
  624. #endif
  625. #include    <sys/time.h>
  626. #include    <string.h>
  627.  
  628. #define        MAXTIME        132
  629.  
  630. /*
  631.  * Array of time formats.
  632.  */
  633.  
  634. static char *array[] =
  635. {
  636.     "(%%A)      full weekday name, var length (Sunday..Saturday)  %A",
  637.     "(%%B)       full month name, var length (January..December)  %B",
  638.     "(%%C)                                               Century  %C",
  639.     "(%%D)                                       date (%%m/%%d/%%y)  %D",
  640.     "(%%E)                           Locale extensions (ignored)  %E",
  641.     "(%%H)                          hour (24-hour clock, 00..23)  %H",
  642.     "(%%I)                          hour (12-hour clock, 01..12)  %I",
  643.     "(%%M)                                       minute (00..59)  %M",
  644.     "(%%O)                           Locale extensions (ignored)  %O",
  645.     "(%%R)                                 time, 24-hour (%%H:%%M)  %R",
  646.     "(%%S)                                       second (00..61)  %S",
  647.     "(%%T)                              time, 24-hour (%%H:%%M:%%S)  %T",
  648.     "(%%U)    week of year, Sunday as first day of week (00..53)  %U",
  649.     "(%%V)                    week of year according to ISO 8601  %V",
  650.     "(%%W)    week of year, Monday as first day of week (00..53)  %W",
  651.     "(%%X)     appropriate locale time representation (%H:%M:%S)  %X",
  652.     "(%%Y)                           year with century (1970...)  %Y",
  653.     "(%%Z) timezone (EDT), or blank if timezone not determinable  %Z",
  654.     "(%%a)          locale's abbreviated weekday name (Sun..Sat)  %a",
  655.     "(%%b)            locale's abbreviated month name (Jan..Dec)  %b",
  656.     "(%%c)           full date (Sat Nov  4 12:02:33 1989)%n%t%t%t  %c",
  657.     "(%%d)                             day of the month (01..31)  %d",
  658.     "(%%e)               day of the month, blank-padded ( 1..31)  %e",
  659.     "(%%h)                                should be same as (%%b)  %h",
  660.     "(%%j)                            day of the year (001..366)  %j",
  661.     "(%%k)               hour, 24-hour clock, blank pad ( 0..23)  %k",
  662.     "(%%l)               hour, 12-hour clock, blank pad ( 0..12)  %l",
  663.     "(%%m)                                        month (01..12)  %m",
  664.     "(%%p)              locale's AM or PM based on 12-hour clock  %p",
  665.     "(%%r)                   time, 12-hour (same as %%I:%%M:%%S %%p)  %r",
  666.     "(%%u) ISO 8601: Weekday as decimal number [1 (Monday) - 7]   %u",
  667.     "(%%v)                                VAX date (dd-bbb-YYYY)  %v",
  668.     "(%%w)                       day of week (0..6, Sunday == 0)  %w",
  669.     "(%%x)                appropriate locale date representation  %x",
  670.     "(%%y)                      last two digits of year (00..99)  %y",
  671.     (char *) NULL
  672. };
  673.  
  674. /* Main routine. */
  675.  
  676. int
  677. main(argc, argv)
  678. int argc;
  679. char **argv;
  680. {
  681.     long time();
  682.  
  683.     char *next;
  684.     char string[MAXTIME];
  685.  
  686.     int k;
  687.     int length;
  688.  
  689.     struct tm *tm;
  690.  
  691.     long clock;
  692.  
  693.     /* Call the function. */
  694.  
  695.     clock = time((long *) 0);
  696.     tm = localtime(&clock);
  697.  
  698.     for (k = 0; next = array[k]; k++) {
  699.         length = strftime(string, MAXTIME, next, tm);
  700.         printf("%s\n", string);
  701.     }
  702.  
  703.     exit(0);
  704. }
  705. #endif    /* TEST_STRFTIME */
  706.