11import { BufferType } from "./BufferType" ;
2- import { encodeUint32 , decodeUint32 , encodeInt32 , decodeInt32 , decodeInt64 , encodeInt64 } from "./utils/int" ;
2+ import { decodeInt64 , encodeInt64 } from "./utils/int" ;
33
44export const EXT_TIMESTAMP = - 1 ;
55
@@ -15,30 +15,33 @@ export type TimeSpec = {
1515const TIMESTAMP32_MAX_SEC = 0x100000000 ; // 32-bit signed int
1616const TIMESTAMP64_MAX_SEC = 0x400000000 ; // 34-bit unsigned int
1717
18- export function encodeTimestampFromTimeSpec ( { sec, nsec } : TimeSpec ) : ReadonlyArray < number > {
18+ export function encodeTimestampFromTimeSpec ( { sec, nsec } : TimeSpec ) : Uint8Array {
1919 if ( sec >= 0 && nsec >= 0 && sec < TIMESTAMP64_MAX_SEC ) {
2020 // Here sec >= 0 && nsec >= 0
2121 if ( nsec === 0 && sec < TIMESTAMP32_MAX_SEC ) {
2222 // timestamp 32 = { sec32 (unsigned) }
23- const rv : Array < number > = [ ] ;
24- encodeUint32 ( rv , sec ) ;
23+ const rv = new Uint8Array ( 4 ) ;
24+ const view = new DataView ( rv . buffer ) ;
25+ view . setUint32 ( 0 , sec ) ;
2526 return rv ;
2627 } else {
2728 // timestamp 64 = { nsec30 (unsigned), sec34 (unsigned) }
2829 const secHigh = sec / 0x100000000 ;
2930 const secLow = sec & 0xffffffff ;
30- const rv : Array < number > = [ ] ;
31+ const rv = new Uint8Array ( 8 ) ;
32+ const view = new DataView ( rv . buffer ) ;
3133 // nsec30 | secHigh2
32- encodeUint32 ( rv , ( nsec << 2 ) | ( secHigh & 0x3 ) ) ;
34+ view . setUint32 ( 0 , ( nsec << 2 ) | ( secHigh & 0x3 ) ) ;
3335 // secLow32
34- encodeUint32 ( rv , secLow ) ;
36+ view . setUint32 ( 4 , secLow ) ;
3537 return rv ;
3638 }
3739 } else {
3840 // timestamp 96 = { nsec32 (signed), sec64 (signed) }
39- const rv : Array < number > = [ ] ;
40- encodeInt32 ( rv , nsec ) ;
41- encodeInt64 ( rv , sec ) ;
41+ const rv = new Uint8Array ( 12 ) ;
42+ const view = new DataView ( rv . buffer ) ;
43+ view . setInt32 ( 0 , nsec ) ;
44+ encodeInt64 ( sec , view , 4 ) ;
4245 return rv ;
4346 }
4447}
@@ -60,20 +63,28 @@ export const decodeTimestampExtension: ExtensionDecoderType = (data: BufferType)
6063 switch ( data . length ) {
6164 case 4 : {
6265 // timestamp 32 = { sec32 }
63- const sec = decodeUint32 ( data [ 0 ] , data [ 1 ] , data [ 2 ] , data [ 3 ] ) ;
66+ const a = Uint8Array . from ( data ) ;
67+ const view = new DataView ( a . buffer ) ;
68+ const sec = view . getUint32 ( 0 ) ;
6469 return new Date ( sec * 1000 ) ;
6570 }
6671 case 8 : {
6772 // timestamp 64 = { nsec30, sec34 }
68- const nsec30AndSecHigh2 = decodeUint32 ( data [ 0 ] , data [ 1 ] , data [ 2 ] , data [ 3 ] ) ;
69- const secLow32 = decodeUint32 ( data [ 4 ] , data [ 5 ] , data [ 6 ] , data [ 7 ] ) ;
73+ const a = Uint8Array . from ( data ) ;
74+ const view = new DataView ( a . buffer ) ;
75+
76+ const nsec30AndSecHigh2 = view . getUint32 ( 0 ) ;
77+ const secLow32 = view . getUint32 ( 4 ) ;
7078 const nsec = nsec30AndSecHigh2 >>> 2 ;
7179 const sec = ( nsec30AndSecHigh2 & 0x3 ) * 0x100000000 + secLow32 ;
7280 return new Date ( sec * 1000 + nsec / 1e6 ) ;
7381 }
7482 case 12 : {
7583 // timestamp 96 = { nsec32 (signed), sec64 (signed) }
76- const nsec = decodeInt32 ( data [ 0 ] , data [ 1 ] , data [ 2 ] , data [ 3 ] ) ;
84+ const a = Uint8Array . from ( data ) ;
85+ const view = new DataView ( a . buffer ) ;
86+
87+ const nsec = view . getInt32 ( 0 )
7788 const sec = decodeInt64 ( data [ 4 ] , data [ 5 ] , data [ 6 ] , data [ 7 ] , data [ 8 ] , data [ 9 ] , data [ 10 ] , data [ 11 ] ) ;
7889
7990 return new Date ( sec * 1000 + nsec / 1e6 ) ;
0 commit comments