iPhone - 时区便利方法之间的差异

发布于 2024-11-06 21:37:04 字数 187 浏览 0 评论 0原文

我看到 NSTimeZone 有这些方法:

defaultTimeZone  
localTimeZone  
systemTimeZone

有人可以简单地向我解释一下这些调用之间有什么区别,以及何时应该使用其中一个而不是另一个?我不明白苹果文档中关于此的任何内容。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

情徒 2024-11-13 21:37:04

可以肯定的是,文档中的语言有点枯燥,而且名称的相似性可能会令人困惑。我将引用 NSTimeZone 文档 在这里并尝试解释它们:

系统时区
系统当前使用的时区。如果无法确定当前时区,则返回 GMT 时区。

这是设备认为其所在的时区;它通常是自动设置的,然后与设备的物理位置相对应,但如果用户在“设置”应用程序中明确设置了特定时区,那么您将得到该时区。

默认时区
当前应用程序的默认时区。如果没有设置默认时区,此方法将调用systemTimeZone并返回系统时区。

您的应用程序可以设置自己的时区,以便您可以像设备位于另一个区域一样执行操作,但不会影响系统时区(从而影响其他应用程序)。该设置是通过调用setDefaultTimeZone:来执行的。如果您还没有这样做,则此调用与调用 systemTimeZone 相同。

本地时区
将所有消息转发到当前应用程序的默认时区的对象。本地时区始终代表默认时区的当前状态。

这就是有点棘手的地方。 localTimeZone 提供的结果与 defaultTimeZone 几乎相同。不同之处在于,您从 localTimeZone 获取的特定 NSTimeZone 实例将始终反映您在应用中对时区所做的设置。您可以调用它一次,保存结果,并始终通过该对象获取当前模拟时区,无论进行什么更改。就好像,当您使用此 NSTimeZone 实例时,框架正在为您调用 defaultTimeZone,以确保您始终获得当前值。

这是上述内容的几个简短说明。您从 systemTimeZone 返回的 NSTimeZone 对象表示您进行调用时的系统时区。如果您再次调用 systemTimeZone,即使用户此后更改了时区,您也会得到相同的时区。您的应用会缓存该值,您必须要求系统使用 resetSystemTimeZone 清除它才能获取更新。

// Say that device is in GMT originally
NSLog(@"%@", [NSTimeZone systemTimeZone]);    // GMT
// User flies into Rome and iPhone changes the zone automatically
NSLog(@"%@", [NSTimeZone systemTimeZone]);    // Still GMT
[NSTimeZone resetSystemTimeZone];    // Clear app's cache
NSLog(@"%@", [NSTimeZone systemTimeZone]);    // Now GMT+2

defaultTimeZone 也会发生类似的情况。当您调用该方法时,您将获得一个始终代表同一时区的对象,即使您稍后调用 setDefaultTimeZone: 也是如此。但是,如果您使用从 localTimeZone 获取的对象,它将遵循您对默认时区*所做的更改。

// Say that defaultTimeZone is originally GMT
NSTimeZone * myDefaultTZ = [NSTimeZone defaultTimeZone];
NSTimeZone * myLocalTZ = [NSTimeZone localTimeZone];
[NSTimeZone setDefaultTimeZone:[NSTimeZone timeZoneWithName:@"Etc/GMT-4"]];
NSLog(@"%@", myDefaultTZ);    // Still gives GMT
NSLog(@"%@", [NSTimeZone defaultTimeZone]);    // GMT-4, the new value
NSLog(@"%@", myLocalTZ);    // Also the new value!

苹果似乎推荐使用 localTimeZone

使用 localTimeZone 类方法,您可以获得一个相对时区对象,该对象将自身解码为它所在的任何计算机上的默认时区。


*注意,localTimeZone仍然受系统时区的应用级缓存的影响。它只会更改以遵循您的默认时区设置。

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 NSTimeZone docs here and try to explain them:

systemTimeZone
The time zone currently used by the system. If the current time zone cannot be determined, returns the GMT time zone.

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.

defaultTimeZone
The default time zone for the current application. If no default time zone has been set, this method invokes systemTimeZone and returns the system time zone.

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 calling systemTimeZone.

localTimeZone
An object that forwards all messages to the default time zone for the current application. The local time zone represents the current state of the default time zone at all times.

This is where it gets a little bit tricky. localTimeZone gives you nearly the same result as defaultTimeZone. The difference is that the specific NSTimeZone instance you get from localTimeZone will 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 this NSTimeZone instance, the framework is calling defaultTimeZone for you, to be sure that you always get the current value.

Here's a couple of brief illustrations of the above. The NSTimeZone object that you get back from systemTimeZone represents the system time zone at the time you make the call. If you call systemTimeZone again, 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 with resetSystemTimeZone to get the update.

// Say that device is in GMT originally
NSLog(@"%@", [NSTimeZone systemTimeZone]);    // GMT
// User flies into Rome and iPhone changes the zone automatically
NSLog(@"%@", [NSTimeZone systemTimeZone]);    // Still GMT
[NSTimeZone resetSystemTimeZone];    // Clear app's cache
NSLog(@"%@", [NSTimeZone systemTimeZone]);    // Now GMT+2

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 call setDefaultTimeZone:. However, if you use the object you get from localTimeZone, it will follow the change you make to the default time zone*.

// Say that defaultTimeZone is originally GMT
NSTimeZone * myDefaultTZ = [NSTimeZone defaultTimeZone];
NSTimeZone * myLocalTZ = [NSTimeZone localTimeZone];
[NSTimeZone setDefaultTimeZone:[NSTimeZone timeZoneWithName:@"Etc/GMT-4"]];
NSLog(@"%@", myDefaultTZ);    // Still gives GMT
NSLog(@"%@", [NSTimeZone defaultTimeZone]);    // GMT-4, the new value
NSLog(@"%@", myLocalTZ);    // Also the new value!

Apple seems to recommend using localTimeZone:

with the localTimeZone class method, you can get a relative time zone object that decodes itself to become the default time zone on any computer on which it finds itself.


*Note that localTimeZone is still subject to the app-level cache of the system time zone. It only changes to follow your setting of the default time zone.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文