angularfire2会将时间戳值转换为js Date吗?

时间:2018-05-14 15:38:54

标签: firebase google-cloud-firestore angularfire2

在2018年5月设置firestore和angularfire2时,会收到以下错误消息:

 @firebase/firestore: Firestore (4.10.1): 
The behavior for Date objects stored in Firestore is going to change
AND YOUR APP MAY BREAK.
To hide this warning and ensure your app does not break, you need to add the
following code to your app before calling any other Cloud Firestore methods:

  const firestore = firebase.firestore();
  const settings = {/* your settings... */ timestampsInSnapshots: true};
  firestore.settings(settings);

With this change, timestamps stored in Cloud Firestore will be read back as
Firebase Timestamp objects instead of as system Date objects. So you will also
need to update code expecting a Date to instead expect a Timestamp. For example:

  // Old:
  const date = snapshot.get('created_at');
  // New:
  const timestamp = snapshot.get('created_at');
  const date = timestamp.toDate();

Please audit all existing usages of Date when you enable the new behavior. In a
future release, the behavior will change to the new behavior, so if you do not
follow these steps, YOUR APP MAY BREAK.

从长远来看,我对Angular应用程序的实践意味着什么感到困惑。具体做法是:

  1. 将angularfire2“包裹”在firestore周围,并通过将所有内容转换为JS-Date来解决此问题?
  2. 我们是否必须将针对firestore的所有查询升级为使用时间戳而不是JS日期?
  3. 我们是否需要将所有时间戳值转换为日期(),就像在代码段中指示一样?
  4. 感谢您的帮助。

    更新(更多信息) 我发现此信息解释了为什么firestore在@firebase/firestore-types中从Date移动到时间戳:

      

    时间戳表示独立于任何时区或日历的时间点,表示为UTC时间段内纳秒分辨率的秒和分数秒。它使用Proleptic Gregorian Calendar进行编码,将公历向后延伸至第一年。它被编码,假设所有分钟都是60秒长,即闰秒被“模糊”,因此解释时不需要闰秒表。范围为0001-01-01T00:00:00Z至9999-12-31T23:59:59.999999999Z。

    这也意味着在typescript中,您可以获得如下类型:import { Timestamp } from '@firebase/firestore-types'; 顺便说一下,如果你去@ firebase / firestore-types / index.d.ts文件,有一个link to github有更多的信息。

1 个答案:

答案 0 :(得分:3)

问题1 “将angularfire2”包裹在firestore周围,并通过将所有内容转换为JS-Date解决此问题?“

不,这是没有意义的,因为信息可能会丢失(即纳秒)。

问题2 “我们是否必须将针对firestore的所有查询升级为使用时间戳,而不是JS日期?”

我不确定这个,我会调查。我想你可以使用Date,但时间戳最好。

问题3 “我们是否需要将所有时间戳值转换为日期(),就像     在片段中指示?“

这取决于。例如,如果您不想在datepicker中使用时间戳,则datepicker可能不支持时间戳,因此必须进行转换。但是,某些信息也可能丢失。

时间戳与Javascript日期(需要帮助)

我创建了这个小表来理解从Date移动到时间戳的选择

+----------------+------------------------------+---------------------------------+-------------------------------------+
|                |        javascript Date       |      firestore "Timestamp"      |         comment/implication         |
+----------------+------------------------------+---------------------------------+-------------------------------------+
| precision      | milliseconds                 | nanosecond                      | better resolution. two objects      |
|                |                              |                                 | created right after each other can  |
|                |                              |                                 | have the same millisecond,          |
|                |                              |                                 | but not nanosecond.                 |
+----------------+------------------------------+---------------------------------+-------------------------------------+
| specific point | Can be both                  |      Yes - independent of       |           ? (help wanted)           |
| in time        |                              |  any time zone or calendar..."  |                                     |
+----------------+------------------------------+---------------------------------+-------------------------------------+
| range          |  8,640 * 10^12 milliseconds  |          Range is from:         |   This restriction on Timestamp,    |
|                | to either side of 01 January |      0001-01-01T00:00:00Z       |    makes it possible to convert     |
|                |          , 1970 UTC          |                to               |      to RFC 3339 date strings.      |
|                |                              | 9999-12-31T23:59:59.999999999Z. |                                     |
+----------------+------------------------------+---------------------------------+-------------------------------------+
| leap seconds   | ignored                      | leap seconds are "smeared"      | ? (help wanted)                     |
+----------------+------------------------------+---------------------------------+-------------------------------------+
| calendar       | Gregorian Calendar?          | Proleptic Gregorian Calendar    | The Gregorian Calendar              |
|                |                              |                                 | only goes back to 1582,             |
|                |                              |                                 | Proleptic goes back to 0001.        |
+----------------+------------------------------+---------------------------------+-------------------------------------+

<强>来源

我使用了以下信息来源:

  1. 您可以在问题中看到的错误消息
  2. @ firebase / firestore-types / index.d.ts文件
  3. link提供给github
  4. ECMAScript Language Specification
  5. This stackoverflow question