Class SqlIntervalUnits
Exact conversions between interval units and TimeSpan ticks.
public static class SqlIntervalUnits
- Inheritance
-
objectSqlIntervalUnits
Remarks
The ratio is kept as a pair of integers rather than a single factor because nanoseconds are finer than a tick - one tick is 100 nanoseconds - so no integral "ticks per unit" exists for them. Keeping it rational lets every conversion stay in exact integer arithmetic; a floating factor would quietly lose the low digits of a large tick count.
Methods
IsFinerThan(SqlIntervalUnit, SqlIntervalUnit)
Whether unit is smaller than other.
public static bool IsFinerThan(SqlIntervalUnit unit, SqlIntervalUnit other)
Parameters
unitSqlIntervalUnitotherSqlIntervalUnit
Returns
Remarks
Compared through the tick ratios rather than the enum's declaration order, so the answer stays right if a member is ever added out of order. Calendar units have no fixed tick count and are not comparable this way - the method reports false for them rather than guessing.
TryGetTicksRatio(SqlIntervalUnit, out long, out long)
Gets the exact ratio converting an amount in unit to ticks:
ticks = amount * numerator / denominator.
public static bool TryGetTicksRatio(SqlIntervalUnit unit, out long numerator, out long denominator)
Parameters
unitSqlIntervalUnitUnit to convert from.
numeratorlongRatio numerator.
denominatorlongRatio denominator, always positive.
Returns
- bool
false for calendar units, whose elapsed length depends on the point in time they are applied to and therefore has no fixed tick count. Callers must reject rather than approximate those.
TryToTicks(long, SqlIntervalUnit, out long)
Converts an amount expressed in unit to ticks, exactly.
public static bool TryToTicks(long amount, SqlIntervalUnit unit, out long ticks)
Parameters
amountlongAmount in
unitunits.unitSqlIntervalUnitUnit
amountis expressed in.tickslongResulting tick count.