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