1717const rclnodejs = require ( 'bindings' ) ( 'rclnodejs' ) ;
1818const Duration = require ( './duration.js' ) ;
1919const ClockType = require ( './clock_type.js' ) ;
20- const int64 = require ( 'int64-napi' ) ;
20+ const S_TO_NS = 10n ** 9n ;
2121
2222/**
2323 * @class - Class representing a Time in ROS
@@ -26,42 +26,43 @@ const int64 = require('int64-napi');
2626class Time {
2727 /**
2828 * Create a Time.
29- * @param {number|string } [seconds=0] - The second part of the time.
30- * @param {number|string } [nanoseconds=0] - The nanosecond part of the time.
29+ * @param {bigint } [seconds=0] - The second part of the time.
30+ * @param {bigint } [nanoseconds=0] - The nanosecond part of the time.
3131 * @param {ClockType } [clockType=Clock.ClockType.SYSTEM_TIME] - The clock type.
3232 */
33- constructor ( seconds = 0 , nanoseconds = 0 , clockType = ClockType . SYSTEM_TIME ) {
34- if ( typeof seconds !== 'number' && typeof seconds !== 'string' ) {
33+ constructor (
34+ seconds = 0n ,
35+ nanoseconds = 0n ,
36+ clockType = ClockType . SYSTEM_TIME
37+ ) {
38+ if ( typeof seconds !== 'bigint' ) {
3539 throw new TypeError ( 'Invalid argument of seconds' ) ;
3640 }
3741
38- if ( typeof nanoseconds !== 'number' && typeof nanoseconds !== 'string ') {
42+ if ( typeof nanoseconds !== 'bigint ' ) {
3943 throw new TypeError ( 'Invalid argument of nanoseconds' ) ;
4044 }
4145
4246 if ( typeof clockType !== 'number' ) {
4347 throw new TypeError ( 'Invalid argument of clockType' ) ;
4448 }
4549
46- if (
47- int64 . lt ( seconds , 0 ) ||
48- ( typeof seconds === 'string' && seconds . startsWith ( '-' ) )
49- ) {
50+ if ( seconds < 0n ) {
5051 throw new RangeError ( 'seconds value must not be negative' ) ;
5152 }
5253
53- if (
54- int64 . lt ( nanoseconds , 0 ) ||
55- ( typeof nanoseconds === 'string' && nanoseconds . startsWith ( '-' ) )
56- ) {
54+ if ( nanoseconds < 0n ) {
5755 throw new RangeError ( 'nanoseconds value must not be negative' ) ;
5856 }
5957
60- this . _nanoseconds = int64 . from ( seconds ) . multiply ( 1e9 ) . add ( nanoseconds ) ;
61- this . _handle = rclnodejs . createTimePoint (
62- this . _nanoseconds . toString ( ) ,
63- clockType
64- ) ;
58+ const total = seconds * S_TO_NS + nanoseconds ;
59+ if ( total >= 2n ** 63n ) {
60+ throw new RangeError (
61+ 'Total nanoseconds value is too large to store in C time point.'
62+ ) ;
63+ }
64+ this . _nanoseconds = total ;
65+ this . _handle = rclnodejs . createTimePoint ( this . _nanoseconds , clockType ) ;
6566 this . _clockType = clockType ;
6667 }
6768
@@ -80,22 +81,11 @@ class Time {
8081 * Get the nanosecond part of the time.
8182 * @name Time#get:nanoseconds
8283 * @function
83- * @return {number|string } - value in nanosecond, if the value is greater than Number.MAX_SAFE_INTEGER (2^53-1), will be presented in string of decimal format .
84+ * @return {bigint } - value in nanosecond.
8485 */
8586
8687 get nanoseconds ( ) {
87- let str = rclnodejs . getNanoseconds ( this . _handle ) ;
88- let nano ;
89-
90- if ( str . startsWith ( '-' ) ) {
91- nano = int64 . negative ( int64 . from ( str ) ) ;
92- } else {
93- nano = int64 . from ( str ) ;
94- }
95- if ( Number . isFinite ( nano . toNumber ( ) ) ) {
96- return nano . toNumber ( ) ;
97- }
98- return nano . toString ( ) ;
88+ return rclnodejs . getNanoseconds ( this . _handle ) ;
9989 }
10090
10191 /**
@@ -106,9 +96,11 @@ class Time {
10696 */
10797
10898 get secondsAndNanoseconds ( ) {
109- const seconds = int64 . from ( this . _nanoseconds ) . divide ( 1e9 ) . toNumber ( ) ;
110- const nanoseconds = int64 . from ( this . _nanoseconds ) . mod ( 1e9 ) . toNumber ( ) ;
111- return { seconds, nanoseconds } ;
99+ const nanoseconds = this . _nanoseconds ;
100+ return {
101+ seconds : nanoseconds / S_TO_NS ,
102+ nanoseconds : nanoseconds % S_TO_NS ,
103+ } ;
112104 }
113105
114106 /**
@@ -119,8 +111,8 @@ class Time {
119111 add ( other ) {
120112 if ( other instanceof Duration ) {
121113 return new Time (
122- 0 ,
123- int64 . add ( this . _nanoseconds , other . nanoseconds ) . toString ( ) ,
114+ 0n ,
115+ this . _nanoseconds + other . nanoseconds ,
124116 this . _clockType
125117 ) ;
126118 }
@@ -137,14 +129,11 @@ class Time {
137129 if ( other . _clockType !== this . _clockType ) {
138130 throw new TypeError ( "Can't subtract times with different clock types" ) ;
139131 }
140- return new Duration (
141- 0 ,
142- int64 . subtract ( this . _nanoseconds , other . _nanoseconds ) . toString ( )
143- ) ;
132+ return new Duration ( 0n , this . _nanoseconds - other . _nanoseconds ) ;
144133 } else if ( other instanceof Duration ) {
145134 return new Time (
146- 0 ,
147- int64 . subtract ( this . _nanoseconds , other . _nanoseconds ) . toString ( ) ,
135+ 0n ,
136+ this . _nanoseconds - other . _nanoseconds ,
148137 this . _clockType
149138 ) ;
150139 }
@@ -161,7 +150,7 @@ class Time {
161150 if ( other . _clockType !== this . _clockType ) {
162151 throw new TypeError ( "Can't compare times with different clock types" ) ;
163152 }
164- return this . _nanoseconds . eq ( other . nanoseconds ) ;
153+ return this . _nanoseconds === other . nanoseconds ;
165154 }
166155 throw new TypeError ( 'Invalid argument' ) ;
167156 }
@@ -176,7 +165,7 @@ class Time {
176165 if ( other . _clockType !== this . _clockType ) {
177166 throw new TypeError ( "Can't compare times with different clock types" ) ;
178167 }
179- return this . _nanoseconds . ne ( other . nanoseconds ) ;
168+ return this . _nanoseconds !== other . nanoseconds ;
180169 }
181170 }
182171
@@ -190,7 +179,7 @@ class Time {
190179 if ( other . _clockType !== this . _clockType ) {
191180 throw new TypeError ( "Can't compare times with different clock types" ) ;
192181 }
193- return this . _nanoseconds . lt ( other . nanoseconds ) ;
182+ return this . _nanoseconds < other . nanoseconds ;
194183 }
195184 throw new TypeError ( 'Invalid argument' ) ;
196185 }
@@ -205,7 +194,7 @@ class Time {
205194 if ( other . _clockType !== this . _clockType ) {
206195 throw new TypeError ( "Can't compare times with different clock types" ) ;
207196 }
208- return this . _nanoseconds . lte ( other . nanoseconds ) ;
197+ return this . _nanoseconds <= other . nanoseconds ;
209198 }
210199 throw new TypeError ( 'Invalid argument' ) ;
211200 }
@@ -220,7 +209,7 @@ class Time {
220209 if ( other . _clockType !== this . _clockType ) {
221210 throw new TypeError ( "Can't compare times with different clock types" ) ;
222211 }
223- return this . _nanoseconds . gt ( other . nanoseconds ) ;
212+ return this . _nanoseconds > other . nanoseconds ;
224213 }
225214 throw new TypeError ( 'Invalid argument' ) ;
226215 }
@@ -235,7 +224,7 @@ class Time {
235224 if ( other . _clockType !== this . _clockType ) {
236225 throw new TypeError ( "Can't compare times with different clock types" ) ;
237226 }
238- return this . _nanoseconds . gte ( other . nanoseconds ) ;
227+ return this . _nanoseconds >= other . nanoseconds ;
239228 }
240229 throw new TypeError ( 'Invalid argument' ) ;
241230 }
@@ -261,7 +250,7 @@ class Time {
261250 * @return {Time } Return the created Time object.
262251 */
263252 static fromMsg ( msg , clockType = ClockType . ROS_TIME ) {
264- return new Time ( msg . sec , msg . nanosec , clockType ) ;
253+ return new Time ( BigInt ( msg . sec ) , BigInt ( msg . nanosec ) , clockType ) ;
265254 }
266255}
267256
0 commit comments