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