home *** CD-ROM | disk | FTP | other *** search
/ Fresh Fish 9 / FreshFishVol9-CD2.bin / bbs / gnu / libnix-0.8-src.lha / libnix-0.8 / sources / nix / time / strftime.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-12-12  |  3.4 KB  |  132 lines

  1. #include <time.h>
  2. #include <locale.h>
  3. #include <libraries/locale.h>
  4. #ifdef __GNUC__
  5. #include <inline/locale.h>
  6. #endif
  7.  
  8. #define ADDS(st)  tmp=strftime(s,maxsize-size,(st),timeptr);break;
  9.  
  10. #define ADDN(a,b) tmp=strfnumb(s,maxsize-size,(a),(b));break;
  11.  
  12. #define STOR(c)   if(++size<=maxsize)*s++=(c);
  13.  
  14. #define STR(a) \
  15. (__localevec[LC_TIME-1]==NULL?strings[(a)-1]:GetLocaleStr(__localevec[LC_TIME-1],(a)))
  16.  
  17. extern struct Locale *__localevec[];
  18.  
  19. /* All calendar strings */
  20. static const unsigned char *strings[]=
  21. { "Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday",
  22.   "Sun","Mon","Tue","Wed","Thu","Fri","Sat",
  23.   "January","February","March","April","May","June",
  24.   "July","August","September","October","November","December",
  25.   "Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec",
  26.   "","","AM","PM" };
  27.  
  28. static size_t strfnumb(char *s,size_t maxsize,signed int places,size_t value)
  29. { size_t size=0;
  30.   if(places>1)
  31.     size=strfnumb(s,maxsize,places-1,value/10);
  32.   else if(value>=10)
  33.     size=strfnumb(s,maxsize,places+1,value/10);
  34.   else
  35.     while ((places++<-1) && (++size<=maxsize)) s[size-1]=' ';
  36.   if(++size<=maxsize)
  37.     s[size-1]=(value%10+'0');
  38.   return size;
  39. }
  40.  
  41. size_t strftime(char *s,size_t maxsize,const char *format,const struct tm *timeptr)
  42. { size_t size=0,tmp;
  43.   while(*format)
  44.   { if(*format=='%')
  45.     { tmp=0;
  46.       switch(*++format)
  47.       { case 'a':
  48.           ADDS(STR(ABDAY_1+timeptr->tm_wday));
  49.         case 'b':
  50.         case 'h':
  51.           ADDS(STR(ABMON_1+timeptr->tm_mon));
  52.         case 'c':
  53.           ADDS("%m/%d/%y");
  54.         case 'd':
  55.           ADDN(2,timeptr->tm_mday);
  56.         case 'e':
  57.           ADDN(-2,timeptr->tm_mday);
  58.         case 'j':
  59.           ADDN(3,timeptr->tm_yday+1);
  60.         case 'k':
  61.           ADDN(-2,timeptr->tm_hour);
  62.         case 'l':
  63.           ADDN(-2,timeptr->tm_hour%12+(timeptr->tm_hour%12==0)*12);
  64.         case 'm':
  65.           ADDN(2,timeptr->tm_mon+1);
  66.         case 'p':
  67.           ADDS(STR(AM_STR+(timeptr->tm_hour>=12)));
  68.         case 'r':
  69.           ADDS("%I:%M:%S %p");
  70.         case 'w':
  71.           ADDN(1,timeptr->tm_wday);
  72.         case 'x':
  73.           ADDS("%m/%d/%y %H:%M:%S");
  74.         case 'y':
  75.           ADDN(2,timeptr->tm_year%100);
  76.         case 'A':
  77.           ADDS(STR(DAY_1+timeptr->tm_wday));
  78.         case 'B':
  79.           ADDS(STR(MON_1+timeptr->tm_mon));
  80.         case 'C':
  81.           ADDS("%a %b %e %H:%M:%S %Y");
  82.         case 'D':
  83.           ADDS("%m/%d/%y");
  84.         case 'H':
  85.           ADDN(2,timeptr->tm_hour);
  86.         case 'I':
  87.           ADDN(2,timeptr->tm_hour%12+(timeptr->tm_hour%12==0)*12);
  88.         case 'M':
  89.           ADDN(2,timeptr->tm_min);
  90.         case 'R':
  91.           ADDS("%H:%M");
  92.         case 'S':
  93.           ADDN(2,timeptr->tm_sec);
  94.         case 'T':
  95.         case 'X':
  96.           ADDS("%H:%M:%S");
  97.         case 'U':
  98.           ADDN(2,(timeptr->tm_yday+7-timeptr->tm_wday)/7);
  99.         case 'W':
  100.           ADDN(2,(timeptr->tm_yday+7-(6+timeptr->tm_wday)%7)/7);
  101.         case 'Y':
  102.           ADDN(4,timeptr->tm_year+1900);
  103.         case 't':
  104.           STOR('\t');
  105.           break;
  106.         case 'n':
  107.           STOR('\n');
  108.           break;
  109.         case '%':
  110.           STOR('%');
  111.           break;
  112.         case '\0':
  113.           format--;
  114.           break;
  115.       }
  116.       size+=tmp;
  117.       s+=tmp;
  118.     }
  119.     else
  120.       STOR(*format);
  121.     format++;
  122.   }
  123.   STOR('\0');
  124.   if(size>maxsize)
  125.   { s-=size;
  126.     if(maxsize) /* Don't know if this is necessary, therefore it's here ;-) */
  127.       s[maxsize-1]='\0';
  128.     size=1;
  129.   }
  130.   return size-1;
  131. }
  132.