home *** CD-ROM | disk | FTP | other *** search
/ Geek Gadgets 1 / ADE-1.bin / ade-dist / gawk-2.15.6-base.tgz / gawk-2.15.6-base.tar / fsf / gawk / missing / strftime.c < prev    next >
C/C++ Source or Header  |  1994-05-19  |  19KB  |  787 lines

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