@@ -12,6 +12,14 @@ import {
1212 PgoutputParser ,
1313} from "./pgoutput.js" ;
1414
15+ /**
16+ * Milliseconds between the Unix epoch (1970-01-01) and the Postgres epoch
17+ * (2000-01-01), used to convert between Postgres replication timestamps and
18+ * `Date.now()`. Mirrors `(POSTGRES_EPOCH_JDATE - UNIX_EPOCH_JDATE) * USECS_PER_DAY`
19+ * in pgoutput.ts (946684800000000 micros).
20+ */
21+ const POSTGRES_EPOCH_MS = 946684800000 ;
22+
1523export interface LogicalReplicationClientOptions {
1624 /**
1725 * The pg client config.
@@ -521,7 +529,7 @@ export class LogicalReplicationClient {
521529 } else if ( buffer [ 0 ] === 0x6b ) {
522530 // Primary keepalive message
523531 const timestamp = Math . floor (
524- buffer . readUInt32BE ( 9 ) * 4294967.296 + buffer . readUInt32BE ( 13 ) / 1000 + 946080000000
532+ buffer . readUInt32BE ( 9 ) * 4294967.296 + buffer . readUInt32BE ( 13 ) / 1000 + POSTGRES_EPOCH_MS
525533 ) ;
526534 const shouldRespond = ! ! buffer . readInt8 ( 17 ) ;
527535 this . events . emit ( "heartbeat" , { lsn, timestamp, shouldRespond } ) ;
@@ -833,7 +841,7 @@ export class LogicalReplicationClient {
833841 const slice = lsn . split ( "/" ) ;
834842 let [ upperWAL , lowerWAL ] : [ number , number ] = [ parseInt ( slice [ 0 ] , 16 ) , parseInt ( slice [ 1 ] , 16 ) ] ;
835843 // Timestamp as microseconds since midnight 2000-01-01
836- const now = Date . now ( ) - 946080000000 ;
844+ const now = Date . now ( ) - POSTGRES_EPOCH_MS ;
837845 const upperTimestamp = Math . floor ( now / 4294967.296 ) ;
838846 const lowerTimestamp = Math . floor ( now - upperTimestamp * 4294967.296 ) ;
839847 if ( lowerWAL === 4294967295 ) {
0 commit comments