Skip to content

Commit

Permalink
SImplify xcurrenttime
Browse files Browse the repository at this point in the history
  • Loading branch information
mugikhan committed Dec 2, 2024
1 parent 94e4b06 commit 7fd92da
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 21 deletions.
2 changes: 1 addition & 1 deletion dist/mc-wa-sqlite-async.mjs

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/wa-sqlite-async-dynamic-main.mjs

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/wa-sqlite-async.mjs

Large diffs are not rendered by default.

32 changes: 14 additions & 18 deletions src/FacadeVFS.js
Original file line number Diff line number Diff line change
Expand Up @@ -233,29 +233,25 @@ export class FacadeVFS extends VFS.Base {
return nByte;
}

/**
/**
* Gets the current time as milliseconds since Unix epoch
* @param {number} pVfs pointer to the VFS
* @param {number} pTime pointer to write the time value
* @returns {number} SQLite error code
*/
xCurrentTimeInt64(pVfs, pTime) {
// Create a DataView to write the current time
const timeView = this.#makeTypedDataView('BigInt64', pTime);

// Exact copy of SQLite's calculation:
// static const sqlite3_int64 unixEpoch = 24405875*(sqlite3_int64)8640000;
// *piNow = unixEpoch + 1000*(sqlite3_int64)sNow.tv_sec + sNow.tv_usec/1000;
const seconds = BigInt(Math.floor(Date.now() / 1000));
const microseconds = BigInt(Date.now() % 1000) * 1000n;

const value = UNIX_EPOCH + (1000n * seconds) + (microseconds / 1000n);

// Write the time value to the pointer location
timeView.setBigInt64(0, value, true);

return VFS.SQLITE_OK;
}
xCurrentTimeInt64(pVfs, pTime) {
// Create a DataView to write the current time
const timeView = this.#makeTypedDataView('BigInt64', pTime);

const currentTime = BigInt(Date.now());
// Convert the current time to milliseconds since Unix epoch
const value = UNIX_EPOCH + currentTime;

// Write the time value to the pointer location
timeView.setBigInt64(0, value, true);

return VFS.SQLITE_OK;
}

/**
* @param {number} pVfs
Expand Down

0 comments on commit 7fd92da

Please sign in to comment.