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);
}
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
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).
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);
}
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;
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
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;