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