Something Annoying I noticed working with time

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

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

@autoreleasepool {
    NSDate *now = [NSDate date];
    
    NSDateComponents *dateComps = [[NSDateComponents alloc] init];
    [dateComps setYear:1978];
    [dateComps setMonth:07];
    [dateComps setDay:21];
    [dateComps setHour:07];
    [dateComps setMinute:20];
    [dateComps setSecond:0];
    
    NSCalendar *newCalendarObj = [[NSCalendar alloc]
                                  initWithCalendarIdentifier:NSGregorianCalendar];
    NSDate *dateOfBirth = [newCalendarObj dateFromComponents:dateComps];
    
    NSLog(@"Incorrect Date of birth time: %@", dateOfBirth);
    NSLog(@"Incorrect Current Time: %@", now);

    // The correct time
    NSLog(@"Date of birth: %@", [dateOfBirth descriptionWithLocale:[NSLocale currentLocale]]);
    NSLog(@"Current Time: %@", [now descriptionWithLocale:[NSLocale currentLocale]]);
    

}
return 0;

}

Returns:
2014-05-13 17:48:40.935 SecondsIHaveBeenAlive[2601:303] Incorrect Date of birth time: 1978-07-21 14:20:00 +0000
2014-05-13 17:48:40.935 SecondsIHaveBeenAlive[2601:303] Incorrect Current Time: 2014-05-14 00:48:40 +0000
2014-05-13 17:48:40.936 SecondsIHaveBeenAlive[2601:303] Date of birth: Friday, July 21, 1978 at 7:20:00 AM Pacific Daylight Time
2014-05-13 17:48:40.937 SecondsIHaveBeenAlive[2601:303] Current Time: Tuesday, May 13, 2014 at 5:48:40 PM Pacific Daylight Time
Program ended with exit code: 0

[/code]

When working with the challenge in this chapter I noticed the times are not right. Why would the time not be right when it is hard-coded into the comps?

Use the NSDateFormatter class instead of descriptionWithLocale: method:

// Print date correctly
// ---------------------------------------------------------------------------------------
//
#import <Foundation/Foundation.h>

NSString *StringFromDate (NSDate *);

// ---------------------------------------------------------------------------------------
//
int main (int argc, const char * argv[])
{
    @autoreleasepool
    {
        NSLog (@"Current time: %@", StringFromDate ([NSDate date]));
        
        NSDateComponents *dateComps = [[NSDateComponents alloc] init];
        [dateComps setYear:1978];
        [dateComps setMonth:07];
        [dateComps setDay:21];
        [dateComps setHour:07];
        [dateComps setMinute:20];
        [dateComps setSecond:0];
        
        NSCalendar *newCalendarObj = [[NSCalendar alloc]
                                      initWithCalendarIdentifier:NSGregorianCalendar];
        NSDate *dateOfBirth = [newCalendarObj dateFromComponents:dateComps];
        
        NSLog (@"Date of birth: %@", StringFromDate (dateOfBirth));

    }
    return 0;
}

// ---------------------------------------------------------------------------------------
//
NSString *StringFromDate (NSDate *date)
{
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setTimeStyle:NSDateFormatterFullStyle];
    [dateFormatter setDateStyle:NSDateFormatterFullStyle];
    
    [dateFormatter setLocale:[NSLocale currentLocale]];
    
    return [dateFormatter stringFromDate:date];
}
// ---------------------------------------------------------------------------------------
//
// Courtesy of Pretty Function - www.pretty-function.org
//
// ---------------------------------------------------------------------------------------

Excellent! Thanks

@Ibex10

I’ve just compiled & executed your version of the code - btw thanks for sharing it - but it has caused a little confusion.

My timezone is set for London UK, GMT.

When the code is executed the current date has a timezone of “Greenwich Mean Time”, whereas the specified time (Date of Birth) is specified as “GMT” …

2014-12-07 15:44:02.354 DateFormatter[2081:303] Current time: Sunday, 7 December 2014 15:44:02 Greenwich Mean Time
2014-12-07 15:44:02.355 DateFormatter[2081:303] Date of birth: Sunday, 5 March 1961 21:20:00 GMT
Program ended with exit code: 0

Is there something I have missed that is changing the timezone format?