Skip to content

Magic numbers: Timestamp conversion thresholds #206

@realfishsam

Description

@realfishsam

Category

Timestamp Conversion Thresholds (seconds ↔ milliseconds detection)

Locations

  • core/src/exchanges/probable/normalizer.ts:109raw.time < 1e12 ? raw.time * 1000 : raw.time (Trade)
  • core/src/exchanges/probable/normalizer.ts:136raw.time > 1e12 ? raw.time : raw.time * 1000 (UserTrade)
  • core/src/exchanges/probable/websocket.ts:103if (timestamp < 10000000000)
  • core/src/exchanges/kalshi/websocket.ts:406if (timestamp < 10000000000)
  • core/src/exchanges/kalshi/websocket.ts:412if (timestamp < 10000000000)
  • core/src/exchanges/polymarket/fetcher.ts:17d < 1e12 ? d * 1000 : d
  • core/src/exchanges/opinion/utils.ts:79ts < 10_000_000_000 ? ts * 1000 : ts
  • sdks/typescript/pmxt/server-manager.ts:304info.timestamp > 1e12 ? info.timestamp / 1000 : info.timestamp
  • sdks/python/pmxt/server_manager.py:207timestamp / 1000.0 if timestamp > 1e12 else float(timestamp)

Inconsistencies

Three different notations for the same value across the codebase:

Notation Files
1e12 probable/normalizer.ts, polymarket/fetcher.ts, server-manager.ts, server_manager.py
10000000000 kalshi/websocket.ts, probable/websocket.ts
10_000_000_000 opinion/utils.ts

Potential logic bug in probable/normalizer.ts: The Trade normalizer (line 109) uses < 1e12 while the UserTrade normalizer (line 136) uses > 1e12 as the condition for the same semantic check. Although both branches produce the same result, the inverted condition is error-prone — a future edit to one method risks introducing an actual divergence.

Risk

A wrong threshold or copy-paste of the inverted condition produces timestamps off by ×1000 (year 33658 vs. 2001), silently corrupting order books, trade history, and any downstream time-series logic. The three-way notation inconsistency makes a search-and-replace correction easy to miss.

Suggested Fix

Extract a single shared utility (already partially present in opinion/utils.ts):

// core/src/utils/timestamp.ts
export const MILLIS_THRESHOLD = 1e12; // ~year 2001 in ms; values below are assumed seconds
export function toMillis(ts: number): number {
  return ts < MILLIS_THRESHOLD ? ts * 1000 : ts;
}

Replace all 9 call-sites with toMillis(). The Python SDK should mirror with a shared helper.


Found by automated magic numbers audit

Metadata

Metadata

Assignees

No one assigned

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions