React/Redux 和 Firebase 实时数据库 - 在 load/update 时转换存储中的所有时间戳,给定已知偏移量

React/Redux and Firebase Realtime Database - Convert all timestamps in the store upon load/update, given a known offset

我有一个React/Redux申请。

我正在从 Firebase 实时数据库中获取一组“笔记”。 “notes”有一个 Unix 纪元时间戳“timestamp”存储在 UTC 时区。我希望用户看到本地时区的所有时间。 获取后我需要调整“时间戳”以反映用户时区。

我正在创建数据库订阅以更新商店中的笔记。

将 UTC 时区调整为用户本地时间的最佳方法是什么? 我想我首先需要获取用户设置,然后即时调整“时间戳”?

我的笔记对象

{
  title: "now",
  timestamp: "161651792000"
}

我的用户对象

{
  displayName: "Mickey",
  userTz: "Europe\Stockholm",
  "userGmtOffset":3600"
}

db订阅码

database.ref(`${dbPathAccess}/${uid}`).on('value', (myNoteAccessSnaps) => {
  ...
}

非常感谢! /K

时区仅在日期如何显示给用户方面起作用。 如果日期对象是使用 Firebase.ServerValue.TIMESTAMP 创建的时间戳构造的,它将自动具有与本地时区相同的时区。以防万一:firebase. firestore. Timestamp.toDate()

另一方面,getTimezoneOffset() 可用于获取客户端的时区。如果您想进行任何研究,请勾选 documentation and help here