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

Will angularfire2 convert timestamp values to js Date?

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 "wrap" 会围绕 firestore,并通过将所有内容转换为 JS-Date 来解决这个问题吗?
  2. 我们是否必须升级所有针对 firestore 的查询以更改为使用时间戳,而不是 JS 日期?
  3. 我们是否需要将所有时间戳值转换为 Date(),如代码段中指示的那样?

感谢您的帮助。

更新(更多信息) 我发现此信息解释了为什么 firestore 在 @firebase/firestore-types:

中从 Date 移动到 Timestamp

A Timestamp represents a point in time independent of any time zone or calendar, represented as seconds and fractions of seconds at nanosecond resolution in UTC Epoch time. It is encoded using the Proleptic Gregorian Calendar which extends the Gregorian calendar backwards to year one. It is encoded assuming all minutes are 60 seconds long, i.e. leap seconds are "smeared" so that no leap second table is needed for interpretation. Range is from 0001-01-01T00:00:00Z to 9999-12-31T23:59:59.999999999Z.

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

问题 1 "Will angularfire2 "wrap" around the firestore, and solve this issue by converting everything to JS-Date?"

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

问题二"Do we have to upgrade all queries against firestore be changed to use timestamp, and not JS date?"

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

问题 3“我们是否需要将所有时间戳值转换为 Date(),例如 片段中有指示吗?

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

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

我创建了这个小 table 来理解从 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 Whosebug question.