19
19
20
20
import * as v1 from './packstream-v1' ;
21
21
import { isPoint , Point } from '../spatial-types' ;
22
- import {
23
- Date ,
24
- DateTimeWithZoneId ,
25
- DateTimeWithZoneOffset ,
26
- Duration ,
27
- isDate ,
28
- isDateTimeWithZoneId ,
29
- isDateTimeWithZoneOffset ,
30
- isDuration ,
31
- isLocalDateTime ,
32
- isLocalTime ,
33
- isTime ,
34
- Time
35
- } from '../temporal-types' ;
22
+ import { Date , DateTime , Duration , isDate , isDateTime , isDuration , isLocalDateTime , isLocalTime , isTime , Time } from '../temporal-types' ;
36
23
import { int , isInt } from '../integer' ;
37
24
import {
38
25
dateToEpochDay ,
@@ -97,10 +84,8 @@ export class Packer extends v1.Packer {
97
84
return ( ) => packDate ( obj , this , onError ) ;
98
85
} else if ( isLocalDateTime ( obj ) ) {
99
86
return ( ) => packLocalDateTime ( obj , this , onError ) ;
100
- } else if ( isDateTimeWithZoneOffset ( obj ) ) {
101
- return ( ) => packDateTimeWithZoneOffset ( obj , this , onError ) ;
102
- } else if ( isDateTimeWithZoneId ( obj ) ) {
103
- return ( ) => packDateTimeWithZoneId ( obj , this , onError ) ;
87
+ } else if ( isDateTime ( obj ) ) {
88
+ return ( ) => packDateTime ( obj , this , onError ) ;
104
89
} else {
105
90
return super . packable ( obj , onError ) ;
106
91
}
@@ -303,7 +288,7 @@ function unpackLocalTime(unpacker, structSize, buffer, disableLosslessIntegers)
303
288
*/
304
289
function packTime ( value , packer , onError ) {
305
290
const nanoOfDay = localTimeToNanoOfDay ( value . hour , value . minute , value . second , value . nanosecond ) ;
306
- const offsetSeconds = int ( value . offsetSeconds ) ;
291
+ const offsetSeconds = int ( value . timeZoneOffsetSeconds ) ;
307
292
308
293
const packableStructFields = [
309
294
packer . packable ( nanoOfDay , onError ) ,
@@ -396,21 +381,35 @@ function unpackLocalDateTime(unpacker, structSize, buffer, disableLosslessIntege
396
381
return convertIntegerPropsIfNeeded ( result , disableLosslessIntegers ) ;
397
382
}
398
383
384
+ /**
385
+ * Pack given date time.
386
+ * @param {DateTime } value the date time value to pack.
387
+ * @param {Packer } packer the packer to use.
388
+ * @param {function } onError the error callback.
389
+ */
390
+ function packDateTime ( value , packer , onError ) {
391
+ if ( value . timeZoneId ) {
392
+ packDateTimeWithZoneId ( value , packer , onError ) ;
393
+ } else {
394
+ packDateTimeWithZoneOffset ( value , packer , onError ) ;
395
+ }
396
+ }
397
+
399
398
/**
400
399
* Pack given date time with zone offset.
401
- * @param {DateTimeWithZoneOffset } value the date time value to pack.
400
+ * @param {DateTime } value the date time value to pack.
402
401
* @param {Packer } packer the packer to use.
403
402
* @param {function } onError the error callback.
404
403
*/
405
404
function packDateTimeWithZoneOffset ( value , packer , onError ) {
406
405
const epochSecond = localDateTimeToEpochSecond ( value . year , value . month , value . day , value . hour , value . minute , value . second , value . nanosecond ) ;
407
406
const nano = int ( value . nanosecond ) ;
408
- const offsetSeconds = int ( value . offsetSeconds ) ;
407
+ const timeZoneOffsetSeconds = int ( value . timeZoneOffsetSeconds ) ;
409
408
410
409
const packableStructFields = [
411
410
packer . packable ( epochSecond , onError ) ,
412
411
packer . packable ( nano , onError ) ,
413
- packer . packable ( offsetSeconds , onError )
412
+ packer . packable ( timeZoneOffsetSeconds , onError )
414
413
] ;
415
414
packer . packStruct ( DATE_TIME_WITH_ZONE_OFFSET , packableStructFields , onError ) ;
416
415
}
@@ -421,36 +420,36 @@ function packDateTimeWithZoneOffset(value, packer, onError) {
421
420
* @param {number } structSize the retrieved struct size.
422
421
* @param {BaseBuffer } buffer the buffer to unpack from.
423
422
* @param {boolean } disableLosslessIntegers if integer properties in the result date-time should be native JS numbers.
424
- * @return {DateTimeWithZoneOffset } the unpacked date time with zone offset value.
423
+ * @return {DateTime } the unpacked date time with zone offset value.
425
424
*/
426
425
function unpackDateTimeWithZoneOffset ( unpacker , structSize , buffer , disableLosslessIntegers ) {
427
426
unpacker . _verifyStructSize ( 'DateTimeWithZoneOffset' , DATE_TIME_WITH_ZONE_OFFSET_STRUCT_SIZE , structSize ) ;
428
427
429
428
const epochSecond = unpacker . unpackInteger ( buffer ) ;
430
429
const nano = unpacker . unpackInteger ( buffer ) ;
431
- const offsetSeconds = unpacker . unpackInteger ( buffer ) ;
430
+ const timeZoneOffsetSeconds = unpacker . unpackInteger ( buffer ) ;
432
431
433
432
const localDateTime = epochSecondAndNanoToLocalDateTime ( epochSecond , nano ) ;
434
- const result = new DateTimeWithZoneOffset ( localDateTime . year , localDateTime . month , localDateTime . day ,
435
- localDateTime . hour , localDateTime . minute , localDateTime . second , localDateTime . nanosecond , offsetSeconds ) ;
433
+ const result = new DateTime ( localDateTime . year , localDateTime . month , localDateTime . day ,
434
+ localDateTime . hour , localDateTime . minute , localDateTime . second , localDateTime . nanosecond , timeZoneOffsetSeconds , null ) ;
436
435
return convertIntegerPropsIfNeeded ( result , disableLosslessIntegers ) ;
437
436
}
438
437
439
438
/**
440
439
* Pack given date time with zone id.
441
- * @param {DateTimeWithZoneId } value the date time value to pack.
440
+ * @param {DateTime } value the date time value to pack.
442
441
* @param {Packer } packer the packer to use.
443
442
* @param {function } onError the error callback.
444
443
*/
445
444
function packDateTimeWithZoneId ( value , packer , onError ) {
446
445
const epochSecond = localDateTimeToEpochSecond ( value . year , value . month , value . day , value . hour , value . minute , value . second , value . nanosecond ) ;
447
446
const nano = int ( value . nanosecond ) ;
448
- const zoneId = value . zoneId ;
447
+ const timeZoneId = value . timeZoneId ;
449
448
450
449
const packableStructFields = [
451
450
packer . packable ( epochSecond , onError ) ,
452
451
packer . packable ( nano , onError ) ,
453
- packer . packable ( zoneId , onError )
452
+ packer . packable ( timeZoneId , onError )
454
453
] ;
455
454
packer . packStruct ( DATE_TIME_WITH_ZONE_ID , packableStructFields , onError ) ;
456
455
}
@@ -461,18 +460,18 @@ function packDateTimeWithZoneId(value, packer, onError) {
461
460
* @param {number } structSize the retrieved struct size.
462
461
* @param {BaseBuffer } buffer the buffer to unpack from.
463
462
* @param {boolean } disableLosslessIntegers if integer properties in the result date-time should be native JS numbers.
464
- * @return {DateTimeWithZoneId } the unpacked date time with zone id value.
463
+ * @return {DateTime } the unpacked date time with zone id value.
465
464
*/
466
465
function unpackDateTimeWithZoneId ( unpacker , structSize , buffer , disableLosslessIntegers ) {
467
466
unpacker . _verifyStructSize ( 'DateTimeWithZoneId' , DATE_TIME_WITH_ZONE_ID_STRUCT_SIZE , structSize ) ;
468
467
469
468
const epochSecond = unpacker . unpackInteger ( buffer ) ;
470
469
const nano = unpacker . unpackInteger ( buffer ) ;
471
- const zoneId = unpacker . unpack ( buffer ) ;
470
+ const timeZoneId = unpacker . unpack ( buffer ) ;
472
471
473
472
const localDateTime = epochSecondAndNanoToLocalDateTime ( epochSecond , nano ) ;
474
- const result = new DateTimeWithZoneId ( localDateTime . year , localDateTime . month , localDateTime . day ,
475
- localDateTime . hour , localDateTime . minute , localDateTime . second , localDateTime . nanosecond , zoneId ) ;
473
+ const result = new DateTime ( localDateTime . year , localDateTime . month , localDateTime . day ,
474
+ localDateTime . hour , localDateTime . minute , localDateTime . second , localDateTime . nanosecond , null , timeZoneId ) ;
476
475
return convertIntegerPropsIfNeeded ( result , disableLosslessIntegers ) ;
477
476
}
478
477
0 commit comments