home *** CD-ROM | disk | FTP | other *** search
/ Aminet 18 / aminetcdnumber181997.iso / Aminet / misc / emu / AROSdev.lha / AROS / compiler / clib / strftime.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-01-29  |  3.2 KB  |  148 lines

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