ARCHIVED from builddistributedsystem.com on 2026-04-28 — URL: https://builddistributedsystem.com/tracks/identifier/tasks/task-2-2-2-timestamp-component
TASK

Implementation

The timestamp component of a Snowflake ID uses 41 bits to represent milliseconds since a custom epoch. Using Unix epoch (1970-01-01) wastes over 50 years of timestamp space. A custom epoch starting in 2024 gives the system ~69 years of IDs.

Your task is to:

  1. Implement timestamp generation relative to a custom epoch (2024-01-01 00:00:00 UTC)
  2. Validate that the timestamp fits within 41 bits
  3. Implement a time_info message that reports the current timestamp state
Request:  {"type": "time_info", "msg_id": 1}
Response: {"type": "time_info_ok", "in_reply_to": 1, 
           "current_ms": 1234567, 
           "custom_epoch_ms": 1704067200000,
           "max_timestamp_ms": 2199023255551,
           "years_remaining": 69}

Also implement generate that uses the custom epoch for timestamps, and verify IDs are monotonically increasing:

Request:  {"type": "generate", "msg_id": 1}
Response: {"type": "generate_ok", "in_reply_to": 1, "id": 1234567890}

Sample Test Cases

Generate produces a numeric IDTimeout: 5000ms
Input
{"src":"c0","dest":"n1","body":{"type":"init","msg_id":1,"node_id":"n1","node_ids":["n1"]}}
{"src":"c1","dest":"n1","body":{"type":"generate","msg_id":2}}
Expected Output
{"src": "n1", "dest": "c0", "body": {"type": "init_ok", "in_reply_to": 1, "msg_id": 0}}
Time info returns epoch and years remainingTimeout: 5000ms
Input
{"src":"c0","dest":"n1","body":{"type":"init","msg_id":1,"node_id":"n1","node_ids":["n1"]}}
{"src":"c1","dest":"n1","body":{"type":"time_info","msg_id":2}}
Expected Output
{"src": "n1", "dest": "c0", "body": {"type": "init_ok", "in_reply_to": 1, "msg_id": 0}}

Hints

Hint 1
A custom epoch starting in 2024 gives you ~69 years before 41 bits overflow
Hint 2
Unix epoch (1970) would waste 54 years of timestamp space
Hint 3
Use time.time() * 1000 to get current timestamp in milliseconds
Hint 4
Subtract the custom epoch to get relative milliseconds
Hint 5
Check that the timestamp fits in 41 bits before composing the ID
OVERVIEW

Theoretical Hub

Concept overview coming soon

Key Concepts

timestampepochtime representationoverflow planning
main.py
python
Implement Timestamp Component with Custom Epoch - The Identifier | Build Distributed Systems