我正在寻找时区列表,但是当尝试生成时区时,我将DST偏移添加到了它。但是我想要标准时间,而不是DST。 这是我正在使用的代码。对于丹麦,我得到的是 GMT + 2 ,而不是 GMT + 1
let dateFormatter = DateFormatter()
dateFormatter.dateStyle = .long
dateFormatter.timeStyle = .long
dateFormatter.dateFormat = "ZZZZ"
let list = TimeZone.knownTimeZoneIdentifiers
for (i, city) in list.enumerated() {
let timezone = TimeZone(identifier: city)
dateFormatter.timeZone = timezone
var isSupportDST = false
if timezone?.nextDaylightSavingTimeTransition != nil {
isSupportDST = true
}
let date = Date()
var timezoneString = dateFormatter.string(from: date)
if timezoneString.count > 3 {
timezoneString.insert(" ", at: timezoneString.index(timezoneString.startIndex, offsetBy: 3))
}
if timezoneString.count > 5 {
timezoneString.insert(" ", at: timezoneString.index(timezoneString.startIndex, offsetBy: 5))
}
var formattedCityName = city
formattedCityName = city.replacingOccurrences(of: "_", with: " ")
var dstoffset = 0 as TimeInterval
if let offset = timezone?.daylightSavingTimeOffset(){
dstoffset = offset
}
let cityWithTimezone = CityWithTimeZone(city: formattedCityName, timeZoneString: timezoneString, timeZoneInSeconds: timezone?.secondsFromGMT() ?? 0, dstOffset: dstoffset, isDSTSupport: isSupportDST)
cityList.insert(cityWithTimezone, at:i)
}
答案 0 :(得分:0)
请尝试这样设置格式化程序的时区
STUFF
它将正确设置当前时间。