objective-c NSTimeZone:localTimeZone 和 systemTimeZone 有什么区别?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1526990/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 22:19:10  来源:igfitidea点击:

NSTimeZone: what is the difference between localTimeZone and systemTimeZone?

objective-ciphonecocoa

提问by Gaius Parx

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 is the difference? Which one I should use to find out the timezone setting of the iPhone? Thanks

在 NSTimeZone 类下,有 +localTimeZone 和 +systemTimeZone。我在 iphone 模拟器上做了一个测试,都返回 NSTimeZone 对象,指示相同的时区。有什么不同?我应该使用哪一个来找出 iPhone 的时区设置?谢谢

My test:

我的测试:

NSLog(@"Local Time Zone %@",[[NSTimeZone localTimeZone] name]);
NSLog(@"System Time Zone %@",[[NSTimeZone systemTimeZone] name]);

回答by Barry Wark

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 currentdefault 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 setDefaultTimeZone])或系统时区(使用系统偏好设置)。+[NSTimeZone localTimeZone]返回一个代理,该代理将始终充当应用程序的当前默认时区,即使该默认值发生变化。您可以更改应用程序的默认时区,使其表现得好像在不同的时区中一样。

+[NSTimeZone systemTimeZone]returns the currentsystem 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).

+[NSTimeZone systemTimeZone]返回当前系统时区(使用系统偏好设置)。在大多数情况下,这些将是相同的(我相信应用程序的默认时区设置为应用程序启动时的系统时区)。

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

如果您想知道系统的时区设置,您可能想使用+[NSTimeZone systemTimeZone]. 如果您只想为您的应用程序使用正确的时区,您可能需要+[NSTimeZone localTimeZone].

回答by Tim

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

文档是这类事情的大量有用信息。在这种情况下,他们说:

  • localTimeZoneis 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 defaultTimeZonewith a level of indirection; if the default time zone changes, localTimeZonewillchange as well.
  • systemTimeZone, on the other hand, is the time zone used by the core system. A cached value for this will notchange if the system time zone changes during the application's execution, unlike localTimeZone.
  • localTimeZone是当前应用程序的默认时区。这可以由用户设置或在应用程序中以编程方式(或可能以其他方式)进行设置。它基本上是defaultTimeZone一个具有间接级别的指针;如果默认时区改变,localTimeZone也会改变。
  • systemTimeZone,另一方面,是核心系统使用的时区。这方面的一个缓存值也不会,如果系统时区的应用程序的执行过程中的变化而改变,不像localTimeZone