Skip to content

Instantly share code, notes, and snippets.

@takuma104
Created April 11, 2009 12:44
Show Gist options
  • Save takuma104/93552 to your computer and use it in GitHub Desktop.
Save takuma104/93552 to your computer and use it in GitHub Desktop.
// NSDate#descriptionWithTwitterStyle
@interface NSDate(TwitterStyle)
- (NSString*)descriptionWithTwitterStyle;
@end
@implementation NSDate(TwitterStyle)
- (NSString*)descriptionWithTwitterStyle {
time_t now = [[NSDate date] timeIntervalSince1970];
time_t t = [self timeIntervalSince1970];
int d = now - t;
if (d < 5) {
return @"less than 5 seconds ago";
} else if (d < 10) {
return @"less than 10 seconds ago";
} else if (d < 15) {
return @"less than 15 seconds ago";
} else if (d < 20) {
return @"less than 20 seconds ago";
} else if (d < 30) {
return @"half a minute ago";
} else if (d < 60) {
return @"less than a minute ago";
} else if (d < 45*60 ) {
return [NSString stringWithFormat:@"about %d minute ago", d/60];
} else if (d < 24*60*60) {
int h = d/3600;
if (h < 1) h = 1;
return [NSString stringWithFormat:@"about %d hour ago",h];
}
char tmp[80];
struct tm *tm_ = localtime(&t);
strftime(tmp, sizeof(tmp), "%I:%M %p %b %dth", tm_);
return [NSString stringWithUTF8String:tmp];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment