|
22 | 22 | @implementation SKDateDistanceCalculator |
23 | 23 |
|
24 | 24 | + (NSTimeInterval)distanceFromNow:(NSDate *)date { |
25 | | - if (!date) { |
26 | | - return 0.0; |
27 | | - } |
| 25 | + if (!date) { |
| 26 | + return 0.0; |
| 27 | + } |
28 | 28 |
|
29 | | - NSDate *now = [NSDate date]; |
30 | | - return [now timeIntervalSinceDate:date]; |
| 29 | + NSDate *now = [NSDate date]; |
| 30 | + return [now timeIntervalSinceDate:date]; |
31 | 31 | } |
32 | 32 |
|
33 | 33 | + (NSString *)humanReadableDistanceFromNow:(NSDate *)date { |
34 | | - if (!date) { |
35 | | - return @"Invalid date"; |
36 | | - } |
| 34 | + if (!date) { |
| 35 | + return @"Invalid date"; |
| 36 | + } |
37 | 37 |
|
38 | | - NSTimeInterval timeInterval = [self distanceFromNow:date]; |
39 | | - BOOL isPast = timeInterval > 0; |
| 38 | + NSTimeInterval timeInterval = [self distanceFromNow:date]; |
| 39 | + BOOL isPast = timeInterval > 0; |
40 | 40 |
|
41 | | - NSCalendar *calendar = [NSCalendar currentCalendar]; |
42 | | - NSDateComponents *components = [calendar components:NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond |
43 | | - fromDate:isPast ? date : [NSDate date] |
44 | | - toDate:isPast ? [NSDate date] : date |
45 | | - options:0]; |
| 41 | + NSCalendar *calendar = [NSCalendar currentCalendar]; |
| 42 | + NSDateComponents *components = |
| 43 | + [calendar components:NSCalendarUnitYear | NSCalendarUnitMonth | |
| 44 | + NSCalendarUnitDay | NSCalendarUnitHour | |
| 45 | + NSCalendarUnitMinute | NSCalendarUnitSecond |
| 46 | + fromDate:isPast ? date : [NSDate date] |
| 47 | + toDate:isPast ? [NSDate date] : date |
| 48 | + options:0]; |
46 | 49 |
|
47 | | - NSString *suffix = isPast ? @"ago" : @"from now"; |
| 50 | + NSString *suffix = isPast ? @"ago" : @"from now"; |
48 | 51 |
|
49 | | - if (components.year > 0) { |
50 | | - return [NSString stringWithFormat:@"%ld year%@ %@", (long)components.year, components.year == 1 ? @"" : @"s", suffix]; |
51 | | - } else if (components.month > 0) { |
52 | | - return [NSString stringWithFormat:@"%ld month%@ %@", (long)components.month, components.month == 1 ? @"" : @"s", suffix]; |
53 | | - } else if (components.day > 0) { |
54 | | - return [NSString stringWithFormat:@"%ld day%@ %@", (long)components.day, components.day == 1 ? @"" : @"s", suffix]; |
55 | | - } else if (components.hour > 0) { |
56 | | - return [NSString stringWithFormat:@"%ld hour%@ %@", (long)components.hour, components.hour == 1 ? @"" : @"s", suffix]; |
57 | | - } else if (components.minute > 0) { |
58 | | - return [NSString stringWithFormat:@"%ld minute%@ %@", (long)components.minute, components.minute == 1 ? @"" : @"s", suffix]; |
59 | | - } else { |
60 | | - return [NSString stringWithFormat:@"%ld second%@ %@", (long)components.second, components.second == 1 ? @"" : @"s", suffix]; |
61 | | - } |
| 52 | + if (components.year > 0) { |
| 53 | + return |
| 54 | + [NSString stringWithFormat:@"%ld year%@ %@", (long)components.year, |
| 55 | + components.year == 1 ? @"" : @"s", suffix]; |
| 56 | + } else if (components.month > 0) { |
| 57 | + return |
| 58 | + [NSString stringWithFormat:@"%ld month%@ %@", (long)components.month, |
| 59 | + components.month == 1 ? @"" : @"s", suffix]; |
| 60 | + } else if (components.day > 0) { |
| 61 | + return [NSString stringWithFormat:@"%ld day%@ %@", (long)components.day, |
| 62 | + components.day == 1 ? @"" : @"s", suffix]; |
| 63 | + } else if (components.hour > 0) { |
| 64 | + return |
| 65 | + [NSString stringWithFormat:@"%ld hour%@ %@", (long)components.hour, |
| 66 | + components.hour == 1 ? @"" : @"s", suffix]; |
| 67 | + } else if (components.minute > 0) { |
| 68 | + return |
| 69 | + [NSString stringWithFormat:@"%ld minute%@ %@", (long)components.minute, |
| 70 | + components.minute == 1 ? @"" : @"s", suffix]; |
| 71 | + } else { |
| 72 | + return |
| 73 | + [NSString stringWithFormat:@"%ld second%@ %@", (long)components.second, |
| 74 | + components.second == 1 ? @"" : @"s", suffix]; |
| 75 | + } |
62 | 76 | } |
63 | 77 |
|
64 | 78 | + (NSInteger)distanceFromNow:(NSDate *)date inUnit:(NSCalendarUnit)unit { |
65 | | - if (!date) { |
66 | | - return 0; |
67 | | - } |
| 79 | + if (!date) { |
| 80 | + return 0; |
| 81 | + } |
68 | 82 |
|
69 | | - NSCalendar *calendar = [NSCalendar currentCalendar]; |
70 | | - NSDateComponents *components = [calendar components:unit |
71 | | - fromDate:date |
72 | | - toDate:[NSDate date] |
73 | | - options:0]; |
| 83 | + NSCalendar *calendar = [NSCalendar currentCalendar]; |
| 84 | + NSDateComponents *components = [calendar components:unit |
| 85 | + fromDate:date |
| 86 | + toDate:[NSDate date] |
| 87 | + options:0]; |
74 | 88 |
|
75 | | - switch (unit) { |
76 | | - case NSCalendarUnitYear: |
77 | | - return components.year; |
78 | | - case NSCalendarUnitMonth: |
79 | | - return components.month; |
80 | | - case NSCalendarUnitDay: |
81 | | - return components.day; |
82 | | - case NSCalendarUnitHour: |
83 | | - return components.hour; |
84 | | - case NSCalendarUnitMinute: |
85 | | - return components.minute; |
86 | | - case NSCalendarUnitSecond: |
87 | | - return components.second; |
88 | | - default: |
89 | | - return 0; |
90 | | - } |
| 89 | + switch (unit) { |
| 90 | + case NSCalendarUnitYear: |
| 91 | + return components.year; |
| 92 | + case NSCalendarUnitMonth: |
| 93 | + return components.month; |
| 94 | + case NSCalendarUnitDay: |
| 95 | + return components.day; |
| 96 | + case NSCalendarUnitHour: |
| 97 | + return components.hour; |
| 98 | + case NSCalendarUnitMinute: |
| 99 | + return components.minute; |
| 100 | + case NSCalendarUnitSecond: |
| 101 | + return components.second; |
| 102 | + default: |
| 103 | + return 0; |
| 104 | + } |
91 | 105 | } |
92 | 106 |
|
93 | 107 | @end |
0 commit comments