有人知道如何在Firestore中持久化UTC时间戳吗?
在我的Angular
应用程序中,如果我将今天的日期转换为以下时间戳,我最终会在Firestore数据库中得到一个UTC+2
的日期(目前瑞士处于夏令时)。
import {firebase} from '@firebase/app';
import '@firebase/firestore';
const myTimestamp = firebase.firestore.Timestamp.fromDate(new Date());
举个例子,如果我试图将一个冬天的日期转换成UTC时间,那么在数据库中会得到一个UTC+1
的时间戳。
const myTimestamp = firebase.firestore.Timestamp.fromDate(new Date(2019, 0, 1));
如果我使用 now()
,将得到 UTC+2
的日期。
const now: firebase.firestore.Timestamp = firebase.firestore.Timestamp.now();
当我持久化数据时,我没有做特别的事情:
const now: firebase.firestore.Timestamp = firebase.firestore.Timestamp.now();
const myTimestamp = firebase.firestore.Timestamp.fromDate(new Date());
const myData = {
created_at: now,
another_value: myTimestamp
};
await this.collection.add(myData);
有什么方法可以为Firestore创建有效的
UTC
时间戳吗?
fromDate
时,它会计算一个偏移量到我的日期,包括时区,我有一种奇怪的感觉。当我在云管理中浏览数据库时,我确实看到了UTC+2
或UTC+1
。此外,更奇怪的是,当我从Firestore函数(在UTC+0
环境中运行)查询数据库时,除非我为时区差异添加1或2个小时进行测试,否则无法检索数据。但是,在纸面上,我同意您的观点,时间戳不应考虑时区。 - David Dal Busco