Skip to content

Commit e37b033

Browse files
authored
Correct the type of return value for Time.toMsg() (#1371)
This pull request corrects the return type of the `Time.toMsg()` method to return JavaScript numbers instead of bigints for the `sec` and `nanosec` fields, aligning with ROS2's builtin_interfaces.msg.Time message specification which uses int32 for sec and uint32 for nanosec. **Changes:** - Modified `Time.toMsg()` to convert bigint values to numbers using `Number()` - Updated test expectations to verify numbers are returned instead of bigints - Added test case verifying `Time.fromMsg()` compatibility with number types Fix: #1370
1 parent 94a1728 commit e37b033

File tree

2 files changed

+11
-4
lines changed

2 files changed

+11
-4
lines changed

lib/time.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -352,8 +352,8 @@ class Time {
352352
toMsg() {
353353
const secondsAndNanoseconds = this.secondsAndNanoseconds;
354354
return {
355-
sec: secondsAndNanoseconds.seconds,
356-
nanosec: secondsAndNanoseconds.nanoseconds,
355+
sec: Number(secondsAndNanoseconds.seconds),
356+
nanosec: Number(secondsAndNanoseconds.nanoseconds),
357357
};
358358
}
359359

test/test-time.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,11 @@ describe('rclnodejs Time/Clock testing', function () {
4848
assert.strictEqual(time.nanoseconds, 1000000064n);
4949
assert.strictEqual(time.clockType, ClockType.ROS_TIME);
5050

51+
// Test with number types (compatible with updated interface)
52+
time = Time.fromMsg({ sec: 1, nanosec: 64 });
53+
assert.strictEqual(time.nanoseconds, 1000000064n);
54+
assert.strictEqual(time.clockType, ClockType.ROS_TIME);
55+
5156
assert.throws(() => {
5257
new Time(1n, 1n, 'SYSTEM_TIME');
5358
}, rclnodejs.TypeValidationError);
@@ -200,7 +205,9 @@ describe('rclnodejs Time/Clock testing', function () {
200205
let time = new Time(100n, 200n);
201206
let msg = time.toMsg();
202207

203-
assert.strictEqual(msg.sec, 100n);
204-
assert.strictEqual(msg.nanosec, 200n);
208+
assert.strictEqual(msg.sec, 100);
209+
assert.strictEqual(msg.nanosec, 200);
210+
assert.strictEqual(typeof msg.sec, 'number');
211+
assert.strictEqual(typeof msg.nanosec, 'number');
205212
});
206213
});

0 commit comments

Comments
 (0)