Skip to content

Commit fd650d6

Browse files
authored
Vendor dateToTimestampProto (#118)
* Vendor dateToTimestampProto * Add return type
1 parent db5f2d3 commit fd650d6

File tree

1 file changed

+15
-1
lines changed

1 file changed

+15
-1
lines changed

src/providers/firestore.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,28 @@
2121
// SOFTWARE.
2222

2323
import { Change } from 'firebase-functions';
24-
import { dateToTimestampProto } from 'firebase-functions/lib/encoder';
2524
import { firestore, app } from 'firebase-admin';
2625
import { has, get, isEmpty, isPlainObject, mapValues } from 'lodash';
2726

2827
import { testApp } from '../app';
2928

3029
import * as http from 'http';
3130

31+
function dateToTimestampProto(timeString?: string): { seconds: number, nanos: number } | undefined {
32+
if (typeof timeString === 'undefined') {
33+
return;
34+
}
35+
const date = new Date(timeString);
36+
const seconds = Math.floor(date.getTime() / 1000);
37+
let nanos = 0;
38+
if (timeString.length > 20) {
39+
const nanoString = timeString.substring(20, timeString.length - 1);
40+
const trailingZeroes = 9 - nanoString.length;
41+
nanos = parseInt(nanoString, 10) * Math.pow(10, trailingZeroes);
42+
}
43+
return { seconds, nanos };
44+
}
45+
3246
/** Optional parameters for creating a DocumentSnapshot. */
3347
export interface DocumentSnapshotOptions {
3448
/** ISO timestamp string for the snapshot was read, default is current time. */

0 commit comments

Comments
 (0)