home *** CD-ROM | disk | FTP | other *** search
/ Atari FTP / ATARI_FTP_0693.zip / ATARI_FTP_0693 / Mint / mntlib32.zoo / ctime.c < prev    next >
C/C++ Source or Header  |  1993-02-28  |  3KB  |  119 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.     divu    #10,%1;
  27.     addb    #48,%1;
  28.     moveb    %1,%0@+;
  29.     swap    %1;
  30.     addb    #48,%1;
  31.     moveb    %1,%0@+"            \
  32.           : "=a"(mptr), "=d"(_i)        \
  33.           : "1"(_i), "0"(mptr));        \
  34. }
  35.  
  36. #else
  37.  
  38. static char * two_dig __PROTO((char *buf, unsigned short num));
  39.  
  40. static char *
  41. two_dig(buf, num)
  42.     char *buf;
  43.     unsigned short num;
  44. {
  45.     unsigned int rem;
  46.  
  47.     rem = num % 10;
  48.     *buf++ = '0' + (num / 10);
  49.     *buf++ = '0' + rem;
  50.     return buf;
  51. }
  52.  
  53. #endif /* __GNUC__ */
  54.  
  55. char *asctime(time)
  56.         register const struct tm *time;
  57. /*
  58.  *      Convert <time> structure value to a string.  The same format, and
  59.  *      the same internal buffer, as for ctime() is used for this function.
  60.  */
  61.         {
  62.     unsigned short values[6], *vpos, *valp;
  63.     int i;
  64.     char * ptr = timebuf;
  65.     
  66.     (void)strcpy(ptr, "??? ??? ?? ??:??:?? ????\n");
  67.     if (time != NULL)
  68.         {
  69.         vpos = values;
  70.         *vpos++ = time->tm_mday;
  71.         *vpos++ = time->tm_hour;
  72.         *vpos++ = time->tm_min;
  73.         *vpos++ = time->tm_sec;
  74.         i = 1900 + time->tm_year;
  75.         *vpos++ = i / 100;
  76.         *vpos   = i % 100;
  77.         ptr = (char *)memcpy(ptr, day[time->tm_wday], 3) + 3;
  78.         ptr += 1;
  79.         ptr = (char *)memcpy(ptr, month[time->tm_mon], 3) + 3;
  80.         valp = values;
  81.         do
  82.         {
  83.         ptr += 1;
  84. #ifdef __GNUC__
  85.         _TWODIG(ptr, *valp++);
  86. #else
  87.         ptr = two_dig(ptr, *valp++);
  88. #endif /* __GNUC__ */
  89.         }
  90.             while (valp < vpos);
  91. #ifdef __GNUC__
  92.         _TWODIG(ptr, *valp);
  93. #else
  94.         ptr = two_dig(ptr, *valp);
  95. #endif /* __GNUC__ */
  96.         if ('0' == timebuf[8])
  97.         timebuf[8] = ' ';    /* blank out leading zero on a day */
  98.  
  99.         }
  100.     return(timebuf);
  101.         }
  102.  
  103.  
  104. char *ctime(rawtime)
  105.         const time_t *rawtime;
  106. /*
  107.  *      Convert <rawtime> to a string.  A 26 character fixed field string
  108.  *      is created from the raw time value.  The following is an example
  109.  *      of what this string might look like:
  110.  *              "Wed Jul  8 18:43:07 1987\n\0"
  111.  *      A 24-hour clock is used, and due to a limitation in the ST system
  112.  *      clock value, only a resolution of 2 seconds is possible.  A pointer
  113.  *      to the formatted string, which is held in an internal buffer, is
  114.  *      returned.
  115.  */
  116.         {
  117.         return(asctime(localtime(rawtime)));
  118.         }
  119.