Created
August 15, 2014 08:30
-
-
Save ethereal-engineer/d08d9d053909f9c77569 to your computer and use it in GitHub Desktop.
Converting between NSTimeInterval to human-readable hours and minutes
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
#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