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