My solution to the challenge


#1

Hi.
I am not sure if I got the point of this chapter, but my code gives me a correct solution.
I knew some ObjC before I started reeding the book, so I am wondering if I created the solution Aaron wanted us to make :slight_smile:

So this is my code:

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

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

@autoreleasepool {
    NSTimeZone *currentTimeZone = [[NSTimeZone alloc] init];
    if ([currentTimeZone isDaylightSavingTime] == YES)
        NSLog(@"YES, the current time zone is currently in daylight savings time.");
    else
        NSLog(@"NO, the current time zone is currently not in daylight savings time.");
}
return 0;

}[/code]


#2

That is a beautiful solution!


#3

Just to be sure, you dont need the == YES right?

When you right if (something) { do this } else { do this }

Its only going to ring true if its yes correct? I just want to make sure, my code worked, but want to make sure im not doing something wrong.

NSTimeZone *zone = [NSTimeZone systemTimeZone];

NSLog(@"%@", zone);

if ([zone isDaylightSavingTime]){
    NSLog(@"You are in daylight savings time");
} else {
    NSLog(@"We are not in daylight savings time");
}

#4

That is correct. You don’t need to put in the == YES. The fact that the condition is true will be implied, and it will execute the next part of code.


#5

I don’t get the same answer when I enter both the solutions above. The first one comes up as a NULL assignment, but the second one works correctly for me. Since they’re equivalent definitions per the book and Aaron, they’re causing confusion to me since I should have the same answer when run. What am I missing?

[code] NSTimeZone *currentTimeZone = [[NSTimeZone alloc] init];
NSLog(@“The current timezone is %@\n”, currentTimeZone);
if ([currentTimeZone isDaylightSavingTime]) {
NSLog(@“The current timezone is in daylight savings time.\n”);
} else {
NSLog(@“The current timezone is NOT in daylight savings time.\n”);
}

NSTimeZone *zone = [NSTimeZone systemTimeZone];    
NSLog(@"%@", zone);
if ([zone isDaylightSavingTime]){
    NSLog(@"You are in daylight savings time");
} else {
    NSLog(@"We are not in daylight savings time");
}

[/code]

Results in:

2011-11-05 16:25:53.632 TimeAfterTime[818:707] The current timezone is (null)
2011-11-05 16:25:53.632 TimeAfterTime[818:707] The current timezone is NOT in daylight savings time.
2011-11-05 16:25:53.633 TimeAfterTime[818:707] America/Chicago (CDT) offset -18000 (Daylight)
2011-11-05 16:25:53.634 TimeAfterTime[818:707] You are in daylight savings time

To me, this indicates that the first bit of code is missing an assignment to the systemTimeZone. I should put out a disclaimer that I’m using Lion 10.7.2 w/ Xcode 4.1 in case that makes a difference. I have several older books that I’m working through, so I don’t want to upgrade to 4.2 or higher so that examples match up.

Thank you,
Brian.


#6

Since we just went through a DST transition, I decided to try to get in over my head and play around with some of the other DST-related functions.

I decided to test the DST time offset from a few days ago. This should have shown a “1” (I think), but it showed a “0”

[code] NSDate *now = [[NSDate alloc] init];
NSLog(@“the date is %@”, now);

    NSDate *then = [now dateByAddingTimeInterval:(24*60*60*3*-1)];
    NSLog(@"3 days ago, it was %@",then);        // this shows the expected date
    
    NSTimeZone *DST = [[NSTimeZone alloc] init];
    int dstOffset = [DST daylightSavingTimeOffsetForDate:then];
    NSLog(@"time zone offset for that date is %d", dstOffset); // this shows '0' [/code]

#7

Since we just did DST here I thought this would be a fun one. Getting from BOOL to a string was a bit tricker than I thought it would be at first!

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

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

@autoreleasepool {
    
    // Get the current time zone
    
    NSTimeZone *currentTimeZone = [NSTimeZone systemTimeZone];
    
    // Determine if we are in DST and put it in a BOOL
    
    BOOL isDST = [currentTimeZone isDaylightSavingTime];
    
    
    // Print out the results
    
    NSLog(@"Is it currently DST here? %@", isDST ? @"YES" : @"NO");
    
}
return 0;

}[/code]

With the BOOL though I just ended up using it in a test. After all that’s what BOOLs are for right?


#8

My code is a bit different from what I’ve seen so far, but works too:

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

int main (int argc, const char * argv[])
{
@autoreleasepool {
NSTimeZone *tz = [NSTimeZone systemTimeZone];
if (tz == nil) return 0;

    NSLog(@"%@",[tz name]);
    
    NSString *dst = nil;        
    if (![tz isDaylightSavingTime]) {
       dst = @"n't";
    }
    NSLog(@"Currently we do%@ have DST.", dst);        
}
return 0;

}[/code]


#9

I suspect mine is a little less elegant than the others but I’m pretty happy that I did it from scratch, i’ve always had such trouble manipulating classes and instances.
It gives me:
2011-11-21 21:40:36.825 TimeAfterTime[7315:707] The time zone here is Australia/Melbourne (AEDT) offset 39600 (Daylight)
2011-11-21 21:40:36.827 TimeAfterTime[7315:707] It is daylight saving time

#import <Foundation/Foundation.h>

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

    @autoreleasepool {
        
        NSTimeZone *now = [[NSTimeZone alloc] init]; 
        now = [NSTimeZone systemTimeZone];
        
        NSLog(@"The time zone here is %@", now);
        
        if ([now isDaylightSavingTime] == YES)
        {
            NSLog(@"It is daylight saving time.");
        }
        else
        {
            NSLog(@"Sorry, still winter...");
        }
        
            
        
            
    
        
}return 0;
}

#10

mine, kinda different

#import <Foundation/Foundation.h>

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

    @autoreleasepool {
        
        NSTimeZone *myTimeZone = [[NSTimeZone alloc] init];
        BOOL timeZone = [myTimeZone isDaylightSavingTime];
        
        if (timeZone) {
        NSLog(@"Day Light Saving time On");
        }else
        {
            NSLog(@"Day Light Saving time Off");
        }
        
    }
    return 0;
}

#11

Regarding Anadox’s code: your variable is actually NULL, initialized and ready to contain something. Use of an NSLog will actually reveal this. Also, the “== YES” in the if statement is redundant since the instance method returns a BOOL.

Alexander2020: Just a note, it’s true many programmers use an expression in a BOOL declaration in order to make later uses more readable and understandable (and it’s a good practice when it achieves the goal).
However, if(timezone) is actually less clear (if timezone… what?).
if( [myTimeZone isDaylightSavingTime] ) is very self explanatory.

The net result is using more memory with an unnecessary variable that actually makes the code less self explanatory.

Memory isn’t nearly a huge issue as when I started (began programming by teaching myself basic in 1984 when memory conservation was EVERYTHING, lol) but still it’s a good idea to use it efficiently. Hope these tips help :slight_smile:


#12

In all fairness, I’d bet any compiler worth its salt would optimize the variable out of existence, so while I agree that the name isn’t helpful, I don’t think its existence is a memory problem.

So I’d say Jason’s suggestion of replacing the variable with the original expression is a good one, but alternatively you could give the variable a better name, like isDST.


#13

Hi there. First post here. I tried this way and it seems to work fine. Any obvious problem with this code? Thanks

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

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

@autoreleasepool {
    
     NSTimeZone *g = [NSTimeZone systemTimeZone];
             
    BOOL p = [g isDaylightSavingTime];
    
    if (p == 0){
        
        NSLog(@"Your curent date is NOT a DTS");
        
                  
    }
    
    else {
        
        NSLog(@"Your current date IS a DTS");
    } 
                   
    
}

return 0;

}[/code]


#14

I suspect your code will read better with YES/NO for BOOL values instead of 1/0, and you might consider more informative variable names in the future (but obviously for a short bit of example code like this, not a big deal).


#15

I tried to put it into one line, just for fun.

Is there a problem about not releasing the memory or so?


#16

Thanks much.


#17

Mine… With a sneak peek at if , else …

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

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

@autoreleasepool {
    NSTimeZone *zone = [[NSTimeZone alloc] init];
    zone = [NSTimeZone systemTimeZone];
    
    NSLog(@"The time zone is %@", zone);
    
    if ([zone isDaylightSavingTime])
    {
        NSLog(@" IT IS");
    }
    else
    {
        NSLog(@"IT IS NOT");
    }
    
}
return 0;

}
[/code]


#18

Just had to share my version of the solution:

        // Get my local time zone and test for daylight savings time
        NSTimeZone *myTimeZone = [NSTimeZone localTimeZone];
        BOOL isDST = [myTimeZone isDaylightSavingTime];
        
        NSLog(@"\n\nMy time zone: %@ (%@)\nIs Daylight Savings? %@", 
              [myTimeZone name], 
              [myTimeZone abbreviation], 
              (isDST ? @"YES" : @"NO"));

Which yields the following result:

[color=#0000CC]My time zone: America/Los_Angeles (PST)
Is Daylight Savings? NO[/color]

Cheers all! —> Kelsey


#19

[quote=“abugida”]I tried to put it into one line, just for fun.

That’s short and sweet. I like it! —> Kelsey


#20

This one has been bothering the hell out of me, I’ve been trying to solve this for the past couple of days. I came up with a code similar to one that was on here earlier and here is what I get:

#import <Foundation/Foundation.h>

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

@autoreleasepool {
    
    NSTimeZone *myTimeZone = [NSTimeZone systemTimeZone];
  
    BOOL daylight = [myTimeZone isDayLightSavingTime];
    NSLog(@"Is it daylight saving time? %@", daylight ? @"YES" : @"NO");
}
return 0;

}

I tried doing a screen capture but for some reason command+v won’t post the image , what you can’t see is that there is a red exclamation point next to the line where I declare “dayligh.” This appears when I attempt to run it, the message says: "no visible @interface for ‘NSTimeZone’ declares the selector ‘isDayLightSavingTime’. I also tried the code with the question mark and the @“yes” : @“no” after it but I got the same message. What am i missing?