@@ -21,6 +21,12 @@ interface INTERVAL {
21
21
milliseconds : number ;
22
22
}
23
23
24
+ interface TIME {
25
+ value : string | bigint | number ;
26
+ unit : 'MILLIS' | 'MICROS' | 'NANOS' ;
27
+ isAdjustedToUTC : boolean ;
28
+ }
29
+
24
30
export function getParquetTypeDataObject (
25
31
type : ParquetType ,
26
32
field ?: ParquetField | Options | FieldDefinition
@@ -46,28 +52,26 @@ export function getParquetTypeDataObject(
46
52
toPrimitive : toPrimitive_INT64 ,
47
53
} ;
48
54
}
49
- } else if ( field ?. logicalType ?. TIME && ( type === 'INT64' || type === 'INT32' ) ) {
50
- const isAdjustedToUTC = field . logicalType . TIME . isAdjustedToUTC ;
55
+ } else if ( field ?. logicalType ?. TIME ) {
51
56
const unit = field . logicalType . TIME . unit ;
52
-
53
57
if ( unit . MILLIS ) {
54
58
return {
55
59
originalType : 'TIME_MILLIS' ,
56
60
primitiveType : 'INT32' ,
57
- toPrimitive : isAdjustedToUTC ? toPrimitive_TIME_MILLIS_UTC : toPrimitive_TIME_MILLIS_LOCAL ,
61
+ toPrimitive : toPrimitive_TIME ,
58
62
} ;
59
63
}
60
64
if ( unit . MICROS ) {
61
65
return {
62
66
originalType : 'TIME_MICROS' ,
63
67
primitiveType : 'INT64' ,
64
- toPrimitive : isAdjustedToUTC ? toPrimitive_TIME_MICROS_UTC : toPrimitive_TIME_MICROS_LOCAL ,
68
+ toPrimitive : toPrimitive_TIME ,
65
69
} ;
66
70
}
67
71
if ( unit . NANOS ) {
68
72
return {
69
73
primitiveType : 'INT64' ,
70
- toPrimitive : isAdjustedToUTC ? toPrimitive_TIME_NANOS_UTC : toPrimitive_TIME_NANOS_LOCAL ,
74
+ toPrimitive : toPrimitive_TIME ,
71
75
} ;
72
76
}
73
77
throw new Error ( 'TIME type must have a valid unit (MILLIS, MICROS, NANOS).' ) ;
@@ -585,63 +589,27 @@ function checkValidValue(lowerRange: number | bigint, upperRange: number | bigin
585
589
}
586
590
}
587
591
588
- /**
589
- * Convert a TIME value in MILLIS to its UTC representation.
590
- * @param value The time value.
591
- */
592
- function toPrimitive_TIME_MILLIS_UTC ( value : number | string ) : number {
593
- return typeof value === 'string' ? Number ( value ) : value ;
594
- }
595
-
596
- /**
597
- * Convert a TIME value in MILLIS to its local time representation.
598
- * @param value The time value.
599
- */
600
- function toPrimitive_TIME_MILLIS_LOCAL ( value : number | string ) : number {
601
- const millis = typeof value === 'string' ? Number ( value ) : value ;
602
- return Number ( adjustToLocalTimestamp ( BigInt ( millis ) , { MILLIS : true } ) ) ;
603
- }
604
-
605
- /**
606
- * Convert a TIME value in MICROS to its UTC representation.
607
- * @param value The time value.
608
- */
609
- function toPrimitive_TIME_MICROS_UTC ( value : bigint | string ) : bigint {
610
- return BigInt ( value ) ;
611
- }
612
-
613
- /**
614
- * Convert a TIME value in MICROS to its local time representation.
615
- * @param value The time value.
616
- */
617
- function toPrimitive_TIME_MICROS_LOCAL ( value : bigint | string ) : bigint {
618
- const micros = BigInt ( value ) ;
619
- return adjustToLocalTimestamp ( micros , { MICROS : true } ) ;
620
- }
592
+ function toPrimitive_TIME ( time : TIME ) : bigint | number {
593
+ const { value, unit, isAdjustedToUTC } = time ;
621
594
622
- /**
623
- * Convert a TIME value in NANOS to its UTC representation.
624
- * @param value The time value.
625
- */
626
- function toPrimitive_TIME_NANOS_UTC ( value : bigint | string ) : bigint {
627
- return BigInt ( value ) ;
628
- }
595
+ const timeValue = typeof value === 'string' ? BigInt ( value ) : BigInt ( value ) ;
629
596
630
- /**
631
- * Convert a TIME value in NANOS to its local time representation.
632
- * @param value The time value.
633
- */
634
- function toPrimitive_TIME_NANOS_LOCAL ( value : bigint | string ) : bigint {
635
- const nanos = BigInt ( value ) ;
636
- return adjustToLocalTimestamp ( nanos , { NANOS : true } ) ;
597
+ if ( isAdjustedToUTC ) {
598
+ return unit === 'MILLIS' ? Number ( timeValue ) : timeValue ;
599
+ } else {
600
+ switch ( unit ) {
601
+ case 'MILLIS' :
602
+ return Number ( adjustToLocalTimestamp ( timeValue , { MILLIS : true } ) ) ;
603
+ case 'MICROS' :
604
+ return adjustToLocalTimestamp ( timeValue , { MICROS : true } ) ;
605
+ case 'NANOS' :
606
+ return adjustToLocalTimestamp ( timeValue , { NANOS : true } ) ;
607
+ default :
608
+ throw new Error ( `Unsupported time unit: ${ unit } ` ) ;
609
+ }
610
+ }
637
611
}
638
612
639
- /**
640
- * Adjust the timestamp to local time based on the unit (MILLIS, MICROS, NANOS).
641
- * @param timestamp The timestamp to adjust.
642
- * @param unit The unit of the timestamp.
643
- * @returns The adjusted timestamp.
644
- */
645
613
function adjustToLocalTimestamp (
646
614
timestamp : bigint ,
647
615
unit : { MILLIS ?: boolean ; MICROS ?: boolean ; NANOS ?: boolean }
0 commit comments