I see that NSTimeZone has these methods :
defaultTimeZone
localTimeZone
systemTimeZone
Can someone explain to me, in simple terms, what the differences are beetween those calls, and when one should be used instead of the other? I don’t understand anything inside the Apple docs about this.
The language in the docs is a bit on the dry side, to be sure, and the similarity of the names is potentially confusing. I’ll quote the
NSTimeZonedocs here and try to explain them:This is the time zone which the device believes it is in; it is often set automatically, and would then correspond to the device’s physical location, but if the user has explicitly set a particular time zone in the Settings App, that’s what you’ll get.
Your application is allowed to set its own time zone, so that you can perform actions as if the device were in another zone, but without affecting the system time zone (and thereby other apps). The setting is performed with a call to
setDefaultTimeZone:. If you haven’t done that, this call is identical to callingsystemTimeZone.This is where it gets a little bit tricky.
localTimeZonegives you nearly the same result asdefaultTimeZone. The difference is that the specificNSTimeZoneinstance you get fromlocalTimeZonewill always reflect the setting you’ve made to the time zone within your app. You can call it once, save the result, and always get the current simulated time zone through that object, no matter the changes made. It is as if, when you use thisNSTimeZoneinstance, the framework is callingdefaultTimeZonefor you, to be sure that you always get the current value.Here’s a couple of brief illustrations of the above. The
NSTimeZoneobject that you get back fromsystemTimeZonerepresents the system time zone at the time you make the call. If you callsystemTimeZoneagain, even if the user has since changed the time zone, you will get the same one. Your app caches that value, and you have to ask the system to clear it withresetSystemTimeZoneto get the update.A similar thing happens with
defaultTimeZone. When you call that method, you get an object that will always represent the same time zone, even if you later callsetDefaultTimeZone:. However, if you use the object you get fromlocalTimeZone, it will follow the change you make to the default time zone*.Apple seems to recommend using
localTimeZone:*Note that
localTimeZoneis still subject to the app-level cache of the system time zone. It only changes to follow your setting of the default time zone.