Skip to content

Instantly share code, notes, and snippets.

@jonswaff
Created July 27, 2012 02:23
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jonswaff/3185845 to your computer and use it in GitHub Desktop.
Save jonswaff/3185845 to your computer and use it in GitHub Desktop.
Convert a NSTimeInterval into a human-readable string. Used to convert EKAlerts to text
+ (NSString *)timeIntervalToStringWithInterval:(NSTimeInterval)interval
{
NSString *retVal = @"At time of event";
if (interval == 0) return retVal;
int second = 1;
int minute = second*60;
int hour = minute*60;
int day = hour*24;
// interval can be before (negative) or after (positive)
int num = abs(interval);
NSString *beforeOrAfter = @"before";
NSString *unit = @"day";
if (interval > 0) {
beforeOrAfter = @"after";
}
if (num >= day) {
num /= day;
if (num > 1) unit = @"days";
} else if (num >= hour) {
num /= hour;
unit = (num > 1) ? @"hours" : @"hour";
} else if (num >= minute) {
num /= minute;
unit = (num > 1) ? @"minutes" : @"minute";
} else if (num >= second) {
num /= second;
unit = (num > 1) ? @"seconds" : @"second";
}
return [NSString stringWithFormat:@"%d %@ %@", num, unit, beforeOrAfter];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment