-
Notifications
You must be signed in to change notification settings - Fork 11
/
WFMachTime.m
56 lines (40 loc) · 1.35 KB
/
WFMachTime.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#import "WFMachTime.h"
#include <mach/mach.h>
#include <mach/mach_time.h>
#include <unistd.h>
@interface WFMachTime ()
@property uint64_t startedTime;
@end
@implementation WFMachTime
@synthesize startedTime;
+ (uint64_t)currentPrimitiveTime {
return mach_absolute_time();
}
+ (WFMachTime *)currentTime {
WFMachTime *time = [[self alloc] init];
time.startedTime = [self currentPrimitiveTime];
return [time autorelease];
}
NSTimeInterval WFMachTimeIntervalBetweenStartTimeEndTime(uint64_t startTime, uint64_t endTime) {
uint64_t elapsed;
uint64_t elapsedNano;
static mach_timebase_info_data_t sTimebaseInfo;
elapsed = endTime - startTime;
if ( sTimebaseInfo.denom == 0 ) {
(void) mach_timebase_info(&sTimebaseInfo);
}
elapsedNano = elapsed * sTimebaseInfo.numer / sTimebaseInfo.denom;
NSTimeInterval duration = (NSTimeInterval)(elapsedNano / 1000000000.0);
return duration;
}
- (NSTimeInterval)intervalSinceTime:(WFMachTime *)time {
uint64_t startTime = time.startedTime;
uint64_t endTime = self.startedTime;
return WFMachTimeIntervalBetweenStartTimeEndTime(startTime, endTime);
}
- (NSTimeInterval)intervalSinceStart {
uint64_t startTime = self.startedTime;
uint64_t endTime = [[self class] currentPrimitiveTime];
return WFMachTimeIntervalBetweenStartTimeEndTime(startTime, endTime);
}
@end