11
11
*/
12
12
13
13
import { add , addTime , addZoned , cycleDate , cycleTime , cycleZoned , set , setTime , setZoned , subtract , subtractTime , subtractZoned } from './manipulation' ;
14
- import { Calendar , CycleOptions , CycleTimeOptions , DateField , DateFields , Disambiguation , Duration , OverflowBehavior , TimeField , TimeFields } from './types' ;
14
+ import { AnyCalendarDate , AnyTime , Calendar , CycleOptions , CycleTimeOptions , DateField , DateFields , Disambiguation , Duration , TimeField , TimeFields } from './types' ;
15
15
import { compareDate , compareTime } from './queries' ;
16
16
import { dateTimeToString , dateToString , timeToString , zonedDateTimeToString } from './string' ;
17
17
import { GregorianCalendar } from './calendars/GregorianCalendar' ;
@@ -38,6 +38,10 @@ function shiftArgs(args: any[]) {
38
38
}
39
39
40
40
export class CalendarDate {
41
+ // This prevents TypeScript from allowing other types with the same fields to match.
42
+ // i.e. a ZonedDateTime should not be be passable to a parameter that expects CalendarDate.
43
+ // If that behavior is desired, use the AnyCalendarDate interface instead.
44
+ #type;
41
45
public readonly calendar : Calendar ;
42
46
public readonly era : string ;
43
47
public readonly year : number ;
@@ -76,8 +80,8 @@ export class CalendarDate {
76
80
return subtract ( this , duration ) ;
77
81
}
78
82
79
- set ( fields : DateFields , behavior ?: OverflowBehavior ) {
80
- return set ( this , fields , behavior ) ;
83
+ set ( fields : DateFields ) {
84
+ return set ( this , fields ) ;
81
85
}
82
86
83
87
cycle ( field : DateField , amount : number , options ?: CycleOptions ) {
@@ -92,12 +96,15 @@ export class CalendarDate {
92
96
return dateToString ( this ) ;
93
97
}
94
98
95
- compare ( b : CalendarDate ) {
99
+ compare ( b : AnyCalendarDate ) {
96
100
return compareDate ( this , b ) ;
97
101
}
98
102
}
99
103
100
104
export class Time {
105
+ // This prevents TypeScript from allowing other types with the same fields to match.
106
+ #type;
107
+
101
108
constructor (
102
109
public readonly hour : number = 0 ,
103
110
public readonly minute : number = 0 ,
@@ -117,8 +124,8 @@ export class Time {
117
124
return subtractTime ( this , duration ) ;
118
125
}
119
126
120
- set ( fields : TimeFields , behavior ?: OverflowBehavior ) {
121
- return setTime ( this , fields , behavior ) ;
127
+ set ( fields : TimeFields ) {
128
+ return setTime ( this , fields ) ;
122
129
}
123
130
124
131
cycle ( field : TimeField , amount : number , options ?: CycleTimeOptions ) {
@@ -129,12 +136,19 @@ export class Time {
129
136
return timeToString ( this ) ;
130
137
}
131
138
132
- compare ( b : Time ) {
139
+ compare ( b : AnyTime ) {
133
140
return compareTime ( this , b ) ;
134
141
}
135
142
}
136
143
137
- export class CalendarDateTime extends CalendarDate {
144
+ export class CalendarDateTime {
145
+ // This prevents TypeScript from allowing other types with the same fields to match.
146
+ #type;
147
+ public readonly calendar : Calendar ;
148
+ public readonly era : string ;
149
+ public readonly year : number ;
150
+ public readonly month : number ;
151
+ public readonly day : number ;
138
152
public readonly hour : number ;
139
153
public readonly minute : number ;
140
154
public readonly second : number ;
@@ -145,7 +159,16 @@ export class CalendarDateTime extends CalendarDate {
145
159
constructor ( calendar : Calendar , era : string , year : number , month : number , day : number , hour ?: number , minute ?: number , second ?: number , millisecond ?: number ) ;
146
160
constructor ( ...args : any [ ] ) {
147
161
let [ calendar , era , year , month , day ] = shiftArgs ( args ) ;
148
- super ( calendar , era , year , month , day ) ;
162
+ this . calendar = calendar ;
163
+ this . era = era ;
164
+ this . year = year ;
165
+ this . month = month ;
166
+ this . day = day ;
167
+
168
+ if ( this . calendar . balanceDate ) {
169
+ this . calendar . balanceDate ( this ) ;
170
+ }
171
+
149
172
this . hour = args . shift ( ) || 0 ;
150
173
this . minute = args . shift ( ) || 0 ;
151
174
this . second = args . shift ( ) || 0 ;
@@ -160,8 +183,16 @@ export class CalendarDateTime extends CalendarDate {
160
183
}
161
184
}
162
185
163
- set ( fields : DateFields & TimeFields , behavior ?: OverflowBehavior ) {
164
- return set ( setTime ( this , fields , behavior ) , fields , behavior ) ;
186
+ add ( duration : Duration ) {
187
+ return add ( this , duration ) ;
188
+ }
189
+
190
+ subtract ( duration : Duration ) {
191
+ return subtract ( this , duration ) ;
192
+ }
193
+
194
+ set ( fields : DateFields & TimeFields ) {
195
+ return set ( setTime ( this , fields ) , fields ) ;
165
196
}
166
197
167
198
cycle ( field : DateField | TimeField , amount : number , options ?: CycleTimeOptions ) {
@@ -176,11 +207,15 @@ export class CalendarDateTime extends CalendarDate {
176
207
}
177
208
}
178
209
210
+ toDate ( timeZone : string ) {
211
+ return toDate ( this , timeZone ) ;
212
+ }
213
+
179
214
toString ( ) {
180
215
return dateTimeToString ( this ) ;
181
216
}
182
217
183
- compare ( b : CalendarDate | CalendarDateTime ) {
218
+ compare ( b : CalendarDate | CalendarDateTime | ZonedDateTime ) {
184
219
let res = compareDate ( this , b ) ;
185
220
if ( res === 0 ) {
186
221
return compareTime ( this , toCalendarDateTime ( b ) ) ;
@@ -190,7 +225,18 @@ export class CalendarDateTime extends CalendarDate {
190
225
}
191
226
}
192
227
193
- export class ZonedDateTime extends CalendarDateTime {
228
+ export class ZonedDateTime {
229
+ // This prevents TypeScript from allowing other types with the same fields to match.
230
+ #type;
231
+ public readonly calendar : Calendar ;
232
+ public readonly era : string ;
233
+ public readonly year : number ;
234
+ public readonly month : number ;
235
+ public readonly day : number ;
236
+ public readonly hour : number ;
237
+ public readonly minute : number ;
238
+ public readonly second : number ;
239
+ public readonly millisecond : number ;
194
240
public readonly timeZone : string ;
195
241
public readonly offset : number ;
196
242
@@ -201,9 +247,22 @@ export class ZonedDateTime extends CalendarDateTime {
201
247
let [ calendar , era , year , month , day ] = shiftArgs ( args ) ;
202
248
let timeZone = args . shift ( ) ;
203
249
let offset = args . shift ( ) ;
204
- super ( calendar , era , year , month , day , ...args ) ;
250
+ this . calendar = calendar ;
251
+ this . era = era ;
252
+ this . year = year ;
253
+ this . month = month ;
254
+ this . day = day ;
255
+
256
+ if ( this . calendar . balanceDate ) {
257
+ this . calendar . balanceDate ( this ) ;
258
+ }
259
+
205
260
this . timeZone = timeZone ;
206
261
this . offset = offset ;
262
+ this . hour = args . shift ( ) || 0 ;
263
+ this . minute = args . shift ( ) || 0 ;
264
+ this . second = args . shift ( ) || 0 ;
265
+ this . millisecond = args . shift ( ) || 0 ;
207
266
}
208
267
209
268
copy ( ) : ZonedDateTime {
@@ -222,8 +281,8 @@ export class ZonedDateTime extends CalendarDateTime {
222
281
return subtractZoned ( this , duration ) ;
223
282
}
224
283
225
- set ( fields : DateFields & TimeFields , behavior ?: OverflowBehavior , disambiguation ?: Disambiguation ) {
226
- return setZoned ( this , fields , behavior , disambiguation ) ;
284
+ set ( fields : DateFields & TimeFields , disambiguation ?: Disambiguation ) {
285
+ return setZoned ( this , fields , disambiguation ) ;
227
286
}
228
287
229
288
cycle ( field : DateField | TimeField , amount : number , options ?: CycleTimeOptions ) {
@@ -244,6 +303,6 @@ export class ZonedDateTime extends CalendarDateTime {
244
303
245
304
compare ( b : CalendarDate | CalendarDateTime | ZonedDateTime ) {
246
305
// TODO: Is this a bad idea??
247
- return this . toDate ( ) - toZoned ( b , this . timeZone ) . toDate ( ) ;
306
+ return this . toDate ( ) . getTime ( ) - toZoned ( b , this . timeZone ) . toDate ( ) . getTime ( ) ;
248
307
}
249
308
}
0 commit comments