【发布时间】:2012-05-17 11:54:47
【问题描述】:
我有一个使用 NSDateFormatter 将 RFC3339 中的 NSString 转换为 NSDate 的函数,但我不知道如何考虑时区的反向操作。
字符串到NSDate转换的相关部分是:
// The result of a call to systemTimeZone is cached by the app automatically, if the user changes it that change isn't
// reflected unless resetSystemTimeZone is called to clear the cache.
[NSTimeZone resetSystemTimeZone];
NSLocale *enUSPOSIXLocale;
enUSPOSIXLocale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"];
[sRFC3339DateFormatter setLocale:enUSPOSIXLocale];
if ([fromString hasSuffix:@"Z"] || [fromString hasSuffix:@"z"])
{
[sRFC3339DateFormatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"UTC"]];
}
else
{
[sRFC3339DateFormatter setTimeZone:[NSTimeZone systemTimeZone]];
}
[sRFC3339DateFormatter setDateFormat:@"yyyy'-'MM'-'dd'T'HH':'mm':'ss'Z'"];
我的问题是,当从 NSDate 转换为 NSString 的另一个方向时,我怎么知道用什么来调用 setTimeZone?当通过查看 Z 的缺失/存在从字符串到 NSDate 时。但是如果我有一个 NSDate,我怎么知道将格式化程序设置为哪个时区?
【问题讨论】:
-
它有时会让人有点困惑,因为如果你 NSDump 一个 NSDate 它确实会显示一个时区。显然,原始时区被隐藏在对象内部的某个地方,但它只能通过
description访问,并且它不参与任何日期转换。
标签: objective-c ios cocoa