Question

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.

Was it helpful?

Solution

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:

  • It returns a new value every time.
  • It returns a cached value unless it detects that it ought to return a new value.
  • It returns a cached value, clearing it when it notices a locale/calendar change.

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:].

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top