Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to get Xcode's debugger to show dates in local timezone (i.e., not UTC)?

I'm trying to debug code that makes pretty heavy use of dates which has me comparing tons of different NSDate values in the debugger. The debugger is displaying those dates in UTC format--for example:

date1 = (NSDate *) 0x01b11460 @"2012-02-15 18:55:00 +0000"

It would be a lot easier for me if it would show them in my local timezone as that is what the test code I'm debugging seems to be using.

I do have feeling that I'm missing something much more basic here so I'm hoping someone can enlighten me. Thanks in advance.

like image 939
GusP Avatar asked Nov 03 '22 21:11

GusP


2 Answers

While a little "hackish" you can create a subclass of NSDate and override just the

-(NSString *)description;

method to return a date formatted however you want.

The debugger isn't doing any special decoding for you, it's just calling the "description" method of the object it wants to display. A handy trick... (Which is why all my objects publish a concise description suitable for viewing in a debugger.)

like image 95
Jim Hayes Avatar answered Nov 09 '22 11:11

Jim Hayes


In the end, what worked best was in fact adding a Category for NSDate that just overrides the description method to return a string that represents the NSDate in my current timezone. I also made it DEBUG only as I really just need this override in place when debugging.

Here's the .h file I used:

#ifdef DEBUG

@interface NSDate (DebugHelper)

-(NSString *)description;

@end

#endif

and the .m file:

#ifdef DEBUG

#import "NSDate+DebugHelper.h"

@implementation NSDate (DebugHelper)

-(NSString *) description
{
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setTimeZone:[NSTimeZone systemTimeZone]];
    [dateFormatter setTimeStyle:NSDateFormatterShortStyle];
    [dateFormatter setDateStyle:NSDateFormatterShortStyle];
    return [dateFormatter stringFromDate:self];
}

@end

#endif

Thanks to Jim Hayes and jrturton for the discussion and ideas that led to this answer.

like image 43
GusP Avatar answered Nov 09 '22 11:11

GusP