@@ -160,7 +160,7 @@ export function ToIntegerThrowOnInfinity(value: unknown): number {
160
160
return integer ;
161
161
}
162
162
163
- export function ToPositiveInteger ( valueParam : unknown , property ?: string ) : number {
163
+ function ToPositiveInteger ( valueParam : unknown , property ?: string ) : number {
164
164
const value = ToInteger ( valueParam ) ;
165
165
if ( ! NumberIsFinite ( value ) ) {
166
166
throw new RangeError ( 'infinity is out of range' ) ;
@@ -710,7 +710,7 @@ function DurationHandleFractions(
710
710
return { minutes, seconds, milliseconds, microseconds, nanoseconds } ;
711
711
}
712
712
713
- export function ToTemporalDurationRecord ( item : Temporal . DurationLike | string ) {
713
+ function ToTemporalDurationRecord ( item : Temporal . DurationLike | string ) {
714
714
if ( ! IsObject ( item ) ) {
715
715
return ParseTemporalDurationString ( ToString ( item ) ) ;
716
716
}
@@ -757,7 +757,7 @@ export function ToTemporalDurationRecord(item: Temporal.DurationLike | string) {
757
757
return { years, months, weeks, days, hours, minutes, seconds, milliseconds, microseconds, nanoseconds } ;
758
758
}
759
759
760
- export function ToLimitedTemporalDuration (
760
+ function ToLimitedTemporalDuration (
761
761
item : Temporal . DurationLike | string ,
762
762
disallowedProperties : ( keyof Temporal . DurationLike ) [ ]
763
763
) {
@@ -4688,7 +4688,7 @@ export function AddISODate(
4688
4688
return { year, month, day } ;
4689
4689
}
4690
4690
4691
- export function AddTime (
4691
+ function AddTime (
4692
4692
hourParam : number ,
4693
4693
minuteParam : number ,
4694
4694
secondParam : number ,
@@ -4727,7 +4727,7 @@ export function AddTime(
4727
4727
return { deltaDays, hour, minute, second, millisecond, microsecond, nanosecond } ;
4728
4728
}
4729
4729
4730
- export function AddDuration (
4730
+ function AddDuration (
4731
4731
y1 : number ,
4732
4732
mon1 : number ,
4733
4733
w1 : number ,
@@ -4863,15 +4863,7 @@ export function AddDuration(
4863
4863
return { years, months, weeks, days, hours, minutes, seconds, milliseconds, microseconds, nanoseconds } ;
4864
4864
}
4865
4865
4866
- export function AddInstant (
4867
- epochNanoseconds : JSBI ,
4868
- h : number ,
4869
- min : number ,
4870
- s : number ,
4871
- ms : number ,
4872
- µs : number ,
4873
- ns : number
4874
- ) {
4866
+ function AddInstant ( epochNanoseconds : JSBI , h : number , min : number , s : number , ms : number , µs : number , ns : number ) {
4875
4867
let sum = ZERO ;
4876
4868
sum = JSBI . add ( sum , JSBI . BigInt ( ns ) ) ;
4877
4869
sum = JSBI . add ( sum , JSBI . multiply ( JSBI . BigInt ( µs ) , THOUSAND ) ) ;
@@ -4885,7 +4877,7 @@ export function AddInstant(
4885
4877
return result ;
4886
4878
}
4887
4879
4888
- export function AddDateTime (
4880
+ function AddDateTime (
4889
4881
year : number ,
4890
4882
month : number ,
4891
4883
day : number ,
@@ -4999,6 +4991,214 @@ export function AddZonedDateTime(
4999
4991
return AddInstant ( GetSlot ( instantIntermediate , EPOCHNANOSECONDS ) , h , min , s , ms , µs , ns ) ;
5000
4992
}
5001
4993
4994
+ type AddSubtractOperation = 'add' | 'subtract' ;
4995
+
4996
+ export function AddDurationToOrSubtractDurationFromDuration (
4997
+ operation : AddSubtractOperation ,
4998
+ duration : Temporal . Duration ,
4999
+ other : DurationParams [ 'add' ] [ 0 ] ,
5000
+ optionsParam : DurationParams [ 'add' ] [ 1 ]
5001
+ ) : Temporal . Duration {
5002
+ const sign = operation === 'subtract' ? - 1 : 1 ;
5003
+ let { years, months, weeks, days, hours, minutes, seconds, milliseconds, microseconds, nanoseconds } =
5004
+ ToTemporalDurationRecord ( other ) ;
5005
+ const options = GetOptionsObject ( optionsParam ) ;
5006
+ const relativeTo = ToRelativeTemporalObject ( options ) ;
5007
+ ( { years, months, weeks, days, hours, minutes, seconds, milliseconds, microseconds, nanoseconds } = AddDuration (
5008
+ GetSlot ( duration , YEARS ) ,
5009
+ GetSlot ( duration , MONTHS ) ,
5010
+ GetSlot ( duration , WEEKS ) ,
5011
+ GetSlot ( duration , DAYS ) ,
5012
+ GetSlot ( duration , HOURS ) ,
5013
+ GetSlot ( duration , MINUTES ) ,
5014
+ GetSlot ( duration , SECONDS ) ,
5015
+ GetSlot ( duration , MILLISECONDS ) ,
5016
+ GetSlot ( duration , MICROSECONDS ) ,
5017
+ GetSlot ( duration , NANOSECONDS ) ,
5018
+ sign * years ,
5019
+ sign * months ,
5020
+ sign * weeks ,
5021
+ sign * days ,
5022
+ sign * hours ,
5023
+ sign * minutes ,
5024
+ sign * seconds ,
5025
+ sign * milliseconds ,
5026
+ sign * microseconds ,
5027
+ sign * nanoseconds ,
5028
+ relativeTo
5029
+ ) ) ;
5030
+ const Duration = GetIntrinsic ( '%Temporal.Duration%' ) ;
5031
+ return new Duration ( years , months , weeks , days , hours , minutes , seconds , milliseconds , microseconds , nanoseconds ) ;
5032
+ }
5033
+
5034
+ export function AddDurationToOrSubtractDurationFromInstant (
5035
+ operation : AddSubtractOperation ,
5036
+ instant : Temporal . Instant ,
5037
+ durationLike : InstantParams [ 'add' ] [ 0 ]
5038
+ ) : Temporal . Instant {
5039
+ const sign = operation === 'subtract' ? - 1 : 1 ;
5040
+ const { hours, minutes, seconds, milliseconds, microseconds, nanoseconds } = ToLimitedTemporalDuration ( durationLike , [
5041
+ 'years' ,
5042
+ 'months' ,
5043
+ 'weeks' ,
5044
+ 'days'
5045
+ ] ) ;
5046
+ const ns = AddInstant (
5047
+ GetSlot ( instant , EPOCHNANOSECONDS ) ,
5048
+ sign * hours ,
5049
+ sign * minutes ,
5050
+ sign * seconds ,
5051
+ sign * milliseconds ,
5052
+ sign * microseconds ,
5053
+ sign * nanoseconds
5054
+ ) ;
5055
+ const Instant = GetIntrinsic ( '%Temporal.Instant%' ) ;
5056
+ return new Instant ( ns ) ;
5057
+ }
5058
+
5059
+ export function AddDurationToOrSubtractDurationFromPlainDateTime (
5060
+ operation : AddSubtractOperation ,
5061
+ dateTime : Temporal . PlainDateTime ,
5062
+ durationLike : PlainDateTimeParams [ 'add' ] [ 0 ] ,
5063
+ optionsParam : PlainDateTimeParams [ 'add' ] [ 1 ]
5064
+ ) : Temporal . PlainDateTime {
5065
+ const sign = operation === 'subtract' ? - 1 : 1 ;
5066
+ const { years, months, weeks, days, hours, minutes, seconds, milliseconds, microseconds, nanoseconds } =
5067
+ ToTemporalDurationRecord ( durationLike ) ;
5068
+ const options = GetOptionsObject ( optionsParam ) ;
5069
+ const calendar = GetSlot ( dateTime , CALENDAR ) ;
5070
+ const { year, month, day, hour, minute, second, millisecond, microsecond, nanosecond } = AddDateTime (
5071
+ GetSlot ( dateTime , ISO_YEAR ) ,
5072
+ GetSlot ( dateTime , ISO_MONTH ) ,
5073
+ GetSlot ( dateTime , ISO_DAY ) ,
5074
+ GetSlot ( dateTime , ISO_HOUR ) ,
5075
+ GetSlot ( dateTime , ISO_MINUTE ) ,
5076
+ GetSlot ( dateTime , ISO_SECOND ) ,
5077
+ GetSlot ( dateTime , ISO_MILLISECOND ) ,
5078
+ GetSlot ( dateTime , ISO_MICROSECOND ) ,
5079
+ GetSlot ( dateTime , ISO_NANOSECOND ) ,
5080
+ calendar ,
5081
+ sign * years ,
5082
+ sign * months ,
5083
+ sign * weeks ,
5084
+ sign * days ,
5085
+ sign * hours ,
5086
+ sign * minutes ,
5087
+ sign * seconds ,
5088
+ sign * milliseconds ,
5089
+ sign * microseconds ,
5090
+ sign * nanoseconds ,
5091
+ options
5092
+ ) ;
5093
+ return CreateTemporalDateTime ( year , month , day , hour , minute , second , millisecond , microsecond , nanosecond , calendar ) ;
5094
+ }
5095
+
5096
+ export function AddDurationToOrSubtractDurationFromPlainTime (
5097
+ operation : AddSubtractOperation ,
5098
+ temporalTime : Temporal . PlainTime ,
5099
+ durationLike : PlainTimeParams [ 'add' ] [ 0 ]
5100
+ ) : Temporal . PlainTime {
5101
+ const sign = operation === 'subtract' ? - 1 : 1 ;
5102
+ const { hours, minutes, seconds, milliseconds, microseconds, nanoseconds } = ToTemporalDurationRecord ( durationLike ) ;
5103
+ let { hour, minute, second, millisecond, microsecond, nanosecond } = AddTime (
5104
+ GetSlot ( temporalTime , ISO_HOUR ) ,
5105
+ GetSlot ( temporalTime , ISO_MINUTE ) ,
5106
+ GetSlot ( temporalTime , ISO_SECOND ) ,
5107
+ GetSlot ( temporalTime , ISO_MILLISECOND ) ,
5108
+ GetSlot ( temporalTime , ISO_MICROSECOND ) ,
5109
+ GetSlot ( temporalTime , ISO_NANOSECOND ) ,
5110
+ sign * hours ,
5111
+ sign * minutes ,
5112
+ sign * seconds ,
5113
+ sign * milliseconds ,
5114
+ sign * microseconds ,
5115
+ sign * nanoseconds
5116
+ ) ;
5117
+ ( { hour, minute, second, millisecond, microsecond, nanosecond } = RegulateTime (
5118
+ hour ,
5119
+ minute ,
5120
+ second ,
5121
+ millisecond ,
5122
+ microsecond ,
5123
+ nanosecond ,
5124
+ 'reject'
5125
+ ) ) ;
5126
+ const PlainTime = GetIntrinsic ( '%Temporal.PlainTime%' ) ;
5127
+ return new PlainTime ( hour , minute , second , millisecond , microsecond , nanosecond ) ;
5128
+ }
5129
+
5130
+ export function AddDurationToOrSubtractDurationFromPlainYearMonth (
5131
+ operation : AddSubtractOperation ,
5132
+ yearMonth : Temporal . PlainYearMonth ,
5133
+ durationLike : PlainYearMonthParams [ 'add' ] [ 0 ] ,
5134
+ optionsParam : PlainYearMonthParams [ 'add' ] [ 1 ]
5135
+ ) : Temporal . PlainYearMonth {
5136
+ let duration = ToTemporalDurationRecord ( durationLike ) ;
5137
+ if ( operation === 'subtract' ) {
5138
+ duration = {
5139
+ years : - duration . years ,
5140
+ months : - duration . months ,
5141
+ weeks : - duration . weeks ,
5142
+ days : - duration . days ,
5143
+ hours : - duration . hours ,
5144
+ minutes : - duration . minutes ,
5145
+ seconds : - duration . seconds ,
5146
+ milliseconds : - duration . milliseconds ,
5147
+ microseconds : - duration . microseconds ,
5148
+ nanoseconds : - duration . nanoseconds
5149
+ } ;
5150
+ }
5151
+ let { years, months, weeks, days, hours, minutes, seconds, milliseconds, microseconds, nanoseconds } = duration ;
5152
+ ( { days } = BalanceDuration ( days , hours , minutes , seconds , milliseconds , microseconds , nanoseconds , 'day' ) ) ;
5153
+
5154
+ const options = GetOptionsObject ( optionsParam ) ;
5155
+
5156
+ const calendar = GetSlot ( yearMonth , CALENDAR ) ;
5157
+ const fieldNames = CalendarFields ( calendar , [ 'monthCode' , 'year' ] ) as ReadonlyArray <
5158
+ keyof Temporal . PlainYearMonthLike
5159
+ > ;
5160
+ const fields = ToTemporalYearMonthFields ( yearMonth , fieldNames ) ;
5161
+ const sign = DurationSign ( years , months , weeks , days , 0 , 0 , 0 , 0 , 0 , 0 ) ;
5162
+ const day = sign < 0 ? ToPositiveInteger ( CalendarDaysInMonth ( calendar , yearMonth ) ) : 1 ;
5163
+ const startDate = CalendarDateFromFields ( calendar , { ...fields , day } ) ;
5164
+ const optionsCopy = { ...options } ;
5165
+ const addedDate = CalendarDateAdd ( calendar , startDate , { ...duration , days } , options ) ;
5166
+ const addedDateFields = ToTemporalYearMonthFields ( addedDate , fieldNames ) ;
5167
+
5168
+ return CalendarYearMonthFromFields ( calendar , addedDateFields , optionsCopy ) ;
5169
+ }
5170
+
5171
+ export function AddDurationToOrSubtractDurationFromZonedDateTime (
5172
+ operation : AddSubtractOperation ,
5173
+ zonedDateTime : Temporal . ZonedDateTime ,
5174
+ durationLike : ZonedDateTimeParams [ 'add' ] [ 0 ] ,
5175
+ optionsParam : ZonedDateTimeParams [ 'add' ] [ 1 ]
5176
+ ) : Temporal . ZonedDateTime {
5177
+ const sign = operation === 'subtract' ? - 1 : 1 ;
5178
+ const { years, months, weeks, days, hours, minutes, seconds, milliseconds, microseconds, nanoseconds } =
5179
+ ToTemporalDurationRecord ( durationLike ) ;
5180
+ const options = GetOptionsObject ( optionsParam ) ;
5181
+ const timeZone = GetSlot ( zonedDateTime , TIME_ZONE ) ;
5182
+ const calendar = GetSlot ( zonedDateTime , CALENDAR ) ;
5183
+ const epochNanoseconds = AddZonedDateTime (
5184
+ GetSlot ( zonedDateTime , INSTANT ) ,
5185
+ timeZone ,
5186
+ calendar ,
5187
+ sign * years ,
5188
+ sign * months ,
5189
+ sign * weeks ,
5190
+ sign * days ,
5191
+ sign * hours ,
5192
+ sign * minutes ,
5193
+ sign * seconds ,
5194
+ sign * milliseconds ,
5195
+ sign * microseconds ,
5196
+ sign * nanoseconds ,
5197
+ options
5198
+ ) ;
5199
+ return CreateTemporalZonedDateTime ( epochNanoseconds , timeZone , calendar ) ;
5200
+ }
5201
+
5002
5202
function RoundNumberToIncrement ( quantity : JSBI , increment : number , mode : Temporal . RoundingMode ) {
5003
5203
if ( increment === 1 ) return quantity ;
5004
5204
let { quotient, remainder } = divmod ( quantity , JSBI . BigInt ( increment ) ) ;
0 commit comments