gpt4 book ai didi

ios - 无法从 TimeZone.abbreviation() 初始化 TimeZone

转载 作者:行者123 更新时间:2023-11-28 13:23:42 25 4
gpt4 key购买 nike

我通过存储日期组件字典在我的后端存储用户出生日期。它看起来像这样:

{
"day": 1,
"month": 1,
"year": 1970,
"timeZone": "GMT"
}

为了存储此对象,它从用户输入中获取用户的生日、月份和年份。然而,用户时区是通过 TimeZone.current.abbreviation() 收集的。

现在,我后端的一些用户生日对象的 "timeZone" 格式为 "CST""BST",或 “PDT”。以这种方式格式化的"timeZone"通过let timeZone = TimeZone(abbreviation: "CST")成功地在前端初始化了一个TimeZone!let timeZone = TimeZone(abbreviation: "BST")!let timeZone = TimeZone(abbreviation: "PDT")!

问题是,我后端的其他用户生日对象的 "timeZone" 格式为 "GMT+8"。当尝试通过 let timeZone = TimeZone(abbreviation: "GMT+8")! 初始化格式如下的 "timeZone" 时,初始化返回 nil。我还尝试了 let timeZone = TimeZone(identifier: "GMT+8")!,但这也返回了 nil

TimeZone 是根据其相对于 GMT 的偏移量而不是其独特的缩写进行格式化时,是否有一种方法可以初始化它?我见过一个 TimeZone 初始化程序,它是 TimeZone(secondsFromGMT: Int)。我可以简单地从 "GMT+8" 中取出 8 并将其乘以 3600(一小时中的秒数)并传递这个结果到 TimeZone(secondsFromGMT: Int)?

最佳答案

我最终编写了代码来调整我的应用程序以解决这些意外的边缘情况,其中 TimeZone 的缩写格式为 "GMT+8" 而不是 "SGT" .我为 TimeZone 创建了一个扩展:

extension TimeZone {
static func timeZone(from string: String) -> TimeZone {
//The string format passed into this function should always be similar to "GMT+8" or "GMT-3:30"

if string.contains("±") {
//This case should always be "GMT±00:00", or simply GMT
return TimeZone(secondsFromGMT: 0)!
} else {

//If the string doesn't contain "±", then there should be some offset. We will split the string into timeZone components. "GMT+8" would split into ["GMT", "8"]. "GMT-3:30" would split int ["GMT","3","30"]
let timeZoneComponents = string.components(separatedBy: CharacterSet(charactersIn: "+-:"))
var isAheadOfGMT: Bool!

//Check if the string contains "+". This will dictate if we add or subtract seconds from GMT
if string.contains("+") {
isAheadOfGMT = true
} else {
isAheadOfGMT = false
}

//Grab the second element in timeZoneElements. This represents the offset in hours
let offsetInHours = Int(timeZoneComponents[1])!

//Convert these hours into seconds
var offsetInSeconds: Int!
if isAheadOfGMT {
offsetInSeconds = offsetInHours * 3600
} else {
offsetInSeconds = offsetInHours * -3600
}

//Check if there is a colon in the passed string. If it does, then there are additional minutes we need to account for
if string.contains(":") {
let additionalMinutes = Int(timeZoneComponents[2])!
let additionalSeconds = additionalMinutes * 60
offsetInSeconds += additionalSeconds
}

//Create a TimeZone from this calculated offset in seconds
let timeZoneFromOffset = TimeZone(secondsFromGMT: offsetInSeconds)!

//Return this value
return timeZoneFromOffset
}
}
}

它是这样使用的:

let json: [String:String] = ["timeZone":"GMT+8"]
let timeZone = json["timeZone"]
let birthDate: BirthDate!
if let timeZoneFromAbbrev = TimeZone(abbreviation: timeZone) {
birthDate = BirthDate(day: birthDay, month: birthMonth, year: birthYear, timeZone: timeZoneFromAbbrev)
} else {
let timeZoneFromOffset = TimeZone.timeZone(from: timeZone)
print(timeZoneFromOffset.abbreviation())
//Prints "GMT+8"

birthDate = BirthDate(day: birthDay, month: birthMonth, year: birthYear, timeZone: timeZoneFromOffset)
}

我的 BirthDate 上下文类:

class BirthDate {
var day: Int
var month: Int
var year: Int
var timeZone: TimeZone

init(day: Int, month: Int, year: Int, timeZone: TimeZone) {
self.day = day
self.month = month
self.year = year
self.timeZone = timeZone
}
}

时区是一件很有趣的事情。如果有人发现上面的 TimeZone 扩展有问题,请告诉我。我认为我已经考虑了所有情况,但可能是错误的。

关于ios - 无法从 TimeZone.abbreviation() 初始化 TimeZone,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58717754/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com