home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1998 / MacHack 1998.toast / Papers / C++ Exceptions / µShell / Core Utilities / TimeUtils.h < prev    next >
Encoding:
C/C++ Source or Header  |  1998-05-25  |  1.8 KB  |  52 lines  |  [TEXT/CWIE]

  1. #ifndef __TIMEUTILS__
  2. #define __TIMEUTILS__
  3. #pragma once
  4.  
  5. //----------------------------------------------------------------------------------------
  6. // This routine checks to see if the timer has run out; it handles the case when
  7. // the stopTime calculation (startTime + timeout) wraps around (overflows a long int).
  8. // This only happens every 771 days or so, but Mike Gough said that I wasn't a stud
  9. // unless I handled this case.
  10.  
  11. inline bool TimeExpired(unsigned long currentTime, unsigned long startTime, unsigned long stopTime)
  12. {
  13.     // First adjust time so that the origin is at 'startTime'
  14.     stopTime     -= startTime;
  15.     currentTime    -= startTime;
  16.     
  17.     // Then, it is a simple matter to compare the current
  18.     // time value with the time that we want to stop at.
  19.     return currentTime >= stopTime;
  20. }
  21.  
  22. //----------------------------------------------------------------------------------------
  23. // It is also important to know which of two ticks will happen first, starting from
  24. // a given tick count.  If one of the ticks == currentTime, we consider that event
  25. // to have already gone by (hence the extra +1's), and won't be encountered again until
  26. // the timer wraps all the way around ~771 days later.
  27.  
  28. inline unsigned long CloserTick(unsigned long currentTime, unsigned long tick1, unsigned long tick2)
  29. {
  30.     currentTime += 1;
  31.  
  32.     //
  33.     // Once again, move the beginning of time, this time to 'currentTime + 1'
  34.     //
  35.     tick1 -= currentTime;
  36.     tick2 -= currentTime;
  37.     
  38.     //
  39.     // Once we've converted to a zero-based system, it's easy to compare
  40.     // the two times.  Convert back to the appropriate time base by adding
  41.     // currentTime + 1 back onto the tick after we've determined which will
  42.     // happen sooner.
  43.     //
  44.     if(tick1 < tick2)
  45.         return tick1 + currentTime;
  46.     else
  47.         return tick2 + currentTime;
  48. }
  49.  
  50.  
  51. #endif __TIMEUTILS__
  52.