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.old < prev    next >
Encoding:
Text File  |  1992-10-23  |  10.6 KB  |  433 lines

  1. /* strftime - custom formatting of date and/or time
  2.    Copyright (C) 1989, 1991, 1992 Free Software Foundation, Inc.
  3.  
  4.    This program is free software; you can redistribute it and/or modify
  5.    it under the terms of the GNU General Public License as published by
  6.    the Free Software Foundation; either version 2, or (at your option)
  7.    any later version.
  8.  
  9.    This program is distributed in the hope that it will be useful,
  10.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.    GNU General Public License for more details.
  13.  
  14.    You should have received a copy of the GNU General Public License
  15.    along with this program; if not, write to the Free Software
  16.    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  17.  
  18. /* Note: this version of strftime lacks locale support,
  19.    but it is standalone.
  20.  
  21.    Performs `%' substitutions similar to those in printf.  Except
  22.    where noted, substituted fields have a fixed size; numeric fields are
  23.    padded if necessary.  Padding is with zeros by default; for fields
  24.    that display a single number, padding can be changed or inhibited by
  25.    following the `%' with one of the modifiers described below.  Unknown
  26.    field specifiers are copied as normal characters.  All other
  27.    characters are copied to the output without change.
  28.  
  29.    Supports a superset of the ANSI C field specifiers.
  30.  
  31.    Literal character fields:
  32.    %    %
  33.    n    newline
  34.    t    tab
  35.  
  36.    Numeric modifiers (a nonstandard extension):
  37.    -    do not pad the field
  38.    _    pad the field with spaces
  39.  
  40.    Time fields:
  41.    %H    hour (00..23)
  42.    %I    hour (01..12)
  43.    %k    hour ( 0..23)
  44.    %l    hour ( 1..12)
  45.    %M    minute (00..59)
  46.    %p    locale's AM or PM
  47.    %r    time, 12-hour (hh:mm:ss [AP]M)
  48.    %R    time, 24-hour (hh:mm)
  49.    %S    second (00..61)
  50.    %T    time, 24-hour (hh:mm:ss)
  51.    %X    locale's time representation (%H:%M:%S)
  52.    %Z    time zone (EDT), or nothing if no time zone is determinable
  53.  
  54.    Date fields:
  55.    %a    locale's abbreviated weekday name (Sun..Sat)
  56.    %A    locale's full weekday name, variable length (Sunday..Saturday)
  57.    %b    locale's abbreviated month name (Jan..Dec)
  58.    %B    locale's full month name, variable length (January..December)
  59.    %c    locale's date and time (Sat Nov 04 12:02:33 EST 1989)
  60.    %C    century (00..99)
  61.    %d    day of month (01..31)
  62.    %e    day of month ( 1..31)
  63.    %D    date (mm/dd/yy)
  64.    %h    same as %b
  65.    %j    day of year (001..366)
  66.    %m    month (01..12)
  67.    %U    week number of year with Sunday as first day of week (00..53)
  68.    %w    day of week (0..6)
  69.    %W    week number of year with Monday as first day of week (00..53)
  70.    %x    locale's date representation (mm/dd/yy)
  71.    %y    last two digits of year (00..99)
  72.    %Y    year (1970...)
  73.  
  74.    David MacKenzie <djm@gnu.ai.mit.edu> */
  75.  
  76. #define HAVE_TZNAME
  77.  
  78. #include <sys/types.h>
  79. #if defined(TM_IN_SYS_TIME) || (!defined(HAVE_TM_ZONE) && !defined(HAVE_TZNAME))
  80. #include <sys/time.h>
  81. #else
  82. #include <time.h>
  83. #endif
  84.  
  85. #ifndef linux
  86. #if defined(HAVE_TZNAME)
  87. extern char *tzname[2];
  88. #endif
  89. #endif
  90.  
  91. #if !__STDC__
  92. #define const
  93. #endif
  94.  
  95. /* Types of padding for numbers in date and time. */
  96. enum padding
  97. {
  98.   none, blank, zero
  99. };
  100.  
  101. static char *days[] =
  102. {
  103.   "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"
  104. };
  105.  
  106. static char *months[] =
  107. {
  108.   "January", "February", "March", "April", "May", "June",
  109.   "July", "August", "September", "October", "November", "December"
  110. };
  111.  
  112. /* Add character C to STRING and increment LENGTH,
  113.    unless LENGTH would exceed MAX. */
  114.  
  115. #define add_char(c) (length + 1 <= max) && (string[length++] = (c))
  116.  
  117. /* Add a 2 digit number to STRING, padding if specified.
  118.    Return the number of characters added, up to MAX. */
  119.  
  120. static int
  121. add_num2 (string, num, max, pad)
  122.      char *string;
  123.      int num;
  124.      int max;
  125.      enum padding pad;
  126. {
  127.   int top = num / 10;
  128.   int length = 0;
  129.  
  130.   if (top == 0 && pad == blank)
  131.     add_char (' ');
  132.   else if (top != 0 || pad == zero)
  133.     add_char (top + '0');
  134.   add_char (num % 10 + '0');
  135.   return length;
  136. }
  137.  
  138. /* Add a 3 digit number to STRING, padding if specified.
  139.    Return the number of characters added, up to MAX. */
  140.  
  141. static int
  142. add_num3 (string, num, max, pad)
  143.      char *string;
  144.      int num;
  145.      int max;
  146.      enum padding pad;
  147. {
  148.   int top = num / 100;
  149.   int mid = (num - top * 100) / 10;
  150.   int length = 0;
  151.  
  152.   if (top == 0 && pad == blank)
  153.     add_char (' ');
  154.   else if (top != 0 || pad == zero)
  155.     add_char (top + '0');
  156.   if (mid == 0 && top == 0 && pad == blank)
  157.     add_char (' ');
  158.   else if (mid != 0 || top != 0 || pad == zero)
  159.     add_char (mid + '0');
  160.   add_char (num % 10 + '0');
  161.   return length;
  162. }
  163.  
  164. /* Like strncpy except return the number of characters copied. */
  165.  
  166. static int
  167. add_str (to, from, max)
  168.      char *to;
  169.      char *from;
  170.      int max;
  171. {
  172.   int i;
  173.  
  174.   for (i = 0; from[i] && i <= max; ++i)
  175.     to[i] = from[i];
  176.   return i;
  177. }
  178.  
  179. /* Return the week in the year of the time in TM, with the weeks
  180.    starting on Sundays. */
  181.  
  182. static int
  183. sun_week (tm)
  184.      struct tm *tm;
  185. {
  186.   int dl;
  187.  
  188.   /* Set `dl' to the day in the year of the last day of the week previous
  189.      to the one containing the day specified in TM.  If the day specified
  190.      in TM is in the first week of the year, `dl' will be negative or 0.
  191.      Otherwise, calculate the number of complete weeks before our week
  192.      (dl / 7) and add any partial week at the start of the year (dl % 7). */
  193.   dl = tm->tm_yday - tm->tm_wday;
  194.   return dl <= 0 ? 0 : dl / 7 + (dl % 7 != 0);
  195. }
  196.  
  197. /* Return the week in the year of the time in TM, with the weeks
  198.    starting on Mondays. */
  199.  
  200. static int
  201. mon_week (tm)
  202.      struct tm *tm;
  203. {
  204.   int dl, wday;
  205.  
  206.   if (tm->tm_wday == 0)
  207.     wday = 6;
  208.   else
  209.     wday = tm->tm_wday - 1;
  210.   dl = tm->tm_yday - wday;
  211.   return dl <= 0 ? 0 : dl / 7 + (dl % 7 != 0);
  212. }
  213.  
  214. #if !defined(HAVE_TM_ZONE) && !defined(HAVE_TZNAME)
  215. char *
  216. zone_name (tp)
  217.      struct tm *tp;
  218. {
  219.   char *timezone ();
  220.   struct timeval tv;
  221.   struct timezone tz;
  222.  
  223.   __gettimeofday (&tv, &tz);
  224.   return timezone (tz.tz_minuteswest, tp->tm_isdst);
  225. }
  226. #endif
  227.  
  228. /* Format the time given in TM according to FORMAT, and put the
  229.    results in STRING.
  230.    Return the number of characters (not including terminating null)
  231.    that were put into STRING, or 0 if the length would have
  232.    exceeded MAX. */
  233.  
  234. size_t
  235. strftime (string, max, format, tm)
  236.      char *string;
  237.      size_t max;
  238.      const char *format;
  239.      const struct tm *tm;
  240. {
  241.   enum padding pad;        /* Type of padding to apply. */
  242.   size_t length = 0;        /* Characters put in STRING so far. */
  243.  
  244.   for (; *format && length < max; ++format)
  245.     {
  246.       if (*format != '%')
  247.     add_char (*format);
  248.       else
  249.     {
  250.       ++format;
  251.       /* Modifiers: */
  252.       if (*format == '-')
  253.         {
  254.           pad = none;
  255.           ++format;
  256.         }
  257.       else if (*format == '_')
  258.         {
  259.           pad = blank;
  260.           ++format;
  261.         }
  262.       else
  263.         pad = zero;
  264.  
  265.       switch (*format)
  266.         {
  267.           /* Literal character fields: */
  268.         case 0:
  269.         case '%':
  270.           add_char ('%');
  271.           break;
  272.         case 'n':
  273.           add_char ('\n');
  274.           break;
  275.         case 't':
  276.           add_char ('\t');
  277.           break;
  278.         default:
  279.           add_char (*format);
  280.           break;
  281.  
  282.           /* Time fields: */
  283.         case 'H':
  284.         case 'k':
  285.           length +=
  286.         add_num2 (&string[length], tm->tm_hour, max - length,
  287.               *format == 'H' ? pad : blank);
  288.           break;
  289.         case 'I':
  290.         case 'l':
  291.           {
  292.         int hour12;
  293.  
  294.         if (tm->tm_hour == 0)
  295.           hour12 = 12;
  296.         else if (tm->tm_hour > 12)
  297.           hour12 = tm->tm_hour - 12;
  298.         else
  299.           hour12 = tm->tm_hour;
  300.         length +=
  301.           add_num2 (&string[length], hour12, max - length,
  302.                 *format == 'I' ? pad : blank);
  303.           }
  304.           break;
  305.         case 'M':
  306.           length +=
  307.         add_num2 (&string[length], tm->tm_min, max - length, pad);
  308.           break;
  309.         case 'p':
  310.           if (tm->tm_hour < 12)
  311.         add_char ('A');
  312.           else
  313.         add_char ('P');
  314.           add_char ('M');
  315.           break;
  316.         case 'r':
  317.           length +=
  318.         strftime (&string[length], max - length, "%I:%M:%S %p", tm);
  319.           break;
  320.         case 'R':
  321.           length +=
  322.         strftime (&string[length], max - length, "%H:%M", tm);
  323.           break;
  324.         case 'S':
  325.           length +=
  326.         add_num2 (&string[length], tm->tm_sec, max - length, pad);
  327.           break;
  328.         case 'T':
  329.           length +=
  330.         strftime (&string[length], max - length, "%H:%M:%S", tm);
  331.           break;
  332.         case 'X':
  333.           length +=
  334.         strftime (&string[length], max - length, "%H:%M:%S", tm);
  335.           break;
  336.         case 'Z':
  337. #ifdef HAVE_TM_ZONE
  338.           length += add_str (&string[length], tm->tm_zone, max - length);
  339. #else
  340. #ifdef HAVE_TZNAME
  341.           if (tm->tm_isdst && tzname[1] && *tzname[1])
  342.         length += add_str (&string[length], tzname[1], max - length);
  343.           else
  344.         length += add_str (&string[length], tzname[0], max - length);
  345. #else
  346.           length += add_str (&string[length], zone_name (tm), max - length);
  347. #endif
  348. #endif
  349.           break;
  350.  
  351.           /* Date fields: */
  352.         case 'a':
  353.           add_char (days[tm->tm_wday][0]);
  354.           add_char (days[tm->tm_wday][1]);
  355.           add_char (days[tm->tm_wday][2]);
  356.           break;
  357.         case 'A':
  358.           length +=
  359.         add_str (&string[length], days[tm->tm_wday], max - length);
  360.           break;
  361.         case 'b':
  362.         case 'h':
  363.           add_char (months[tm->tm_mon][0]);
  364.           add_char (months[tm->tm_mon][1]);
  365.           add_char (months[tm->tm_mon][2]);
  366.           break;
  367.         case 'B':
  368.           length +=
  369.         add_str (&string[length], months[tm->tm_mon], max - length);
  370.           break;
  371.         case 'c':
  372.           length +=
  373.         strftime (&string[length], max - length,
  374.               "%a %b %d %H:%M:%S %Z %Y", tm);
  375.           break;
  376.         case 'C':
  377.           length +=
  378.         add_num2 (&string[length], (tm->tm_year + 1900) / 100,
  379.               max - length, pad);
  380.           break;
  381.         case 'd':
  382.           length +=
  383.         add_num2 (&string[length], tm->tm_mday, max - length, pad);
  384.           break;
  385.         case 'e':
  386.           length +=
  387.         add_num2 (&string[length], tm->tm_mday, max - length, blank);
  388.           break;
  389.         case 'D':
  390.           length +=
  391.         strftime (&string[length], max - length, "%m/%d/%y", tm);
  392.           break;
  393.         case 'j':
  394.           length +=
  395.         add_num3 (&string[length], tm->tm_yday + 1, max - length, pad);
  396.           break;
  397.         case 'm':
  398.           length +=
  399.         add_num2 (&string[length], tm->tm_mon + 1, max - length, pad);
  400.           break;
  401.         case 'U':
  402.           length +=
  403.         add_num2 (&string[length], sun_week (tm), max - length, pad);
  404.           break;
  405.         case 'w':
  406.           add_char (tm->tm_wday + '0');
  407.           break;
  408.         case 'W':
  409.           length +=
  410.         add_num2 (&string[length], mon_week (tm), max - length, pad);
  411.           break;
  412.         case 'x':
  413.           length +=
  414.         strftime (&string[length], max - length, "%m/%d/%y", tm);
  415.           break;
  416.         case 'y':
  417.           length +=
  418.         add_num2 (&string[length], tm->tm_year % 100,
  419.               max - length, pad);
  420.           break;
  421.         case 'Y':
  422.           add_char ((tm->tm_year + 1900) / 1000 + '0');
  423.           length +=
  424.         add_num3 (&string[length],
  425.               (1900 + tm->tm_year) % 1000, max - length, zero);
  426.           break;
  427.         }
  428.     }
  429.     }
  430.   add_char (0);
  431.   return length - 1;
  432. }
  433.