home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume25 / posix-date / strftime.c < prev   
Encoding:
C/C++ Source or Header  |  1992-02-29  |  8.3 KB  |  373 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.  * For stuff needed to implement the P1003.2 date command, add POSIX2_DATE.
  12.  *
  13.  * The code for %c, %x, and %X is my best guess as to what's "appropriate".
  14.  * This version ignores LOCALE information.
  15.  * It also doesn't worry about multi-byte characters.
  16.  * So there.
  17.  *
  18.  * Arnold Robbins
  19.  * January, February, March, 1991
  20.  *
  21.  * Fixes from ado@elsie.nci.nih.gov
  22.  * February 1991
  23.  */
  24.  
  25. #include <stdio.h>
  26. #include <ctype.h>
  27. #include <string.h>
  28. #include <time.h>
  29. #include <sys/types.h>
  30.  
  31. #ifndef __STDC__
  32. #define const    /**/
  33. #endif
  34.  
  35. #ifndef __STDC__
  36. extern void tzset();
  37. extern char *strchr();
  38. static int weeknumber();
  39. #else
  40. extern void tzset(void);
  41. extern char *strchr(const char *str, int ch);
  42. static int weeknumber(const struct tm *timeptr, int firstweekday);
  43. #endif
  44.  
  45. extern char *tzname[2];
  46. extern int daylight;
  47.  
  48. #define SYSV_EXT    1    /* stuff in System V ascftime routine */
  49. #define POSIX2_DATE    1    /* stuff in Posix 1003.2 date command */
  50. #define VMS_EXT        1    /* include %V for VMS date format */
  51.  
  52. #if defined(POSIX2_DATE) && ! defined(SYSV_EXT)
  53. #define SYSV_EXT    1
  54. #endif
  55.  
  56. /* strftime --- produce formatted time */
  57.  
  58. #ifndef __STDC__
  59. size_t
  60. strftime(s, maxsize, format, timeptr)
  61. char *s;
  62. size_t maxsize;
  63. const char *format;
  64. const struct tm *timeptr;
  65. #else
  66. size_t
  67. strftime(char *s, size_t maxsize, const char *format, const struct tm *timeptr)
  68. #endif
  69. {
  70.     char *endp = s + maxsize;
  71.     char *start = s;
  72.     char tbuf[100];
  73.     int i;
  74.     static short first = 1;
  75.  
  76.     /* various tables, useful in North America */
  77.     static char *days_a[] = {
  78.         "Sun", "Mon", "Tue", "Wed",
  79.         "Thu", "Fri", "Sat",
  80.     };
  81.     static char *days_l[] = {
  82.         "Sunday", "Monday", "Tuesday", "Wednesday",
  83.         "Thursday", "Friday", "Saturday",
  84.     };
  85.     static char *months_a[] = {
  86.         "Jan", "Feb", "Mar", "Apr", "May", "Jun",
  87.         "Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
  88.     };
  89.     static char *months_l[] = {
  90.         "January", "February", "March", "April",
  91.         "May", "June", "July", "August", "September",
  92.         "October", "November", "December",
  93.     };
  94.     static char *ampm[] = { "AM", "PM", };
  95.  
  96.     if (s == NULL || format == NULL || timeptr == NULL || maxsize == 0)
  97.         return 0;
  98.  
  99.     if (strchr(format, '%') == NULL && strlen(format) + 1 >= maxsize)
  100.         return 0;
  101.  
  102.     if (first) {
  103.         tzset();
  104.         first = 0;
  105.     }
  106.  
  107.     for (; *format && s < endp - 1; format++) {
  108.         tbuf[0] = '\0';
  109.         if (*format != '%') {
  110.             *s++ = *format;
  111.             continue;
  112.         }
  113.     again:
  114.         switch (*++format) {
  115.         case '\0':
  116.             *s++ = '%';
  117.             goto out;
  118.  
  119.         case '%':
  120.             *s++ = '%';
  121.             continue;
  122.  
  123.         case 'a':    /* abbreviated weekday name */
  124.             if (timeptr->tm_wday < 0 || timeptr->tm_wday > 6)
  125.                 strcpy(tbuf, "?");
  126.             else
  127.                 strcpy(tbuf, days_a[timeptr->tm_wday]);
  128.             break;
  129.  
  130.         case 'A':    /* full weekday name */
  131.             if (timeptr->tm_wday < 0 || timeptr->tm_wday > 6)
  132.                 strcpy(tbuf, "?");
  133.             else
  134.                 strcpy(tbuf, days_l[timeptr->tm_wday]);
  135.             break;
  136.  
  137. #ifdef SYSV_EXT
  138.         case 'h':    /* abbreviated month name */
  139. #endif
  140.         case 'b':    /* abbreviated month name */
  141.             if (timeptr->tm_mon < 0 || timeptr->tm_mon > 11)
  142.                 strcpy(tbuf, "?");
  143.             else
  144.                 strcpy(tbuf, months_a[timeptr->tm_mon]);
  145.             break;
  146.  
  147.         case 'B':    /* full month name */
  148.             if (timeptr->tm_mon < 0 || timeptr->tm_mon > 11)
  149.                 strcpy(tbuf, "?");
  150.             else
  151.                 strcpy(tbuf, months_l[timeptr->tm_mon]);
  152.             break;
  153.  
  154.         case 'c':    /* appropriate date and time representation */
  155.             sprintf(tbuf, "%s %s %2d %02d:%02d:%02d %d",
  156.                 days_a[timeptr->tm_wday],
  157.                 months_a[timeptr->tm_mon],
  158.                 timeptr->tm_mday,
  159.                 timeptr->tm_hour,
  160.                 timeptr->tm_min,
  161.                 timeptr->tm_sec,
  162.                 timeptr->tm_year + 1900);
  163.             break;
  164.  
  165.         case 'd':    /* day of the month, 01 - 31 */
  166.             sprintf(tbuf, "%02d", timeptr->tm_mday);
  167.             break;
  168.  
  169.         case 'H':    /* hour, 24-hour clock, 00 - 23 */
  170.             sprintf(tbuf, "%02d", timeptr->tm_hour);
  171.             break;
  172.  
  173.         case 'I':    /* hour, 12-hour clock, 01 - 12 */
  174.             i = timeptr->tm_hour;
  175.             if (i == 0)
  176.                 i = 12;
  177.             else if (i > 12)
  178.                 i -= 12;
  179.             sprintf(tbuf, "%02d", i);
  180.             break;
  181.  
  182.         case 'j':    /* day of the year, 001 - 366 */
  183.             sprintf(tbuf, "%03d", timeptr->tm_yday + 1);
  184.             break;
  185.  
  186.         case 'm':    /* month, 01 - 12 */
  187.             sprintf(tbuf, "%02d", timeptr->tm_mon + 1);
  188.             break;
  189.  
  190.         case 'M':    /* minute, 00 - 59 */
  191.             sprintf(tbuf, "%02d", timeptr->tm_min);
  192.             break;
  193.  
  194.         case 'p':    /* am or pm based on 12-hour clock */
  195.             if (timeptr->tm_hour < 12)
  196.                 strcpy(tbuf, ampm[0]);
  197.             else
  198.                 strcpy(tbuf, ampm[1]);
  199.             break;
  200.  
  201.         case 'S':    /* second, 00 - 61 */
  202.             sprintf(tbuf, "%02d", timeptr->tm_sec);
  203.             break;
  204.  
  205.         case 'U':    /* week of year, Sunday is first day of week */
  206.             sprintf(tbuf, "%d", weeknumber(timeptr, 0));
  207.             break;
  208.  
  209.         case 'w':    /* weekday, Sunday == 0, 0 - 6 */
  210.             sprintf(tbuf, "%d", timeptr->tm_wday);
  211.             break;
  212.  
  213.         case 'W':    /* week of year, Monday is first day of week */
  214.             sprintf(tbuf, "%d", weeknumber(timeptr, 1));
  215.             break;
  216.  
  217.         case 'x':    /* appropriate date representation */
  218.             sprintf(tbuf, "%s %s %2d %d",
  219.                 days_a[timeptr->tm_wday],
  220.                 months_a[timeptr->tm_mon],
  221.                 timeptr->tm_mday,
  222.                 timeptr->tm_year + 1900);
  223.             break;
  224.  
  225.         case 'X':    /* appropriate time representation */
  226.             sprintf(tbuf, "%02d:%02d:%02d",
  227.                 timeptr->tm_hour,
  228.                 timeptr->tm_min,
  229.                 timeptr->tm_sec);
  230.             break;
  231.  
  232.         case 'y':    /* year without a century, 00 - 99 */
  233.             i = timeptr->tm_year % 100;
  234.             sprintf(tbuf, "%d", i);
  235.             break;
  236.  
  237.         case 'Y':    /* year with century */
  238.             sprintf(tbuf, "%d", 1900 + timeptr->tm_year);
  239.             break;
  240.  
  241.         case 'Z':    /* time zone name or abbrevation */
  242.             i = 0;
  243.             if (daylight && timeptr->tm_isdst)
  244.                 i = 1;
  245.             strcpy(tbuf, tzname[i]);
  246.             break;
  247.  
  248. #ifdef SYSV_EXT
  249.         case 'n':    /* same as \n */
  250.             tbuf[0] = '\n';
  251.             tbuf[1] = '\0';
  252.             break;
  253.  
  254.         case 't':    /* same as \t */
  255.             tbuf[0] = '\t';
  256.             tbuf[1] = '\0';
  257.             break;
  258.  
  259.         case 'D':    /* date as %m/%d/%y */
  260.             strftime(tbuf, sizeof tbuf, "%m/%d/%y", timeptr);
  261.             break;
  262.  
  263.         case 'e':    /* day of month, blank padded */
  264.             sprintf(tbuf, "%2d", timeptr->tm_mday);
  265.             break;
  266.  
  267.         case 'r':    /* time as %I:%M:%S %p */
  268.             strftime(tbuf, sizeof tbuf, "%I:%M:%S %p", timeptr);
  269.             break;
  270.  
  271.         case 'R':    /* time as %H:%M */
  272.             strftime(tbuf, sizeof tbuf, "%H:%M", timeptr);
  273.             break;
  274.  
  275.         case 'T':    /* time as %H:%M:%S */
  276.             strftime(tbuf, sizeof tbuf, "%H:%M:%S", timeptr);
  277.             break;
  278. #endif
  279.  
  280.  
  281. #ifdef VMS_EXT
  282.         case 'V':    /* date as dd-bbb-YYYY */
  283.             sprintf(tbuf, "%2d-%3.3s-%4d",
  284.                 timeptr->tm_mday,
  285.                 months_a[timeptr->tm_mon],
  286.                 timeptr->tm_year + 1900);
  287.             for (i = 3; i < 6; i++)
  288.                 if (islower(tbuf[i]))
  289.                     tbuf[i] = toupper(tbuf[i]);
  290.             break;
  291. #endif
  292.  
  293.  
  294. #ifdef POSIX2_DATE
  295.         case 'C':
  296.             sprintf(tbuf, "%02d", (timeptr->tm_year + 1900) / 100);
  297.             break;
  298.  
  299.  
  300.         case 'E':
  301.         case 'O':
  302.             /* POSIX locale extensions, ignored for now */
  303.             goto again;
  304. #endif
  305.         default:
  306.             tbuf[0] = '%';
  307.             tbuf[1] = *format;
  308.             tbuf[2] = '\0';
  309.             break;
  310.         }
  311.         i = strlen(tbuf);
  312.         if (i)
  313.             if (s + i < endp - 1) {
  314.                 strcpy(s, tbuf);
  315.                 s += i;
  316.             } else
  317.                 return 0;
  318.     }
  319. out:
  320.     if (s < endp && *format == '\0') {
  321.         *s = '\0';
  322.         return (s - start);
  323.     } else
  324.         return 0;
  325. }
  326.  
  327. /* weeknumber --- figure how many weeks into the year */
  328.  
  329. /* With thanks and tip of the hatlo to ado@elsie.nci.nih.gov */
  330.  
  331. #ifndef __STDC__
  332. static int
  333. weeknumber(timeptr, firstweekday)
  334. const struct tm *timeptr;
  335. int firstweekday;
  336. #else
  337. static int
  338. weeknumber(const struct tm *timeptr, int firstweekday)
  339. #endif
  340. {
  341.     if (firstweekday == 0)
  342.         return (timeptr->tm_yday + 7 - timeptr->tm_wday) / 7;
  343.     else
  344.         return (timeptr->tm_yday + 7 -
  345.             (timeptr->tm_wday ? (timeptr->tm_wday - 1) : 6)) / 7;
  346. }
  347.  
  348. #if 0
  349. /* ADR --- I'm loathe to mess with ado's code ... */
  350.  
  351. Date:         Wed, 24 Apr 91 20:54:08 MDT
  352. From: Michal Jaegermann <audfax!emory!vm.ucs.UAlberta.CA!NTOMCZAK>
  353. To: arnold@audiofax.com
  354.  
  355. Hi Arnold,
  356. in a process of fixing of strftime() in libraries on Atari ST I grabbed
  357. some pieces of code from your own strftime.  When doing that it came
  358. to mind that your weeknumber() function compiles a little bit nicer
  359. in the following form:
  360. /*
  361.  * firstweekday is 0 if starting in Sunday, non-zero if in Monday
  362.  */
  363. {
  364.     return (timeptr->tm_yday - timeptr->tm_wday +
  365.         (firstweekday ? (timeptr->tm_wday ? 8 : 1) : 7)) / 7;
  366. }
  367. How nicer it depends on a compiler, of course, but always a tiny bit.
  368.  
  369.    Cheers,
  370.    Michal
  371.    ntomczak@vm.ucs.ualberta.ca
  372. #endif
  373.