Snowflake.fromDateTime constructor

Snowflake.fromDateTime(
  1. DateTime dateTime
)

Create a snowflake with a timestamp equal to dateTime.

dateTime must be a DateTime which is at the same moment as or after epoch.

Snowflakes are generally unique across the API except in some cases where children share their parent's IDs.

{@template snowflake_ordering} Snowflakes are ordered first by their timestamp, then by workerId, processId and increment. The last three fields are only used internally by Discord so the only ordering visible through the API is by timestamp.

Implementation

factory Snowflake.fromDateTime(DateTime dateTime) {
  assert(
    dateTime.isAfter(epoch) || dateTime.isAtSameMomentAs(epoch),
    'Cannot create a Snowflake before the epoch.',
  );

  return Snowflake(dateTime.difference(epoch).inMilliseconds << 22);
}