NSTimeZone: what is the difference between localTimeZone and systemTimeZone?

前端 未结 2 966
长情又很酷
长情又很酷 2020-12-01 07:50

Under NSTimeZone class, there is both +localTimeZone and +systemTimeZone. I did a test on iphone simulator, both return NSTimeZone object indicating the same timezone. What

相关标签:
2条回答
  • 2020-12-01 07:51

    The user (or your application or an other application) may change either the default time zone for an application (using +[NSTimeZone setDefaultTimeZone]) or the system time zone (using System Preferences) at any time. +[NSTimeZone localTimeZone] returns a proxy that will always act as if it is the current default time zone for the application, even if that default changes. You could change the default time zone for an application to make it behave as if it were in a different time zone.

    +[NSTimeZone systemTimeZone] returns the current system time zone (as set using System Preferences). In most cases, these will be the same (the app's default time zone is set to the system time zone at app startup, I believe).

    If you want to know the system's time zone setting, you probably want to use +[NSTimeZone systemTimeZone]. If you just want the correct time zone for your app to work in, you probably want +[NSTimeZone localTimeZone].

    0 讨论(0)
  • 2020-12-01 07:57

    The docs are a wealth of useful information for this kind of thing. In this case, they say:

    • localTimeZone is the default time zone for the current application. This can be set by the user or programmatically in the app (or possibly in other ways). It's basically a pointer to defaultTimeZone with a level of indirection; if the default time zone changes, localTimeZone will change as well.
    • systemTimeZone, on the other hand, is the time zone used by the core system. A cached value for this will not change if the system time zone changes during the application's execution, unlike localTimeZone.
    0 讨论(0)
提交回复
热议问题