Just for other peoples interest using Peter Hoseys advice heres my simple command line tool which solves this problem: (if anyone has any tips for bettering my solution it would be very much appreciated). Also is there any advantage to using the remainder function
over the %
?
#import <Foundation/Foundation.h>
NSDateComponents *totalTime;
void timeByAddingComponents(NSDateComponents *firstTime, NSDateComponents *secondTime) {
int addMin = [firstTime minute] + [secondTime minute];
int addHour = floor(addMin/60);
addMin = addMin % 60;
addHour = addHour + [firstTime hour] + [secondTime hour];
[totalTime setMinute:addMin];
[totalTime setHour:addHour];
}
int main (int argc, const char * argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
totalTime = [[NSDateComponents alloc] init];
[totalTime setHour:0];
[totalTime setMinute:0];
NSDateComponents *addTime = [[NSDateComponents alloc] init];
while (TRUE) {
int x, y;
printf("Please Enter Number of Hours: ");
scanf("%d", &x);
printf("Please Enter Number of Minutes: ");
scanf("%d", &y);
if (x == 0 && y == 0)
break;
NSLog(@"Add %i:%i to %i:%i", x,y, [totalTime hour], [totalTime minute]);
[addTime setHour:x];
[addTime setMinute:y];
timeByAddingComponents(totalTime, addTime);
NSString *time = [NSString stringWithFormat:@"%.2d:%.2d",[totalTime hour],[totalTime minute]];
NSLog(@"Total time now: %@", time);
}
[totalTime release];
[addTime release];
[pool drain];
return 0;
}