在我的用例中,我得到了一个 Microsoft 时区索引值。你找到的索引here .我需要这个索引来获取项目的时区。
为了在 Swift 中设置自定义时区,我刚刚找到了这个片段。
dateformatter.timeZone = NSTimeZone(abbreviation: "CEST")
和here我找到了一个包含所有缩写的列表
ADT = "America/Halifax";
AKDT = "America/Juneau";
AKST = "America/Juneau";
ART = "America/Argentina/Buenos_Aires";
AST = "America/Halifax";
BDT = "Asia/Dhaka";
[...]
WET = "Europe/Lisbon";
WIT = "Asia/Jakarta";
所以我掌握了所有信息。 UTC/GMT 时间。 Swift 中的索引值和所有可能的时区。但我不知道哪个时区是什么意思。
例如,在 Microsoft 时区索引值中是 001“萨摩亚标准时间 - GMT-11:00 中途岛,萨摩亚”
但这在 swift 中是什么?我不能做类似的事情
dateformatter.timeZone = NSTimeZone(abbreviation: "GMT-11") //not found the abbreviation
有人有解决问题的想法或意见吗?
最佳答案
我有一个可能有用的解决方案,但它有点老套而且不是最干净的。因为您可以从 NSTimeZone
中获取缩写值的字典,所以我们可以在 for
循环中迭代它。如果你有一个我们可以插入 NSTimeZone(abbreviation:)
的缩写,我们可以使用 .secondsFromGMT
找到 GMT 的秒数,然后创建一个 if
语句来检查它是否是我们正在寻找的 GMT。她是一个有效的例子:
// Get the full name and abbreviation from the NSTimeZone dictionary
for (abbreviation, fullName) in NSTimeZone.abbreviationDictionary() {
// Plug the abbreviation into NSTimeZone and get the seconds from GMT and match it to the GMT we want (in this case, GMT+8)
if NSTimeZone(abbreviation: abbreviation)?.secondsFromGMT == 28800 {
// Print the result if a match was found.
print(fullName)
}
}
关于swift - 如何将自定义时区从 Microsoft Time Zone Index Value 设置为 Swift?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38324064/