格式化 NSDate & 时出现问题使用 GData-Objectivec-client 时的 NSString
我正在与一种奇怪的情况作斗争:相同的代码在两个不同的项目中工作不同。该项目只是带有此代码的空命令行实用程序。第二个项目是链接的 gdata-objectivec-client 库。
这是代码:
static NSString * const dateFormat = @"MM/dd/yyyy HH:mm:ss Z";
NSString *tmp_string = @"03/08/2011 10:07:36 +0300";
NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease] ;
[dateFormatter setLocale:[[[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"] autorelease]];
[dateFormatter setDateStyle:NSDateFormatterMediumStyle];
[dateFormatter setDateFormat: dateFormat ];
NSDate *newDate = [dateFormatter dateFromString: tmp_string];
NSLog(@"dateFromThatString: %@", newDate);
在命令行实用程序中,结果是相同的
“2011年3月8日10:07:36 +0300”
。
但在链接了 gdata-objectivec-client 的项目中,结果改为
“2011年3月8日07:07:36 +0000”
我找不到问题所在,有什么建议吗?
I'm fighting with a strange situation: same code works different in two different projects. The one project is just empty command line utility with this code. The second project is with linked gdata-objectivec-client library.
Here is the code:
static NSString * const dateFormat = @"MM/dd/yyyy HH:mm:ss Z";
NSString *tmp_string = @"03/08/2011 10:07:36 +0300";
NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease] ;
[dateFormatter setLocale:[[[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"] autorelease]];
[dateFormatter setDateStyle:NSDateFormatterMediumStyle];
[dateFormatter setDateFormat: dateFormat ];
NSDate *newDate = [dateFormatter dateFromString: tmp_string];
NSLog(@"dateFromThatString: %@", newDate);
In just command line utility the result is same
"03/08/2011 10:07:36 +0300"
.
But in the project with gdata-objectivec-client linked to it, the result is changed to
"03/08/2011 07:07:36 +0000"
I cant find what's the problem, any suggestions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
阅读这个主题我了解到“NSDate 不知道时区,它总是以独立于时区的方式存储日期(作为自特定参考日期以来的时间跨度)”,因此这两个 NSDate 对象代表两个不同的两个不同项目中的字符串是相同的,只是 NSDate 对象的描述之间存在一些差异,所以......这对未来的工作来说不是一个大问题,因为我需要这些描述只是为了方便调试。我不会使用描述方法,而是使用 [NSFormatted stringFromDate:]。
有趣的是 gdata-objectivec-client 对项目的影响,nsdate obj 的描述同时返回,但响应 +0000 gmt 偏移量。
但这仅用于讨论。
Reading about this subject i've learned that "NSDate is not aware of time zones, it always stores dates in a time zone independent manner (as a span of time since a specific reference date)", so those two NSDate objects representing two different strings in two different projects are the same, there is just some problem in difference between description of NSDate objects, so.. it's not a big problem for future work, because i needed these description only for an easy debug. I will just not use description method, but [NSFormatted stringFromDate:].
It's interesting how gdata-objectivec-client influenced on a project, that description of nsdate obj returns same time, but responding to +0000 gmt offset.
But it's only for discussion.
看起来日期格式化程序在每种情况下都有不同的时区。您可以使用
-[NSDateFormatter setTimeZone:]
。It looks like the date formatter has different time zones in each case. You can change the time zone using
-[NSDateFormatter setTimeZone:]
.