@@ -15,7 +15,11 @@ import { hasProperty, isBigInt, isNumber } from "./Predicate.js"
15
15
16
16
const TypeId : unique symbol = Symbol . for ( "effect/Duration" )
17
17
18
+ const bigint0 = BigInt ( 0 )
19
+ const bigint24 = BigInt ( 24 )
20
+ const bigint60 = BigInt ( 60 )
18
21
const bigint1e3 = BigInt ( 1_000 )
22
+ const bigint1e6 = BigInt ( 1_000_000 )
19
23
const bigint1e9 = BigInt ( 1_000_000_000 )
20
24
21
25
/**
@@ -162,7 +166,7 @@ const DurationProto: Omit<Duration, "value"> = {
162
166
const make = ( input : number | bigint ) : Duration => {
163
167
const duration = Object . create ( DurationProto )
164
168
if ( isNumber ( input ) ) {
165
- if ( isNaN ( input ) || input < 0 ) {
169
+ if ( isNaN ( input ) || input <= 0 ) {
166
170
duration . value = zeroValue
167
171
} else if ( ! Number . isFinite ( input ) ) {
168
172
duration . value = infinityValue
@@ -171,7 +175,7 @@ const make = (input: number | bigint): Duration => {
171
175
} else {
172
176
duration . value = { _tag : "Millis" , millis : input }
173
177
}
174
- } else if ( input < BigInt ( 0 ) ) {
178
+ } else if ( input <= bigint0 ) {
175
179
duration . value = zeroValue
176
180
} else {
177
181
duration . value = { _tag : "Nanos" , nanos : input }
@@ -522,6 +526,22 @@ export const times: {
522
526
} )
523
527
)
524
528
529
+ /**
530
+ * @since 2.0.0
531
+ * @category math
532
+ */
533
+ export const subtract : {
534
+ ( that : DurationInput ) : ( self : DurationInput ) => Duration
535
+ ( self : DurationInput , that : DurationInput ) : Duration
536
+ } = dual (
537
+ 2 ,
538
+ ( self : DurationInput , that : DurationInput ) : Duration =>
539
+ matchWith ( self , that , {
540
+ onMillis : ( self , that ) => make ( self - that ) ,
541
+ onNanos : ( self , that ) => make ( self - that )
542
+ } )
543
+ )
544
+
525
545
/**
526
546
* @since 2.0.0
527
547
* @category math
@@ -631,32 +651,32 @@ export const format = (self: DurationInput): string => {
631
651
632
652
const nanos = unsafeToNanos ( duration )
633
653
634
- if ( nanos % 1000000n ) {
635
- parts . push ( `${ nanos % 1000000n } ns` )
654
+ if ( nanos % bigint1e6 ) {
655
+ parts . push ( `${ nanos % bigint1e6 } ns` )
636
656
}
637
657
638
- const ms = nanos / 1000000n
639
- if ( ms % 1000n !== 0n ) {
640
- parts . push ( `${ ms % 1000n } ms` )
658
+ const ms = nanos / bigint1e6
659
+ if ( ms % bigint1e3 !== bigint0 ) {
660
+ parts . push ( `${ ms % bigint1e3 } ms` )
641
661
}
642
662
643
- const sec = ms / 1000n
644
- if ( sec % 60n !== 0n ) {
645
- parts . push ( `${ sec % 60n } s` )
663
+ const sec = ms / bigint1e3
664
+ if ( sec % bigint60 !== bigint0 ) {
665
+ parts . push ( `${ sec % bigint60 } s` )
646
666
}
647
667
648
- const min = sec / 60n
649
- if ( min % 60n !== 0n ) {
650
- parts . push ( `${ min % 60n } m` )
668
+ const min = sec / bigint60
669
+ if ( min % bigint60 !== bigint0 ) {
670
+ parts . push ( `${ min % bigint60 } m` )
651
671
}
652
672
653
- const hr = min / 60n
654
- if ( hr % 24n !== 0n ) {
655
- parts . push ( `${ hr % 24n } h` )
673
+ const hr = min / bigint60
674
+ if ( hr % bigint24 !== bigint0 ) {
675
+ parts . push ( `${ hr % bigint24 } h` )
656
676
}
657
677
658
- const days = hr / 24n
659
- if ( days !== 0n ) {
678
+ const days = hr / bigint24
679
+ if ( days !== bigint0 ) {
660
680
parts . push ( `${ days } d` )
661
681
}
662
682
0 commit comments