简体   繁体   English

iOS API TimeZone.current 不返回实际系统时间

[英]iOS API TimeZone.current does not return actual system time

My iOS swift application calls TimeZone.current when the application comes to the foreground.当应用程序进入前台时,我的 iOS swift 应用程序调用TimeZone.current If I manually change the timezone from Settings->General->Date&Time, and then bring my application to the foreground, I get the correct timezone.如果我从 Settings->General->Date&Time 手动更改时区,然后将我的应用程序带到前台,我会得到正确的时区。 But if my timezone setting was set to automatic, and then I turn off the automatic, and bring back my application to the foreground, the TimeZone.current API does not reflect the new system timezone!但是,如果我的时区设置设置为自动,然后我关闭自动,并将我的应用程序带回前台,则TimeZone.current API 不会反映新的系统时区!

If I put the application to background and come back again to the foreground, this time it reflects the proper system timezone.如果我将应用程序置于后台并再次回到前台,这一次它反映了正确的系统时区。 Is this a bug in iOS SDK?这是 iOS SDK 中的错误吗? Is there a workaround for this?有解决方法吗?

Try using NSTimeZone.local , which is:尝试使用NSTimeZone.local ,即:

An object that tracks the current system time zone.跟踪当前系统时区的 object。

You can also use the NSTimeZone.system property, but you'll need to manually call NSTimeZone.resetSystemTimeZone() to clear and refresh the current cached system timezone value.您也可以使用NSTimeZone.system属性,但您需要手动调用NSTimeZone.resetSystemTimeZone()来清除和刷新当前缓存的系统时区值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM