TimestampFragment class for timestamp IDs.

Hierarchy

  • default
    • default

Constructors

  • Throws

    [TIMESTAMP_BITS_INVALID_RANGE] If bits is less than 38

    Throws

    [EPOCH_INVALID_TYPE] If epoch is not a number

    Throws

    [EPOCH_INVALID_RANGE] If epoch is not within 0 and Date.now()

    Parameters

    • bits: number

      The number of bits for the fragment.

    • epoch: number = DEFAULT_EPOCH

      A custom epoch timestamp.

      Defaults to 1420070400000 (2015-01-01 00:00:00) if omitted.

    Returns default

Properties

bitMask: bigint

The bit mask used to isolate the fragment's bits from the snowflake.

bitMask is equal to maxValue << bitShift

bitMaskHex: string

The hexadecimal representation of the bit mask as string.

bitShift: bigint

The number of bits for left and right shifting.

bitShift is equal to the number of bits on the right side

bits: number
epoch: bigint

Epoch timestamp used to calculate the fragment's timestamps.

identifier: string

The fragment identifier.

lastTimestamp: bigint

Value of the last timestamp.

maxValue: bigint

The maximum value that can be represented by this fragment.

maxValue is equal to 2 ** bits - 1

nanoTimeAnchor: bigint

Nanosecond time reference.

Remarks

Date.now() returns the number of milliseconds since the Unix epoch. hrtime.bigint() returns a high-resolution time value in nanoseconds, however it is relative to an arbitrary time in the past and not the Unix epoch.

If x hrtime was taken at y Unix timestamp, then when we recapture a hrttime at x' we can calculate the time with the following formula:

y + (x' - x) or (y - x) + x'

sequenceFragmentRef: default

Reference to the SequenceFragment instance. This is used to check for sequence collisions, and to reset the sequence when necessary.

timeUnit: bigint

Time unit used to convert the nanosecond timestamp to.

  • 1: nanosecond
  • 10 ** 3: microsecond
  • 10 ** 6: millisecond
value: bigint

The current value of the fragment.

Accessors

Methods

  • Internal

    Check for sequence collision.

    Remarks

    If a sequence completes its cycle, and the timestamp is still the same, an already generated snowflake will be re-generated.

    Returns void

  • Internal

    Parameters

    • bitsRightSide: number

    Returns void

  • Internal

    Returns a Unix timestamp.

    Remarks

    The number of the fragment's bits defines the time unit. This is done to avoid overflow when left shifting.

    Returns

    A Unix timestamp in the fragment's time unit.

    Returns bigint

Generated using TypeDoc