2016-11-21 3 views
-3

Kann jemand die Zeit am Ende des Standortes drucken? wenn die vollständige Lage in dem folgenden Code ausdrucken:CLLocation GeoFire Ort Zeit

Es gibt eine Zeitdifferenz zwischen der Zeit, in der Folge, während die Lage Ausdruck und die Zeit location?.timestamp Optional:

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

ERGEBNIS: Optional (< + xx.xxxxxx, + xx.xxxxxxxx> +/- 5.00m (Geschwindigkeit 0,00 mps/Kurs -1,00) @ 21/11/2016, 16.04.32 Central European Standard Time)

und durch Ausdruck nur:

print(location?.timestamp) 

ERGEBNIS: Optional (2016-11-21 15:04:32 +0000)

Wie nur "16.04.32 Central European Standard Time" oder sogar drucken mit dem Datum vor "21/11/2016, 16:04:32 Mitteleuropäische Standardzeit"? Danke

+0

Mögliche Duplikat überprüfen (http://stackoverflow.com/questions/28489227/swift-ios-dates-and-times-in -unterschiedliches Format – xoudini

Antwort

0

Der Zeitstempel in CLLocation ist einfach eine Date Variable. Sie erhalten unterschiedliche Ergebnisse beim Drucken des Standorts und des Zeitstempels, da sie in zwei verschiedene Zeitzonen übersetzt werden.

A Date Zeitstempel stellen einen abstrakten Moment in der Zeit und hat kein Kalendersystem oder einen bestimmten Zeitzone. CLLocation Die Beschreibung hingegen konvertiert diese Zeit in Ihre lokale Zeitzone zur besseren Klärung. Sie sind beide gleichwertig; Eins (der Zeitstempel) zeigt 15:04:32 GMT, das andere zeigt 16:04:32 Central European Standard Time, was +1 GMT ohne DST ist.

Um die Ortszeit aus dem Zeitstempel zu erhalten, können Sie das Date Objekt wie dieses

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" 

umformatieren Wenn lokale Zeit, um Ihre Implementierung entscheidend ist, würde ich auch die Überprüfung für DST vorschlagen. [- IOS - Swift Termine und Zeiten in verschiedenem Format] Sie können wie dieses

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

}