Skip to content

Commit

Permalink
feat: add now function to Timestamp
Browse files Browse the repository at this point in the history
  • Loading branch information
Tobi committed Feb 27, 2020
1 parent bd85b69 commit 52393a3
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 0 deletions.
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
7 changes: 7 additions & 0 deletions test/unit/timestamp.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,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 52393a3

Please sign in to comment.