iPhone - 时区方便方法的区别 [英] iPhone - Differences among time zone convenience methods

查看:190
本文介绍了iPhone - 时区方便方法的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看到 NSTimeZone 有以下方法:

defaultTimeZone  
localTimeZone  
systemTimeZone

有人可以向我解释一下,差异是在那些呼叫之间,当一个应该使用而不是另一个?我不明白Apple文档中有关这一点的任何内容。

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.

推荐答案

文档中的语言有点干,可以肯定,名称的相似性可能会混淆。我将引用 NSTimeZone docs ,并尝试解释它们:

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

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

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

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

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

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

将所有邮件转发到当前应用程序的默认时区的对象。本地时区表示所有时间默认时区的当前状态。

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.

这是一个有点棘手的地方。 localTimeZone 给出了与 defaultTimeZone 几乎相同的结果。区别在于,从 localTimeZone 获得的特定 NSTimeZone 实例将始终反映您对时间的设置区域。您可以调用一次,保存结果,并始终通过该对象获取当前模拟时区,无论所做的更改。当你使用这个 NSTimeZone 实例时,框架正在为你调用 defaultTimeZone 你总是得到当前的值。

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.

这里是上面的几个简要说明。从 systemTimeZone 返回的 NSTimeZone 对象表示您打电话时的系统时区。如果再次调用 systemTimeZone ,即使用户以后更改了时区,您将获得相同的时区。您的应用缓存该值,您必须要求系统使用 resetSystemTimeZone 清除它。

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

类似的事情发生在 defaultTimeZone 。当您调用该方法时,您将获得一个始终表示相同时区的对象,即使您稍后调用 setDefaultTimeZone:。但是,如果您使用从 localTimeZone 获得的对象,它将跟随您对默认时区*所做的更改。

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似乎建议使用 localTimeZone


使用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.






*请注意 localTimeZone 仍然受制于系统时区的应用程序级缓存。它只会更改为按照您的默认时区设置。


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

这篇关于iPhone - 时区方便方法的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆