home *** CD-ROM | disk | FTP | other *** search
/ Atari FTP / ATARI_FTP_0693.zip / ATARI_FTP_0693 / Mint / mntlib25.zoo / ctime.c < prev    next >
C/C++ Source or Header  |  1992-09-05  |  3KB  |  116 lines

  1. #include <time.h>
  2. #include <string.h>
  3. #include <stdio.h>
  4.  
  5. /* taken from Dale Schumacher's dLibs library */
  6.  
  7. static char             timebuf[26];
  8.  
  9. static char             *day[] =
  10.                         {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
  11. static char             *month[] =
  12.                         {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
  13.                         "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
  14.  
  15.  
  16. #ifdef __GNUC__
  17. /* 
  18.  * macro to produce two decimal digits of mval and write them
  19.  * at position pointed by mptr - moves mptr to the next free position;
  20.  * mval has to be in range 0-99, or banshee will get you
  21.  */
  22. #define _TWODIG(mptr,mval)        \
  23. {                    \
  24.         long _i = (long)(mval);        \
  25.      __asm__ volatile("
  26.     extl    %1;
  27.     divu    #10,%1;
  28.     addb    #48,%1;
  29.     moveb    %1,%0@+;
  30.     swap    %1;
  31.     addb    #48,%1;
  32.     moveb    %1,%0@+"            \
  33.           : "=a"((long)mptr)    \
  34.           : "d"(_i), "g"((unsigned short)mval), \
  35.             "0"(mptr));        \
  36. }
  37.  
  38. #else
  39.  
  40. static char *
  41. two_dig(char *buf, unsigned short num) {
  42.     unsigned int rem;
  43.  
  44.     rem = num % 10;
  45.     *buf++ = '0' + (num / 10);
  46.     *buf++ = '0' + rem;
  47.     return buf;
  48. }
  49.  
  50. #endif /* __GNUC__ */
  51.  
  52. char *asctime(time)
  53.         register const struct tm *time;
  54. /*
  55.  *      Convert <time> structure value to a string.  The same format, and
  56.  *      the same internal buffer, as for ctime() is used for this function.
  57.  */
  58.         {
  59.     unsigned short values[6], *vpos, *valp;
  60.     int i;
  61.     char * ptr = timebuf;
  62.     
  63.     (void)strcpy(ptr, "??? ??? ?? ??:??:?? ????\n");
  64.     if (time != NULL)
  65.         {
  66.         vpos = values;
  67.         *vpos++ = time->tm_mday;
  68.         *vpos++ = time->tm_hour;
  69.         *vpos++ = time->tm_min;
  70.         *vpos++ = time->tm_sec;
  71.         i = 1900 + time->tm_year;
  72.         *vpos++ = i / 100;
  73.         *vpos   = i % 100;
  74.         ptr = (char *)memcpy(ptr, day[time->tm_wday], 3) + 3;
  75.         ptr += 1;
  76.         ptr = (char *)memcpy(ptr, month[time->tm_mon], 3) + 3;
  77.         valp = values;
  78.         do
  79.         {
  80.         ptr += 1;
  81. #ifdef __GNUC__
  82.         _TWODIG(ptr, *valp++);
  83. #else
  84.         ptr = two_dig(ptr, *valp++);
  85. #endif /* __GNUC__ */
  86.         }
  87.             while (valp < vpos);
  88. #ifdef __GNUC__
  89.         _TWODIG(ptr, *valp);
  90. #else
  91.         ptr = two_dig(ptr, *valp);
  92. #endif /* __GNUC__ */
  93.         if ('0' == timebuf[8])
  94.         timebuf[8] = ' ';    /* blank out leading zero on a day */
  95.  
  96.         }
  97.     return(timebuf);
  98.         }
  99.  
  100.  
  101. char *ctime(rawtime)
  102.         const time_t *rawtime;
  103. /*
  104.  *      Convert <rawtime> to a string.  A 26 character fixed field string
  105.  *      is created from the raw time value.  The following is an example
  106.  *      of what this string might look like:
  107.  *              "Wed Jul  8 18:43:07 1987\n\0"
  108.  *      A 24-hour clock is used, and due to a limitation in the ST system
  109.  *      clock value, only a resolution of 2 seconds is possible.  A pointer
  110.  *      to the formatted string, which is held in an internal buffer, is
  111.  *      returned.
  112.  */
  113.         {
  114.         return(asctime(localtime(rawtime)));
  115.         }
  116.