Skip to content

Commit

Permalink
Tweak prettyDate to handle future dates
Browse files Browse the repository at this point in the history
  • Loading branch information
dennisreimann committed Jun 18, 2013
1 parent 037ca69 commit 2522454
Showing 1 changed file with 15 additions and 10 deletions.
25 changes: 15 additions & 10 deletions Classes/Extensions/NSDate+Nibware.m
Original file line number Diff line number Diff line change
Expand Up @@ -12,32 +12,37 @@
@implementation NSDate (Nibware)

- (NSString *)prettyDateWithReference:(NSDate *)reference {
NSString *inTime = @"ago";
float diff = [reference timeIntervalSinceDate:self];
if (diff < 0) {
diff = [self timeIntervalSinceDate:reference];
inTime = @"from now";
}
float day_diff = floor(diff / 86400);
int days = (int)day_diff;
int weeks = (int)ceil(day_diff / 7);
int months = (int)ceil(day_diff / 30);
int years = (int)ceil(day_diff / 365);
if (day_diff <= 0) {
if (diff < 60) return @"just now";
if (diff < 120) return @"1 minute ago";
if (diff < 3600) return [NSString stringWithFormat:@"%d minutes ago", (int)floor( diff / 60 )];
if (diff < 7200) return @"1 hour ago";
if (diff < 86400) return [NSString stringWithFormat:@"%d hours ago", (int)floor( diff / 3600 )];
if (diff < 120) return [NSString stringWithFormat:@"1 minute %@", inTime];
if (diff < 3600) return [NSString stringWithFormat:@"%d minutes %@", (int)floor(diff / 60), inTime];
if (diff < 7200) return [NSString stringWithFormat:@"1 hour %@", inTime];
if (diff < 86400) return [NSString stringWithFormat:@"%d hours %@", (int)floor(diff / 3600), inTime];
} else if (days < 7) {
return [NSString stringWithFormat:@"%d day%@ ago", days, days == 1 ? @"" : @"s"];
return [NSString stringWithFormat:@"%d day%@ %@", days, days == 1 ? @"" : @"s", inTime];
} else if (weeks < 4) {
return [NSString stringWithFormat:@"%d week%@ ago", weeks, weeks == 1 ? @"" : @"s"];
return [NSString stringWithFormat:@"%d week%@ %@", weeks, weeks == 1 ? @"" : @"s", inTime];
} else if (months < 12) {
return [NSString stringWithFormat:@"%d month%@ ago", months, months == 1 ? @"" : @"s"];
return [NSString stringWithFormat:@"%d month%@ %@", months, months == 1 ? @"" : @"s", inTime];
} else {
return [NSString stringWithFormat:@"%d year%@ ago", years, years == 1 ? @"" : @"s"];
return [NSString stringWithFormat:@"%d year%@ %@", years, years == 1 ? @"" : @"s", inTime];
}
return [self description];
return self.description;
}

- (NSString *)prettyDate {
return [self prettyDateWithReference:[NSDate date]];
return [self prettyDateWithReference:NSDate.date];
}

@end

0 comments on commit 2522454

Please sign in to comment.