Time

uint64_t cardano_compute_slot_from_unix_time(cardano_network_magic_t magic, uint64_t unix_time)

Computes the Cardano network slot for a given Unix time.

This function calculates the slot number on the Cardano network corresponding to a specified Unix timestamp. Since slot duration may vary across different networks and over time, this computation requires both the network magic and the Unix time.

Usage Example:

cardano_network_magic_t magic = CARDANO_NETWORK_MAGIC_MAINNET; // Obtain network magic
uint64_t unix_time = 1700000000; // Example Unix timestamp
uint64_t slot = cardano_compute_slot_from_unix_time(magic, unix_time);

printf("Computed slot: %llu\n", (unsigned long long)slot);

Note

Slot duration and epoch boundaries may vary across networks and can change over time. This function takes these network-specific configurations into account.

Parameters:
cardano_network_magic_t magic

[in] The network magic (cardano_network_magic_t) identifying the specific Cardano network.

uint64_t unix_time

[in] The Unix timestamp in seconds for which to compute the slot.

Returns:

The computed slot number as a uint64_t, representing the slot at the specified Unix time.


uint64_t cardano_compute_unix_time_from_slot(cardano_network_magic_t magic, uint64_t slot)

Computes the Unix time corresponding to a given Cardano network slot.

This function calculates the Unix timestamp for a specified slot number on the Cardano network. Slot-to-time mapping depends on the network’s specific slot duration and other time-related parameters that may vary across different Cardano networks and epochs.

Usage Example:

cardano_network_magic_t magic = ...; // Obtain network magic
uint64_t slot = 500000; // Example slot number
uint64_t unix_time = cardano_compute_unix_time_from_slot(magic, slot);

printf("Computed Unix time: %llu\n", (unsigned long long)unix_time);

Note

The conversion relies on the network’s configuration, as different networks and epochs may have varying slot durations, affecting the calculated Unix time.

Parameters:
cardano_network_magic_t magic

[in] The network magic (cardano_network_magic_t) identifying the specific Cardano network.

uint64_t slot

[in] The slot number for which to compute the Unix time.

Returns:

The computed Unix timestamp in seconds as a uint64_t, representing the time at the specified slot.


uint64_t unix_time_to_enclosing_slot(uint64_t unix_time, const cardano_slot_config_t *config)

Converts a Unix timestamp (in milliseconds) to the enclosing slot number.

This function calculates the slot that contains the specified Unix time using the linear relation defined by cardano_slot_config_t. The operation determines how many full slot intervals have elapsed since zero_time, and returns the slot at or immediately before the provided timestamp.

The formula used is:

slot = config->zero_slot
     + (unix_time - config->zero_time) / config->slot_length;

Note

This function uses truncation toward zero to determine the enclosing slot. It assumes a fixed slot length and does not account for historical chain eras with variable slot durations.

Parameters:
uint64_t unix_time

[in] The Unix timestamp in milliseconds for which the corresponding slot should be determined.

const cardano_slot_config_t *config

[in] Pointer to a valid cardano_slot_config_t supplying slot timing rules. Must not be NULL.

Returns:

The slot number that encloses the given timestamp. If config is NULL, the returned value is unspecified.


uint64_t slot_to_begin_unix_time(uint64_t slot, const cardano_slot_config_t *config)

Computes the Unix start time (in milliseconds) of a given slot.

This function converts a slot number into the Unix timestamp (milliseconds) representing the exact beginning of that slot. The calculation is performed using the linear slot progression model defined by the supplied cardano_slot_config_t structure, which specifies:

  • zero_time — the Unix time (ms) corresponding to zero_slot

  • zero_slot — the reference slot number

  • slot_length — duration of a single slot in milliseconds

The formula used is:

unix_time = config->zero_time
          + (slot - config->zero_slot) * config->slot_length;

Note

This function assumes constant slot duration and does not model historical hard-fork boundaries or era transitions where slot lengths changed.

Parameters:
uint64_t slot

[in] The absolute slot number whose starting Unix time should be computed.

const cardano_slot_config_t *config

[in] Pointer to a valid cardano_slot_config_t providing slot timing rules. Must not be NULL.

Returns:

The Unix start time of the given slot, expressed in milliseconds since the Unix epoch. If config is NULL, the returned value is unspecified.