Re: How to get the time zone of a parsed date/time string?
Re: How to get the time zone of a parsed date/time string?
- Subject: Re: How to get the time zone of a parsed date/time string?
- From: Chuck Soper <email@hidden>
- Date: Wed, 30 Jul 2014 14:23:34 -0700
- Thread-topic: How to get the time zone of a parsed date/time string?
On 7/30/14, 11:24 AM, "Jens Alfke" <email@hidden> wrote:
>After NSDateFormatter parses a date string that includes a time zone, how
>can the caller determine the time zone? The result is an NSDate object,
>which is simply a timestamp with no notion of time zone.
>
>For example, how does one determine from the string "Wed, 30 Jul 2014
>08:21:35 -0700² that the time zone is GMT-7:00?
>
>(IIRC, in the olden days NSDateFormatter would return an NSCalendarDate
>object, which did have a timeZone property, but no more.)
Your date string does not have a time zone. These internet date strings
are always in UTC. Your string has a GMT offset. You could parse "-0700"
to determine the GMT offset. It starts with a '+' or '-' followed by 2
digits for hours, then 2 digits for minutes. I realize that this is not
the answer you're looking for, but your string doesn't contain a time zone.
You could also parse "Wed, 30 Jul 2014 08:21:35 -0000" (replacing "-0700"
with "-0000"), then use timeIntervalSinceDate: with the original date to
get the GMT offset in seconds.
The following code will parse your time date string.
NSDateFormatter * dateFormatter;
NSLocale * enUSPOSIXLocale;
NSString * dateString = @"Wed, 30 Jul 2014 08:21:35 -0700";
dateFormatter = [[NSDateFormatter alloc] init];
enUSPOSIXLocale = [[NSLocale alloc]
initWithLocaleIdentifier:@"en_US_POSIX"];
[dateFormatter setLocale:enUSPOSIXLocale];
[dateFormatter setDateFormat:@"EEE, dd MMM yyyy HH:mm:ss z"];
[dateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
NSDate * myDate = [dateFormatter dateFromString:dateString];
NSLog(@"%@", [myDate description]);
More info:
https://developer.apple.com/library/ios/qa/qa1480/_index.html
Chuck
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden