I have a function that converts NSString in RFC3339 to NSDate using NSDateFormatter, but I don't know how to take into account the timezone doing the opposite.
The corresponding portion of the string for converting NSDate:
[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'"];
My question is that when converting in the other direction from NSDate to NSString, how do I know what can I call setTimeZone with? When you go from line to NSDate, looking at the absence / presence of Z. But if I have NSDate, how do I know in what time zone to set the formatter?
source
share