Pergunta

What's the best way to compare two time zones?

I'm facing an issue while comparing two NSTimeZone instances using - (BOOL)isEqualToTimeZone:(NSTimeZone *)aTimeZone method.

NSString *timeZoneName = ...

NSTimeZone *sytemTimeZone = [NSTimeZone systemTimeZone];
NSTimeZone *selectedTimeZone = [NSTimeZone timeZoneWithName:timeZoneName];

if ([sytemTimeZone isEqualToTimeZone:selectedTimeZone]) 
    isEqual = YES;  
else 
    isEqual = NO;

Step 1: Go to Settings (Application) > General > Date & Time > Time Zone and search for "Austin". The entry that you'll get will be "Austin, U.S.A". Select this Time Zone. This SHOULD be your new system time zone now!

Step 2: Create a small iPhone/iPad application. Use [NSTimeZone knownTimeZoneNames] to get a list of time zone names. Then try to find "Austin". It's not there! So, i guess we can use "America/Chicago" as the timezone?

Why is the Setting's timezone list different from ours?

Step 3: Now compare the system timezone with time zone for "America/Chicago". They don't match.

Foi útil?

Solução

I've posted a bug for this - The bug is that Austin isn't in the known time zone list yet is offered by the settings app.

As per an Apple Engineer:

For two NSTimeZones to compare equal, both the names and the data (as returns by -data) must be equal. You have to be careful when comparing time zones, because two zones can have the same offset from GMT but not be the same time zone for historical reasons. Remember that time zones are used for all sorts of calendrical calculations, meaning that they are only equal if, throughout history, they've always had the same GMT offset, the same daylight savings transitions, and so on.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top