@@ -118,8 +118,18 @@ export class TimeValue {
118118 * @param other The time value to add to this one.
119119 */
120120 add ( other : TimeValue ) : TimeValue {
121- if ( other . isNever ( ) ) {
122- return this ;
121+ if (
122+ ( this . isNever ( ) && other . isForever ( ) ) ||
123+ ( this . isForever ( ) && other . isNever ( ) )
124+ ) {
125+ // The sum of minus infinity and infinity is zero.
126+ return TimeValue . zero ( ) ;
127+ } else if ( this . isNever ( ) || other . isNever ( ) ) {
128+ // The sum of minus infinity and a normal tag is minus infinity.
129+ return TimeValue . never ( ) ;
130+ } else if ( this . isForever ( ) || other . isForever ( ) ) {
131+ // The sum of infinity and a normal tag is infinity.
132+ return TimeValue . forever ( ) ;
123133 }
124134 let seconds = this . seconds + other . seconds ;
125135 let nanoseconds = this . nanoseconds + other . nanoseconds ;
@@ -129,7 +139,14 @@ export class TimeValue {
129139 seconds += 1 ;
130140 nanoseconds -= TimeUnit . sec ;
131141 }
132- return TimeValue . secsAndNs ( seconds , nanoseconds ) ;
142+
143+ // Check an overflow.
144+ if ( ! Number . isSafeInteger ( seconds ) ) {
145+ // The overflow happens.
146+ return TimeValue . forever ( ) ;
147+ } else {
148+ return TimeValue . secsAndNs ( seconds , nanoseconds ) ;
149+ }
133150 }
134151
135152 /**
@@ -205,6 +222,14 @@ export class TimeValue {
205222 }
206223 }
207224
225+ isForever ( ) : boolean {
226+ if ( this . seconds === Number . MAX_SAFE_INTEGER && this . nanoseconds === 0 ) {
227+ return true ;
228+ } else {
229+ return false ;
230+ }
231+ }
232+
208233 /**
209234 * Return true if this time value denotes a time interval of smaller length
210235 * than the time interval encoded by the time value given as a parameter;
@@ -438,12 +463,15 @@ export class Tag {
438463 * the given delay. The `microstep` of this time instant is ignored;
439464 * the returned time instant has a `microstep` of zero if the delay
440465 * is greater than zero. If the delay equals zero, the tag is returned
441- * unchanged with its current `microstep`.
466+ * unchanged with its current `microstep`. If the delay is undefined,
467+ * the tag is returned as it was.
442468 * @param delay The time interval to add to this time instant.
443469 */
444- getLaterTag ( delay : TimeValue ) : Tag {
445- if ( delay . isZero ( ) || delay . isNever ( ) ) {
470+ getLaterTag ( delay : TimeValue | undefined ) : Tag {
471+ if ( delay === undefined ) {
446472 return this ;
473+ } else if ( delay . isZero ( ) ) {
474+ return new Tag ( this . time , this . microstep + 1 ) ;
447475 } else {
448476 return new Tag ( delay . add ( this . time ) , 0 ) ;
449477 }
0 commit comments