Skip to content

Instantly share code, notes, and snippets.

@masbog
Created October 25, 2011 04:53
Show Gist options
  • Save masbog/1311338 to your computer and use it in GitHub Desktop.
Save masbog/1311338 to your computer and use it in GitHub Desktop.
epoc time convert to string
- (NSString*)getDate:(int)time
{
NSMutableString *since = [[[NSMutableString alloc] initWithCapacity:0] autorelease];
NSDate *dateNow = [NSDate date];
NSTimeZone *localeTimeZone = [NSTimeZone localTimeZone];
int timeZoneOffset = [localeTimeZone secondsFromGMT];
int epochnow = [dateNow timeIntervalSince1970];
int when = epochnow - time - timeZoneOffset;
int day = when / 86400;
int dayreminder = when % 86400;
int hour = dayreminder / 3600;
int hourreminder = dayreminder % 3600;
int minute = hourreminder / 60;
int second = hourreminder % 60;
if (day > 0) {
[since appendFormat:(day > 1 ? @"%d days " : @"%d day "), day];
} else
if (hour > 0) {
[since appendFormat:(hour > 1 ? @"%d hours " : @"%d hour "), hour];
} else
if (minute > 0) {
[since appendFormat:(minute > 1 ? @"%d minutes " : @"%d minute "), minute];
} else
{
[since appendFormat:@"%d seconds ", second];
}
[since appendString:@"ago"];
return since;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment