timestamp.d.ts 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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. import * as firestore from '@google-cloud/firestore';
  17. import { google } from '../protos/firestore_v1_proto_api';
  18. import api = google.firestore.v1;
  19. /**
  20. * A Timestamp represents a point in time independent of any time zone or
  21. * calendar, represented as seconds and fractions of seconds at nanosecond
  22. * resolution in UTC Epoch time. It is encoded using the Proleptic Gregorian
  23. * Calendar which extends the Gregorian calendar backwards to year one. It is
  24. * encoded assuming all minutes are 60 seconds long, i.e. leap seconds are
  25. * "smeared" so that no leap second table is needed for interpretation. Range is
  26. * from 0001-01-01T00:00:00Z to 9999-12-31T23:59:59.999999999Z.
  27. *
  28. * @see https://github.com/google/protobuf/blob/master/src/google/protobuf/timestamp.proto
  29. */
  30. export declare class Timestamp implements firestore.Timestamp {
  31. private readonly _seconds;
  32. private readonly _nanoseconds;
  33. /**
  34. * Creates a new timestamp with the current date, with millisecond precision.
  35. *
  36. * @example
  37. * ```
  38. * let documentRef = firestore.doc('col/doc');
  39. *
  40. * documentRef.set({ updateTime:Firestore.Timestamp.now() });
  41. *
  42. * ```
  43. * @return {Timestamp} A new `Timestamp` representing the current date.
  44. */
  45. static now(): Timestamp;
  46. /**
  47. * Creates a new timestamp from the given date.
  48. *
  49. * @example
  50. * ```
  51. * let documentRef = firestore.doc('col/doc');
  52. *
  53. * let date = Date.parse('01 Jan 2000 00:00:00 GMT');
  54. * documentRef.set({ startTime:Firestore.Timestamp.fromDate(date) });
  55. *
  56. * ```
  57. * @param {Date} date The date to initialize the `Timestamp` from.
  58. * @return {Timestamp} A new `Timestamp` representing the same point in time
  59. * as the given date.
  60. */
  61. static fromDate(date: Date): Timestamp;
  62. /**
  63. * Creates a new timestamp from the given number of milliseconds.
  64. *
  65. * @example
  66. * ```
  67. * let documentRef = firestore.doc('col/doc');
  68. *
  69. * documentRef.set({ startTime:Firestore.Timestamp.fromMillis(42) });
  70. *
  71. * ```
  72. * @param {number} milliseconds Number of milliseconds since Unix epoch
  73. * 1970-01-01T00:00:00Z.
  74. * @return {Timestamp} A new `Timestamp` representing the same point in time
  75. * as the given number of milliseconds.
  76. */
  77. static fromMillis(milliseconds: number): Timestamp;
  78. /**
  79. * Generates a `Timestamp` object from a Timestamp proto.
  80. *
  81. * @private
  82. * @internal
  83. * @param {Object} timestamp The `Timestamp` Protobuf object.
  84. */
  85. static fromProto(timestamp: google.protobuf.ITimestamp): Timestamp;
  86. /**
  87. * Creates a new timestamp.
  88. *
  89. * @example
  90. * ```
  91. * let documentRef = firestore.doc('col/doc');
  92. *
  93. * documentRef.set({ startTime:new Firestore.Timestamp(42, 0) });
  94. *
  95. * ```
  96. * @param {number} seconds The number of seconds of UTC time since Unix epoch
  97. * 1970-01-01T00:00:00Z. Must be from 0001-01-01T00:00:00Z to
  98. * 9999-12-31T23:59:59Z inclusive.
  99. * @param {number} nanoseconds The non-negative fractions of a second at
  100. * nanosecond resolution. Negative second values with fractions must still
  101. * have non-negative nanoseconds values that count forward in time. Must be
  102. * from 0 to 999,999,999 inclusive.
  103. */
  104. constructor(seconds: number, nanoseconds: number);
  105. /**
  106. * The number of seconds of UTC time since Unix epoch 1970-01-01T00:00:00Z.
  107. *
  108. * @example
  109. * ```
  110. * let documentRef = firestore.doc('col/doc');
  111. *
  112. * documentRef.get().then(snap => {
  113. * let updated = snap.updateTime;
  114. * console.log(`Updated at ${updated.seconds}s ${updated.nanoseconds}ns`);
  115. * });
  116. *
  117. * ```
  118. * @type {number}
  119. */
  120. get seconds(): number;
  121. /**
  122. * The non-negative fractions of a second at nanosecond resolution.
  123. *
  124. * @example
  125. * ```
  126. * let documentRef = firestore.doc('col/doc');
  127. *
  128. * documentRef.get().then(snap => {
  129. * let updated = snap.updateTime;
  130. * console.log(`Updated at ${updated.seconds}s ${updated.nanoseconds}ns`);
  131. * });
  132. *
  133. * ```
  134. * @type {number}
  135. */
  136. get nanoseconds(): number;
  137. /**
  138. * Returns a new `Date` corresponding to this timestamp. This may lose
  139. * precision.
  140. *
  141. * @example
  142. * ```
  143. * let documentRef = firestore.doc('col/doc');
  144. *
  145. * documentRef.get().then(snap => {
  146. * console.log(`Document updated at: ${snap.updateTime.toDate()}`);
  147. * });
  148. *
  149. * ```
  150. * @return {Date} JavaScript `Date` object representing the same point in time
  151. * as this `Timestamp`, with millisecond precision.
  152. */
  153. toDate(): Date;
  154. /**
  155. * Returns the number of milliseconds since Unix epoch 1970-01-01T00:00:00Z.
  156. *
  157. * @example
  158. * ```
  159. * let documentRef = firestore.doc('col/doc');
  160. *
  161. * documentRef.get().then(snap => {
  162. * let startTime = snap.get('startTime');
  163. * let endTime = snap.get('endTime');
  164. * console.log(`Duration: ${endTime - startTime}`);
  165. * });
  166. *
  167. * ```
  168. * @return {number} The point in time corresponding to this timestamp,
  169. * represented as the number of milliseconds since Unix epoch
  170. * 1970-01-01T00:00:00Z.
  171. */
  172. toMillis(): number;
  173. /**
  174. * Returns 'true' if this `Timestamp` is equal to the provided one.
  175. *
  176. * @example
  177. * ```
  178. * let documentRef = firestore.doc('col/doc');
  179. *
  180. * documentRef.get().then(snap => {
  181. * if (snap.createTime.isEqual(snap.updateTime)) {
  182. * console.log('Document is in its initial state.');
  183. * }
  184. * });
  185. *
  186. * ```
  187. * @param {any} other The `Timestamp` to compare against.
  188. * @return {boolean} 'true' if this `Timestamp` is equal to the provided one.
  189. */
  190. isEqual(other: firestore.Timestamp): boolean;
  191. /**
  192. * Generates the Protobuf `Timestamp` object for this timestamp.
  193. *
  194. * @private
  195. * @internal
  196. * @returns {Object} The `Timestamp` Protobuf object.
  197. */
  198. toProto(): api.IValue;
  199. /**
  200. * Converts this object to a primitive `string`, which allows `Timestamp` objects to be compared
  201. * using the `>`, `<=`, `>=` and `>` operators.
  202. *
  203. * @return {string} a string encoding of this object.
  204. */
  205. valueOf(): string;
  206. }