OGeek|极客世界-中国程序员成长平台

标题: ios - swift中以下时区之间有什么区别 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-11 19:55
标题: ios - swift中以下时区之间有什么区别

let timeZone = NSTimeZone.system.description
let localTimeZone = TimeZone.ReferenceType.local.description
let currentTimeZone = TimeZone.current.description
let defaultTimeZone = TimeZone.ReferenceType.default.description
let autoUpdateTimezon = TimeZone.autoupdatingCurrent.description

print ("System Timezone \(timeZone)")
print ("Local Timezone \(localTimeZone)")
print ("Current Timezone \(currentTimeZone)")
print ("Default Timezone \(defaultTimeZone)")
print ("Auto updating Timezone \(autoUpdateTimezon)")

输出

System Timezone Asia/Kolkata (current)

Local Timezone Asia/Kolkata (autoupdatingCurrent)

Current Timezone Asia/Kolkata (current)

Default Timezone Asia/Kolkata (current)

Auto updating Timezone Asia/Kolkata (autoupdatingCurrent)

所以,我得到的所有输出都是相同的,所以这些时区之间有什么区别,在这种情况下我们应该使用哪个时区。

问题

我使用以下代码进行日期转换

static func stringToString(strDate:String, fromFormat:String, toFormat:String)->String{
        let dateFormatter = DateFormatter()
        dateFormatter.timeZone = TimeZone.init(abbreviation: "UTC") ?? TimeZone(identifier: "UTC") ?? TimeZone.ReferenceType.default
        dateFormatter.dateFormat = fromFormat
        let currentDate = dateFormatter.date(from: strDate) ?? Date()
        dateFormatter.dateFormat =  toFormat
        dateFormatter.timeZone = TimeZone.ReferenceType.default
        let currentDates = dateFormatter.string(from: currentDate)
        return currentDates
    }

Scene : My app is crashing in qatar if user set timezone automatically and off the 24 hours, but in india there is no crash (TimeZone.ReferenceType.local)

I have given next build with TimeZone.ReferenceType.default and issue is solved

So, i cant understand what was the issue.

崩溃报告

enter image description here

我正在崩溃的旧代码

enter image description here



Best Answer-推荐答案


Local -> 跟踪当前系统时区的对象。如果您想要一个始终反射(reflect)当前系统时区的对象,请使用此属性。从 ios 11 开始,本地类属性反射(reflect)当前系统时区,而之前它反射(reflect)的是默认时区。

系统 -> 系统当前使用的时区。如果您访问系统类属性,则其值由应用程序缓存,并且如果用户随后更改系统时区,则不会更新。为了让系统属性反射(reflect)新的时区,必须先调用resetSystemTimeZone()方法清除缓存值。

默认 -> 当前应用的默认时区。如果没有设置默认时区,则使用当前系统时区。如果无法确定当前系统时区,则使用 GMT 时区。应用程序使用默认时区进行日期和时间操作。您可以将其设置为使应用像在不同的时区一样运行。

当前 -> 系统当前使用的时区。

autoupdatingCurrent -> 系统当前使用的时区,自动更新到用户当前的偏好。

来源-> https://developer.apple.com/documentation/foundation/nstimezone

关于ios - swift中以下时区之间有什么区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49504976/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://sqlite.in/) Powered by Discuz! X3.4