home *** CD-ROM | disk | FTP | other *** search
/ ftp.wwiv.com / ftp.wwiv.com.zip / ftp.wwiv.com / pub / MISC / SQDEV200.ZIP / SRC / STRFTIM.C < prev    next >
C/C++ Source or Header  |  1994-05-23  |  5KB  |  183 lines

  1. /***************************************************************************
  2.  *                                                                         *
  3.  *  Squish Developers Kit Source, Version 2.00                             *
  4.  *  Copyright 1989-1994 by SCI Communications.  All rights reserved.       *
  5.  *                                                                         *
  6.  *  USE OF THIS FILE IS SUBJECT TO THE RESTRICTIONS CONTAINED IN THE       *
  7.  *  SQUISH DEVELOPERS KIT LICENSING AGREEMENT IN SQDEV.PRN.  IF YOU DO NOT *
  8.  *  FIND THE TEXT OF THIS AGREEMENT IN THE AFOREMENTIONED FILE, OR IF YOU  *
  9.  *  DO NOT HAVE THIS FILE, YOU SHOULD IMMEDIATELY CONTACT THE AUTHOR AT    *
  10.  *  ONE OF THE ADDRESSES LISTED BELOW.  IN NO EVENT SHOULD YOU PROCEED TO  *
  11.  *  USE THIS FILE WITHOUT HAVING ACCEPTED THE TERMS OF THE SQUISH          *
  12.  *  DEVELOPERS KIT LICENSING AGREEMENT, OR SUCH OTHER AGREEMENT AS YOU ARE *
  13.  *  ABLE TO REACH WITH THE AUTHOR.                                         *
  14.  *                                                                         *
  15.  *  You can contact the author at one of the address listed below:         *
  16.  *                                                                         *
  17.  *  Scott Dudley       FidoNet     1:249/106                               *
  18.  *  777 Downing St.    Internet    sjd@f106.n249.z1.fidonet.org            *
  19.  *  Kingston, Ont.     CompuServe  >INTERNET:sjd@f106.n249.z1.fidonet.org  *
  20.  *  Canada  K7M 5N3    BBS         1-613-634-3058, V.32bis                 *
  21.  *                                                                         *
  22.  ***************************************************************************/
  23.  
  24. /*# name=strftime() function, since BoreLand (heh) doesn't include
  25.     name=one with TC 2.0!
  26. */
  27.  
  28. #include <stdio.h>
  29. #include <time.h>
  30. #include <stdlib.h>
  31. #include <string.h>
  32. #include "unistr.h"
  33. #include "prog.h"
  34.  
  35. #ifndef NO_STRFTIME
  36.  
  37. /* Note:  TZ environment variable MUST be defined to use the %Z function! */
  38.  
  39. size_t cdecl strftime(char *string, size_t maxsize, const char *format, const struct tm *current_time)
  40. {
  41.   const char *in;
  42.   
  43.   char *out,
  44.        *scrptr;
  45.  
  46.   char temp[250];
  47.  
  48.   maxsize=min(maxsize,230);
  49.  
  50.   for (in=format,out=temp;*in;in++)
  51.   {
  52.     if ((int)(out-(int)temp) >= maxsize)
  53.       break;
  54.  
  55.     if (*in=='%')
  56.     {
  57.       switch(*++in)
  58.       {
  59.         case 'a':
  60.           strcpy(out,weekday_ab[current_time->tm_wday]);
  61.           break;
  62.  
  63.         case 'A':
  64.           strcpy(out,weekday[current_time->tm_wday]);
  65.           break;
  66.  
  67.         case 'b':
  68.           strcpy(out,months_ab[current_time->tm_mon]);
  69.           break;
  70.  
  71.         case 'B':
  72.           strcpy(out,months[current_time->tm_mon]);
  73.           break;
  74.  
  75.         case 'c':
  76.           sprintf(out,"%02d-%02d-%02d %02d:%02d:%02d",
  77.                   current_time->tm_mon+1,
  78.                   current_time->tm_mday,
  79.                   current_time->tm_year,
  80.                   current_time->tm_hour,
  81.                   current_time->tm_min,
  82.                   current_time->tm_sec);
  83.           break;
  84.  
  85.         case 'd':
  86.           sprintf(out,"%02d",current_time->tm_mday);
  87.           break;
  88.  
  89.         case 'H':
  90.           sprintf(out,"%02d",current_time->tm_hour);
  91.           break;
  92.  
  93.         case 'I':
  94.           sprintf(out,"%02d",
  95.                   (current_time->tm_hour >= 0 && current_time->tm_hour <= 12 ?
  96.                    current_time->tm_hour :
  97.                    current_time->tm_hour-12));
  98.           break;
  99.  
  100.         case 'j':
  101.           sprintf(out,"%03d",current_time->tm_yday+1);
  102.           break;
  103.  
  104.         case 'm':
  105.           sprintf(out,"%02d",(current_time->tm_mon)+1);
  106.           break;
  107.  
  108.         case 'M':
  109.           sprintf(out,"%02d",current_time->tm_min);
  110.           break;
  111.  
  112.         case 'p':
  113.           strcpy(out,(current_time->tm_hour < 12 ? "am" : "pm"));
  114.           break;
  115.  
  116.         case 'S':
  117.           sprintf(out,"%02d",current_time->tm_sec);
  118.           break;
  119.  
  120.         case 'U': /* ??? */
  121.           sprintf(out,"%02d",(current_time->tm_yday)/7);
  122.           break;
  123.  
  124.         case 'w':
  125.           sprintf(out,"%d",current_time->tm_wday);
  126.           break;
  127.  
  128.         case 'W': /* ??? */
  129.           sprintf(out,"%02d",(current_time->tm_yday)/7);
  130.           break;
  131.  
  132.         case 'x':
  133.           sprintf(out,"%02d-%02d-%02d",
  134.                   (current_time->tm_mon)+1,
  135.                   current_time->tm_mday,
  136.                   current_time->tm_year);
  137.           break;
  138.  
  139.         case 'X':
  140.           sprintf(out,"%02d:%02d:%02d",
  141.                   current_time->tm_hour,
  142.                   current_time->tm_min,
  143.                   current_time->tm_sec);
  144.           break;
  145.  
  146.         case 'y':
  147.           sprintf(out,"%02d",current_time->tm_year % 100);
  148.           break;
  149.  
  150.         case 'Y':
  151.           sprintf(out,"%02d",current_time->tm_year+1900);
  152.           break;
  153.  
  154.         case 'Z': /* ??? */
  155.  
  156.           if ((scrptr=getenv("TZ")) != 0)
  157.           {
  158.             scrptr[3]=0;
  159.             strcpy(out,strupr(scrptr));
  160.           }
  161.           else strcpy(string,"??T");
  162.           break;
  163.  
  164.         case '%':
  165.           strcpy(out,"%");
  166.           break;
  167.       }
  168.  
  169.       out += strlen(out);
  170.     }
  171.     else *out++=*in;
  172.   }
  173.  
  174.   *out='\0';
  175.  
  176.   strcpy(string,temp);
  177.  
  178.   return(strlen(string));
  179. }
  180.  
  181. #endif /* !NO_STRFTIME */
  182.  
  183.