@@ -40,12 +40,27 @@ import type { Codecs } from "./codecs";
4040 * which is specified in the below constant (in milliseconds.)
4141 */
4242const TIMESHIFT = 946684800000 ;
43+ const BI_TIMESHIFT = BigInt ( TIMESHIFT ) ;
44+ const BI_TIMESHIFT_US = BigInt ( TIMESHIFT ) * 1000n ;
45+
4346const DATESHIFT_ORD = ymd2ord ( 2000 , 1 , 1 ) ;
4447
4548export class DateTimeCodec extends ScalarCodec implements ICodec {
4649 override tsType = "Date" ;
4750
48- encode ( buf : WriteBuffer , object : unknown ) : void {
51+ encode ( buf : WriteBuffer , object : unknown , ctx : CodecContext ) : void {
52+ if ( ctx . hasOverload ( this ) ) {
53+ const val = ctx . preEncode < Codecs . DateTimeCodec > ( this , object ) ;
54+ if ( typeof val != 'bigint' ) {
55+ throw new InvalidArgumentError (
56+ `a bigint was expected out of a custom std::datetime codec`
57+ ) ;
58+ }
59+ buf . writeInt32 ( 8 ) ;
60+ buf . writeBigInt64 ( ( val - BI_TIMESHIFT ) * 1000n ) ;
61+ return ;
62+ }
63+
4964 if ( ! ( object instanceof Date ) ) {
5065 throw new InvalidArgumentError (
5166 `a Date instance was expected, got "${ object } "` ,
@@ -79,7 +94,20 @@ export class LocalDateTimeCodec extends ScalarCodec implements ICodec {
7994 override tsType = "LocalDateTime" ;
8095 override tsModule = "edgedb" ;
8196
82- encode ( buf : WriteBuffer , object : unknown ) : void {
97+ encode ( buf : WriteBuffer , object : unknown , ctx : CodecContext ) : void {
98+ if ( ctx . hasOverload ( this ) ) {
99+ let us = ctx . preEncode < Codecs . LocalDateTimeCodec > ( this , object ) ;
100+ if ( typeof us != 'bigint' ) {
101+ throw new InvalidArgumentError (
102+ `a bigint was expected out of a custom cal_local_datetime codec`
103+ ) ;
104+ }
105+ us -= BI_TIMESHIFT_US ;
106+ buf . writeInt32 ( 8 ) ;
107+ buf . writeBigInt64 ( us ) ;
108+ return ;
109+ }
110+
83111 if ( ! ( object instanceof LocalDateTime ) ) {
84112 throw new InvalidArgumentError (
85113 `a LocalDateTime instance was expected, got "${ object } "` ,
@@ -105,7 +133,7 @@ export class LocalDateTimeCodec extends ScalarCodec implements ICodec {
105133 }
106134
107135 buf . writeInt32 ( 8 ) ;
108- buf . writeBigInt64 ( us as bigint ) ;
136+ buf . writeBigInt64 ( us ) ;
109137 }
110138
111139 decode ( buf : ReadBuffer , ctx : CodecContext ) : LocalDateTime {
@@ -142,14 +170,24 @@ export class LocalDateTimeCodec extends ScalarCodec implements ICodec {
142170export class LocalDateCodec extends ScalarCodec implements ICodec {
143171 override tsType = "LocalDate" ;
144172 override tsModule = "edgedb" ;
145- encode ( buf : WriteBuffer , object : unknown ) : void {
173+ encode ( buf : WriteBuffer , object : unknown , ctx : CodecContext ) : void {
174+ if ( ctx . hasOverload ( this ) ) {
175+ const ret = ctx . preEncode < Codecs . LocalDateCodec > ( this , object ) ;
176+ const ord = ymd2ord ( ...ret ) ;
177+ buf . writeInt32 ( 4 ) ;
178+ buf . writeInt32 ( ord - DATESHIFT_ORD ) ;
179+ return ;
180+ }
181+
146182 if ( ! ( object instanceof LocalDate ) ) {
147183 throw new InvalidArgumentError (
148184 `a LocalDate instance was expected, got "${ object } "` ,
149185 ) ;
150186 }
187+
188+ const ord = LocalDateToOrdinal ( object ) ;
151189 buf . writeInt32 ( 4 ) ;
152- buf . writeInt32 ( LocalDateToOrdinal ( object ) - DATESHIFT_ORD ) ;
190+ buf . writeInt32 ( ord - DATESHIFT_ORD ) ;
153191 }
154192
155193 decode ( buf : ReadBuffer , ctx : CodecContext ) : LocalDate {
@@ -166,7 +204,19 @@ export class LocalDateCodec extends ScalarCodec implements ICodec {
166204export class LocalTimeCodec extends ScalarCodec implements ICodec {
167205 override tsType = "LocalTime" ;
168206 override tsModule = "edgedb" ;
169- encode ( buf : WriteBuffer , object : unknown ) : void {
207+ encode ( buf : WriteBuffer , object : unknown , ctx : CodecContext ) : void {
208+ if ( ctx . hasOverload ( this ) ) {
209+ const us = ctx . preEncode < Codecs . LocalTimeCodec > ( this , object ) ;
210+ if ( typeof us != 'bigint' ) {
211+ throw new InvalidArgumentError (
212+ `a bigint was expected out of a custom cal::local_time codec`
213+ ) ;
214+ }
215+ buf . writeInt32 ( 8 ) ;
216+ buf . writeBigInt64 ( us ) ;
217+ return ;
218+ }
219+
170220 if ( ! ( object instanceof LocalTime ) ) {
171221 throw new InvalidArgumentError (
172222 `a LocalTime instance was expected, got "${ object } "` ,
@@ -230,7 +280,22 @@ export function checkValidEdgeDBDuration(duration: Duration): null | string {
230280export class DurationCodec extends ScalarCodec implements ICodec {
231281 override tsType = "Duration" ;
232282 override tsModule = "edgedb" ;
233- encode ( buf : WriteBuffer , object : unknown ) : void {
283+ encode ( buf : WriteBuffer , object : unknown , ctx : CodecContext ) : void {
284+ if ( ctx . hasOverload ( this ) ) {
285+ const us = ctx . preEncode < Codecs . DurationCodec > ( this , object ) ;
286+ if ( typeof us != 'bigint' ) {
287+ throw new InvalidArgumentError (
288+ `a bigint was expected out of a custom std::duration codec`
289+ ) ;
290+ }
291+
292+ buf . writeInt32 ( 16 ) ;
293+ buf . writeBigInt64 ( us ) ;
294+ buf . writeInt32 ( 0 ) ;
295+ buf . writeInt32 ( 0 ) ;
296+ return ;
297+ }
298+
234299 if ( ! ( object instanceof Duration ) ) {
235300 throw new InvalidArgumentError (
236301 `a Duration instance was expected, got "${ object } "` ,
@@ -318,7 +383,16 @@ export class DurationCodec extends ScalarCodec implements ICodec {
318383export class RelativeDurationCodec extends ScalarCodec implements ICodec {
319384 override tsType = "RelativeDuration" ;
320385 override tsModule = "edgedb" ;
321- encode ( buf : WriteBuffer , object : unknown ) : void {
386+ encode ( buf : WriteBuffer , object : unknown , ctx : CodecContext ) : void {
387+ if ( ctx . hasOverload ( this ) ) {
388+ const ret = ctx . preEncode < Codecs . RelativeDurationCodec > ( this , object ) ;
389+ buf . writeInt32 ( 16 ) ;
390+ buf . writeBigInt64 ( ret [ 2 ] ) ;
391+ buf . writeInt32 ( ret [ 1 ] ) ;
392+ buf . writeInt32 ( ret [ 0 ] ) ;
393+ return ;
394+ }
395+
322396 if ( ! ( object instanceof RelativeDuration ) ) {
323397 throw new InvalidArgumentError ( `
324398 a RelativeDuration instance was expected, got "${ object } "
@@ -393,7 +467,16 @@ export class RelativeDurationCodec extends ScalarCodec implements ICodec {
393467export class DateDurationCodec extends ScalarCodec implements ICodec {
394468 override tsType = "DateDuration" ;
395469 override tsModule = "edgedb" ;
396- encode ( buf : WriteBuffer , object : unknown ) : void {
470+ encode ( buf : WriteBuffer , object : unknown , ctx : CodecContext ) : void {
471+ if ( ctx . hasOverload ( this ) ) {
472+ const ret = ctx . preEncode < Codecs . DateDurationCodec > ( this , object ) ;
473+ buf . writeInt32 ( 16 ) ;
474+ buf . writeInt64 ( 0 ) ;
475+ buf . writeInt32 ( ret [ 1 ] ) ;
476+ buf . writeInt32 ( ret [ 0 ] ) ;
477+ return ;
478+ }
479+
397480 if ( ! ( object instanceof DateDuration ) ) {
398481 throw new InvalidArgumentError ( `
399482 a DateDuration instance was expected, got "${ object } "
0 commit comments