-
Notifications
You must be signed in to change notification settings - Fork 4
/
NSDate+TBL.m
107 lines (88 loc) · 3.73 KB
/
NSDate+TBL.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
#import "NSDate+TBL.h"
@implementation NSDate (TBL)
-(NSString *)timeAgo {
return [self timeAgoWithSeconds:NO];
}
-(NSString *)timeAgoWithSeconds {
return [self timeAgoWithSeconds:YES];
}
// TODO: add version with format specifier
-(NSString *)timeAgoWithSeconds:(BOOL)withSeconds {
NSDate *now = [NSDate date];
double deltaSeconds = [now timeIntervalSinceDate:self];
if (deltaSeconds <= 0) {
return (withSeconds ? @"1s" : @"1m"); // special case for clock wonkiness
} else if (deltaSeconds < 60) {
if (withSeconds) {
return [NSString stringWithFormat:@"%ds", (int)deltaSeconds];
} else {
return @"0m";
}
} else if (deltaSeconds < (60 * 60)) {
return [NSString stringWithFormat:@"%dm", (int)floor(deltaSeconds/60)];
} else if (deltaSeconds < (24 * 60 * 60)) {
return [NSString stringWithFormat:@"%dh", (int)floor(deltaSeconds/(60 * 60))];
} else if (deltaSeconds < (7 * 24 * 60 * 60)) {
return [NSString stringWithFormat:@"%dd", (int)floor(deltaSeconds/(24 * 60 * 60))];
} else {
return [NSString stringWithFormat:@"%dw", (int)floor(deltaSeconds/(7 * 24 * 60 * 60))];
}
}
-(BOOL)isNewerThanDate:(NSDate *)aDate {
return [self compare:aDate] == NSOrderedDescending;
}
-(BOOL)isOlderThanDate:(NSDate *)aDate {
return [self compare:aDate] == NSOrderedAscending;
}
-(BOOL)isOlderThanDaysSinceNow:(NSTimeInterval)days {
return [self isOlderThanMinutesSinceNow:days * 24 * 60];
}
-(BOOL)isNewerThanDaysSinceNow:(NSTimeInterval)days {
return [self isNewerThanMinutesSinceNow:days * 24 * 60];
}
-(BOOL)isOlderThanHoursSinceNow:(NSTimeInterval)hours {
return [self isOlderThanMinutesSinceNow:hours * 60];
}
-(BOOL)isNewerThanHoursSinceNow:(NSTimeInterval)hours {
return [self isNewerThanMinutesSinceNow:hours * 60];
}
-(BOOL)isOlderThanMinutesSinceNow:(NSTimeInterval)min {
return self.timeIntervalSinceNow < min * 60;
}
-(BOOL)isNewerThanMinutesSinceNow:(NSTimeInterval)min {
return self.timeIntervalSinceNow > min * 60;
}
-(BOOL)isToday{
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *components = [calendar components:(NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay) fromDate:[NSDate date]];
NSDate *today = [calendar dateFromComponents:components];
components = [calendar components:(NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay) fromDate:self];
NSDate *fakeSelfDate = [calendar dateFromComponents:components];
if([today isEqualToDate:fakeSelfDate]) {
return YES;
}
return NO;
}
-(BOOL)isTomorrow{
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *components = [calendar components:(NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay) fromDate:[NSDate date]];
NSDate *today = [calendar dateFromComponents:components];
components = [calendar components:(NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay) fromDate:self];
NSDate *fakeSelfDate = [calendar dateFromComponents:components];
if([today timeIntervalSinceDate:fakeSelfDate] == -60*60*24) {
return YES;
}
return NO;
}
-(BOOL)isSoonerThanDaysFromNow:(NSTimeInterval)days {
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *components = [calendar components:(NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay) fromDate:[NSDate date]];
NSDate *today = [calendar dateFromComponents:components];
components = [calendar components:(NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay) fromDate:self];
NSDate *fakeSelfDate = [calendar dateFromComponents:components];
if([fakeSelfDate timeIntervalSinceDate:today] < 60*60*24*days) {
return YES;
}
return NO;
}
@end