Conversions for Tempo's sub-second component.
Tempo represents sub-second resolution with a :microsecond time
component carrying the same {value, precision} shape used by
Elixir's Time, NaiveDateTime, and DateTime structs:
valueis an absolute microsecond count in the range0..999_999.precisionis the number of significant fractional digits, in the range0..6. It records the declared resolution and therefore the width of the implied interval —{120_000, 2}is the centisecond interval[.12, .13)whereas{120_000, 3}is the millisecond interval[.120, .121).
Because value is always normalised to microseconds, ordering two
sub-second parts is a direct integer comparison of their values;
precision plays no role in comparison, only in interval width and
formatting.
The maximum precision is 6 (microsecond), matching Elixir. Fractional input carrying more than 6 digits is truncated to microsecond resolution.
Summary
Types
A sub-second component: {microsecond_value, precision} where
microsecond_value is 0..999_999 and precision is 0..6.
Functions
Convert parsed fractional-second digits into a {value, precision}
microsecond component.
Render a microsecond component as its fractional-digit string,
zero-padded to precision digits (no leading decimal separator).
Return true when value is a well-formed microsecond component.
Types
Functions
@spec from_fraction(non_neg_integer(), non_neg_integer()) :: t()
Convert parsed fractional-second digits into a {value, precision}
microsecond component.
Arguments
fraction_integeris the fractional digits parsed as an integer (for example the digits after the decimal separator in45.0123parse to123).digit_countis the number of fractional digits as written, including leading zeros (for45.0123it is4). The digit count, not the integer, determines precision — leading zeros are significant.
Returns
- A
t/0tuple{microsecond_value, precision}. Whendigit_countexceeds 6, the value is truncated to microsecond resolution andprecisionis capped at 6.
Examples
iex> Tempo.Microsecond.from_fraction(123, 3)
{123000, 3}
iex> Tempo.Microsecond.from_fraction(123, 4)
{12300, 4}
iex> Tempo.Microsecond.from_fraction(123456, 6)
{123456, 6}
iex> Tempo.Microsecond.from_fraction(1234567, 7)
{123456, 6}
Render a microsecond component as its fractional-digit string,
zero-padded to precision digits (no leading decimal separator).
Arguments
microsecondis at/0tuple{value, precision}.
Returns
- A string of exactly
precisiondigits, or the empty string whenprecisionis0.
Examples
iex> Tempo.Microsecond.to_digits_string({123000, 3})
"123"
iex> Tempo.Microsecond.to_digits_string({120000, 3})
"120"
iex> Tempo.Microsecond.to_digits_string({123, 6})
"000123"
iex> Tempo.Microsecond.to_digits_string({0, 0})
""
Return true when value is a well-formed microsecond component.
Arguments
valueis any term.
Returns
trueifvalueis a{microsecond_value, precision}tuple withmicrosecond_valuein0..999_999andprecisionin0..6;falseotherwise.
Examples
iex> Tempo.Microsecond.valid?({123000, 3})
true
iex> Tempo.Microsecond.valid?({1_000_000, 6})
false
iex> Tempo.Microsecond.valid?({100, 7})
false