If I try and set the time zone arbitrarily from anywhere within the app using the following :
[[NSCalendar currentCalendar] setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"EST"]];
NSTimeZone *tz = [[NSCalendar currentCalendar] timeZone];
NSLog(@"%@", tz);
the result of the log statement is :
America/Los_Angeles (PDT) offset -25200 (Daylight)
(which is my local timezone, i.e. [NSTimeZone systemTimeZone])
However, functionally similar code within a category on NSCalendar works fine :
[self setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"EST"]];
NSTimeZone *tz = [self timeZone];
NSLog(@"%@", tz);
and the log yields :
America/New_York (EDT) offset -14400 (Daylight)
What is going with setting the time zone for [NSCalendar currentCalendar]? The behavior is counterintuitive.
Simple answer: +[NSCalendar currentCalendar]
isn't returning the same instance. It is supposed to return you an instance that reflects the currently selected locale and time zone at the time that +currentCalendar
is called. There are three possible reasonable behaviours:
Modifying the calendar is only sensible in the first case. (In the second case, previous calls to +currentCalendar
will point to the same instance. In the third case, all calls to +currentCalendar
will return the same instance until the user changes locale/time zone.)
The correct way to set the app's time zone is +[NSTimeZone setDefaultTimeZone:]
.