home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 7 / Apprentice-Release7.iso / Source Code / C / Applications / Moscow ML 1.42 / e_mac / e_gettimeofday.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-05-18  |  1.4 KB  |  63 lines  |  [TEXT/CWIE]

  1. /* e_gettimeofday.c */
  2. /* 18Oct95  e   -- from runtime.c */
  3.  
  4. #include <utime.h>
  5.  
  6. #define TICKS_PER_SEC 60
  7.  
  8. #if 0
  9.  
  10. // this still gives "backward running" time sometimes...
  11.  
  12. void gettimeofday ( struct timeval *t, int huh )
  13. {
  14.     unsigned long now1, now2, ticks;
  15.     int be_safe = 1000;
  16.  
  17.     now2 = LMGetTime();
  18.     do
  19.     { now1 = now2;
  20.       ticks = LMGetTicks();
  21.       now2 = LMGetTime();
  22.     } while( now1 != now2 && --be_safe > 0 );
  23.     ticks = ((ticks % TICKS_PER_SEC) * 1000000) / TICKS_PER_SEC;
  24.     t->tv_sec = now1;
  25.     t->tv_usec = ticks;
  26. }
  27.  
  28. #else
  29.  
  30. static long tod_offset;
  31.  
  32. void gettimeofday ( struct timeval *t, int huh )
  33. {
  34.   #pragma unused ( huh )
  35.   unsigned long now, ticks, secs;
  36.   long offs;
  37.   now = LMGetTime();    // get Mac Time
  38.   ticks = LMGetTicks(); // get Mac Ticks
  39.   // convert ticks to timeofday
  40.   secs = ticks / TICKS_PER_SEC;  // floor
  41.   ticks -= secs * TICKS_PER_SEC; // mod
  42.   secs += tod_offset;            // offset
  43.   ticks = (ticks * 1000000) / TICKS_PER_SEC; // ticks -> usecs
  44.   // see if the Mac clock was changed
  45.   offs = secs - now;
  46.   if( offs != 0 && offs != 1 && offs != -1)
  47.   { // off by one second is OK
  48.     tod_offset -= offs;
  49.     secs -= offs;        // rts - (rts - now) => now
  50.   }
  51. #if ( __MWERKS__ >= 0x1100 )
  52.   /* seconds between 1/1/1900 and 1/1/1904 */
  53.   t->tv_sec = secs + (365L * 4L) * 24L * 60L * 60L;
  54. #else
  55.   t->tv_sec = secs;
  56. #endif
  57.   t->tv_usec = ticks;
  58. }
  59.  
  60. #endif
  61.  
  62. // end
  63.