home *** CD-ROM | disk | FTP | other *** search
/ pc.louisiana.edu/pub/unix/ / Louisiana_UNIX.tar / Louisiana_UNIX / date+.zoo / strftime.shar / strftime.c < prev   
C/C++ Source or Header  |  1991-10-15  |  7KB  |  306 lines

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