@@ -23,6 +23,7 @@ const {
23
23
LocalDateTime,
24
24
OffsetDateTime,
25
25
} = require ( '../../../lib/core/DateTimeClasses' ) ;
26
+ const { leftZeroPadInteger } = require ( '../../../lib/util/DateTimeUtil' ) ;
26
27
27
28
describe ( 'DateTimeClassesTest' , function ( ) {
28
29
describe ( 'LocalTimeTest' , function ( ) {
@@ -110,21 +111,43 @@ describe('DateTimeClassesTest', function () {
110
111
( ( ) => LocalTime . fromString ( null ) ) . should . throw ( TypeError , 'String expected' ) ;
111
112
( ( ) => LocalTime . fromString ( ) ) . should . throw ( TypeError , 'String expected' ) ;
112
113
} ) ;
114
+
115
+ it ( 'should construct from fromDate correctly' , function ( ) {
116
+ const localTime1 = LocalTime . fromDate ( new Date ( 2000 , 2 , 29 , 0 , 0 , 0 , 0 ) ) ;
117
+ localTime1 . toString ( ) . should . be . eq ( '00:00:00' ) ;
118
+ const localTime2 = LocalTime . fromDate ( new Date ( 2000 , 0 , 29 , 2 , 3 , 4 , 6 ) ) ;
119
+ localTime2 . toString ( ) . should . be . eq ( '02:03:04.006000000' ) ;
120
+ } ) ;
121
+
122
+ it ( 'should throw when constructed from fromDate with a non-date thing' , function ( ) {
123
+ const nonDateThings = [ 1 , null , '' , { } , [ ] , function ( ) { } , class A { } , LocalDateTime . fromDate ( new Date ( ) ) ] ;
124
+ nonDateThings . forEach ( nonDateThing => {
125
+ ( ( ) => LocalTime . fromDate ( nonDateThing ) ) . should . throw ( TypeError , 'A Date is not passed' ) ;
126
+ } ) ;
127
+ } ) ;
128
+
129
+ it ( 'should throw when constructed from fromDate with an invalid date' , function ( ) {
130
+ const invalidDates = [ new Date ( 'aa' ) , new Date ( { } ) , new Date ( undefined ) ] ;
131
+ invalidDates . forEach ( invalidDate => {
132
+ isNaN ( invalidDate . getTime ) . should . be . true ;
133
+ ( ( ) => LocalTime . fromDate ( invalidDate ) ) . should . throw ( RangeError , 'Invalid Date is passed' ) ;
134
+ } ) ;
135
+ } ) ;
113
136
} ) ;
114
137
describe ( 'LocalDateTest' , function ( ) {
115
- it ( 'should throw RangeError if year is not an integer between -999_999_999-999_999_999(inclusive)' ,
116
- function ( ) {
117
- ( ( ) => new LocalDate ( 1e9 , 1 , 1 ) ) . should . throw ( RangeError , 'Year' ) ;
118
- ( ( ) => new LocalDate ( - 1e9 , 1 , 1 ) ) . should . throw ( RangeError , 'Year' ) ;
119
- ( ( ) => new LocalDate ( 1.1 , 1 , 1 ) ) . should . throw ( RangeError , 'All arguments must be integers' ) ;
120
- ( ( ) => new LocalDate ( '1' , 1 , 1 ) ) . should . throw ( TypeError , 'All arguments must be numbers' ) ;
121
- ( ( ) => new LocalDate ( { 1 : 1 } , 1 , 1 ) ) . should . throw ( TypeError , 'All arguments must be numbers' ) ;
122
- ( ( ) => new LocalDate ( [ ] , 1 , 1 ) ) . should . throw ( TypeError , 'All arguments must be numbers' ) ;
123
- ( ( ) => new LocalDate ( 1e12 , 1 , 1 ) ) . should . throw ( RangeError , 'Year' ) ;
124
- } ) ;
138
+ it ( 'should throw RangeError if year is not an integer between -999_999_999-999_999_999(inclusive)' , function ( ) {
139
+ ( ( ) => new LocalDate ( 1e9 , 1 , 1 ) ) . should . throw ( RangeError , 'Year' ) ;
140
+ ( ( ) => new LocalDate ( - 1e9 , 1 , 1 ) ) . should . throw ( RangeError , 'Year' ) ;
141
+ ( ( ) => new LocalDate ( 1.1 , 1 , 1 ) ) . should . throw ( RangeError , 'All arguments must be integers' ) ;
142
+ ( ( ) => new LocalDate ( '1' , 1 , 1 ) ) . should . throw ( TypeError , 'All arguments must be numbers' ) ;
143
+ ( ( ) => new LocalDate ( { 1 : 1 } , 1 , 1 ) ) . should . throw ( TypeError , 'All arguments must be numbers' ) ;
144
+ ( ( ) => new LocalDate ( [ ] , 1 , 1 ) ) . should . throw ( TypeError , 'All arguments must be numbers' ) ;
145
+ ( ( ) => new LocalDate ( 1e12 , 1 , 1 ) ) . should . throw ( RangeError , 'Year' ) ;
146
+ } ) ;
125
147
126
- it ( 'should throw RangeError if month is not an integer between 0-59 (inclusive)' , function ( ) {
148
+ it ( 'should throw RangeError if month is not an integer between 1-12 (inclusive)' , function ( ) {
127
149
( ( ) => new LocalDate ( 1 , - 1 , 1 ) ) . should . throw ( RangeError , 'Month' ) ;
150
+ ( ( ) => new LocalDate ( 1 , 0 , 1 ) ) . should . throw ( RangeError , 'Month' ) ;
128
151
( ( ) => new LocalDate ( 1 , 1.1 , 1 ) ) . should . throw ( RangeError , 'All arguments must be integers' ) ;
129
152
( ( ) => new LocalDate ( 1 , 233 , 1 ) ) . should . throw ( RangeError , 'Month' ) ;
130
153
( ( ) => new LocalDate ( 1 , '1' , 1 ) ) . should . throw ( TypeError , 'All arguments must be numbers' ) ;
@@ -147,6 +170,8 @@ describe('DateTimeClassesTest', function () {
147
170
} ) ;
148
171
149
172
it ( 'should convert to string correctly' , function ( ) {
173
+ new LocalDate ( 999999999 , 12 , 31 ) . toString ( ) . should . be . eq ( '999999999-12-31' ) ;
174
+ new LocalDate ( 0 , 1 , 1 ) . toString ( ) . should . be . eq ( '0000-01-01' ) ;
150
175
new LocalDate ( 2000 , 2 , 29 ) . toString ( ) . should . be . eq ( '2000-02-29' ) ;
151
176
new LocalDate ( 2001 , 2 , 1 ) . toString ( ) . should . be . eq ( '2001-02-01' ) ;
152
177
new LocalDate ( 35 , 2 , 28 ) . toString ( ) . should . be . eq ( '0035-02-28' ) ;
@@ -181,6 +206,16 @@ describe('DateTimeClassesTest', function () {
181
206
localtime5 . year . should . be . eq ( 29999 ) ;
182
207
localtime5 . month . should . be . eq ( 3 ) ;
183
208
localtime5 . date . should . be . eq ( 29 ) ;
209
+
210
+ const localtime6 = LocalDate . fromString ( '999999999-12-31' ) ;
211
+ localtime6 . year . should . be . eq ( 999999999 ) ;
212
+ localtime6 . month . should . be . eq ( 12 ) ;
213
+ localtime6 . date . should . be . eq ( 31 ) ;
214
+
215
+ const localtime7 = LocalDate . fromString ( '0000-01-01' ) ;
216
+ localtime7 . year . should . be . eq ( 0 ) ;
217
+ localtime7 . month . should . be . eq ( 1 ) ;
218
+ localtime7 . date . should . be . eq ( 1 ) ;
184
219
} ) ;
185
220
186
221
it ( 'should throw RangeError on invalid string' , function ( ) {
@@ -205,6 +240,32 @@ describe('DateTimeClassesTest', function () {
205
240
( ( ) => LocalDate . fromString ( null ) ) . should . throw ( TypeError , 'String expected' ) ;
206
241
( ( ) => LocalDate . fromString ( ) ) . should . throw ( TypeError , 'String expected' ) ;
207
242
} ) ;
243
+
244
+ it ( 'should construct from fromDate correctly' , function ( ) {
245
+ const date1 = LocalDate . fromDate ( new Date ( 2000 , 2 , 29 , 2 , 3 , 4 , 6 ) ) ;
246
+ date1 . toString ( ) . should . be . eq ( '2000-03-29' ) ;
247
+ const date2 = LocalDate . fromDate ( new Date ( 2000 , 0 , 29 , 2 , 3 , 4 , 6 ) ) ;
248
+ date2 . toString ( ) . should . be . eq ( '2000-01-29' ) ;
249
+ const date3 = LocalDate . fromDate ( new Date ( - 2000 , 2 , 29 , 2 , 3 , 4 , 6 ) ) ;
250
+ date3 . toString ( ) . should . be . eq ( '-2000-03-29' ) ;
251
+ const date4 = LocalDate . fromDate ( new Date ( - 2000 , 0 , 29 , 2 , 3 , 4 , 6 ) ) ;
252
+ date4 . toString ( ) . should . be . eq ( '-2000-01-29' ) ;
253
+ } ) ;
254
+
255
+ it ( 'should throw when constructed from fromDate with a non-date thing' , function ( ) {
256
+ const nonDateThings = [ 1 , null , '' , { } , [ ] , function ( ) { } , class A { } , LocalDateTime . fromDate ( new Date ( ) ) ] ;
257
+ nonDateThings . forEach ( nonDateThing => {
258
+ ( ( ) => LocalDate . fromDate ( nonDateThing ) ) . should . throw ( TypeError , 'A Date is not passed' ) ;
259
+ } ) ;
260
+ } ) ;
261
+
262
+ it ( 'should throw when constructed from fromDate with an invalid date' , function ( ) {
263
+ const invalidDates = [ new Date ( 'aa' ) , new Date ( { } ) , new Date ( undefined ) ] ;
264
+ invalidDates . forEach ( invalidDate => {
265
+ isNaN ( invalidDate . getTime ) . should . be . true ;
266
+ ( ( ) => LocalDate . fromDate ( invalidDate ) ) . should . throw ( RangeError , 'Invalid Date is passed' ) ;
267
+ } ) ;
268
+ } ) ;
208
269
} ) ;
209
270
describe ( 'LocalDateTimeTest' , function ( ) {
210
271
it ( 'should throw RangeError if local time is not valid' , function ( ) {
@@ -272,21 +333,30 @@ describe('DateTimeClassesTest', function () {
272
333
} ) ;
273
334
274
335
it ( 'fromDate should throw RangeError if date is invalid' , function ( ) {
275
- ( ( ) => LocalDateTime . fromDate ( new Date ( - 1 ) ) ) . should . throw ( RangeError , 'Invalid Date' ) ;
276
336
( ( ) => LocalDateTime . fromDate ( new Date ( 's' ) ) ) . should . throw ( RangeError , 'Invalid Date' ) ;
277
337
( ( ) => LocalDateTime . fromDate ( 1 , 1 ) ) . should . throw ( TypeError , 'A Date is not passed' ) ;
278
338
( ( ) => LocalDateTime . fromDate ( 's' , 1 ) ) . should . throw ( TypeError , 'A Date is not passed' ) ;
279
339
( ( ) => LocalDateTime . fromDate ( [ ] , 1 ) ) . should . throw ( TypeError , 'A Date is not passed' ) ;
280
340
} ) ;
281
341
282
342
it ( 'should construct from fromDate correctly' , function ( ) {
283
- const dateTime = LocalDateTime . fromDate ( new Date ( Date . UTC ( 2000 , 2 , 29 , 2 , 3 , 4 , 6 ) ) ) ;
284
- dateTime . toString ( ) . should . be . eq ( '2000-02-29T02:03:04.006000000' ) ;
343
+ const dateTime1 = LocalDateTime . fromDate ( new Date ( 2000 , 2 , 29 , 2 , 3 , 4 , 6 ) ) ;
344
+ dateTime1 . toString ( ) . should . be . eq ( '2000-03-29T02:03:04.006000000' ) ;
345
+ const dateTime2 = LocalDateTime . fromDate ( new Date ( 2000 , 0 , 29 , 2 , 3 , 4 , 6 ) ) ;
346
+ dateTime2 . toString ( ) . should . be . eq ( '2000-01-29T02:03:04.006000000' ) ;
285
347
} ) ;
286
348
287
349
it ( 'should convert to date correctly' , function ( ) {
288
350
const dateTime = new LocalDateTime ( new LocalDate ( 2000 , 2 , 29 ) , new LocalTime ( 2 , 19 , 4 , 6000000 ) ) ;
289
- dateTime . asDate ( ) . toISOString ( ) . should . be . eq ( '2000-02-29T02:19:04.006Z' ) ;
351
+ const asDate = dateTime . asDate ( ) ;
352
+ const date = leftZeroPadInteger ( asDate . getDate ( ) , 2 ) ;
353
+ const month = leftZeroPadInteger ( asDate . getMonth ( ) + 1 , 2 ) ; // Date's month is 0-based
354
+ const year = leftZeroPadInteger ( asDate . getFullYear ( ) , 4 ) ;
355
+ const hours = leftZeroPadInteger ( asDate . getHours ( ) , 2 ) ;
356
+ const minutes = leftZeroPadInteger ( asDate . getMinutes ( ) , 2 ) ;
357
+ const seconds = leftZeroPadInteger ( asDate . getSeconds ( ) , 2 ) ;
358
+
359
+ `${ date } .${ month } .${ year } ${ hours } :${ minutes } :${ seconds } ` . should . be . eq ( '29.02.2000 02:19:04' ) ;
290
360
} ) ;
291
361
} ) ;
292
362
describe ( 'OffsetDateTimeTest' , function ( ) {
@@ -304,7 +374,6 @@ describe('DateTimeClassesTest', function () {
304
374
} ) ;
305
375
306
376
it ( 'fromDate should throw RangeError if date is invalid' , function ( ) {
307
- ( ( ) => OffsetDateTime . fromDate ( new Date ( - 1 ) , 1 ) ) . should . throw ( RangeError , 'Invalid Date' ) ;
308
377
( ( ) => OffsetDateTime . fromDate ( new Date ( 's' ) , 1 ) ) . should . throw ( RangeError , 'Invalid Date' ) ;
309
378
( ( ) => OffsetDateTime . fromDate ( 1 , 1 ) ) . should . throw ( TypeError , 'A Date is not passed' ) ;
310
379
( ( ) => OffsetDateTime . fromDate ( 's' , 1 ) ) . should . throw ( TypeError , 'A Date is not passed' ) ;
@@ -320,16 +389,29 @@ describe('DateTimeClassesTest', function () {
320
389
} ) ;
321
390
322
391
it ( 'should construct from fromDate correctly' , function ( ) {
323
- const dateTime3 = OffsetDateTime . fromDate ( new Date ( Date . UTC ( 2000 , 2 , 29 , 2 , 3 , 4 , 6 ) ) , 1800 ) ;
324
- dateTime3 . toString ( ) . should . be . eq ( '2000-02-29T02:03:04.006000000+00:30' ) ;
392
+ const offsetDateTime1 = OffsetDateTime . fromDate ( new Date ( 2000 , 2 , 29 , 2 , 3 , 4 , 6 ) , 1800 ) ;
393
+ offsetDateTime1 . toString ( ) . should . be . eq ( '2000-03-29T02:03:04.006000000+00:30' ) ;
394
+ const offsetDateTime2 = OffsetDateTime . fromDate ( new Date ( 2000 , 0 , 29 , 2 , 3 , 4 , 6 ) , 1800 ) ;
395
+ offsetDateTime2 . toString ( ) . should . be . eq ( '2000-01-29T02:03:04.006000000+00:30' ) ;
325
396
} ) ;
326
397
327
398
const dateTime1 = new OffsetDateTime (
328
399
new LocalDateTime ( new LocalDate ( 2000 , 2 , 29 ) , new LocalTime ( 2 , 19 , 4 , 6000000 ) ) , 1000
329
400
) ;
330
401
331
402
it ( 'should convert to date correctly' , function ( ) {
332
- dateTime1 . asDate ( ) . toISOString ( ) . should . be . eq ( '2000-02-29T02:02:24.006Z' ) ;
403
+ const asDate = dateTime1 . asDate ( ) ;
404
+
405
+ const date = leftZeroPadInteger ( asDate . getDate ( ) , 2 ) ;
406
+ const month = leftZeroPadInteger ( asDate . getMonth ( ) + 1 , 2 ) ; // Date's month is 0-based
407
+ const year = leftZeroPadInteger ( asDate . getFullYear ( ) , 4 ) ;
408
+ const hours = leftZeroPadInteger ( asDate . getHours ( ) , 2 ) ;
409
+ const minutes = leftZeroPadInteger ( asDate . getMinutes ( ) , 2 ) ;
410
+ const seconds = leftZeroPadInteger ( asDate . getSeconds ( ) , 2 ) ;
411
+
412
+ `${ date } .${ month } .${ year } ${ hours } :${ minutes } :${ seconds } ` . should . be . eq ( '29.02.2000 02:02:24' ) ;
413
+
414
+ asDate . getMilliseconds ( ) . should . be . equal ( 6 ) ;
333
415
} ) ;
334
416
335
417
it ( 'should convert to string correctly' , function ( ) {
@@ -390,7 +472,6 @@ describe('DateTimeClassesTest', function () {
390
472
391
473
offsetSeconds3 . should . be . eq ( 0 ) ;
392
474
393
- // Timezone info omitted, UTC should be assumed
394
475
const offsetDateTime4 = OffsetDateTime . fromString ( '2021-04-15T07:33:04.914Z' ) ;
395
476
const offsetSeconds4 = offsetDateTime4 . offsetSeconds ;
396
477
const localDateTime4 = offsetDateTime4 . localDateTime ;
0 commit comments