Chapter 13 challenge solution


#1

@autoreleasepool {

    NSTimeZone *timeZone = [[NSTimeZone alloc] init];
    
    timeZone = [NSTimeZone systemTimeZone];
    
    BOOL isDaylight = [timeZone isDaylightSavingTime];
    
    NSString *text = [[NSString alloc] init];
    
    text = @"The time zone of this Mac is";
    
    NSString *daylightDescription = [[NSString alloc] init];
    
    if (YES == isDaylight) {
        daylightDescription = @"The time zone is currently in daylight saving time.";
    }else if (NO == isDaylight){
        daylightDescription = @"The time zone is not currently in daylight saving time.";
    }
    
    NSLog(@"%@ %@. %@", text, timeZone, daylightDescription);
}

#2

I completed the challenge successfully but I decided to see how a it should be done. The results were as I expected with the exception of "…this Mac is American /Detroit (EDT) offset -14400 (daylight)… Where is the offset coming from


#3

Hi there,

I’ve been reading this forum in parallel to the book for the last 12 chapters. Now, I’ve decided to contribute myself. I have basic knowledge of C. However, Objective-C is fairly new to me. Here’s my solution for this challenge. I was really close to solving it all by myself and needed just a little hint from Christ (see above).

[code] NSTimeZone *myTimeZone = [[NSTimeZone alloc] init];
myTimeZone = [NSTimeZone systemTimeZone];

    BOOL isDaylight = [myTimeZone isDaylightSavingTime];

    if (isDaylight == YES) {
        NSLog(@"YES, it is Daylight Saving Time and your time zone is %@", myTimeZone);
    } else {
        NSLog(@"NO, it is not Daylight Saving Time. Your time zone is %@", myTimeZone);
    }

[/code]

Regards
Jay


#4

Did mine a little bit different. Ended up using an instance method called isDaylightSavingTimeForDate:. Xcode’s auto complete function filled that in, so I just went with it. Also, I am running 3.2.6, so mind my memory management differences. (I really need to look into the AutoreleasePool class reference and understand what that is all about).

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

int main (int argc, const char * argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

NSTimeZone *zone = [NSTimeZone systemTimeZone]; 
NSDate *now = [[NSDate alloc] init];
BOOL isDaylight = [zone isDaylightSavingTimeForDate:now]; 

if (isDaylight == NO) {
	NSLog(@"Daylight savings is in effect!"); 
	NSLog(@" Your timezone is %@", zone);
}else {
	NSLog(@"Daylight savings is not in effect!");
	NSLog(@" Your timezone is %@", zone);
}

					

[pool drain];
return 0;

}
[/code]


#5

I’m not sure if i have done this correctly or not as my version seems a little less complex to the other answers?

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

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

@autoreleasepool {
    
    NSTimeZone *local = [NSTimeZone systemTimeZone];
    
    if ([local isDaylightSavingTime]) {
        NSLog(@"Yes It Is!");
    } else {
        NSLog(@"No It's Not!");
    }
    
    
}
return 0;

}
[/code]


#6

{
@autoreleasepool{

    NSTimeZone *systemTimeZone = [[NSTimeZone alloc] init];{
    NSDate *date = [[NSDate alloc] init];
    [systemTimeZone isDaylightSavingTimeForDate:date];
    {
        if (YES) {
            NSLog(@"YES, we are in DST");
            } else {
                NSLog(@"No, we are not");
            }
        }
    }                 }
                                  

return 0;

}
This is what I did, but after reading everyone else’s solution I’m wondering if I did this wrong. Any thoughts would be appreciated.


#7

[quote=“Futhark”]I’m not sure if i have done this correctly or not as my version seems a little less complex to the other answers?

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

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

@autoreleasepool {
    
    NSTimeZone *local = [NSTimeZone systemTimeZone];
    
    if ([local isDaylightSavingTime]) {
        NSLog(@"Yes It Is!");
    } else {
        NSLog(@"No It's Not!");
    }
    
    
}
return 0;

}
[/code][/quote]

Hi Futhark,

My code is almost the same with yours (yours is lesser complex),

[code] NSTimeZone *timeZone = [NSTimeZone systemTimeZone];

    BOOL dst = [timeZone isDaylightSavingTime];
    
    if (dst) {
        NSLog(@"It's daylight saving time");
    }else NSLog(@"It's not daylight saving time");[/code]

but I’m interested on this:

In other codes I can see [[NSTimeZone alloc] init], while we are using [NSTimeZone systemTimeZone].

I’m just wondering… what is the difference between them? If there is none, why we need to use [[NSTimeZone alloc] init] then?

If anyone understand, really appreciate the enlightenment :slight_smile:


#8

This solution seems to work. I like to keep things as compact as possible:

    @autoreleasepool {
        
        NSTimeZone *time = [NSTimeZone systemTimeZone];
        NSLog(@"The timezone of my laptop is %@", time);
        
        BOOL tz = [time isDaylightSavingTime];
        NSLog(@"Is my current time zone in daylight savings? %@", (tz ? @"YES":@"NO"));
        //ternary operator at the end returns a YES if true, and a NO if false
        
    }

If I were just trying to get a response from the BOOL in my NSLog I would use %d


#9

hi, there are lot of version, that is my solution.

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

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

@autoreleasepool {
    
    NSTimeZone *fuseau = [NSTimeZone systemTimeZone];
    NSLog(@"le fuseau est %@", fuseau);
    
    BOOL horaire = [fuseau isDaylightSavingTime];
    
    if (horaire) {
        NSLog(@"On est bien en horaire ete");
    } else {
        NSLog(@"On est bien en horaire d'hiver");
       }
    
    }
return 0;

}[/code]