首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从TimeZone.abbreviation()初始化TimeZone时出现问题

从TimeZone.abbreviation()初始化TimeZone时出现问题
EN

Stack Overflow用户
提问于 2019-11-06 02:54:34
回答 1查看 100关注 0票数 0

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

代码语言:javascript
复制
{
    "day": 1,
    "month": 1,
    "year": 1970,
    "timeZone": "GMT"
}

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

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

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

有没有一种方法可以在格式化TimeZone时初始化它,它的偏移量是GMT,而不是它唯一的缩写?我见过一个名为TimeZone(secondsFromGMT: Int)TimeZone初始化器。我是否可以简单地将"GMT+8"中的8乘以3600 (一小时内的秒数)并将结果传递给TimeZone(secondsFromGMT: Int)

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-11-06 03:51:12

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

代码语言:javascript
复制
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
        }
    }
}

它的用法如下:

代码语言:javascript
复制
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类:

代码语言:javascript
复制
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扩展的问题,请让我知道。我想我已经考虑了所有的情况,但可能是错误的。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58717754

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档