MCPcopy Create free account
hub / github.com/Effect-TS/effect / timestampInt64

Function timestampInt64

packages/sql/pg/src/PgTypes.ts:1121–1155  ·  view source on GitHub ↗
(value: unknown)

Source from the content-addressed store, hash-verified

1119 * Writes the wire int64 to timestampHigh/Low, avoiding a pair allocation.
1120 */
1121const timestampInt64 = (value: unknown): void => {
1122 let ms: number
1123 if (value instanceof Date) {
1124 ms = value.getTime()
1125 if (Number.isNaN(ms)) fail("timestamp cannot be an invalid Date")
1126 } else {
1127 ms = typeof value === "number" ? value : fail("Expected a Date or number for timestamp")
1128 if (Number.isNaN(ms)) fail("timestamp cannot be NaN")
1129 }
1130 if (ms === Number.POSITIVE_INFINITY) {
1131 timestampHigh = INT32_MAX
1132 timestampLow = -1
1133 } else if (ms === Number.NEGATIVE_INFINITY) {
1134 timestampHigh = INT32_MIN
1135 timestampLow = 0
1136 } else {
1137 const unixMicros = Math.trunc(ms * 1000)
1138 if (!Number.isFinite(unixMicros)) {
1139 fail(`timestamp out of range: ${ms}`)
1140 }
1141 const micros = unixMicros - PG_EPOCH_MICROS
1142 if (micros > -MAX_EXACT && micros < MAX_EXACT) {
1143 timestampHigh = Math.floor(micros / 4294967296)
1144 timestampLow = micros - timestampHigh * 4294967296
1145 } else {
1146 const exact = BigInt(unixMicros) - BigInt(PG_EPOCH_MICROS)
1147 if (exact < INT64_MIN || exact > INT64_MAX) {
1148 fail(`timestamp out of range: ${ms}`)
1149 }
1150 scratchView8.setBigInt64(0, exact)
1151 timestampHigh = scratchView8.getInt32(0)
1152 timestampLow = scratchView8.getInt32(4)
1153 }
1154 }
1155}
1156
1157const timestampCodec: UnsafeCodec<any> = codecOf(
1158 (bytes, offset, size) => {

Callers 1

PgTypes.tsFile · 0.85

Calls 3

getTimeMethod · 0.80
failFunction · 0.70
BigIntInterface · 0.50

Tested by

no test coverage detected