Table of Contents

Class SqlIntervalUnits

Namespace
LinqToDB.Internal.SqlQuery
Assembly
linq2db.dll

Exact conversions between interval units and TimeSpan ticks.

public static class SqlIntervalUnits
Inheritance
object
SqlIntervalUnits

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

unit SqlIntervalUnit
other SqlIntervalUnit

Returns

bool

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

unit SqlIntervalUnit

Unit to convert from.

numerator long

Ratio numerator.

denominator long

Ratio 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

amount long

Amount in unit units.

unit SqlIntervalUnit

Unit amount is expressed in.

ticks long

Resulting tick count.

Returns

bool

false when the unit is a calendar unit, or when the result overflows long. A silent wrap here would be a wrong duration, so overflow is a failure, not a value.