home *** CD-ROM | disk | FTP | other *** search
/ Serving the Web / ServingTheWeb1995.disc1of1.iso / linux / slacksrce / d / libc / libc-4.6 / libc-4 / libc-linux / time / strftime.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-11-19  |  9.2 KB  |  400 lines

  1. /* Extensions for GNU date that are still missing here:
  2.    -
  3.    _
  4. */
  5.  
  6. /* Copyright (C) 1991, 1992, 1993 Free Software Foundation, Inc.
  7. This file is part of the GNU C Library.
  8.  
  9. The GNU C Library is free software; you can redistribute it and/or
  10. modify it under the terms of the GNU Library General Public License as
  11. published by the Free Software Foundation; either version 2 of the
  12. License, or (at your option) any later version.
  13.  
  14. The GNU C Library is distributed in the hope that it will be useful,
  15. but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  17. Library General Public License for more details.
  18.  
  19. You should have received a copy of the GNU Library General Public
  20. License along with the GNU C Library; see the file COPYING.LIB.  If
  21. not, write to the Free Software Foundation, Inc., 675 Mass Ave,
  22. Cambridge, MA 02139, USA.  */
  23.  
  24. #include <ansidecl.h>
  25. #include <localeinfo.h>
  26. #include <ctype.h>
  27. #include <limits.h>
  28. #include <stddef.h>
  29. #include <stdio.h>
  30. #include <stdlib.h>
  31. #include <string.h>
  32. #include <time.h>
  33.  
  34. #if !defined(HAVE_GNU_LD) && !defined (__ELF__)
  35. #define    __tzname    tzname
  36. #define    __daylight    daylight
  37. #define    __timezone    timezone
  38. #endif
  39.  
  40.  
  41. #define    add(n, f)                                  \
  42.   do                                          \
  43.     {                                          \
  44.       i += (n);                                      \
  45.       if (i >= maxsize)                                  \
  46.     return 0;                                  \
  47.       else                                      \
  48.     if (p != NULL)                                  \
  49.       {                                      \
  50.         f;                                      \
  51.         p += (n);                                  \
  52.       }                                      \
  53.     } while (0)
  54. #define    cpy(n, s)    add((n), memcpy((PTR) p, (PTR) (s), (n)))
  55. #define    fmt(n, args)    add((n), if (sprintf args != (n)) return 0)
  56.  
  57. /* Return the week in the year specified by TP,
  58.    with weeks starting on STARTING_DAY.  */
  59. #ifdef    __GNUC__
  60. inline
  61. #endif
  62. static unsigned int
  63. DEFUN(week, (tp, starting_day),
  64.       CONST struct tm *CONST tp AND int starting_day)
  65. {
  66.   int wday, dl;
  67.  
  68.   wday = tp->tm_wday - starting_day;
  69.   if (wday < 0)
  70.     wday += 7;
  71.  
  72.   /* Set DL to the day in the year of the last day of the week previous to the
  73.      one containing the day specified in TP.  If DL is negative or zero, the
  74.      day specified in TP is in the first week of the year.  Otherwise,
  75.      calculate the number of complete weeks before our week (DL / 7) and
  76.      add any partial week at the start of the year (DL % 7).  */
  77.   dl = tp->tm_yday - wday;
  78.   return dl <= 0 ? 0 : ((dl / 7) + ((dl % 7) == 0 ? 0 : 1));
  79. }
  80.  
  81. static unsigned int
  82. DEFUN(vweek, (tp),
  83.       CONST struct tm *CONST tp)
  84. {
  85.   int vday;
  86.   int numweeks = 0;
  87.  
  88.   /* The week starts on a Monday.  Calculate the number of days since Mon. */
  89.   vday = tp->tm_wday - 1;
  90.   if (vday < 0)
  91.     vday += 7;
  92.  
  93.   /* Calculate the week number of the year.  The first calculation gives
  94.      the number of weeks since the first week of the year. */
  95.   numweeks = ((tp->tm_yday - vday) + 6) / 7;
  96.  
  97.   /* We now analyse the first week of the year.  If there are more than
  98.      4 days in the week containing Jan. 1, then we can count it as 1 week.
  99.      If not, then we consider that week to be part of the prior year */
  100.   if (((tp->tm_yday - vday + 7)  % 7 >= 4) || ((tp->tm_yday - vday + 7)  % 7 == 0))
  101.     numweeks += 1;
  102.  
  103.   /* The value returned must be in the range [1, 53].  If numweeks equals
  104.      0, then it means that that week is really the last week of the previous
  105.      year.  Hence we must return the value 53 to indicate it is the 53rd
  106.      week of the previous year. */
  107.   if (numweeks == 0)
  108.     numweeks = 53;
  109.  
  110.   return numweeks;
  111. }
  112.  
  113. /* Write information from TP into S according to the format
  114.    string FORMAT, writing no more that MAXSIZE characters
  115.    (including the terminating '\0') and returning number of
  116.    characters written.  If S is NULL, nothing will be written
  117.    anywhere, so to determine how many characters would be
  118.    written, use NULL for S and (size_t) UINT_MAX for MAXSIZE.  */
  119. size_t
  120. DEFUN(strftime, (s, maxsize, format, tp),
  121.       char *s AND size_t maxsize AND
  122.       CONST char *format AND register CONST struct tm *tp)
  123. {
  124. #if 0
  125.   CONST char *CONST a_wkday = _time_info->abbrev_wkday[tp->tm_wday];
  126.   CONST char *CONST f_wkday = _time_info->full_wkday[tp->tm_wday];
  127.   CONST char *CONST a_month = _time_info->abbrev_month[tp->tm_mon];
  128.   CONST char *CONST f_month = _time_info->full_month[tp->tm_mon];
  129.   size_t aw_len = strlen(a_wkday);
  130.   size_t am_len = strlen(a_month);
  131.   size_t wkday_len = strlen(f_wkday);
  132.   size_t month_len = strlen(f_month);
  133.   CONST char *CONST ampm = _time_info->ampm[hour12 >= 12];
  134.   size_t ap_len = strlen(ampm);
  135.   CONST unsigned int y_week0 = week(tp, 0);
  136.   CONST unsigned int y_week1 = week(tp, 1);
  137.   CONST unsigned int v_week = vweek(tp);
  138.   CONST char *zone;
  139.   size_t zonelen;
  140. #else
  141. #define    SET_AWEEK  if (a_wkday == NULL) { \
  142.         a_wkday = _time_info->abbrev_wkday[tp->tm_wday]; \
  143.         aw_len = strlen(a_wkday); \
  144.     }
  145. #define SET_AMONTH if (a_month == NULL) { \
  146.       a_month = _time_info->abbrev_month[tp->tm_mon]; \
  147.       am_len = strlen(a_month); \
  148.     }
  149. #define    SET_FWEEK  if (f_wkday == NULL) { \
  150.       f_wkday = _time_info->full_wkday[tp->tm_wday]; \
  151.       wkday_len = strlen(f_wkday); \
  152.     }
  153. #define SET_FMONTH if (f_month == NULL) { \
  154.       f_month = _time_info->full_month[tp->tm_mon]; \
  155.       month_len = strlen(f_month); \
  156.     }
  157. #define SET_AMPM if (ampm == NULL) { \
  158.       ampm = _time_info->ampm[tp->tm_hour >= 12]; \
  159.       ap_len = strlen(ampm); \
  160.     }
  161. #define SET_Y_WEEK0 if (y_week0 == -1) { y_week0 = week(tp, 0); }
  162. #define SET_Y_WEEK1 if (y_week1 == -1) { y_week1 = week(tp, 1); }
  163. #define SET_V_WEEK if (v_week == -1) { v_week = vweek(tp); }
  164. #define SET_ZONE if (tp->tm_isdst < 0) \
  165.     { zone = ""; zonelen = 0; } \
  166.   else { \
  167.       zone = __tzname[tp->tm_isdst]; \
  168.       zonelen = strlen(zone); \
  169.     }
  170.  
  171.   CONST char *a_wkday = NULL;
  172.   CONST char *f_wkday = NULL;
  173.   CONST char *a_month = NULL;
  174.   CONST char *f_month = NULL;
  175.   size_t aw_len = 0;
  176.   size_t am_len = 0;
  177.   size_t wkday_len = 0;
  178.   size_t month_len = 0;
  179.   CONST char *ampm = NULL;
  180.   size_t ap_len = 0;
  181.   unsigned int y_week0 = -1;
  182.   unsigned int y_week1 = -1;
  183.   unsigned int v_week = -1;
  184.   CONST char *zone = NULL;
  185.   size_t zonelen = 0;
  186. #endif
  187.   int hour12 = tp->tm_hour;
  188.   int dayweek = tp->tm_wday;
  189.   register size_t i = 0;
  190.   register char *p = s;
  191.   register CONST char *f;
  192.  
  193. #if 0
  194.   if (tp->tm_isdst < 0)
  195.     {
  196.       zone = "";
  197.       zonelen = 0;
  198.     }
  199.   else
  200.     {
  201.       zone = __tzname[tp->tm_isdst];
  202.       zonelen = strlen(zone);
  203.     }
  204. #endif
  205.  
  206.   if (hour12 > 12)
  207.     hour12 -= 12;
  208.   else
  209.     if (hour12 == 0) hour12 = 12;
  210.  
  211.   if (dayweek < 1)
  212.     dayweek += 7;
  213.  
  214.   for (f = format; *f != '\0'; ++f)
  215.     {
  216.       CONST char *subfmt;
  217.  
  218.       if (!isascii(*f))
  219.     {
  220.       /* Non-ASCII, may be a multibyte.  */
  221.       int len = mblen(f, strlen(f));
  222.       if (len > 0)
  223.         {
  224.           cpy(len, f);
  225.           continue;
  226.         }
  227.     }
  228.  
  229.       if (*f != '%')
  230.     {
  231.       add(1, *p = *f);
  232.       continue;
  233.     }
  234.  
  235.       ++f;
  236.       switch (*f)
  237.     {
  238.     case '\0':
  239.     case '%':
  240.       add(1, *p = *f);
  241.       break;
  242.  
  243.     case 'a':
  244.       SET_AWEEK;
  245.       cpy(aw_len, a_wkday);
  246.       break;
  247.  
  248.     case 'A':
  249.       SET_FWEEK;
  250.       cpy(wkday_len, f_wkday);
  251.       break;
  252.  
  253.     case 'b':
  254.     case 'h':        /* XOpen extension.  */
  255.       SET_AMONTH;
  256.       cpy(am_len, a_month);
  257.       break;
  258.  
  259.     case 'B':
  260.       SET_FMONTH;
  261.       cpy(month_len, f_month);
  262.       break;
  263.  
  264.     case 'c':
  265.       subfmt = _time_info->date_time;
  266.     subformat:
  267.       {
  268.         size_t len = strftime (p, maxsize - i, subfmt, tp);
  269.         add(len, );
  270.       }
  271.       break;
  272.  
  273.     case 'C':        /* XOpen extension. */
  274.       fmt (2, (p, "%.2d", (1900 + tp->tm_year) / 100));
  275.       break;
  276.  
  277.     case 'D':        /* XOpen extension.  */
  278.       subfmt = "%m/%d/%y";
  279.       goto subformat;
  280.  
  281.     case 'd':
  282.       fmt(2, (p, "%.2d", tp->tm_mday));
  283.       break;
  284.  
  285.     case 'e':        /* XOpen extension: %d, but blank-padded.  */
  286.       fmt(2, (p, "%2d", tp->tm_mday));
  287.       break;
  288.  
  289.     case 'H':
  290.       fmt(2, (p, "%.2d", tp->tm_hour));
  291.       break;
  292.  
  293.     case 'I':
  294.       fmt(2, (p, "%.2d", hour12));
  295.       break;
  296.  
  297.     case 'k':        /* GNU extension.  */
  298.       fmt(2, (p, "%2d", tp->tm_hour));
  299.       break;
  300.  
  301.     case 'l':        /* GNU extension.  */
  302.       fmt(2, (p, "%2d", hour12));
  303.       break;
  304.  
  305.     case 'j':
  306.       fmt(3, (p, "%.3d", tp->tm_yday + 1));
  307.       break;
  308.  
  309.     case 'M':
  310.       fmt(2, (p, "%.2d", tp->tm_min));
  311.       break;
  312.  
  313.     case 'm':
  314.       fmt(2, (p, "%.2d", tp->tm_mon + 1));
  315.       break;
  316.  
  317.     case 'n':        /* XOpen extension.  */
  318.       add (1, *p = '\n');
  319.       break;
  320.  
  321.     case 'p':
  322.       SET_AMPM;
  323.       cpy(ap_len, ampm);
  324.       break;
  325.  
  326.     case 'R':        /* XOpen extension.  */
  327.       subfmt = "%H:%M";
  328.       goto subformat;
  329.  
  330.     case 'r':        /* XOpen extension.  */
  331.       subfmt = "%I:%M:%S %p";
  332.       goto subformat;
  333.  
  334.     case 'S':
  335.       fmt(2, (p, "%.2d", tp->tm_sec));
  336.       break;
  337.  
  338.     case 'T':        /* XOpen extension.  */
  339.       subfmt = "%H:%M:%S";
  340.       goto subformat;
  341.  
  342.     case 't':        /* XOpen extension.  */
  343.       add (1, *p = '\t');
  344.       break;
  345.  
  346.     case 'U':
  347.       SET_Y_WEEK0;
  348.       fmt(2, (p, "%.2u", y_week0));
  349.       break;
  350.  
  351.     case 'u':        /* XPG4 extension.  */
  352.       fmt(2, (p, "%.2d", dayweek));
  353.       break;
  354.  
  355.     case 'V':        /* XPG4 extension.  */
  356.       SET_V_WEEK;
  357.       fmt(2, (p, "%.2u", v_week));
  358.       break;
  359.  
  360.     case 'W':
  361.       SET_Y_WEEK1;
  362.       fmt(2, (p, "%.2u", y_week1));
  363.       break;
  364.  
  365.     case 'w':
  366.       fmt(2, (p, "%.2d", tp->tm_wday));
  367.       break;
  368.  
  369.     case 'X':
  370.       subfmt = _time_info->time;
  371.       goto subformat;
  372.  
  373.     case 'x':
  374.       subfmt = _time_info->date;
  375.       goto subformat;
  376.  
  377.     case 'Y':
  378.       fmt(4, (p, "%.4d", 1900 + tp->tm_year));
  379.       break;
  380.  
  381.     case 'y':
  382.       fmt(2, (p, "%.2d", tp->tm_year));
  383.       break;
  384.  
  385.     case 'Z':
  386.       SET_ZONE;
  387.       cpy(zonelen, zone);
  388.       break;
  389.  
  390.     default:
  391.       /* Bad format.  */
  392.       break;
  393.     }
  394.     }
  395.  
  396.   if (p != NULL)
  397.     *p = '\0';
  398.   return i;
  399. }
  400.