home *** CD-ROM | disk | FTP | other *** search
/ The Atari Compendium / The Atari Compendium (Toad Computers) (1994).iso / files / prgtools / gnustuff / tos / futils / futils~1 / src / shell13s.zoo / shell1.3 / lib / strftime.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-11-06  |  10.5 KB  |  417 lines

  1. /* strftime - custom formatting of date and/or time
  2.    Copyright (C) 1989-1991 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 (00..12)
  43.    %M    minute (00..59)
  44.    %p    locale's AM or PM
  45.    %r    time, 12-hour (hh:mm:ss [AP]M)
  46.    %R    time, 24-hour (hh:mm)
  47.    %S    second (00..61)
  48.    %T    time, 24-hour (hh:mm:ss)
  49.    %X    locale's time representation (%H:%M:%S)
  50.    %Z    time zone (EDT), or nothing if no time zone is determinable
  51.  
  52.    Date fields:
  53.    %a    locale's abbreviated weekday name (Sun..Sat)
  54.    %A    locale's full weekday name, variable length (Sunday..Saturday)
  55.    %b    locale's abbreviated month name (Jan..Dec)
  56.    %B    locale's full month name, variable length (January..December)
  57.    %c    locale's date and time (Sat Nov 04 12:02:33 EST 1989)
  58.    %C    century (00..99)
  59.    %d    day of month (01..31)
  60.    %e    day of month ( 1..31)
  61.    %D    date (mm/dd/yy)
  62.    %h    same as %b
  63.    %j    day of year (001..366)
  64.    %m    month (01..12)
  65.    %U    week number of year with Sunday as first day of week (00..53)
  66.    %w    day of week (0..6)
  67.    %W    week number of year with Monday as first day of week (00..53)
  68.    %x    locale's date representation (mm/dd/yy)
  69.    %y    last two digits of year (00..99)
  70.    %Y    year (1970...)
  71.  
  72.    David MacKenzie <djm@ai.mit.edu> */
  73.  
  74. #include <sys/types.h>
  75. #if defined(TM_IN_SYS_TIME) || defined(TZNAME_MISSING)
  76. #include <sys/time.h>
  77. #else
  78. #include <time.h>
  79. #endif
  80.  
  81. #if defined(TM_ZONE_MISSING) || !defined(TZNAME_MISSING)
  82. extern char *tzname[2];
  83. #endif
  84.  
  85. /* Types of padding for numbers in date and time. */
  86. enum padding
  87. {
  88.   none, blank, zero
  89. };
  90.  
  91. static char *days[] =
  92. {
  93.   "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"
  94. };
  95.  
  96. static char *months[] =
  97. {
  98.   "January", "February", "March", "April", "May", "June",
  99.   "July", "August", "September", "October", "November", "December"
  100. };
  101.  
  102. /* Add character C to STRING and increment LENGTH,
  103.    unless LENGTH would exceed MAX. */
  104.  
  105. #define add_char(c) (length + 1 <= max) && (string[length++] = (c))
  106.  
  107. /* Add a 2 digit number to STRING, padding if specified.
  108.    Return the number of characters added, up to MAX. */
  109.  
  110. static int
  111. add_num2 (string, num, max, pad)
  112.      char *string;
  113.      int num;
  114.      int max;
  115.      enum padding pad;
  116. {
  117.   int top = num / 10;
  118.   int length = 0;
  119.  
  120.   if (top == 0 && pad == blank)
  121.     add_char (' ');
  122.   else if (top != 0 || pad == zero)
  123.     add_char (top + '0');
  124.   add_char (num % 10 + '0');
  125.   return length;
  126. }
  127.  
  128. /* Add a 3 digit number to STRING, padding if specified.
  129.    Return the number of characters added, up to MAX. */
  130.  
  131. static int
  132. add_num3 (string, num, max, pad)
  133.      char *string;
  134.      int num;
  135.      int max;
  136.      enum padding pad;
  137. {
  138.   int top = num / 100;
  139.   int mid = (num - top * 100) / 10;
  140.   int length = 0;
  141.  
  142.   if (top == 0 && pad == blank)
  143.     add_char (' ');
  144.   else if (top != 0 || pad == zero)
  145.     add_char (top + '0');
  146.   if (mid == 0 && top == 0 && pad == blank)
  147.     add_char (' ');
  148.   else if (mid != 0 || top != 0 || pad == zero)
  149.     add_char (mid + '0');
  150.   add_char (num % 10 + '0');
  151.   return length;
  152. }
  153.  
  154. /* Like strncpy except return the number of characters copied. */
  155.  
  156. static int
  157. add_str (to, from, max)
  158.      char *to;
  159.      char *from;
  160.      int max;
  161. {
  162.   int i;
  163.  
  164.   for (i = 0; from[i] && i <= max; ++i)
  165.     to[i] = from[i];
  166.   return i;
  167. }
  168.  
  169. /* Return the week in the year of the time in TM, with the weeks
  170.    starting on Sundays. */
  171.  
  172. static int
  173. sun_week (tm)
  174.      struct tm *tm;
  175. {
  176.   int dl;
  177.  
  178.   /* Set `dl' to the day in the year of the last day of the week previous
  179.      to the one containing the day specified in TM.  If the day specified
  180.      in TM is in the first week of the year, `dl' will be negative or 0.
  181.      Otherwise, calculate the number of complete weeks before our week
  182.      (dl / 7) and add any partial week at the start of the year (dl % 7). */
  183.   dl = tm->tm_yday - tm->tm_wday;
  184.   return dl <= 0 ? 0 : dl / 7 + (dl % 7 != 0);
  185. }
  186.  
  187. /* Return the week in the year of the time in TM, with the weeks
  188.    starting on Mondays. */
  189.  
  190. static int
  191. mon_week (tm)
  192.      struct tm *tm;
  193. {
  194.   int dl, wday;
  195.  
  196.   if (tm->tm_wday == 0)
  197.     wday = 6;
  198.   else
  199.     wday = tm->tm_wday - 1;
  200.   dl = tm->tm_yday - wday;
  201.   return dl <= 0 ? 0 : dl / 7 + (dl % 7 != 0);
  202. }
  203.  
  204. #ifdef TZNAME_MISSING
  205. char *
  206. zone_name (tp)
  207.      struct tm *tp;
  208. {
  209.   char *timezone ();
  210.   struct timeval tv;
  211.   struct timezone tz;
  212.  
  213.   gettimeofday (&tv, &tz);
  214.   return timezone (tz.tz_minuteswest, tp->tm_isdst);
  215. }
  216. #endif
  217.  
  218. /* Format the time given in TM according to FORMAT, and put the
  219.    results in STRING.
  220.    Return the number of characters (not including terminating null)
  221.    that were put into STRING, or 0 if the length would have
  222.    exceeded MAX. */
  223.  
  224. size_t
  225. strftime (string, max, format, tm)
  226.      char *string;
  227.      size_t max;
  228.      char *format;
  229.      struct tm *tm;
  230. {
  231.   enum padding pad;        /* Type of padding to apply. */
  232.   size_t length = 0;        /* Characters put in STRING so far. */
  233.  
  234.   for (; *format && length < max; ++format)
  235.     {
  236.       if (*format != '%')
  237.     add_char (*format);
  238.       else
  239.     {
  240.       ++format;
  241.       /* Modifiers: */
  242.       if (*format == '-')
  243.         {
  244.           pad = none;
  245.           ++format;
  246.         }
  247.       else if (*format == '_')
  248.         {
  249.           pad = blank;
  250.           ++format;
  251.         }
  252.       else
  253.         pad = zero;
  254.  
  255.       switch (*format)
  256.         {
  257.           /* Literal character fields: */
  258.         case 0:
  259.         case '%':
  260.           add_char ('%');
  261.           break;
  262.         case 'n':
  263.           add_char ('\n');
  264.           break;
  265.         case 't':
  266.           add_char ('\t');
  267.           break;
  268.         default:
  269.           add_char (*format);
  270.           break;
  271.  
  272.           /* Time fields: */
  273.         case 'H':
  274.           length +=
  275.         add_num2 (&string[length], tm->tm_hour, max - length, pad);
  276.           break;
  277.         case 'I':
  278.           if (tm->tm_hour == 0)
  279.         length +=
  280.           add_num2 (&string[length], 12, max - length, pad);
  281.           else if (tm->tm_hour > 12)
  282.         length +=
  283.           add_num2 (&string[length],
  284.                 tm->tm_hour - 12, max - length, pad);
  285.           else
  286.         length +=
  287.           add_num2 (&string[length], tm->tm_hour, max - length, pad);
  288.           break;
  289.         case 'M':
  290.           length +=
  291.         add_num2 (&string[length], tm->tm_min, max - length, pad);
  292.           break;
  293.         case 'p':
  294.           if (tm->tm_hour < 12)
  295.         add_char ('A');
  296.           else
  297.         add_char ('P');
  298.           add_char ('M');
  299.           break;
  300.         case 'r':
  301.           length +=
  302.         strftime (&string[length], max - length, "%I:%M:%S %p", tm);
  303.           break;
  304.         case 'R':
  305.           length +=
  306.         strftime (&string[length], max - length, "%H:%M", tm);
  307.           break;
  308.         case 'S':
  309.           length +=
  310.         add_num2 (&string[length], tm->tm_sec, max - length, pad);
  311.           break;
  312.         case 'T':
  313.           length +=
  314.         strftime (&string[length], max - length, "%H:%M:%S", tm);
  315.           break;
  316.         case 'X':
  317.           length +=
  318.         strftime (&string[length], max - length, "%H:%M:%S", tm);
  319.           break;
  320.         case 'Z':
  321. #ifndef TM_ZONE_MISSING
  322.           length += add_str (&string[length], tm->tm_zone, max - length);
  323. #else
  324. #ifndef TZNAME_MISSING
  325.           if (tm->tm_isdst && tzname[1] && *tzname[1])
  326.         length += add_str (&string[length], tzname[1], max - length);
  327.           else
  328.         length += add_str (&string[length], tzname[0], max - length);
  329. #else
  330.           length += add_str (&string[length], zone_name (tm), max - length);
  331. #endif
  332. #endif
  333.           break;
  334.  
  335.           /* Date fields: */
  336.         case 'a':
  337.           add_char (days[tm->tm_wday][0]);
  338.           add_char (days[tm->tm_wday][1]);
  339.           add_char (days[tm->tm_wday][2]);
  340.           break;
  341.         case 'A':
  342.           length +=
  343.         add_str (&string[length], days[tm->tm_wday], max - length);
  344.           break;
  345.         case 'b':
  346.         case 'h':
  347.           add_char (months[tm->tm_mon][0]);
  348.           add_char (months[tm->tm_mon][1]);
  349.           add_char (months[tm->tm_mon][2]);
  350.           break;
  351.         case 'B':
  352.           length +=
  353.         add_str (&string[length], months[tm->tm_mon], max - length);
  354.           break;
  355.         case 'c':
  356.           length +=
  357.         strftime (&string[length], max - length,
  358.               "%a %b %d %H:%M:%S %Z %Y", tm);
  359.           break;
  360.         case 'C':
  361.           length +=
  362.         add_num2 (&string[length], (tm->tm_year + 1900) / 100,
  363.               max - length, pad);
  364.           break;
  365.         case 'd':
  366.           length +=
  367.         add_num2 (&string[length], tm->tm_mday, max - length, pad);
  368.           break;
  369.         case 'e':
  370.           length +=
  371.         add_num2 (&string[length], tm->tm_mday, max - length, blank);
  372.           break;
  373.         case 'D':
  374.           length +=
  375.         strftime (&string[length], max - length, "%m/%d/%y", tm);
  376.           break;
  377.         case 'j':
  378.           length +=
  379.         add_num3 (&string[length], tm->tm_yday + 1, max - length, pad);
  380.           break;
  381.         case 'm':
  382.           length +=
  383.         add_num2 (&string[length], tm->tm_mon + 1, max - length, pad);
  384.           break;
  385.         case 'U':
  386.           length +=
  387.         add_num2 (&string[length], sun_week (tm), max - length, pad);
  388.           break;
  389.         case 'w':
  390.           add_char (tm->tm_wday + '0');
  391.           break;
  392.         case 'W':
  393.           length +=
  394.         add_num2 (&string[length], mon_week (tm), max - length, pad);
  395.           break;
  396.         case 'x':
  397.           length +=
  398.         strftime (&string[length], max - length, "%m/%d/%y", tm);
  399.           break;
  400.         case 'y':
  401.           length +=
  402.         add_num2 (&string[length], tm->tm_year % 100,
  403.               max - length, pad);
  404.           break;
  405.         case 'Y':
  406.           add_char ((tm->tm_year + 1900) / 1000 + '0');
  407.           length +=
  408.         add_num3 (&string[length],
  409.               (1900 + tm->tm_year) % 1000, max - length, zero);
  410.           break;
  411.         }
  412.     }
  413.     }
  414.   add_char (0);
  415.   return length - 1;
  416. }
  417.