GitHub

snowflake

API reference for snowflake.

Snowflake ids (Effect's Snowflake): 64-bit ids that sort by the millisecond they were made, timestamp - EPOCH_MILLIS in the top bits, then a 10-bit machine id and a 12-bit sequence.

Values

AttributeTypeDescription
Snowflaketype[Snowflake]
SnowflakeFromBigIntBrandSchema[Snowflake, int, Never, Never]

Snowflakes as integers.

SnowflakeFromStringBrandSchema[Snowflake, str, Never, Never]

Snowflakes encoded as their decimal digits.

EPOCH_MILLISLiteral[1735689600000] = 1735689600000

2025-01-01T00:00:00Z, the zero of a snowflake's timestamp.

layer_generatorlayer_.Layer[Generator, Never]

A snowflake Generator from make_generator: the current Clock and a random machine id until set_machine_id.

Classes

Functions

make

make(*, machine_id, sequence, timestamp) -> Snowflake

The snowflake made at timestamp (Unix milliseconds); the machine id is taken mod 1024 and the sequence mod 4096.

ParameterTypeDescription
machine_idMachineId
sequenceint
timestampint

Returns Snowflake

def make(*, machine_id: MachineId, sequence: int, timestamp: int) -> Snowflake:
    """The snowflake made at ``timestamp`` (Unix milliseconds); the machine
    id is taken mod 1024 and the sequence mod 4096."""
    return Snowflake((timestamp - EPOCH_MILLIS) << 22 | (machine_id % 1024) << 12 | sequence % 4096)

timestamp

timestamp(snowflake) -> int

The Unix milliseconds snowflake was made at.

ParameterTypeDescription
snowflakeSnowflake

Returns int

def timestamp(snowflake: Snowflake) -> int:
    """The Unix milliseconds ``snowflake`` was made at."""
    return (snowflake >> 22) + EPOCH_MILLIS

date_time

date_time(snowflake) -> datetime
ParameterTypeDescription
snowflakeSnowflake

Returns datetime

def date_time(snowflake: Snowflake) -> datetime:
    return from_epoch_millis(timestamp(snowflake))

machine_id

machine_id(snowflake) -> MachineId
ParameterTypeDescription
snowflakeSnowflake

Returns MachineId

def machine_id(snowflake: Snowflake) -> MachineId:
    return MachineId((snowflake >> 12) % 1024)

sequence

sequence(snowflake) -> int
ParameterTypeDescription
snowflakeSnowflake

Returns int

def sequence(snowflake: Snowflake) -> int:
    return snowflake % 4096

to_parts

to_parts(snowflake) -> Parts
ParameterTypeDescription
snowflakeSnowflake

Returns Parts

def to_parts(snowflake: Snowflake) -> Parts:
    return Parts(timestamp(snowflake), machine_id(snowflake), sequence(snowflake))

make_generator

make_generator() -> Effect[SnowflakeGenerator]

A generator on the current Clock, with a random machine id until set_machine_id.

Returns Effect[SnowflakeGenerator]

def make_generator() -> Effect[SnowflakeGenerator]:
    """A generator on the current ``Clock``, with a random machine id
    until ``set_machine_id``."""
    return Clock().map(lambda clock: SnowflakeGenerator(clock, MachineId(random.randrange(1024))))