Chapter 14 Solution

Shows seconds, minutes and hours that I have been alive.

[code]#import <Foundation/Foundation.h>

// Function Prototype
NSString *prettyDateAndTime (NSDate *datetime);

int main(int argc, const char * argv[]) {
@autoreleasepool {

    // Date and Time right now
    NSDate *now = [[NSDate alloc]init];
    
    NSDateComponents *comps = [[NSDateComponents alloc]init];
    [comps setYear:1978];
    [comps setMonth:7];
    [comps setDay:21];
    [comps setHour:07];
    [comps setMinute:00];
    [comps setSecond:00];
    
    NSCalendar *g = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    
    // Create an instance of NSDate initialize with values from comps instance
    NSDate *dateOfBirth = [g dateFromComponents:comps];
    
    
    double secondsSinceEarlierDate = [now timeIntervalSinceDate:dateOfBirth];
    
    
    // Display my date of birth in the local time zone rather than UTC
    NSLog(@"Born on %@", prettyDateAndTime(dateOfBirth));
    
    // Shows how long I been alive for seconds, minutes and hours.
    NSLog(@"Been alive for %f seconds, %f minutes, %f hours",
          secondsSinceEarlierDate, secondsSinceEarlierDate / 60,
          (secondsSinceEarlierDate / 60) / 60);
}
return 0;

}

/*************************************************************************************/

// A function that displays the date and time in local time when using NSLog rather than UTC
NSString *prettyDateAndTime (NSDate *datetime)
{

NSDateFormatter *prettyDateFormat = [[NSDateFormatter alloc] init];
[prettyDateFormat setTimeStyle:NSDateFormatterMediumStyle];
[prettyDateFormat setDateStyle:NSDateFormatterMediumStyle];
[prettyDateFormat setLocale:[NSLocale currentLocale]];

return [prettyDateFormat stringFromDate];

}[/code]

[quote][code]// A function that displays the date and time in local time when using NSLog rather than UTC
NSString *prettyDateAndTime (NSDate *datetime)
{

NSDateFormatter *prettyDateFormat = [[NSDateFormatter alloc] init];
[prettyDateFormat setTimeStyle:NSDateFormatterMediumStyle];
[prettyDateFormat setDateStyle:NSDateFormatterMediumStyle];
[prettyDateFormat setLocale:[NSLocale currentLocale]];

return [prettyDateFormat stringFromDate];

}[/code][/quote]
Good function, but comment is misleading.

–> // A function that returns the time string in in local time for the given absolute time

I came up with the following for the challenge.

[code]#import <Foundation/Foundation.h>

int main(int argc, const char * argv[]) {
@autoreleasepool {

    NSDate *now = [[NSDate alloc] init];
    
    NSCalendar *calendar = [NSCalendar currentCalendar];
    
    NSDateComponents *components = [[NSDateComponents alloc] init];
    [components setYear:1969];
    [components setMonth:11];
    [components setDay:24];
    [components setHour:12];
    [components setMinute:5];
    
    NSDate *dateOfBirth = [calendar dateFromComponents:components];
    
    double secondsSinceBirthDate = [now timeIntervalSinceDate:dateOfBirth];

    NSLog(@"The total seconds since birth %.0f", secondsSinceBirthDate);
}

return 0;

}[/code]