@@ -12,6 +12,7 @@ import {
12
12
DateTime as LuxonDateTime ,
13
13
Info as LuxonInfo ,
14
14
DateTimeOptions as LuxonDateTimeOptions ,
15
+ CalendarSystem as LuxonCalendarSystem ,
15
16
} from 'luxon' ;
16
17
17
18
/** Configurable options for the `LuxonDateAdapter`. */
@@ -27,6 +28,12 @@ export interface MatLuxonDateAdapterOptions {
27
28
* Changing this will change how Angular Material components like DatePicker shows start of week.
28
29
*/
29
30
firstDayOfWeek : number ;
31
+
32
+ /**
33
+ * Sets the output Calendar.
34
+ * Changing this will change how Angular Material components like DatePicker output dates.
35
+ */
36
+ defaultOutputCalendar : LuxonCalendarSystem ;
30
37
}
31
38
32
39
/** InjectionToken for LuxonDateAdapter to configure options. */
@@ -43,6 +50,7 @@ export function MAT_LUXON_DATE_ADAPTER_OPTIONS_FACTORY(): MatLuxonDateAdapterOpt
43
50
return {
44
51
useUtc : false ,
45
52
firstDayOfWeek : 0 ,
53
+ defaultOutputCalendar : 'gregory' ,
46
54
} ;
47
55
}
48
56
@@ -60,6 +68,7 @@ function range<T>(length: number, valueFunction: (index: number) => T): T[] {
60
68
export class LuxonDateAdapter extends DateAdapter < LuxonDateTime > {
61
69
private _useUTC : boolean ;
62
70
private _firstDayOfWeek : number ;
71
+ private _defaultOutputCalendar : LuxonCalendarSystem ;
63
72
64
73
constructor (
65
74
@Optional ( ) @Inject ( MAT_DATE_LOCALE ) dateLocale : string ,
@@ -70,6 +79,7 @@ export class LuxonDateAdapter extends DateAdapter<LuxonDateTime> {
70
79
super ( ) ;
71
80
this . _useUTC = ! ! options ?. useUtc ;
72
81
this . _firstDayOfWeek = options ?. firstDayOfWeek || 0 ;
82
+ this . _defaultOutputCalendar = options ?. defaultOutputCalendar || 'gregory' ;
73
83
this . setLocale ( dateLocale || LuxonDateTime . local ( ) . locale ) ;
74
84
}
75
85
@@ -91,7 +101,11 @@ export class LuxonDateAdapter extends DateAdapter<LuxonDateTime> {
91
101
}
92
102
93
103
getMonthNames ( style : 'long' | 'short' | 'narrow' ) : string [ ] {
94
- return LuxonInfo . months ( style , { locale : this . locale } ) ;
104
+ // Adding outputCalendar option, because LuxonInfo doesn't get effected by LuxonSettings
105
+ return LuxonInfo . months ( style , {
106
+ locale : this . locale ,
107
+ outputCalendar : this . _defaultOutputCalendar ,
108
+ } ) ;
95
109
}
96
110
97
111
getDateNames ( ) : string [ ] {
@@ -113,7 +127,7 @@ export class LuxonDateAdapter extends DateAdapter<LuxonDateTime> {
113
127
}
114
128
115
129
getYearName ( date : LuxonDateTime ) : string {
116
- return date . toFormat ( 'yyyy' ) ;
130
+ return date . toFormat ( 'yyyy' , this . _getOptions ( ) ) ;
117
131
}
118
132
119
133
getFirstDayOfWeek ( ) : number {
@@ -125,10 +139,12 @@ export class LuxonDateAdapter extends DateAdapter<LuxonDateTime> {
125
139
}
126
140
127
141
clone ( date : LuxonDateTime ) : LuxonDateTime {
128
- return LuxonDateTime . fromObject ( date . toObject ( ) ) ;
142
+ return LuxonDateTime . fromObject ( date . toObject ( ) , this . _getOptions ( ) ) ;
129
143
}
130
144
131
145
createDate ( year : number , month : number , date : number ) : LuxonDateTime {
146
+ const options = this . _getOptions ( ) ;
147
+
132
148
if ( month < 0 || month > 11 ) {
133
149
throw Error ( `Invalid month index "${ month } ". Month index has to be between 0 and 11.` ) ;
134
150
}
@@ -139,18 +155,20 @@ export class LuxonDateAdapter extends DateAdapter<LuxonDateTime> {
139
155
140
156
// Luxon uses 1-indexed months so we need to add one to the month.
141
157
const result = this . _useUTC
142
- ? LuxonDateTime . utc ( year , month + 1 , date )
143
- : LuxonDateTime . local ( year , month + 1 , date ) ;
158
+ ? LuxonDateTime . utc ( year , month + 1 , date , options )
159
+ : LuxonDateTime . local ( year , month + 1 , date , options ) ;
144
160
145
161
if ( ! this . isValid ( result ) ) {
146
162
throw Error ( `Invalid date "${ date } ". Reason: "${ result . invalidReason } ".` ) ;
147
163
}
148
164
149
- return result . setLocale ( this . locale ) ;
165
+ return result ;
150
166
}
151
167
152
168
today ( ) : LuxonDateTime {
153
- return ( this . _useUTC ? LuxonDateTime . utc ( ) : LuxonDateTime . local ( ) ) . setLocale ( this . locale ) ;
169
+ const options = this . _getOptions ( ) ;
170
+
171
+ return this . _useUTC ? LuxonDateTime . utc ( options ) : LuxonDateTime . local ( options ) ;
154
172
}
155
173
156
174
parse ( value : any , parseFormat : string | string [ ] ) : LuxonDateTime | null {
@@ -201,15 +219,15 @@ export class LuxonDateAdapter extends DateAdapter<LuxonDateTime> {
201
219
}
202
220
203
221
addCalendarYears ( date : LuxonDateTime , years : number ) : LuxonDateTime {
204
- return date . plus ( { years} ) . setLocale ( this . locale ) ;
222
+ return date . reconfigure ( this . _getOptions ( ) ) . plus ( { years} ) ;
205
223
}
206
224
207
225
addCalendarMonths ( date : LuxonDateTime , months : number ) : LuxonDateTime {
208
- return date . plus ( { months} ) . setLocale ( this . locale ) ;
226
+ return date . reconfigure ( this . _getOptions ( ) ) . plus ( { months} ) ;
209
227
}
210
228
211
229
addCalendarDays ( date : LuxonDateTime , days : number ) : LuxonDateTime {
212
- return date . plus ( { days} ) . setLocale ( this . locale ) ;
230
+ return date . reconfigure ( this . _getOptions ( ) ) . plus ( { days} ) ;
213
231
}
214
232
215
233
toIso8601 ( date : LuxonDateTime ) : string {
@@ -256,6 +274,7 @@ export class LuxonDateAdapter extends DateAdapter<LuxonDateTime> {
256
274
return {
257
275
zone : this . _useUTC ? 'utc' : undefined ,
258
276
locale : this . locale ,
277
+ outputCalendar : this . _defaultOutputCalendar ,
259
278
} ;
260
279
}
261
280
}
0 commit comments