-
Notifications
You must be signed in to change notification settings - Fork 275
/
Copy pathNSDate+Additions.m
executable file
·111 lines (89 loc) · 3.22 KB
/
NSDate+Additions.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
//
// NSDate+Additions.m
// Pods
//
// Created by Benjamin Smiley-andrews on 15/09/2016.
//
//
#import "NSDate+Additions.h"
#import <ChatSDK/Core.h>
@implementation NSDate (Additions)
-(NSString *) threadTimeAgo {
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:self.timeFormat];
NSString * time = [formatter stringFromDate:self];
NSDateComponents *otherDay = [[NSCalendar currentCalendar] components:NSCalendarUnitDay fromDate:self];
NSDateComponents *today = [[NSCalendar currentCalendar] components:NSCalendarUnitDay fromDate:[NSDate date]];
// We check if the last date was in the last few days
// Then check if it was exactly yesterday
if ([self daysAgo] < 3 && today.day == otherDay.day + 1) {
time = [NSBundle t: NSLocalizedString(bYesterday, nil)];
}
else if (self.daysAgo > 1 && self.daysAgo < 7) {
[formatter setDateFormat:@"EEEE"];
time = [formatter stringFromDate:self];
}
else if (self.daysAgo >= 7) {
[formatter setDateFormat:@"dd/MM/yy"];
time = [formatter stringFromDate:self];
}
return time;
}
-(NSString *) timeFormat {
return BChatSDK.config.timeFormat;
}
-(NSString *) messageTimeAt {
// if (self.daysAgo < 1) {
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:self.timeFormat];
return [formatter stringFromDate:self];
// }
// else {
// return [self timeAgoWithFormatString:b_at_];
// }
}
-(NSString *) lastSeenTimeAgo {
return [self timeAgoWithFormatString:bLastSeen_at_];
}
-(NSString *) dateAgo {
NSString * day = @"";
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
if (self.isToday) {
day = [NSBundle t: NSLocalizedString(bToday, nil)];
}
else if (self.isYesterday) {
day = [NSBundle t: NSLocalizedString(bYesterday, nil)];
}
else if (self.daysAgo < 7) {
[formatter setDateFormat:@"EEE"];
day = [formatter stringFromDate:self];
}
else {
[formatter setDateFormat:@"MM/yy"];
day = [formatter stringFromDate:self];
}
return day;
}
-(BOOL) isDateWithOffset: (int) days to: (NSDate *) date {
NSCalendar *cal = [NSCalendar currentCalendar];
// NSDateComponents *comps = [cal components:(NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit)
// fromDate:self];
NSDateComponents * comps = [[NSDateComponents alloc] init];
[comps setDay:days];
NSDate * offset = [cal dateByAddingComponents:comps toDate:self options:0];
return date.day == offset.day && date.month == offset.month && date.year == offset.year;
}
-(BOOL) isNextDay: (NSDate *) date {
return [self isDateWithOffset:-1 to:date];
}
-(BOOL) isPreviousDay: (NSDate *) date {
return [self isDateWithOffset:1 to:date];
}
-(NSString *) timeAgoWithFormatString: (NSString *) formatString {
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:self.timeFormat];
NSString * time = [formatter stringFromDate:self];
NSString * day = [self dateAgo];
return [NSString stringWithFormat:[NSBundle t:formatString], day, time];
}
@end