|
| 1 | +/*! |
| 2 | + * Copyright 2018 Google Inc. All Rights Reserved. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +'use strict'; |
| 18 | + |
| 19 | +const is = require('is'); |
| 20 | +const validate = require('./validate')(); |
| 21 | + |
| 22 | +/*! |
| 23 | + * Number of nanoseconds in a millisecond. |
| 24 | + * |
| 25 | + * @type {number} |
| 26 | + */ |
| 27 | +const MS_TO_NANOS = 1000000; |
| 28 | + |
| 29 | +/** |
| 30 | + * A Timestamp represents a point in time independent of any time zone or |
| 31 | + * calendar, represented as seconds and fractions of seconds at nanosecond |
| 32 | + * resolution in UTC Epoch time. It is encoded using the Proleptic Gregorian |
| 33 | + * Calendar which extends the Gregorian calendar backwards to year one. It is |
| 34 | + * encoded assuming all minutes are 60 seconds long, i.e. leap seconds are |
| 35 | + * "smeared" so that no leap second table is needed for interpretation. Range is |
| 36 | + * from 0001-01-01T00:00:00Z to 9999-12-31T23:59:59.999999999Z. |
| 37 | + * |
| 38 | + * @see https://github.com/google/protobuf/blob/master/src/google/protobuf/timestamp.proto |
| 39 | + */ |
| 40 | +class Timestamp { |
| 41 | + /** |
| 42 | + * Creates a new timestamp with the current date, with millisecond precision. |
| 43 | + * |
| 44 | + * @example |
| 45 | + * let documentRef = firestore.doc('col/doc'); |
| 46 | + * |
| 47 | + * documentRef.set({ updateTime:Firestore.Timestamp.now() }); |
| 48 | + * |
| 49 | + * @return {Timestamp} A new `Timestamp` representing the current date. |
| 50 | + */ |
| 51 | + static now() { |
| 52 | + return Timestamp.fromMillis(Date.now()); |
| 53 | + } |
| 54 | + |
| 55 | + /** |
| 56 | + * Creates a new timestamp from the given date. |
| 57 | + * |
| 58 | + * @example |
| 59 | + * let documentRef = firestore.doc('col/doc'); |
| 60 | + * |
| 61 | + * let date = Date.parse('01 Jan 2000 00:00:00 GMT'); |
| 62 | + * documentRef.set({ startTime:Firestore.Timestamp.fromDate(date) }); |
| 63 | + * |
| 64 | + * @param {Date} date The date to initialize the `Timestamp` from. |
| 65 | + * @return {Timestamp} A new `Timestamp` representing the same point in time |
| 66 | + * as the given date. |
| 67 | + */ |
| 68 | + static fromDate(date) { |
| 69 | + return Timestamp.fromMillis(date.getTime()); |
| 70 | + } |
| 71 | + |
| 72 | + /** |
| 73 | + * Creates a new timestamp from the given number of milliseconds. |
| 74 | + * |
| 75 | + * @example |
| 76 | + * let documentRef = firestore.doc('col/doc'); |
| 77 | + * |
| 78 | + * documentRef.set({ startTime:Firestore.Timestamp.fromMillis(42) }); |
| 79 | + * |
| 80 | + * @param {number} milliseconds Number of milliseconds since Unix epoch |
| 81 | + * 1970-01-01T00:00:00Z. |
| 82 | + * @return {Timestamp} A new `Timestamp` representing the same point in time |
| 83 | + * as the given number of milliseconds. |
| 84 | + */ |
| 85 | + static fromMillis(milliseconds) { |
| 86 | + const seconds = Math.floor(milliseconds / 1000); |
| 87 | + const nanos = (milliseconds - seconds * 1000) * MS_TO_NANOS; |
| 88 | + return new Timestamp(seconds, nanos); |
| 89 | + } |
| 90 | + |
| 91 | + /** |
| 92 | + * Creates a new timestamp. |
| 93 | + * |
| 94 | + * @example |
| 95 | + * let documentRef = firestore.doc('col/doc'); |
| 96 | + * |
| 97 | + * documentRef.set({ startTime:new Firestore.Timestamp(42, 0) }); |
| 98 | + * |
| 99 | + * @param {number} seconds The number of seconds of UTC time since Unix epoch |
| 100 | + * 1970-01-01T00:00:00Z. Must be from 0001-01-01T00:00:00Z to |
| 101 | + * 9999-12-31T23:59:59Z inclusive. |
| 102 | + * @param {number} nanoseconds The non-negative fractions of a second at |
| 103 | + * nanosecond resolution. Negative second values with fractions must still |
| 104 | + * have non-negative nanoseconds values that count forward in time. Must be |
| 105 | + * from 0 to 999,999,999 inclusive. |
| 106 | + */ |
| 107 | + constructor(seconds, nanoseconds) { |
| 108 | + validate.isInteger('seconds', seconds); |
| 109 | + validate.isInteger('nanoseconds', nanoseconds, 0, 999999999); |
| 110 | + |
| 111 | + this._seconds = seconds; |
| 112 | + this._nanoseconds = nanoseconds; |
| 113 | + } |
| 114 | + |
| 115 | + /** |
| 116 | + * The number of seconds of UTC time since Unix epoch 1970-01-01T00:00:00Z. |
| 117 | + * |
| 118 | + * @example |
| 119 | + * let documentRef = firestore.doc('col/doc'); |
| 120 | + * |
| 121 | + * documentRef.get().then(snap => { |
| 122 | + * let updatedAt = snap.updateTime; |
| 123 | + * console.log(`Updated at ${updated.seconds}s ${updated.nanoseconds}ns`); |
| 124 | + * }); |
| 125 | + * |
| 126 | + * @type {number} |
| 127 | + */ |
| 128 | + get seconds() { |
| 129 | + return this._seconds; |
| 130 | + } |
| 131 | + |
| 132 | + /** |
| 133 | + * The non-negative fractions of a second at nanosecond resolution. |
| 134 | + * |
| 135 | + * @example |
| 136 | + * let documentRef = firestore.doc('col/doc'); |
| 137 | + * |
| 138 | + * documentRef.get().then(snap => { |
| 139 | + * let updated = snap.updateTime; |
| 140 | + * console.log(`Updated at ${updated.seconds}s ${updated.nanoseconds}ns`); |
| 141 | + * }); |
| 142 | + * |
| 143 | + * @type {number} |
| 144 | + */ |
| 145 | + get nanoseconds() { |
| 146 | + return this._nanoseconds; |
| 147 | + } |
| 148 | + |
| 149 | + /** |
| 150 | + * Returns a new `Date` corresponding to this timestamp. This may lose |
| 151 | + * precision. |
| 152 | + * |
| 153 | + * @example |
| 154 | + * let documentRef = firestore.doc('col/doc'); |
| 155 | + * |
| 156 | + * documentRef.get().then(snap => { |
| 157 | + * console.log(`Document updated at: ${snap.updateTime.toDate()}`); |
| 158 | + * }); |
| 159 | + * |
| 160 | + * @return {Date} JavaScript `Date` object representing the same point in time |
| 161 | + * as this `Timestamp`, with millisecond precision. |
| 162 | + */ |
| 163 | + toDate() { |
| 164 | + return new Date( |
| 165 | + this._seconds * 1000 + Math.round(this._nanoseconds / MS_TO_NANOS) |
| 166 | + ); |
| 167 | + } |
| 168 | + |
| 169 | + /** |
| 170 | + * Returns the number of milliseconds since Unix epoch 1970-01-01T00:00:00Z. |
| 171 | + * |
| 172 | + * @example |
| 173 | + * let documentRef = firestore.doc('col/doc'); |
| 174 | + * |
| 175 | + * documentRef.get().then(snap => { |
| 176 | + * let startTime = snap.get('startTime'); |
| 177 | + * let endTime = snap.get('endTime'); |
| 178 | + * console.log(`Duration: ${endTime - startTime}`); |
| 179 | + * }); |
| 180 | + * |
| 181 | + * @return {number} The point in time corresponding to this timestamp, |
| 182 | + * represented as the number of milliseconds since Unix epoch |
| 183 | + * 1970-01-01T00:00:00Z. |
| 184 | + */ |
| 185 | + toMillis() { |
| 186 | + return this._seconds * 1000 + Math.floor(this._nanoseconds / MS_TO_NANOS); |
| 187 | + } |
| 188 | + |
| 189 | + /** |
| 190 | + * Returns 'true' if this `Timestamp` is equal to the provided one. |
| 191 | + * |
| 192 | + * @example |
| 193 | + * let documentRef = firestore.doc('col/doc'); |
| 194 | + * |
| 195 | + * documentRef.get().then(snap => { |
| 196 | + * if (snap.createTime.isEqual(snap.updateTime)) { |
| 197 | + * console.log('Document is in its initial state.'); |
| 198 | + * } |
| 199 | + * }); |
| 200 | + * |
| 201 | + * @param {any} other The `Timestamp` to compare against. |
| 202 | + * @return {boolean} 'true' if this `Timestamp` is equal to the provided one. |
| 203 | + */ |
| 204 | + isEqual(other) { |
| 205 | + return ( |
| 206 | + this === other || |
| 207 | + (is.instanceof(other, Timestamp) && |
| 208 | + this._seconds === other.seconds && |
| 209 | + this._nanoseconds === other.nanoseconds) |
| 210 | + ); |
| 211 | + } |
| 212 | +} |
| 213 | + |
| 214 | +module.exports = Timestamp; |
0 commit comments