torrust_tracker_clock

Module time_extent

source
Expand description

It includes functionality to handle time extents.

Time extents are used to represent a duration of time which contains N times intervals of the same duration.

Given a duration of: 60 seconds.

|------------------------------------------------------------|

If we define a base duration of 10 seconds, we would have 6 intervals.

|----------|----------|----------|----------|----------|----------|
           ^--- 10 seconds

Then, You can represent half of the duration (30 seconds) as:

|----------|----------|----------|----------|----------|----------|
                                 ^--- 30 seconds

3 times (multiplier) the base interval (3*10 = 30 seconds):

|----------|----------|----------|----------|----------|----------|
                                 ^--- 30 seconds (3 units of 10 seconds)

Time extents are a way to measure time duration using only one unit of time (base duration) repeated N times (multiplier).

Time extents are not clocks in a sense that they do not have a start time. They are not synchronized with the real time. In order to measure time, you need to define a start time for the intervals.

For example, we could measure time is “lustrums” (5 years) since the start of the 21st century. The time extent would contains a base 5-year duration and the multiplier. The current “lustrum” (2023) would be 5th one if we start counting “lustrums” at 1.

Lustrum 1: 2000-2004
Lustrum 2: 2005-2009
Lustrum 3: 2010-2014
Lustrum 4: 2015-2019
Lustrum 5: 2020-2024

More practically time extents are used to represent number of time intervals since the Unix Epoch. Each interval is typically an amount of seconds. It’s specially useful to check expiring dates. For example, you can have an authentication token that expires after 120 seconds. If you divide the current timestamp by 120 you get the number of 2-minute intervals since the Unix Epoch, you can hash that value with a secret key and send it to a client. The client can authenticate by sending the hashed value back to the server. The server can build the same hash and compare it with the one sent by the client. The hash would be the same during the 2-minute interval, but it would change after that. This method is one of the methods used by UDP trackers to generate and verify a connection ID, which a a token sent to the client to identify the connection.

Structs§

  • A TimeExtent maker which makes TimeExtents.
  • A TimeExtent is a duration of time which contains N times intervals of the same duration.

Constants§

  • The maximum value for a TimeExtent.
  • A zero time extent. It’s the additive identity for a TimeExtent.

Traits§

  • This trait defines the operations that can be performed on a TimeExtent.
  • A TimeExtent maker. It’s a clock base on time extents. It gives you the time in time extents.

Type Aliases§

  • The TimeExtent base Duration, which is the duration of a single interval.
  • The TimeExtent Multiplier, which is the number of Base duration intervals.
  • The TimeExtent product, which is the total duration of the TimeExtent.
  • A TimeExtent maker which makes TimeExtents from the Stopped clock.
  • A TimeExtent maker which makes TimeExtents from the Working clock.