@@ -67,16 +67,18 @@ export class Duration {
67
67
* @returns the parsed `Duration`.
68
68
*/
69
69
public static parse ( duration : string ) : Duration {
70
- const matches = duration . match ( / ^ P (?: ( \d + ) D ) ? (?: T (?: ( \d + ) H ) ? (?: ( \d + ) M ) ? (?: ( \d + ) S ) ? ) ? $ / ) ;
70
+ const matches = duration . match ( / ^ P (?: ( \d + ) D ) ? (?: T (?: ( \d + ) H ) ? (?: ( \d + ) M ) ? (?: ( \d + ) \. ? ( \d { 1 , 3 } ) ? S ) ? ) ? $ / ) ;
71
71
if ( ! matches ) {
72
72
throw new Error ( `Not a valid ISO duration: ${ duration } ` ) ;
73
73
}
74
- const [ , days , hours , minutes , seconds ] = matches ;
75
- if ( ! days && ! hours && ! minutes && ! seconds ) {
74
+ const [ , days , hours , minutes , seconds , milliseconds ] = matches ;
75
+ if ( ! days && ! hours && ! minutes && ! seconds && ! milliseconds ) {
76
76
throw new Error ( `Not a valid ISO duration: ${ duration } ` ) ;
77
77
}
78
+ const millis = milliseconds ? milliseconds . padEnd ( 3 , '0' ) : '' ;
78
79
return Duration . millis (
79
- _toInt ( seconds ) * TimeUnit . Seconds . inMillis
80
+ _toInt ( millis )
81
+ + _toInt ( seconds ) * TimeUnit . Seconds . inMillis
80
82
+ ( _toInt ( minutes ) * TimeUnit . Minutes . inMillis )
81
83
+ ( _toInt ( hours ) * TimeUnit . Hours . inMillis )
82
84
+ ( _toInt ( days ) * TimeUnit . Days . inMillis ) ,
@@ -251,8 +253,8 @@ export class Duration {
251
253
}
252
254
}
253
255
254
- // Remainder in millis
255
- if ( millis > 0 ) {
256
+ // Remainder in millis (keep only above threshold)
257
+ if ( millis > 0.0001 ) {
256
258
ret . push ( [ millis , TimeUnit . Milliseconds ] ) ;
257
259
}
258
260
return ret ;
0 commit comments