Function makeNormalizeTimestampFn

  • Creates a function to normalize unsigned counter timestamps relative to an initial value, correctly handling wrap-around for counters with any bit width from 1 to 32 bits.

    This is useful when timestamps from a hardware or software counter may wrap around, and you want strictly increasing normalized timestamps starting from zero.

    Returns

    A function that takes a timestamp and returns a normalized, strictly increasing timestamp.

    Example

    // 8-bit counter
    const normalize8 = makeNormalizeTimestampFn(100, 8);
    console.log(normalize8(100)); // 0
    console.log(normalize8(255)); // 155
    console.log(normalize8(3)); // 159

    Example

    // 16-bit counter
    const normalize16 = makeNormalizeTimestampFn(1000, 16);
    console.log(normalize16(1000)); // 0
    console.log(normalize16(65535)); // 64535
    console.log(normalize16(10)); // 65546

    Example

    // 32-bit counter (microseconds)
    const normalize32 = makeNormalizeTimestampFn(100, 32);
    console.log(normalize32(100)); // 0
    console.log(normalize32(4294967295)); // 4294967195
    console.log(normalize32(3)); // 4294967299

    Throws

    If bits is not in the range 1..32

    Parameters

    • initial: number

      The first timestamp received. This value will normalize to 0.

    • bits: number

      The bit width of the counter (1..32). Determines the maximum value before wrap-around.

    Returns ((timestamp: number) => number)

      • (timestamp: number): number
      • Parameters

        • timestamp: number

        Returns number

Generated using TypeDoc