Created
April 11, 2009 12:44
-
-
Save takuma104/93552 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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