home *** CD-ROM | disk | FTP | other *** search
/ Programmer 7500 / MAX_PROGRAMMERS.iso / CLIPPER / MISC / EMXLIB8F.ZIP / EMX / LIB / TIME / GMTIME.C < prev    next >
Encoding:
C/C++ Source or Header  |  1993-01-02  |  1.4 KB  |  58 lines

  1. /* gmtime.c (emx+gcc) -- Copyright (c) 1990-1993 by Eberhard Mattes */
  2.  
  3. #include <sys/emx.h>
  4. #include <time.h>
  5. #include <stdlib.h>
  6.  
  7. static __inline__ int leap (unsigned int y)
  8. {
  9.   return (y % 4 != 0 ? 0 : y % 100 != 0 ? 1 : y % 400 != 0 ? 0 : 1);
  10. }
  11.  
  12.  
  13. struct tm *gmtime (const time_t *t)
  14. {
  15. #if defined (__MT__)
  16.   struct _thread *tp = _thread ();
  17. #define result (tp->_th_gmtime_buf)
  18. #else
  19.   static struct tm result;
  20. #endif
  21.   time_t t0, t1;
  22.   _uldiv_t q;
  23.   static int const mon_len[12] = {31,28,31,30,31,30,31,31,30,31,30,31};
  24.  
  25.   t0 = *t;
  26.   if (t0 == (time_t)(-1))
  27.     return (NULL);
  28.   q = _uldiv (t0, 60); result.tm_sec = q.rem; t0 = q.quot;
  29.   q = _uldiv (t0, 60); result.tm_min = q.rem; t0 = q.quot;
  30.   q = _uldiv (t0, 24); result.tm_hour = q.rem; t0 = q.quot;
  31.   result.tm_wday = (t0+4) % 7;  /* 01-Jan-1970 was Thursday, ie, 4 */
  32.   result.tm_year = 70;          /* 1970 */
  33.   for (;;)
  34.     {
  35.       t1 = (leap (result.tm_year+1900) ? 366 : 365);
  36.       if (t1 > t0)
  37.         break;
  38.       t0 -= t1;
  39.       ++result.tm_year;
  40.     }
  41.   result.tm_mon = 0;
  42.   result.tm_yday = t0;
  43.   for (;;)
  44.     {
  45.       if (result.tm_mon == 1)       /* February */
  46.         t1 = (leap (result.tm_year+1900) ? 29 : 28);
  47.       else
  48.         t1 = mon_len[result.tm_mon];
  49.       if (t1 > t0)
  50.         break;
  51.       t0 -= t1;
  52.       ++result.tm_mon;
  53.     }
  54.   result.tm_mday = t0 + 1;
  55.   result.tm_isdst = 0;
  56.   return (&result);
  57. }
  58.