home *** CD-ROM | disk | FTP | other *** search
/ Nebula 1994 June / NEBULA_SE.ISO / SourceCode / MiscKit / Source / MiscStopwatch.m < prev    next >
Encoding:
Text File  |  1993-09-28  |  2.7 KB  |  123 lines

  1. //
  2. //    MiscStopwatch.m -- a generic class to time things (measure)
  3. //        Written by Don Yacktman (c) 1993 by Don Yacktman.
  4. //                Version 1.0.  All rights reserved.
  5. //
  6. //        This notice may not be removed from this source code.
  7. //
  8. //        This is a free object!  Contact the author for the latest version.
  9. //        Don Yacktman, 4279 N. Ivy Lane, Provo, UT, 84604
  10. //        e-mail:  Don_Yacktman@byu.edu
  11. //
  12. //    This object is included in the MiscKit by permission from the author
  13. //    and its use is governed by the MiscKit license, found in the file
  14. //    "LICENSE.rtf" in the MiscKit distribution.  Please refer to that file
  15. //    for a list of all applicable permissions and restrictions.
  16. //    
  17.  
  18. #import <misckit/MiscStopwatch.h>
  19.  
  20. @implementation MiscStopwatch
  21.  
  22. - init
  23. {
  24.     id ret = [super init];
  25.     [self clearTiming:self];
  26.     isRelative = YES;
  27.     paused = YES;
  28.     return ret;
  29. }
  30.  
  31. - setRelative:(BOOL)t // overridden
  32. {
  33.     isRelative = YES;
  34.     return self;
  35. }
  36.  
  37.  
  38. - clearTiming:sender
  39. {
  40.     struct timezone tzp;
  41.     gettimeofday(&startTime, &tzp);
  42.     startTime.tv_sec = 0;
  43.     startTime.tv_usec = 0;
  44.     [[[[[[[[self setMicrosecond:0] setSecond:0] setMinute:0] setHour:0]
  45.             setDay:0] setMonth:0] setYear:0] calcDayOfWeek];
  46.     return self;
  47. }
  48.  
  49. - startTiming:sender
  50. {
  51.     [self clearTiming:sender];
  52.     [self continueTiming:sender];
  53.     return self;
  54. }
  55.  
  56. - continueTiming:sender
  57. {
  58.     struct timezone tzp;
  59.     if (!paused) return self;    // already running
  60.     paused = NO;
  61.     gettimeofday(&startTime, &tzp);
  62.     return self;
  63. }
  64.  
  65. - pauseTiming:sender
  66. {
  67.     [self calcElapsedTime:sender];
  68.     paused = YES;
  69.     return self;
  70. }
  71.  
  72. - calcElapsedTime:sender
  73. {    // adds elapsed time to the time's value and continues
  74.     struct timeval endTime;
  75.     struct timezone tzp;
  76.     
  77.     if (!paused) { // if we're paused, no time to add.
  78.         gettimeofday(&endTime, &tzp);
  79.         [self addMicroseconds:(endTime.tv_usec - startTime.tv_usec)];
  80.         [self addSeconds:(endTime.tv_sec - startTime.tv_sec)];
  81.         startTime.tv_sec = endTime.tv_sec;
  82.         startTime.tv_usec = endTime.tv_usec;
  83.     }
  84.     return self;
  85. }
  86.  
  87. - read:(NXTypedStream *)stream
  88. {
  89.     [super read:stream];
  90.     NXReadTypes(stream, "cll",
  91.             &paused, &startTime.tv_sec, &startTime.tv_usec);
  92.     return self;
  93. }
  94.  
  95. - write:(NXTypedStream *)stream
  96. {
  97.     [super write:stream];
  98.     NXWriteTypes(stream, "cll",
  99.             &paused, &startTime.tv_sec, &startTime.tv_usec);
  100.     return self;
  101. }
  102.  
  103. // NXTransport protocol implementation:
  104. - encodeUsing:(id <NXEncoding>)portal
  105. {
  106.     [super encodeUsing:portal];
  107.     [portal encodeData:&paused            ofType:"c"];
  108.     [portal encodeData:&startTime.tv_sec  ofType:"i"];
  109.     [portal encodeData:&startTime.tv_usec ofType:"i"];
  110.     return self;
  111. }
  112.  
  113. - decodeUsing:(id <NXDecoding>)portal
  114. {
  115.     [super decodeUsing:portal];
  116.     [portal decodeData:&paused            ofType:"c"];
  117.     [portal decodeData:&startTime.tv_sec  ofType:"i"];
  118.     [portal decodeData:&startTime.tv_usec ofType:"i"];
  119.     return self;
  120. }
  121.  
  122. @end
  123.