Firebase 服务器时间戳与正确时间不同
Firebase server-timestamp differs from the correct time
我正在尝试使用 firebase-server-timestamp
在 he/she 向我们发送订单时向用户显示。
我遇到的问题是,server-timestamp
显示错误的时间(例如:当前时间是 12:30,但时间戳显示 12:15)。
这怎么可能?
Android代码
data class MyTimestampPOJO(
val date: Timestamp = Timestamp.now(), // wrong, 15 minutes too early
)
数据库屏幕截图(发送是在 12:50 而不是 12:35)
正在将数据写入 firestore
suspend fun sendEmail() {
val data = MyTimestampPOJO()
FirebaseFirestore..getInstance().collection("orders_service").add(data).await()
}
正在读取数据
export const dbOrderServiceOnCreated = functions
.region("europe-west1")
.firestore
.document("orders_service/{id}")
.onCreate((snapshot, context) => {
const data = snapshot.data()
const date = convertTimestampToDate(data.date)
return Promise.resolve()
}
function convertTimestampToDate(stamp: firestore.Timestamp): string {
return new Intl.DateTimeFormat('de-De', { year: 'numeric', month: 'numeric', day: 'numeric', hour: 'numeric', minute: 'numeric' }).format(stamp.toDate())
}
我正在 firebase-emulator
中测试这个。我的 phone 上的时间是正确的(即使这无关紧要,因为 server-timestamp
独立于 phone 时间,它应该是这样。)
编辑答案
正如@DIVYANSHU SAHU 和@MiniDev99 所指出的,我有几个问题。首先,而不是使用
data class MyTimestampPOJO(val date: Timestamp = Timestamp.now())
应该使用
data class MyTimestampPOJO(@ServerTimestamp val date: Timestamp? = null)
原因是,当文档写入数据库时,第二个 POJO-Timestamp 将被填充:
Annotation used to mark a timestamp field to be populated with a
server timestamp. If a POJO * being written contains {@code null} for
a @ServerTimestamp-annotated field, it will be replaced * with a
server-generated timestamp.
此外,每个函数都可以有自己的区域(可调用和基于事件)。因此,应该始终将功能区域指定到他们的特定区域(例如,我的功能区域是自动分配给我们的,但我住在欧洲)。
export const dbOrderServiceOnCreated = functions
.region("europe-west1") // IMPORTANT!!!!
.firestore
...
我不确定,但我想我以前见过这种情况,在那种情况下(但我在这里可能是错的)我认为应用 运行 所在的 emulator/phone date/time 设置错误。所以Timestamp现在也是错误的。
您可以保存 Firebase 服务器的时间和日期,而不是 phone 的日期和时间,
因为如果您要保存 phone 的时间戳,它会有所不同,因为每个 phone 都会有不同的时间。
DatabaseReference ref = FirebaseDatabase.getInstance().getReference();
Map map = new HashMap();
map.put("timestamp", ServerValue.TIMESTAMP);
ref.child("yourNode").updateChildren(map);
我正在尝试使用 firebase-server-timestamp
在 he/she 向我们发送订单时向用户显示。
我遇到的问题是,server-timestamp
显示错误的时间(例如:当前时间是 12:30,但时间戳显示 12:15)。
这怎么可能?
Android代码
data class MyTimestampPOJO(
val date: Timestamp = Timestamp.now(), // wrong, 15 minutes too early
)
数据库屏幕截图(发送是在 12:50 而不是 12:35)
正在将数据写入 firestore
suspend fun sendEmail() {
val data = MyTimestampPOJO()
FirebaseFirestore..getInstance().collection("orders_service").add(data).await()
}
正在读取数据
export const dbOrderServiceOnCreated = functions
.region("europe-west1")
.firestore
.document("orders_service/{id}")
.onCreate((snapshot, context) => {
const data = snapshot.data()
const date = convertTimestampToDate(data.date)
return Promise.resolve()
}
function convertTimestampToDate(stamp: firestore.Timestamp): string {
return new Intl.DateTimeFormat('de-De', { year: 'numeric', month: 'numeric', day: 'numeric', hour: 'numeric', minute: 'numeric' }).format(stamp.toDate())
}
我正在 firebase-emulator
中测试这个。我的 phone 上的时间是正确的(即使这无关紧要,因为 server-timestamp
独立于 phone 时间,它应该是这样。)
编辑答案
正如@DIVYANSHU SAHU 和@MiniDev99 所指出的,我有几个问题。首先,而不是使用
data class MyTimestampPOJO(val date: Timestamp = Timestamp.now())
应该使用
data class MyTimestampPOJO(@ServerTimestamp val date: Timestamp? = null)
原因是,当文档写入数据库时,第二个 POJO-Timestamp 将被填充:
Annotation used to mark a timestamp field to be populated with a server timestamp. If a POJO * being written contains {@code null} for a @ServerTimestamp-annotated field, it will be replaced * with a server-generated timestamp.
此外,每个函数都可以有自己的区域(可调用和基于事件)。因此,应该始终将功能区域指定到他们的特定区域(例如,我的功能区域是自动分配给我们的,但我住在欧洲)。
export const dbOrderServiceOnCreated = functions
.region("europe-west1") // IMPORTANT!!!!
.firestore
...
我不确定,但我想我以前见过这种情况,在那种情况下(但我在这里可能是错的)我认为应用 运行 所在的 emulator/phone date/time 设置错误。所以Timestamp现在也是错误的。
您可以保存 Firebase 服务器的时间和日期,而不是 phone 的日期和时间, 因为如果您要保存 phone 的时间戳,它会有所不同,因为每个 phone 都会有不同的时间。
DatabaseReference ref = FirebaseDatabase.getInstance().getReference();
Map map = new HashMap();
map.put("timestamp", ServerValue.TIMESTAMP);
ref.child("yourNode").updateChildren(map);