Skip to content

Commit

Permalink
Merge pull request #56 from nui-os/master
Browse files Browse the repository at this point in the history
feat: add now function to Timestamp
  • Loading branch information
dmurvihill authored Mar 2, 2020
2 parents bd85b69 + 689add6 commit d1c7634
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
behavior of `onAuthStateChanged()` (see below)
- Support for Firebase Messaging (Admin API)
- Support for [FieldValue.increment](https://firebase.google.com/docs/reference/js/firebase.firestore.FieldValue#increment)
- Support for [firestore.Timestamp.now()](https://firebase.google.com/docs/reference/js/firebase.firestore.Timestamp#now)

### Changed
- (Breaking) Consistent with Firebase SDK [version 4.0.0](https://firebase.google.com/support/release-notes/js#version_500_-_may_8_2018) and later,
Expand Down
10 changes: 10 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions src/timestamp.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ Timestamp.fromMillis = function(ms) {
return new Timestamp(sec, ns);
};

Timestamp.now = function() {
return Timestamp.fromDate(new Date());
};

Timestamp.prototype.toDate = function () {
var millis = this.seconds * 1000 + this.nanoseconds / (1000 * 1000);
return new Date(millis);
Expand Down
15 changes: 15 additions & 0 deletions test/unit/timestamp.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,14 @@ var sinon = require('sinon');
var Timestamp = require('../../src/timestamp');

describe('Timestamp', function () {
var clock;
beforeEach(function () {
clock = sinon.useFakeTimers();
});
afterEach(function () {
clock.restore();
});

describe('fromDate', function () {
it('should convert from date', function () {
var date = new Date('2009-02-13T23:31:30.123456789Z');
Expand All @@ -20,6 +28,13 @@ describe('Timestamp', function () {
expect(timestamp.nanoseconds).to.equal(123000000);
});
});
describe('now', function () {
it('should create a current Timestamp', function () {
var now = new Date();
var timestamp = Timestamp.now();
expect(timestamp.toDate().getTime()).to.equal(now.getTime());
});
});
describe('#toDate', function () {
it('should convert to date', function () {
var ts = new Timestamp(1234567890, 123456789);
Expand Down

0 comments on commit d1c7634

Please sign in to comment.