home *** CD-ROM | disk | FTP | other *** search
/ back2roots/padua / padua.7z / padua / uucp / auucp+-1.02 / fuucp_plus_src.lzh / uucplib / strftime.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-03-31  |  6.8 KB  |  313 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.  * Fixes from ado@elsie.nci.nih.gov
  21.  * February 1991
  22.  */
  23.  
  24. #include <stdio.h>
  25. #include <string.h>
  26. #include <time.h>
  27. #include <sys/types.h>
  28.  
  29. #ifndef __STDC__
  30. #define const    /**/
  31. #endif
  32.  
  33. #ifndef __STDC__
  34. extern void tzset();
  35. extern char *strchr();
  36. static int weeknumber();
  37. #else
  38. extern void tzset(void);
  39. extern char *strchr(const char *str, int ch);
  40. static int weeknumber(const struct tm *timeptr, int firstweekday);
  41. #endif
  42.  
  43. extern char *tzname[2];
  44. extern int daylight;
  45.  
  46. #define SYSV_EXT    1    /* stuff in System V ascftime routine */
  47.  
  48. /* strftime --- produce formatted time */
  49.  
  50. #ifndef __STDC__
  51. size_t
  52. strftime(s, maxsize, format, timeptr)
  53. char *s;
  54. size_t maxsize;
  55. const char *format;
  56. const struct tm *timeptr;
  57. #else
  58. size_t
  59. strftime(char *s, size_t maxsize, const char *format, const struct tm *timeptr)
  60. #endif
  61. {
  62.     char *endp = s + maxsize;
  63.     char *start = s;
  64.     char tbuf[100];
  65.     int i;
  66.     static short first = 1;
  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.     if (first) {
  95.         tzset();
  96.         first = 0;
  97.     }
  98.  
  99.     for (; *format && s < endp - 1; format++) {
  100.         tbuf[0] = '\0';
  101.         if (*format != '%') {
  102.             *s++ = *format;
  103.             continue;
  104.         }
  105.         switch (*++format) {
  106.         case '\0':
  107.             *s++ = '%';
  108.             goto out;
  109.  
  110.         case '%':
  111.             *s++ = '%';
  112.             continue;
  113.  
  114.         case 'a':    /* abbreviated weekday name */
  115.             if (timeptr->tm_wday < 0 || timeptr->tm_wday > 6)
  116.                 strcpy(tbuf, "?");
  117.             else
  118.                 strcpy(tbuf, days_a[timeptr->tm_wday]);
  119.             break;
  120.  
  121.         case 'A':    /* full weekday name */
  122.             if (timeptr->tm_wday < 0 || timeptr->tm_wday > 6)
  123.                 strcpy(tbuf, "?");
  124.             else
  125.                 strcpy(tbuf, days_l[timeptr->tm_wday]);
  126.             break;
  127.  
  128. #ifdef SYSV_EXT
  129.         case 'h':    /* abbreviated month name */
  130. #endif
  131.         case 'b':    /* abbreviated month name */
  132.             if (timeptr->tm_mon < 0 || timeptr->tm_mon > 11)
  133.                 strcpy(tbuf, "?");
  134.             else
  135.                 strcpy(tbuf, months_a[timeptr->tm_mon]);
  136.             break;
  137.  
  138.         case 'B':    /* full month name */
  139.             if (timeptr->tm_mon < 0 || timeptr->tm_mon > 11)
  140.                 strcpy(tbuf, "?");
  141.             else
  142.                 strcpy(tbuf, months_l[timeptr->tm_mon]);
  143.             break;
  144.  
  145.         case 'c':    /* appropriate date and time representation */
  146.             sprintf(tbuf, "%s %s %2d %02d:%02d:%02d %d",
  147.                 days_a[timeptr->tm_wday],
  148.                 months_a[timeptr->tm_mon],
  149.                 timeptr->tm_mday,
  150.                 timeptr->tm_hour,
  151.                 timeptr->tm_min,
  152.                 timeptr->tm_sec,
  153.                 timeptr->tm_year + 1900);
  154.             break;
  155.  
  156.         case 'd':    /* day of the month, 01 - 31 */
  157.             sprintf(tbuf, "%02d", timeptr->tm_mday);
  158.             break;
  159.  
  160.         case 'H':    /* hour, 24-hour clock, 00 - 23 */
  161.             sprintf(tbuf, "%02d", timeptr->tm_hour);
  162.             break;
  163.  
  164.         case 'I':    /* hour, 12-hour clock, 01 - 12 */
  165.             i = timeptr->tm_hour;
  166.             if (i == 0)
  167.                 i = 12;
  168.             else if (i > 12)
  169.                 i -= 12;
  170.             sprintf(tbuf, "%02d", i);
  171.             break;
  172.  
  173.         case 'j':    /* day of the year, 001 - 366 */
  174.             sprintf(tbuf, "%03d", timeptr->tm_yday + 1);
  175.             break;
  176.  
  177.         case 'm':    /* month, 01 - 12 */
  178.             sprintf(tbuf, "%02d", timeptr->tm_mon + 1);
  179.             break;
  180.  
  181.         case 'M':    /* minute, 00 - 59 */
  182.             sprintf(tbuf, "%02d", timeptr->tm_min);
  183.             break;
  184.  
  185.         case 'p':    /* am or pm based on 12-hour clock */
  186.             if (timeptr->tm_hour < 12)
  187.                 strcpy(tbuf, ampm[0]);
  188.             else
  189.                 strcpy(tbuf, ampm[1]);
  190.             break;
  191.  
  192.         case 'S':    /* second, 00 - 61 */
  193.             sprintf(tbuf, "%02d", timeptr->tm_sec);
  194.             break;
  195.  
  196.         case 'U':    /* week of year, Sunday is first day of week */
  197.             sprintf(tbuf, "%d", weeknumber(timeptr, 0));
  198.             break;
  199.  
  200.         case 'w':    /* weekday, Sunday == 0, 0 - 6 */
  201.             sprintf(tbuf, "%d", timeptr->tm_wday);
  202.             break;
  203.  
  204.         case 'W':    /* week of year, Monday is first day of week */
  205.             sprintf(tbuf, "%d", weeknumber(timeptr, 1));
  206.             break;
  207.  
  208.         case 'x':    /* appropriate date representation */
  209.             sprintf(tbuf, "%s %s %2d %d",
  210.                 days_a[timeptr->tm_wday],
  211.                 months_a[timeptr->tm_mon],
  212.                 timeptr->tm_mday,
  213.                 timeptr->tm_year + 1900);
  214.             break;
  215.  
  216.         case 'X':    /* appropriate time representation */
  217.             sprintf(tbuf, "%02d:%02d:%02d",
  218.                 timeptr->tm_hour,
  219.                 timeptr->tm_min,
  220.                 timeptr->tm_sec);
  221.             break;
  222.  
  223.         case 'y':    /* year without a century, 00 - 99 */
  224.             i = timeptr->tm_year % 100;
  225.             sprintf(tbuf, "%d", i);
  226.             break;
  227.  
  228.         case 'Y':    /* year with century */
  229.             sprintf(tbuf, "%d", 1900 + timeptr->tm_year);
  230.             break;
  231.  
  232.         case 'Z':    /* time zone name or abbrevation */
  233.             i = 0;
  234.             if (daylight && timeptr->tm_isdst)
  235.                 i = 1;
  236.             strcpy(tbuf, tzname[i]);
  237.             break;
  238.  
  239. #ifdef SYSV_EXT
  240.         case 'n':    /* same as \n */
  241.             tbuf[0] = '\n';
  242.             tbuf[1] = '\0';
  243.             break;
  244.  
  245.         case 't':    /* same as \t */
  246.             tbuf[0] = '\t';
  247.             tbuf[1] = '\0';
  248.             break;
  249.  
  250.         case 'D':    /* date as %m/%d/%y */
  251.             strftime(tbuf, sizeof tbuf, "%m/%d/%y", timeptr);
  252.             break;
  253.  
  254.         case 'e':    /* day of month, blank padded */
  255.             sprintf(tbuf, "%2d", timeptr->tm_mday);
  256.             break;
  257.  
  258.         case 'r':    /* time as %I:%M:%S %p */
  259.             strftime(tbuf, sizeof tbuf, "%I:%M:%S %p", timeptr);
  260.             break;
  261.  
  262.         case 'R':    /* time as %H:%M */
  263.             strftime(tbuf, sizeof tbuf, "%H:%M", timeptr);
  264.             break;
  265.  
  266.         case 'T':    /* time as %H:%M:%S */
  267.             strftime(tbuf, sizeof tbuf, "%H:%M:%S", timeptr);
  268.             break;
  269. #endif
  270.  
  271.         default:
  272.             tbuf[0] = '%';
  273.             tbuf[1] = *format;
  274.             tbuf[2] = '\0';
  275.             break;
  276.         }
  277.         i = strlen(tbuf);
  278.         if (i)
  279.             if (s + i < endp - 1) {
  280.                 strcpy(s, tbuf);
  281.                 s += i;
  282.             } else
  283.                 return 0;
  284.     }
  285. out:
  286.     if (s < endp && *format == '\0') {
  287.         *s = '\0';
  288.         return (s - start);
  289.     } else
  290.         return 0;
  291. }
  292.  
  293. /* weeknumber --- figure how many weeks into the year */
  294.  
  295. /* With thanks and tip of the hatlo to ado@elsie.nci.nih.gov */
  296.  
  297. #ifndef __STDC__
  298. static int
  299. weeknumber(timeptr, firstweekday)
  300. const struct tm *timeptr;
  301. int firstweekday;
  302. #else
  303. static int
  304. weeknumber(const struct tm *timeptr, int firstweekday)
  305. #endif
  306. {
  307.     if (firstweekday == 0)
  308.         return (timeptr->tm_yday + 7 - timeptr->tm_wday) / 7;
  309.     else
  310.         return (timeptr->tm_yday + 7 -
  311.             (timeptr->tm_wday ? (timeptr->tm_wday - 1) : 6)) / 7;
  312. }
  313.