Skip to content

Instantly share code, notes, and snippets.

@ethereal-engineer
Created August 15, 2014 08:30
Show Gist options
  • Save ethereal-engineer/d08d9d053909f9c77569 to your computer and use it in GitHub Desktop.
Save ethereal-engineer/d08d9d053909f9c77569 to your computer and use it in GitHub Desktop.
Converting between NSTimeInterval to human-readable hours and minutes
#pragma mark - Common Routines
// I would prefer a standard ObjC way to do this but this is what's in place for now
static inline void timeIntervalToHoursAndMinutes(NSTimeInterval timeInterval, double *hours, double *mins)
{
double fullMinutes = trunc(timeInterval / 60.0);
if (hours)
{
*hours = trunc(fullMinutes / 60.0);
}
if (mins)
{
*mins = trunc(fmod(fullMinutes, 60.0));
}
}
static inline NSTimeInterval hoursAndMinutesToTimeInterval(double hours, double mins)
{
return (hours * 60.0 + mins) * 60.0;
}
static inline NSString *formatTimeInterval(NSTimeInterval timeInterval)
{
double hours;
double mins;
timeIntervalToHoursAndMinutes(timeInterval, &hours, &mins);
return [NSString stringWithFormat:NSLocalizedString(@"%02.0fh%02.0fm", @"{# of hours}hours-abbreviation{# of minutes}minutes-abbreviation"), hours, mins];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment