@@ -183,41 +183,63 @@ export default (options, dayjsClass, dayjsFactory) => {
183
183
const old$Set = dayjsClass . prototype . $set ;
184
184
// Override the default $set method to convert the date to the specified calendar system
185
185
dayjsClass . prototype . $set = function ( units , int ) {
186
- if ( ( "$C" in this && this . $C === "gregory" ) || ! ( "$C" in this ) ) {
187
- return old$Set . bind ( this ) ( units , int ) ;
188
- }
189
- const instanceFactory = ( d , m , y = this . $y ) => {
190
- const convertedDate = calendarSystems [ this . $C ] . convertToGregorian (
186
+ const isGregory = ! ( "$C" in this ) || this . $C === "gregory" ;
187
+
188
+ if ( isGregory ) return old$Set . call ( this , units , int ) ;
189
+
190
+ const { $d, $u, $C, $y, $M, $D, $H, $m, $s, $ms } = this ;
191
+ const utcPrefix = $u ? "UTC" : "" ;
192
+ const setFn = ( fn , value ) => $d [ `set${ utcPrefix } ${ fn } ` ] ( value ) ;
193
+
194
+ const instanceFactory = (
195
+ d ,
196
+ M ,
197
+ y = $y ,
198
+ h = $H ,
199
+ m = $m ,
200
+ s = $s ,
201
+ ms = $ms
202
+ ) => {
203
+ const { year, month, day } = calendarSystems [ $C ] . convertToGregorian (
191
204
y ,
192
- m ,
205
+ M ,
193
206
d
194
207
) ;
195
- this . $d . setFullYear ( convertedDate . year ) ;
196
- this . $d . setMonth ( convertedDate . month ) ;
197
- this . $d . setDate ( convertedDate . day ) ;
208
+
209
+ setFn ( "FullYear" , year ) ;
210
+ setFn ( "Month" , month ) ;
211
+ setFn ( "Date" , day ) ;
212
+ setFn ( "Hours" , h ) ;
213
+ setFn ( "Minutes" , m ) ;
214
+ setFn ( "Seconds" , s ) ;
215
+ setFn ( "Milliseconds" , ms ) ;
216
+
198
217
return this ;
199
218
} ;
200
- switch ( units ) {
201
- case "date" :
202
- case "day" :
203
- instanceFactory ( int , this . $M ) ;
204
- break ;
205
- case "month" :
206
- instanceFactory ( this . $D , int ) ;
207
- break ;
208
- case "year" :
209
- instanceFactory ( this . $D , this . $M , int ) ;
210
- break ;
211
- default :
212
- return old$Set . bind ( this ) ( units , int ) ;
219
+
220
+ const unitSetters = {
221
+ date : ( ) => instanceFactory ( int , $M ) ,
222
+ day : ( ) => instanceFactory ( int , $M ) ,
223
+ month : ( ) => instanceFactory ( $D , int ) ,
224
+ year : ( ) => instanceFactory ( $D , $M , int ) ,
225
+ hour : ( ) => instanceFactory ( $D , $M , $y , int ) ,
226
+ minute : ( ) => instanceFactory ( $D , $M , $y , $H , int ) ,
227
+ second : ( ) => instanceFactory ( $D , $M , $y , $H , $m , int ) ,
228
+ millisecond : ( ) => instanceFactory ( $D , $M , $y , $H , $m , $s , int ) ,
229
+ } ;
230
+
231
+ if ( units in unitSetters ) {
232
+ unitSetters [ units ] ( ) ;
233
+ } else {
234
+ return old$Set . call ( this , units , int ) ;
213
235
}
236
+
214
237
this . init ( ) ;
215
- if ( "$C" in this && this . $C !== "gregory" ) {
216
- return this . toCalendarSystem ( this . $C ) ;
217
- }
218
- return this ;
238
+
239
+ return $C !== "gregory" ? this . toCalendarSystem ( $C ) : this ;
219
240
} ;
220
241
242
+ // Override the default add method to convert the date to the specified calendar system
221
243
const oldAdd = dayjsClass . prototype . add ;
222
244
dayjsClass . prototype . add = function ( number , units ) {
223
245
number = Number ( number ) ; // eslint-disable-line no-param-reassign
@@ -289,7 +311,9 @@ export default (options, dayjsClass, dayjsFactory) => {
289
311
} ;
290
312
291
313
// Convert a Day.js instance to a specific calendar system
292
- dayjsClass . prototype . toCalendarSystem = function ( calendar ) {
314
+ dayjsClass . prototype . toCalendarSystem = function (
315
+ calendar = defaultCalendarSystem
316
+ ) {
293
317
if ( ! calendarSystems [ calendar ] ) {
294
318
throw new Error ( `Calendar system '${ calendar } ' is not registered.` ) ;
295
319
}
@@ -333,7 +357,10 @@ export default (options, dayjsClass, dayjsFactory) => {
333
357
}
334
358
}
335
359
// Update the locale to reflect the new calendar system
336
- dayjsFactory . updateLocale ( newInstance . $L , calendarSystems [ calendar ] . localeOverride ( newInstance . $L ) ) ;
360
+ dayjsFactory . updateLocale (
361
+ newInstance . $L ,
362
+ calendarSystems [ calendar ] . localeOverride ( newInstance . $L )
363
+ ) ;
337
364
// dayjsFactory.locale(
338
365
// newInstance.$L,
339
366
// {
0 commit comments