Skip to content

Commit 49facc2

Browse files
authored
Added Node#now() with test #598 (#605)
Adds convenient method to Node for accessing the current time. added now() method to Node that returns current Time using the node's clock added now() to node.d.ts added test case to test-node.js Fix #598
1 parent dbf130e commit 49facc2

File tree

3 files changed

+28
-0
lines changed

3 files changed

+28
-0
lines changed

lib/node.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -626,6 +626,14 @@ class Node {
626626
return this._clock;
627627
}
628628

629+
/**
630+
* Get the current time using the node's clock.
631+
* @returns {Time} - The current time.
632+
*/
633+
now() {
634+
return this.getClock().now();
635+
}
636+
629637
/**
630638
* Get the list of published topics discovered by the provided node for the remote node name.
631639
* @param {string} nodeName - The name of the node.

test/test-node.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
'use strict';
1616

17+
const IsClose = require('is-close');
1718
const assert = require('assert');
1819
const rclnodejs = require('../index.js');
1920
const assertUtils = require('./utils.js');
@@ -365,6 +366,16 @@ describe('rcl node methods testing', function() {
365366
assert.strictEqual(clock.clockType, rclnodejs.ClockType.ROS_TIME);
366367
});
367368

369+
it('node.now', function() {
370+
const time = node.now();
371+
assert.ok(time);
372+
assert.strictEqual(node.getClock().clockType, time.clockType);
373+
374+
const seconds = time.secondsAndNanoseconds.seconds;
375+
const dateSeconds = Date.now() / 1000;
376+
assert.ok(IsClose.isClose(seconds, dateSeconds, 1));
377+
});
378+
368379
it('node.getNodeNames', function() {
369380
var nodeNames = node.getNodeNames();
370381

types/node.d.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,8 +173,17 @@ declare module 'rclnodejs' {
173173
*/
174174
getClock(): Clock;
175175

176+
/**
177+
* Get the current time using the node's clock.
178+
*
179+
* @returns The current time.
180+
*/
181+
now(): Time;
182+
176183
/**
177184
* Get the nodeOptions provided through the constructor.
185+
*
186+
* @returns The nodeOptions.
178187
*/
179188
options(): NodeOptions;
180189

0 commit comments

Comments
 (0)