First of all, I would like to thank everyone in the forum for their enormous help. I am having a lotta of fun reading the book because of you. The forum is a natural extension of my reading and I check every single post on each chapter that I finish.
I was trying for a solution that returned the years, months and minutes since I was born. The first one I came up with used simple arithmetic to calculate that using the % operator.
[code] NSDateComponents *birth = [[NSDateComponents alloc] init];
[birth setYear:1975];
[birth setMonth:12];
[birth setDay:31];
[birth setHour:11];
[birth setMinute:0];
[birth setSecond:0];
NSCalendar *g = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDate *dateOfBirth = [g dateFromComponents:birth];
NSDate *now = [NSDate date];
double secondsSinceBirth = [now timeIntervalSinceDate:dateOfBirth];
NSLog(@"It has been %f seconds since my birth", secondsSinceBirth);
int yearsAlive = (int) secondsSinceBirth / 31536000;
double remainder = (int) secondsSinceBirth % 31536000;
int monthsAlive = (int) remainder / 2592000;
int daysAlive = (int) remainder % 2592000 / 86400;
int hoursAlive = (int) remainder % 2592000 % 86400 / 3600;
int minutesAlive = (int) remainder % 259200 % 86400 % 3600 / 60;
NSLog(@"I am %d years, %d months, %d days, %d hours, %d minutes old", yearsAlive, monthsAlive, daysAlive, hoursAlive, minutesAlive);
}
return 0;
}
[/code]
It logs:
2013-12-13 19:31:48.042 Challenge 5.1[60424:303] It has been 1197703908.042589 seconds since my birth
2013-12-13 19:31:48.043 Challenge 5.1[60424:303] I am 37 years, 11 months, 27 days, 7 hours, 31 minutes old
Program ended with exit code: 0
I don’t really know why the need for (int) before each division, but (after a few experiments) it only worked that way. I know that it has something to do with the fact that is a integer division with a double variable. But other than that I am at a loss. Could someone explain why?
My second solution made use of the array data structure and a loop to convert seconds into years, months, hours and minutes.
[code] NSDateComponents *birth = [[NSDateComponents alloc] init];
[birth setYear:1975];
[birth setMonth:12];
[birth setDay:31];
[birth setHour:11];
[birth setMinute:0];
[birth setSecond:0];
NSCalendar *g = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDate *dateOfBirth = [g dateFromComponents:birth];
NSDate *now = [NSDate date];
double secondsSinceBirth = [now timeIntervalSinceDate:dateOfBirth];
NSLog(@"It has been %f seconds since my birth", secondsSinceBirth);
int quotientValues [] = {31536000, 2592000, 86400, 3600, 60};
int results [5];
for (int i = 0; i < 5; i ++) {
results [i] = (int) secondsSinceBirth/ quotientValues [i];
secondsSinceBirth = (int) secondsSinceBirth % quotientValues [i];
}
NSLog(@"%d years, %d months, %d days, %d hours, %d minutes old", results [0], results [1], results [2], results [3], results [4]);
}
return 0;[/code]
Is it possible to log the array results without having to call each member (“results [0], results [1], …, etc”)?
Thank you.