Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Swift 如果在洛杉矶,时区是空的_Swift_Timezone_Location_Mapkit - Fatal编程技术网

Swift 如果在洛杉矶,时区是空的

Swift 如果在洛杉矶,时区是空的,swift,timezone,location,mapkit,Swift,Timezone,Location,Mapkit,当用户在美国时,为什么此代码返回空时区。。? 在其他国家也能很好地工作 // Get local time zone let localTimeZoneAbbreviation = TimeZone.current.abbreviation() ?? "" let indexStartOfText = localTimeZoneAbbreviation.index(localTimeZoneAbbreviation.startIndex, offsetBy: 3) // 3 let timeZo

当用户在美国时,为什么此代码返回空时区。。? 在其他国家也能很好地工作

// Get local time zone
let localTimeZoneAbbreviation = TimeZone.current.abbreviation() ?? ""
let indexStartOfText = localTimeZoneAbbreviation.index(localTimeZoneAbbreviation.startIndex, offsetBy: 3) // 3
let timeZone = localTimeZoneAbbreviation.substring(from: indexStartOfText)
print("TimeZone:",timeZone)
我需要数字格式的结果:

-洛杉矶7号

-4纽约

+汉堡2美元


等等。

缩写为PDT,后面没有我所在位置的任何内容。所以,没有偏移量

你可以像rmaddy说的那样用秒来得到偏移量

let seconds = TimeZone.current.secondsFromGMT()
let hours = seconds / 3600
let minutes = abs(seconds / 60) % 60
let timezoneDiff = String(format: "%+.1d:%.2d", hours, minutes)
print(timezoneDiff)

一定要考虑到不同地点的确切小时数。有些是30分钟。可能还有其他的。

为什么要分析字符串?使用时区的secondsFromGMT方法。如何从secondsFromGMT中提取我需要的内容,并在全世界范围内准确无误您可以使用DateFormatter并设置DateFormatter=xlet DateFormatter=DateFormatter DateFormatter.dateFormat=x DateFormatter.stringfrom:Date/-03@Leo是,它必须没有0。在您的示例中,这是完全正确的,但在本例中,服务器只需要整数。我想检查值,比如“let check=timeZoneInt>0?”?1:timeZoneInt==0?0 : -1;' 而不是在任何情况下以服务器所需的任何格式进行操作。