forked from kevinlawler/NSDate-TimeAgo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
NSDate+TimeAgo.m
382 lines (340 loc) · 13.7 KB
/
NSDate+TimeAgo.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
#import "NSDate+TimeAgo.h"
@interface NSDate()
-(NSString *)getLocaleFormatUnderscoresWithValue:(double)value;
@end
@interface DummyClass: NSObject
@end
@implementation DummyClass
@end
@implementation NSDate (TimeAgo)
#ifndef NSDateTimeAgoLocalizedStrings
#define NSDateTimeAgoLocalizedStrings(key) \
NSLocalizedStringFromTableInBundle(key, @"NSDateTimeAgo", [NSBundle bundleWithPath:[[[NSBundle bundleForClass:[DummyClass class]] resourcePath] stringByAppendingPathComponent:@"NSDateTimeAgo.bundle"]], nil)
#endif
// shows 1 or two letter abbreviation for units.
// does not include 'ago' text ... just {value}{unit-abbreviation}
// does not include interim summary options such as 'Just now'
- (NSString *)timeAgoSimple
{
NSDate *now = [NSDate date];
double deltaSeconds = fabs([self timeIntervalSinceDate:now]);
double deltaMinutes = deltaSeconds / 60.0f;
int value;
if(deltaSeconds < 60)
{
return [self stringFromFormat:@"%%d%@s" withValue:deltaSeconds];
}
else if (deltaMinutes < 60)
{
return [self stringFromFormat:@"%%d%@m" withValue:deltaMinutes];
}
else if (deltaMinutes < (24 * 60))
{
value = (int)floor(deltaMinutes/60);
return [self stringFromFormat:@"%%d%@h" withValue:value];
}
else if (deltaMinutes < (24 * 60 * 7))
{
value = (int)floor(deltaMinutes/(60 * 24));
return [self stringFromFormat:@"%%d%@d" withValue:value];
}
else if (deltaMinutes < (24 * 60 * 31))
{
value = (int)floor(deltaMinutes/(60 * 24 * 7));
return [self stringFromFormat:@"%%d%@w" withValue:value];
}
else if (deltaMinutes < (24 * 60 * 365.25))
{
value = (int)floor(deltaMinutes/(60 * 24 * 30));
return [self stringFromFormat:@"%%d%@mo" withValue:value];
}
value = (int)floor(deltaMinutes/(60 * 24 * 365));
return [self stringFromFormat:@"%%d%@yr" withValue:value];
}
- (NSString *)timeAgo
{
NSDate *now = [NSDate date];
double deltaSeconds = fabs([self timeIntervalSinceDate:now]);
double deltaMinutes = deltaSeconds / 60.0f;
int minutes;
if(deltaSeconds < 5)
{
return NSDateTimeAgoLocalizedStrings(@"Just now");
}
else if(deltaSeconds < 60)
{
return [self stringFromFormat:@"%%d %@seconds ago" withValue:deltaSeconds];
}
else if(deltaSeconds < 120)
{
return NSDateTimeAgoLocalizedStrings(@"A minute ago");
}
else if (deltaMinutes < 60)
{
return [self stringFromFormat:@"%%d %@minutes ago" withValue:deltaMinutes];
}
else if (deltaMinutes < 120)
{
return NSDateTimeAgoLocalizedStrings(@"An hour ago");
}
else if (deltaMinutes < (24 * 60))
{
minutes = (int)floor(deltaMinutes/60);
return [self stringFromFormat:@"%%d %@hours ago" withValue:minutes];
}
else if (deltaMinutes < (24 * 60 * 2))
{
return NSDateTimeAgoLocalizedStrings(@"Yesterday");
}
else if (deltaMinutes < (24 * 60 * 7))
{
minutes = (int)floor(deltaMinutes/(60 * 24));
return [self stringFromFormat:@"%%d %@days ago" withValue:minutes];
}
else if (deltaMinutes < (24 * 60 * 14))
{
return NSDateTimeAgoLocalizedStrings(@"Last week");
}
else if (deltaMinutes < (24 * 60 * 31))
{
minutes = (int)floor(deltaMinutes/(60 * 24 * 7));
return [self stringFromFormat:@"%%d %@weeks ago" withValue:minutes];
}
else if (deltaMinutes < (24 * 60 * 61))
{
return NSDateTimeAgoLocalizedStrings(@"Last month");
}
else if (deltaMinutes < (24 * 60 * 365.25))
{
minutes = (int)floor(deltaMinutes/(60 * 24 * 30));
return [self stringFromFormat:@"%%d %@months ago" withValue:minutes];
}
else if (deltaMinutes < (24 * 60 * 731))
{
return NSDateTimeAgoLocalizedStrings(@"Last year");
}
minutes = (int)floor(deltaMinutes/(60 * 24 * 365));
return [self stringFromFormat:@"%%d %@years ago" withValue:minutes];
}
// Similar to timeAgo, but only returns "
- (NSString *)dateTimeAgo
{
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDate * now = [NSDate date];
NSDateComponents *components = [calendar components:
NSCalendarUnitYear|
NSCalendarUnitMonth|
NSCalendarUnitWeekOfYear|
NSCalendarUnitDay|
NSCalendarUnitHour|
NSCalendarUnitMinute|
NSCalendarUnitSecond
fromDate:self
toDate:now
options:0];
if (components.year >= 1)
{
if (components.year == 1)
{
return NSDateTimeAgoLocalizedStrings(@"1 year ago");
}
return [self stringFromFormat:@"%%d %@years ago" withValue:components.year];
}
else if (components.month >= 1)
{
if (components.month == 1)
{
return NSDateTimeAgoLocalizedStrings(@"1 month ago");
}
return [self stringFromFormat:@"%%d %@months ago" withValue:components.month];
}
else if (components.weekOfYear >= 1)
{
if (components.weekOfYear == 1)
{
return NSDateTimeAgoLocalizedStrings(@"1 week ago");
}
return [self stringFromFormat:@"%%d %@weeks ago" withValue:components.weekOfYear];
}
else if (components.day >= 1) // up to 6 days ago
{
if (components.day == 1)
{
return NSDateTimeAgoLocalizedStrings(@"1 day ago");
}
return [self stringFromFormat:@"%%d %@days ago" withValue:components.day];
}
else if (components.hour >= 1) // up to 23 hours ago
{
if (components.hour == 1)
{
return NSDateTimeAgoLocalizedStrings(@"An hour ago");
}
return [self stringFromFormat:@"%%d %@hours ago" withValue:components.hour];
}
else if (components.minute >= 1) // up to 59 minutes ago
{
if (components.minute == 1)
{
return NSDateTimeAgoLocalizedStrings(@"A minute ago");
}
return [self stringFromFormat:@"%%d %@minutes ago" withValue:components.minute];
}
else if (components.second < 5)
{
return NSDateTimeAgoLocalizedStrings(@"Just now");
}
// between 5 and 59 seconds ago
return [self stringFromFormat:@"%%d %@seconds ago" withValue:components.second];
}
- (NSString *)dateTimeUntilNow
{
NSDate * now = [NSDate date];
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *components = [calendar components:NSCalendarUnitHour
fromDate:self
toDate:now
options:0];
if (components.hour >= 6) // if more than 6 hours ago, change precision
{
NSInteger startDay = [calendar ordinalityOfUnit:NSCalendarUnitDay
inUnit:NSCalendarUnitEra
forDate:self];
NSInteger endDay = [calendar ordinalityOfUnit:NSCalendarUnitDay
inUnit:NSCalendarUnitEra
forDate:now];
NSInteger diffDays = endDay - startDay;
if (diffDays == 0) // today!
{
NSDateComponents * startHourComponent = [calendar components:NSCalendarUnitHour fromDate:self];
NSDateComponents * endHourComponent = [calendar components:NSCalendarUnitHour fromDate:self];
if (startHourComponent.hour < 12 &&
endHourComponent.hour > 12)
{
return NSDateTimeAgoLocalizedStrings(@"This morning");
}
else if (startHourComponent.hour >= 12 &&
startHourComponent.hour < 18 &&
endHourComponent.hour >= 18)
{
return NSDateTimeAgoLocalizedStrings(@"This afternoon");
}
return NSDateTimeAgoLocalizedStrings(@"Today");
}
else if (diffDays == 1)
{
return NSDateTimeAgoLocalizedStrings(@"Yesterday");
}
else
{
NSInteger startWeek = [calendar ordinalityOfUnit:NSCalendarUnitWeekOfYear
inUnit:NSCalendarUnitEra
forDate:self];
NSInteger endWeek = [calendar ordinalityOfUnit:NSCalendarUnitWeekOfYear
inUnit:NSCalendarUnitEra
forDate:now];
NSInteger diffWeeks = endWeek - startWeek;
if (diffWeeks == 0)
{
return NSDateTimeAgoLocalizedStrings(@"This week");
}
else if (diffWeeks == 1)
{
return NSDateTimeAgoLocalizedStrings(@"Last week");
}
else
{
NSInteger startMonth = [calendar ordinalityOfUnit:NSCalendarUnitMonth
inUnit:NSCalendarUnitEra
forDate:self];
NSInteger endMonth = [calendar ordinalityOfUnit:NSCalendarUnitMonth
inUnit:NSCalendarUnitEra
forDate:now];
NSInteger diffMonths = endMonth - startMonth;
if (diffMonths == 0)
{
return NSDateTimeAgoLocalizedStrings(@"This month");
}
else if (diffMonths == 1)
{
return NSDateTimeAgoLocalizedStrings(@"Last month");
}
else
{
NSInteger startYear = [calendar ordinalityOfUnit:NSCalendarUnitYear
inUnit:NSCalendarUnitEra
forDate:self];
NSInteger endYear = [calendar ordinalityOfUnit:NSCalendarUnitYear
inUnit:NSCalendarUnitEra
forDate:now];
NSInteger diffYears = endYear - startYear;
if (diffYears == 0)
{
return NSDateTimeAgoLocalizedStrings(@"This year");
}
else if (diffYears == 1)
{
return NSDateTimeAgoLocalizedStrings(@"Last year");
}
}
}
}
}
// anything else uses "time ago" precision
return [self dateTimeAgo];
}
- (NSString *) stringFromFormat:(NSString *)format withValue:(NSInteger)value
{
NSString * localeFormat = [NSString stringWithFormat:format, [self getLocaleFormatUnderscoresWithValue:value]];
return [NSString stringWithFormat:NSDateTimeAgoLocalizedStrings(localeFormat), value];
}
- (NSString *) timeAgoWithLimit:(NSTimeInterval)limit
{
return [self timeAgoWithLimit:limit dateFormat:NSDateFormatterFullStyle andTimeFormat:NSDateFormatterFullStyle];
}
- (NSString *) timeAgoWithLimit:(NSTimeInterval)limit dateFormat:(NSDateFormatterStyle)dFormatter andTimeFormat:(NSDateFormatterStyle)tFormatter
{
if (fabs([self timeIntervalSinceDate:[NSDate date]]) <= limit)
return [self timeAgo];
return [NSDateFormatter localizedStringFromDate:self
dateStyle:dFormatter
timeStyle:tFormatter];
}
- (NSString *) timeAgoWithLimit:(NSTimeInterval)limit dateFormatter:(NSDateFormatter *)formatter
{
if (fabs([self timeIntervalSinceDate:[NSDate date]]) <= limit)
return [self timeAgo];
return [formatter stringFromDate:self];
}
// Helper functions
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wobjc-protocol-method-implementation"
/*
- Author : Almas Adilbek
- Method : getLocaleFormatUnderscoresWithValue
- Param : value (Double value of seconds or minutes)
- Return : @"" or the set of underscores ("_")
in order to define exact translation format for specific translation rules.
(Ex: "%d _seconds ago" for "%d секунды назад", "%d __seconds ago" for "%d секунда назад",
and default format without underscore %d seconds ago" for "%d секунд назад")
Updated : 12/12/2012
Note : This method must be used for all languages that have specific translation rules.
Using method argument "value" you must define all possible conditions language have for translation
and return set of underscores ("_") as it is an ID for locale format. No underscore ("") means default locale format;
*/
-(NSString *)getLocaleFormatUnderscoresWithValue:(double)value
{
NSString *localeCode = [[NSLocale preferredLanguages] objectAtIndex:0];
// Russian (ru)
if([localeCode isEqual:@"ru"]) {
int XY = (int)floor(value) % 100;
int Y = (int)floor(value) % 10;
if(Y == 0 || Y > 4 || (XY > 10 && XY < 15)) return @"";
if(Y > 1 && Y < 5 && (XY < 10 || XY > 20)) return @"_";
if(Y == 1 && XY != 11) return @"__";
}
// Add more languages here, which are have specific translation rules...
return @"";
}
#pragma clang diagnostic pop
@end