|
| 1 | +// |
| 2 | +// GitXRelativeDateFormatter.m |
| 3 | +// GitX |
| 4 | +// |
| 5 | +// Created by Nathan Kinsinger on 9/1/10. |
| 6 | +// Copyright 2010 Nathan Kinsinger. All rights reserved. |
| 7 | +// |
| 8 | + |
| 9 | +#import "GitXRelativeDateFormatter.h" |
| 10 | + |
| 11 | + |
| 12 | +#define MINUTE 60 |
| 13 | +#define HOUR (60 * MINUTE) |
| 14 | + |
| 15 | +#define WEEK 7 |
| 16 | + |
| 17 | + |
| 18 | +@implementation GitXRelativeDateFormatter |
| 19 | + |
| 20 | +- (NSString *)stringForObjectValue:(id)date |
| 21 | +{ |
| 22 | + if (![date isKindOfClass:[NSDate class]]) |
| 23 | + return nil; |
| 24 | + |
| 25 | + NSDate *now = [NSDate date]; |
| 26 | + |
| 27 | + NSInteger secondsAgo = lround([now timeIntervalSinceDate:date]); |
| 28 | + |
| 29 | + if (secondsAgo < 0) |
| 30 | + return @"In the future!"; |
| 31 | + |
| 32 | + if (secondsAgo < MINUTE) |
| 33 | + return @"seconds ago"; |
| 34 | + |
| 35 | + if (secondsAgo < (2 * MINUTE)) |
| 36 | + return @"1 minute ago"; |
| 37 | + |
| 38 | + if (secondsAgo < HOUR) |
| 39 | + return [NSString stringWithFormat:@"%d minutes ago", (secondsAgo / MINUTE)]; |
| 40 | + |
| 41 | + if (secondsAgo < (2 * HOUR)) |
| 42 | + return @"1 hour ago"; |
| 43 | + |
| 44 | + // figure out # of days ago based on calender days (so yesterday is the day before today not 24 hours ago) |
| 45 | + NSDateFormatter *midnightFormmatter = [[NSDateFormatter alloc] init]; |
| 46 | + [midnightFormmatter setDateFormat:@"yyyy-MM-dd"]; |
| 47 | + NSDate *midnightOnTargetDate = [midnightFormmatter dateFromString:[midnightFormmatter stringFromDate:date]]; |
| 48 | + NSDate *midnightToday = [midnightFormmatter dateFromString:[midnightFormmatter stringFromDate:now]]; |
| 49 | + |
| 50 | + // use NSCalendar so it will handle things like leap years correctly |
| 51 | + NSDateComponents *components = [[NSCalendar currentCalendar] components:(NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit) |
| 52 | + fromDate:midnightOnTargetDate |
| 53 | + toDate:midnightToday |
| 54 | + options:0]; |
| 55 | + NSInteger yearsAgo = [components year]; |
| 56 | + NSInteger monthsAgo = [components month]; |
| 57 | + NSInteger daysAgo = [components day]; |
| 58 | + |
| 59 | + if (yearsAgo == 0) { |
| 60 | + if (monthsAgo == 0) { |
| 61 | + // return "hours ago" if it's still today, but "Yesterday" only if more than 6 hours ago |
| 62 | + // gives people a little time to get used to the idea that yesterday is over :) |
| 63 | + if ((daysAgo == 0) || (secondsAgo < (6 * HOUR))) |
| 64 | + return [NSString stringWithFormat:@"%d hours ago", (secondsAgo / HOUR)]; |
| 65 | + if (daysAgo == 1) |
| 66 | + return @"Yesterday"; |
| 67 | + |
| 68 | + if (daysAgo >= (2 * WEEK)) |
| 69 | + return [NSString stringWithFormat:@"%d weeks ago", (daysAgo / WEEK)]; |
| 70 | + |
| 71 | + return [NSString stringWithFormat:@"%d days ago", daysAgo]; |
| 72 | + } |
| 73 | + |
| 74 | + if (monthsAgo == 1) |
| 75 | + return @"1 month ago"; |
| 76 | + |
| 77 | + return [NSString stringWithFormat:@"%d months ago", monthsAgo]; |
| 78 | + } |
| 79 | + |
| 80 | + if (yearsAgo == 1) { |
| 81 | + if (monthsAgo == 0) |
| 82 | + return @"1 year ago"; |
| 83 | + |
| 84 | + if (monthsAgo == 1) |
| 85 | + return @"1 year 1 month ago"; |
| 86 | + |
| 87 | + return [NSString stringWithFormat:@"1 year %d months ago", monthsAgo]; |
| 88 | + } |
| 89 | + |
| 90 | + return [NSString stringWithFormat:@"%d years ago", yearsAgo]; |
| 91 | +} |
| 92 | + |
| 93 | +@end |
0 commit comments