timestamp.js 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. 'use strict';
  2. const Long = require('./long');
  3. /**
  4. * @class
  5. * @param {number} low the low (signed) 32 bits of the Timestamp.
  6. * @param {number} high the high (signed) 32 bits of the Timestamp.
  7. * @return {Timestamp}
  8. */
  9. class Timestamp extends Long {
  10. constructor(low, high) {
  11. if (low instanceof Long) {
  12. super(low.low, low.high);
  13. } else {
  14. super(low, high);
  15. }
  16. }
  17. /**
  18. * Return the JSON value.
  19. *
  20. * @method
  21. * @return {String} the JSON representation.
  22. */
  23. toJSON() {
  24. return {
  25. $timestamp: this.toString()
  26. };
  27. }
  28. /**
  29. * Returns a Timestamp represented by the given (32-bit) integer value.
  30. *
  31. * @method
  32. * @param {number} value the 32-bit integer in question.
  33. * @return {Timestamp} the timestamp.
  34. */
  35. static fromInt(value) {
  36. return new Timestamp(Long.fromInt(value));
  37. }
  38. /**
  39. * Returns a Timestamp representing the given number value, provided that it is a finite number. Otherwise, zero is returned.
  40. *
  41. * @method
  42. * @param {number} value the number in question.
  43. * @return {Timestamp} the timestamp.
  44. */
  45. static fromNumber(value) {
  46. return new Timestamp(Long.fromNumber(value));
  47. }
  48. /**
  49. * Returns a Timestamp for the given high and low bits. Each is assumed to use 32 bits.
  50. *
  51. * @method
  52. * @param {number} lowBits the low 32-bits.
  53. * @param {number} highBits the high 32-bits.
  54. * @return {Timestamp} the timestamp.
  55. */
  56. static fromBits(lowBits, highBits) {
  57. return new Timestamp(lowBits, highBits);
  58. }
  59. /**
  60. * Returns a Timestamp from the given string, optionally using the given radix.
  61. *
  62. * @method
  63. * @param {String} str the textual representation of the Timestamp.
  64. * @param {number} [opt_radix] the radix in which the text is written.
  65. * @return {Timestamp} the timestamp.
  66. */
  67. static fromString(str, opt_radix) {
  68. return new Timestamp(Long.fromString(str, opt_radix));
  69. }
  70. /**
  71. * @ignore
  72. */
  73. toExtendedJSON() {
  74. return { $timestamp: { t: this.high, i: this.low } };
  75. }
  76. /**
  77. * @ignore
  78. */
  79. static fromExtendedJSON(doc) {
  80. return new Timestamp(doc.$timestamp.i, doc.$timestamp.t);
  81. }
  82. }
  83. Object.defineProperty(Timestamp.prototype, '_bsontype', { value: 'Timestamp' });
  84. module.exports = Timestamp;