2016-11-21 31 views
-3

有谁知道如何打印位置结束时的时间?下面的代码打印出完整的位置时:CLLocation GeoFire位置时间

有在结果的时间之间的时间差,而印刷出来的位置和时间location?.timestamp可选:

geofire?.setLocation(location, forKey: uid) { (error) in 
      if (error != nil) { 
       print("An error occured: \(error)") 
      } else { 
       print(location) 

结果:可选( < + xx.xxxxxx,+ xx.xxxxxxxx> +/-5.00米(速度0.00 MPS /当然-1.00)@ 21/11/2016,16时04分32秒中欧标准时间)

和仅打印:

print(location?.timestamp) 

结果:可选(2016年11月21日15时04分32秒+0000)

如何打印唯一的 “16点04分32秒中欧标准时间” 甚至与“中欧标准时间”21/11/2016,16:04:32之前的日期?谢谢

+0

的可能的复制[斯威夫特 - IOS - 日期和时间以不同的格式(http://stackoverflow.com/questions/28489227/swift-ios-dates-and-times-in - 不同格式) – xoudini

回答

0

CLLocation中的时间戳只是一个Date变量。打印位置和时间戳时会得到不同的结果,因为它们被翻译为两个不同的时区。

A Date timestamp代表抽象时刻,没有日历系统或特定时区。另一方面,CLLocation的描述将该时间转换为您当地的时区,以便更好地进行说明。他们都是等同的;一个(时间戳)显示15:04:32 GMT,另一个显示16:04:32 Central European Standard Time,这是+1 GMT没有DST。

从时间戳得到您的本地时间,你可以重新格式化Date对象这样

let formatter = DateFormatter() 
    formatter.dateFormat = "HH:mm:ss" // use "dd/MM/yyyy, HH:mm:ss" if you want the date included not just the time 
    formatter.timeZone = NSTimeZone.local 
    let timestampFormattedStr = formatter.string(from: (location?.timestamp)!) // result: "16:04:32" 

    // get timezone name (Central European Standard Time in this case) 
    let timeZone = NSTimeZone(forSecondsFromGMT: NSTimeZone.local.secondsFromGMT()) 
    let timeZoneName = timeZone.localizedName(.standard, locale: NSLocale.current)! 
    let timestampWithTimeZone = "\(timestampFormattedStr!) \(timeZoneName)" // results: "16:04:32 Central European Standard Time" 

如果本地时间是你的执行至关重要,我建议检查DST为好。您可以检查这样

if timeZone.isDaylightSavingTimeForDate((location?.timestamp)!) { 

}