home *** CD-ROM | disk | FTP | other *** search
/ Mac-Source 1994 July / Mac-Source_July_1994.iso / Other Langs / Tickle-4.0 (tcl) / tcl / extend / osSupport / strftime.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-10-26  |  6.8 KB  |  299 lines  |  [TEXT/MPS ]

  1. /*
  2.  * strftime.c
  3.  *
  4.  * Public-domain relatively quick-and-dirty implemenation of
  5.  * ANSI library routine for System V Unix systems.
  6.  *
  7.  * It's written in old-style C for maximal portability.
  8.  *
  9.  * The code for %c, %x, and %X is my best guess as to what's "appropriate".
  10.  * This version ignores LOCALE information.
  11.  * It also doesn't worry about multi-byte characters.
  12.  * So there.
  13.  *
  14.  * Arnold Robbins
  15.  * January, February, 1991
  16.  *
  17.  * Fixes from ado@elsie.nci.nih.gov
  18.  * February 1991
  19.  *-----------------------------------------------------------------------------
  20.  * $Id: strftime.c,v 2.2 1993/08/13 15:01:21 markd Exp $
  21.  *-----------------------------------------------------------------------------
  22.  */
  23.  
  24. /*
  25.  * To avoid Unix version problems, this code has been simplified to avoid
  26.  * const and size_t, however this can cause an incompatible definition on
  27.  * ansi-C systems, so a game is played with defines to ignore a strftime
  28.  * declaration in time.h
  29.  */
  30.  
  31. #define strftime ___srtftime
  32.  
  33. #include <stdio.h>
  34. #include <string.h>
  35. #include <time.h>
  36. #include <sys/types.h>
  37.  
  38. #undef strftime
  39.  
  40. #include "tclXconfig.h"
  41.  
  42. extern char *strchr();
  43. static int weeknumber();
  44.  
  45. #ifndef HAVE_TM_ZONE
  46. extern char *tzname[2];
  47. extern int daylight;
  48. #endif
  49.  
  50. /* strftime --- produce formatted time */
  51.  
  52. int
  53. strftime(s, maxsize, format, timeptr)
  54.     char            *s;
  55.     int              maxsize;
  56.     char            *format;
  57.     struct tm       *timeptr;
  58. {
  59.     char *endp = s + maxsize;
  60.     char *start = s;
  61.     char tbuf[100];
  62.     int i;
  63.  
  64.     /* various tables, useful in North America */
  65.     static char *days_a[] = {
  66.         "Sun", "Mon", "Tue", "Wed",
  67.         "Thu", "Fri", "Sat",
  68.     };
  69.     static char *days_l[] = {
  70.         "Sunday", "Monday", "Tuesday", "Wednesday",
  71.         "Thursday", "Friday", "Saturday",
  72.     };
  73.     static char *months_a[] = {
  74.         "Jan", "Feb", "Mar", "Apr", "May", "Jun",
  75.         "Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
  76.     };
  77.     static char *months_l[] = {
  78.         "January", "February", "March", "April",
  79.         "May", "June", "July", "August", "September",
  80.         "October", "November", "December",
  81.     };
  82.     static char *ampm[] = { "AM", "PM", };
  83.  
  84.     if (s == NULL || format == NULL || timeptr == NULL || maxsize == 0)
  85.         return 0;
  86.  
  87.     if (strchr(format, '%') == NULL && strlen(format) + 1 >= maxsize)
  88.         return 0;
  89.  
  90.     for (; *format && s < endp - 1; format++) {
  91.         tbuf[0] = '\0';
  92.         if (*format != '%') {
  93.             *s++ = *format;
  94.             continue;
  95.         }
  96.         switch (*++format) {
  97.         case '\0':
  98.             *s++ = '%';
  99.             goto out;
  100.  
  101.         case '%':
  102.             *s++ = '%';
  103.             continue;
  104.  
  105.         case 'a':    /* abbreviated weekday name */
  106.             if (timeptr->tm_wday < 0 || timeptr->tm_wday > 6)
  107.                 strcpy(tbuf, "?");
  108.             else
  109.                 strcpy(tbuf, days_a[timeptr->tm_wday]);
  110.             break;
  111.  
  112.         case 'A':    /* full weekday name */
  113.             if (timeptr->tm_wday < 0 || timeptr->tm_wday > 6)
  114.                 strcpy(tbuf, "?");
  115.             else
  116.                 strcpy(tbuf, days_l[timeptr->tm_wday]);
  117.             break;
  118.  
  119.         case 'h':    /* abbreviated month name */
  120.         case 'b':    /* abbreviated month name */
  121.             if (timeptr->tm_mon < 0 || timeptr->tm_mon > 11)
  122.                 strcpy(tbuf, "?");
  123.             else
  124.                 strcpy(tbuf, months_a[timeptr->tm_mon]);
  125.             break;
  126.  
  127.         case 'B':    /* full month name */
  128.             if (timeptr->tm_mon < 0 || timeptr->tm_mon > 11)
  129.                 strcpy(tbuf, "?");
  130.             else
  131.                 strcpy(tbuf, months_l[timeptr->tm_mon]);
  132.             break;
  133.  
  134.         case 'c':    /* appropriate date and time representation */
  135.             sprintf(tbuf, "%s %s %2d %02d:%02d:%02d %d",
  136.                 days_a[timeptr->tm_wday],
  137.                 months_a[timeptr->tm_mon],
  138.                 timeptr->tm_mday,
  139.                 timeptr->tm_hour,
  140.                 timeptr->tm_min,
  141.                 timeptr->tm_sec,
  142.                 timeptr->tm_year + 1900);
  143.             break;
  144.  
  145.         case 'd':    /* day of the month, 01 - 31 */
  146.             sprintf(tbuf, "%02d", timeptr->tm_mday);
  147.             break;
  148.  
  149.         case 'H':    /* hour, 24-hour clock, 00 - 23 */
  150.             sprintf(tbuf, "%02d", timeptr->tm_hour);
  151.             break;
  152.  
  153.         case 'I':    /* hour, 12-hour clock, 01 - 12 */
  154.             i = timeptr->tm_hour;
  155.             if (i == 0)
  156.                 i = 12;
  157.             else if (i > 12)
  158.                 i -= 12;
  159.             sprintf(tbuf, "%02d", i);
  160.             break;
  161.  
  162.         case 'j':    /* day of the year, 001 - 366 */
  163.             sprintf(tbuf, "%03d", timeptr->tm_yday + 1);
  164.             break;
  165.  
  166.         case 'm':    /* month, 01 - 12 */
  167.             sprintf(tbuf, "%02d", timeptr->tm_mon + 1);
  168.             break;
  169.  
  170.         case 'M':    /* minute, 00 - 59 */
  171.             sprintf(tbuf, "%02d", timeptr->tm_min);
  172.             break;
  173.  
  174.         case 'p':    /* am or pm based on 12-hour clock */
  175.             if (timeptr->tm_hour < 12)
  176.                 strcpy(tbuf, ampm[0]);
  177.             else
  178.                 strcpy(tbuf, ampm[1]);
  179.             break;
  180.  
  181.         case 'S':    /* second, 00 - 61 */
  182.             sprintf(tbuf, "%02d", timeptr->tm_sec);
  183.             break;
  184.  
  185.         case 'U':    /* week of year, Sunday is first day of week */
  186.             sprintf(tbuf, "%d", weeknumber(timeptr, 0));
  187.             break;
  188.  
  189.         case 'w':    /* weekday, Sunday == 0, 0 - 6 */
  190.             sprintf(tbuf, "%d", timeptr->tm_wday);
  191.             break;
  192.  
  193.         case 'W':    /* week of year, Monday is first day of week */
  194.             sprintf(tbuf, "%d", weeknumber(timeptr, 1));
  195.             break;
  196.  
  197.         case 'x':    /* appropriate date representation */
  198.             sprintf(tbuf, "%s %s %2d %d",
  199.                 days_a[timeptr->tm_wday],
  200.                 months_a[timeptr->tm_mon],
  201.                 timeptr->tm_mday,
  202.                 timeptr->tm_year + 1900);
  203.             break;
  204.  
  205.         case 'X':    /* appropriate time representation */
  206.             sprintf(tbuf, "%02d:%02d:%02d",
  207.                 timeptr->tm_hour,
  208.                 timeptr->tm_min,
  209.                 timeptr->tm_sec);
  210.             break;
  211.  
  212.         case 'y':    /* year without a century, 00 - 99 */
  213.             i = timeptr->tm_year % 100;
  214.             sprintf(tbuf, "%d", i);
  215.             break;
  216.  
  217.         case 'Y':    /* year with century */
  218.             sprintf(tbuf, "%d", 1900 + timeptr->tm_year);
  219.             break;
  220.  
  221.         case 'Z':    /* time zone name or abbrevation */
  222. #ifdef HAVE_TM_ZONE
  223.                         strcpy(tbuf, timeptr->tm_zone);
  224. #else
  225.             i = 0;
  226.             if (daylight && timeptr->tm_isdst)
  227.                 i = 1;
  228.             strcpy(tbuf, tzname[i]);
  229. #endif
  230.             break;
  231.  
  232.         case 'n':    /* same as \n */
  233.             tbuf[0] = '\n';
  234.             tbuf[1] = '\0';
  235.             break;
  236.  
  237.         case 't':    /* same as \t */
  238.             tbuf[0] = '\t';
  239.             tbuf[1] = '\0';
  240.             break;
  241.  
  242.         case 'D':    /* date as %m/%d/%y */
  243.             strftime(tbuf, sizeof tbuf, "%m/%d/%y", timeptr);
  244.             break;
  245.  
  246.         case 'e':    /* day of month, blank padded */
  247.             sprintf(tbuf, "%2d", timeptr->tm_mday);
  248.             break;
  249.  
  250.         case 'r':    /* time as %I:%M:%S %p */
  251.             strftime(tbuf, sizeof tbuf, "%I:%M:%S %p", timeptr);
  252.             break;
  253.  
  254.         case 'R':    /* time as %H:%M */
  255.             strftime(tbuf, sizeof tbuf, "%H:%M", timeptr);
  256.             break;
  257.  
  258.         case 'T':    /* time as %H:%M:%S */
  259.             strftime(tbuf, sizeof tbuf, "%H:%M:%S", timeptr);
  260.             break;
  261.  
  262.         default:
  263.             tbuf[0] = '%';
  264.             tbuf[1] = *format;
  265.             tbuf[2] = '\0';
  266.             break;
  267.         }
  268.         i = strlen(tbuf);
  269.         if (i)
  270.             if (s + i < endp - 1) {
  271.                 strcpy(s, tbuf);
  272.                 s += i;
  273.             } else
  274.                 return 0;
  275.     }
  276. out:
  277.     if (s < endp && *format == '\0') {
  278.         *s = '\0';
  279.         return (s - start);
  280.     } else
  281.         return 0;
  282. }
  283.  
  284. /* weeknumber --- figure how many weeks into the year */
  285.  
  286. /* With thanks and tip of the hatlo to ado@elsie.nci.nih.gov */
  287.  
  288. static int
  289. weeknumber(timeptr, firstweekday)
  290.     struct tm *timeptr;
  291.     int        firstweekday;
  292. {
  293.     if (firstweekday == 0)
  294.         return (timeptr->tm_yday + 7 - timeptr->tm_wday) / 7;
  295.     else
  296.         return (timeptr->tm_yday + 7 -
  297.             (timeptr->tm_wday ? (timeptr->tm_wday - 1) : 6)) / 7;
  298. }
  299.