【问题标题】:Convert the Time to GMT and then upload the timestamp to Firebase (Swift)将时间转换为 GMT,然后将时间戳上传到 Firebase (Swift)
【发布时间】:2020-06-26 18:18:39
【问题描述】:

我想做的事:将用户的当前时间转换为GMT时间戳,然后上传到firebase。

问题:我的 Firestore 数据库中的时间戳不是 GMT 时间戳,而是具有用户唯一时区的常规时间戳。

我的代码:

let formatter = DateFormatter()
formatter.timeZone = TimeZone(secondsFromGMT: 0)
formatter.dateFormat = "yyyy-MM-dd HH:mm:ss Z"
let date = Date()
self.db.collection("posts").document(combined).setData([
    "time": date,
]) { err in
    if let err = err {
        print("Error writing document: \(err)")
    } else {
        print("Post Document successfully written!")
        print(date)
    }
}

到目前为止实际发生的情况: 我在上传到 Firebase 后打印出日期时收到2020-06-26 17:57:40 +0000。这是我要上传的时间戳。

但是,一旦我检查了 firebase,我就会看到:

我尝试了什么:

  • 删除应用并重新安装
  • 清除我的 Firebase Firestore

【问题讨论】:

  • GCP 控制台正在将您的 UTC 日期转换为您的本地日期,例如尝试创建一个新的测试实体。添加“日期和时间”类型的属性,选择以 GTM/UTC 作为时区的日期和时间。添加另一个属性,此时第一个属性的卡片将被折叠并应显示本地时间之前的属性值

标签: ios swift firebase google-cloud-firestore timestamp


【解决方案1】:

这两个时间戳实际上是相同的。即 UTC+2 中的 19:57:40 与 UTC+0 中的 17:57:40 相同。

所以我怀疑问题出在 Firebase 控制台中实际显示的内容。可能有一个设置可以控制日期的显示方式。

【讨论】:

  • 其实没有设置。控制台始终使用本地计算机设置的时区。
  • 好的,谢谢。我担心我没有做正确的事情。
猜你喜欢
  • 2016-11-12
  • 1970-01-01
  • 1970-01-01
  • 2018-09-17
  • 2018-03-07
  • 2018-12-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多