有没有办法让Xcode的调试器显示本地时区的日期(即不是UTC)? [英] Is there a way to get Xcode's debugger to show dates in local timezone (i.e., not UTC)?

查看:318
本文介绍了有没有办法让Xcode的调试器显示本地时区的日期(即不是UTC)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试调试代码,使得相当大的使用日期,我比较了不同的 NSDate 值。调试器以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

对我来说,如果它会在我的本地 timezone ,因为我正在调试的测试代码似乎正在使用。

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.

推荐答案

最后,最有效的是添加一个NSDate类别,只是覆盖 description 方法返回一个表示当前时区的NSDate的字符串。我也做了DEBUG,因为我真的只是在调试时需要这个覆盖。

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.

这是我使用的.h文件:

Here's the .h file I used:

#ifdef DEBUG

@interface NSDate (DebugHelper)

-(NSString *)description;

@end

#endif

.m文件:

#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

感谢Jim Hayes和jrturton的讨论和想法,导致了这个答案。

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

这篇关于有没有办法让Xcode的调试器显示本地时区的日期(即不是UTC)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆