-
Notifications
You must be signed in to change notification settings - Fork 7
/
Timer.m
129 lines (88 loc) · 3.76 KB
/
Timer.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
//
// Timer.m
// Snap
//
// Created by Abdullah Bakhach on 10/3/12.
// Copyright (c) 2012 Hollance. All rights reserved.
//
#import "Timer.h"
@implementation Timer
@synthesize referencePoint = _referencePoint;
+(double)getCurTime
{
return (double)CFAbsoluteTimeGetCurrent();
}
+(double)getTimeDifference:(double)time1
time2:(double)time2
{
CFDateRef newDate = CFDateCreate(NULL, time2);
CFDateRef oldDate = CFDateCreate(NULL, time1);
CFTimeInterval difference = CFDateGetTimeIntervalSinceDate(newDate, oldDate);
//NSLog(@"this is time difference %f",fabs(difference));
CFRelease(oldDate); CFRelease(newDate);
// fabs = absolute value
return fabs(difference);
}
#pragma mark - Helper functions
+(void)printCurTime
{
//NSLog(@"date: %lu", (double)([NSNumber numberWithLongLong:[[NSDate date] timeIntervalSince1970] * 100]));
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd 'at' HH:mm:ss.SSS"];
double curTime = [[self class] getCurTime];
NSDate *date = [NSDate dateWithTimeIntervalSinceReferenceDate:curTime];
NSString *formattedDateString = [dateFormatter stringFromDate:date];
NSLog(@"formattedDateString: %@ actual date %f", formattedDateString, curTime);
}
+(void)printTime:(double)absTime
{
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd 'at' HH:mm:ss.SSS"];
NSDate *date = [NSDate dateWithTimeIntervalSinceReferenceDate:absTime];
NSString *formattedDateString = [dateFormatter stringFromDate:date];
NSLog(@"formattedDateString: %@ actual date %f", formattedDateString, absTime);
}
-(void)setCurrentTimeAsReferencepoint
{
_referencePoint = [[self class] getCurTime];
[[self class] printTime:_referencePoint];
}
-(void)setReferencePoint:(double)referencePoint
{
_referencePoint = referencePoint;
[[self class] printTime:_referencePoint];
}
-(double)getTimeElapsedInMilliSec
{
CFDateRef oldDate = CFDateCreate(NULL, _referencePoint);
double curTime = [[self class] getCurTime];
CFDateRef newDate = CFDateCreate(NULL,curTime);
CFTimeInterval difference = CFDateGetTimeIntervalSinceDate(newDate, oldDate);
NSLog(@"this is time difference %f",difference);
double difference2 = _referencePoint - curTime;
NSLog(@"this is time difference in absolute terms between _referencePoint (%f) and curTime (%f) %f",_referencePoint, curTime, fabs(difference2));
CFRelease(oldDate); CFRelease(newDate);
return difference;
}
-(double)getTimeElapsedinAbsTime
{
double difference = _referencePoint - [[self class] getCurTime];
NSLog(@"this is time difference in absolute terms %f",difference);
return difference;
}
-(void)getAbsTimeInFuture
{
double futureTime = _referencePoint + 10;
CFDateRef futureDate = CFDateCreate(NULL, futureTime);
CFDateRef oldDate = CFDateCreate(NULL, _referencePoint);
CFTimeInterval difference = CFDateGetTimeIntervalSinceDate(futureDate, oldDate);
NSLog(@"getAbsTimeInFuture: this is time difference %f",fabs(difference));
double difference2 = _referencePoint - futureTime;
NSLog(@"getAbsTimeInFuture: this is time difference in absolute terms between _referencePoint (%f) and futureTime (%f) %f",_referencePoint, futureTime, fabs(difference2));
NSLog(@"getAbsTimeInFuture: this is what future time looks like in date terms");
[[self class] printTime:futureTime];
NSLog(@"getAbsTimeInFuture: this is what the curtime looks like in date terms");
[[self class] printTime:_referencePoint];
CFRelease(oldDate); CFRelease(futureDate);
}
@end