Skip to content

PgInterval

Namespace: Ankus

Assembly: Ankus.Runtime.dll

Preserves PostgreSQL interval’s independent month, day, and microsecond components, including mixed signs. Equality and ordering use PostgreSQL’s thirty-day-month comparison convention without changing stored components. JSON uses the session-independent ISO 8601 form returned by ToIsoString.

[JsonConverter(typeof(PgIntervalConverter))]
public readonly struct PgInterval : IComparable<PgInterval>, IParsable<PgInterval>, IEquatable<PgInterval>

Implements: IComparable<PgInterval>, IParsable<PgInterval>, IEquatable<PgInterval>

Creates an interval without normalizing months to days or days to hours.

public PgInterval(int months, int days, long microseconds)

Parameters:

months — int

The signed month component.

days — int

The signed day component.

microseconds — long

The signed time component.

Gets the absolute value under PostgreSQL’s interval comparison, consistent with Sign, CompareTo, and equality. No backend is required.

public PgInterval Abs()

Returns: PgInterval

This interval with unchanged components when its Sign is zero or positive, otherwise its negation. Both infinities return positive infinity.

Exceptions:

  • OverflowException: A negative interval has a component equal to its signed minimum value.

A negative interval negates every component together, preserving their separation and mixed signs. One month minus thirty-one days therefore becomes minus one month plus thirty-one days, which compares as one day. The result never sorts below zero, and equivalent intervals have equivalent absolute values.

Adds interval components using PostgreSQL’s overflow and infinity rules.

public PgInterval Add(PgInterval other)

Parameters:

other — PgInterval

The interval to add.

Returns: PgInterval

The resulting interval.

Compares with PostgreSQL’s thirty-day-month convention, consistent with managed equality.

public int CompareInPostgres(PgInterval other)

Parameters:

other — PgInterval

The interval to compare.

Returns: int

A negative value, zero, or a positive value when this interval sorts before, equals, or sorts after the other.

Compares intervals using PostgreSQL’s thirty-day months and twenty-four-hour days without requiring a backend. Infinity sorts outside every finite interval. Distinct stored components can compare as equal.

public int CompareTo(PgInterval other)

Parameters:

other — PgInterval

The interval to compare.

Returns: int

A negative value, zero, or a positive value for a smaller, equivalent, or larger comparison duration.

Create(int, int, int, int, int, int, double)

Section titled “Create(int, int, int, int, int, int, double)”

Constructs an interval with PostgreSQL’s make_interval rules, allowing mixed component signs.

public static PgInterval Create(int years = 0, int months = 0, int weeks = 0, int days = 0, int hours = 0, int minutes = 0, double seconds = 0)

Parameters:

years — int

Calendar years.

months — int

Additional calendar months.

weeks — int

Calendar weeks.

days — int

Additional calendar days.

hours — int

Elapsed hours.

minutes — int

Elapsed minutes.

seconds — double

Elapsed fractional seconds, rounded by PostgreSQL.

Returns: PgInterval

The interval, retaining calendar and elapsed-time components.

Component arithmetic and conversion to microseconds reject overflow on every supported PostgreSQL version. Fractional seconds use PostgreSQL’s native rounding rules.

Divides the interval with PostgreSQL’s fractional-month and fractional-day rules.

public PgInterval Divide(double divisor)

Parameters:

divisor — double

The divisor.

Returns: PgInterval

The divided interval.

Tests equality using PostgreSQL’s interval comparison, preserving the original calendar components.

public bool Equals(PgInterval other)

Parameters:

other — PgInterval

The interval to compare.

Returns: bool

Whether both intervals have the same comparison duration or the same infinity.

public override bool Equals(object obj)

Parameters:

obj — object

Returns: bool

Extracts a numeric field exactly on PostgreSQL 14+; PostgreSQL 13 converts its floating-point result.

public PgNumeric? Extract(PgDateTimePart part)

Parameters:

part — PgDateTimePart

The field to extract.

Returns: PgNumeric?

The numeric field, or null for an undefined field of infinity.

Constructs calendar days without requiring backend access.

public static PgInterval FromDays(int days)

Parameters:

days — int

The signed number of days.

Returns: PgInterval

The interval.

Constructs elapsed hours using PostgreSQL’s make_interval rules.

public static PgInterval FromHours(int hours)

Parameters:

hours — int

The signed number of hours.

Returns: PgInterval

The interval.

Constructs exact elapsed microseconds without a floating-point intermediary or backend access.

public static PgInterval FromMicroseconds(long microseconds)

Parameters:

microseconds — long

The signed microseconds.

Returns: PgInterval

The interval.

Constructs elapsed minutes using PostgreSQL’s make_interval rules.

public static PgInterval FromMinutes(int minutes)

Parameters:

minutes — int

The signed number of minutes.

Returns: PgInterval

The interval.

Constructs calendar months without requiring backend access.

public static PgInterval FromMonths(int months)

Parameters:

months — int

The signed number of months.

Returns: PgInterval

The interval.

Constructs elapsed seconds using PostgreSQL’s fractional rounding and range rules.

public static PgInterval FromSeconds(double seconds)

Parameters:

seconds — double

The signed fractional seconds.

Returns: PgInterval

The interval.

Converts a fixed duration to elapsed microseconds without adding calendar-day semantics.

public static PgInterval FromTimeSpan(TimeSpan value)

Parameters:

value — TimeSpan

The elapsed duration at whole-microsecond precision.

Returns: PgInterval

An interval with zero months and days.

Exceptions:

Constructs calendar weeks with PostgreSQL overflow checks.

public static PgInterval FromWeeks(int weeks)

Parameters:

weeks — int

The signed number of weeks.

Returns: PgInterval

The interval.

Constructs calendar years with PostgreSQL overflow checks.

public static PgInterval FromYears(int years)

Parameters:

years — int

The signed number of years.

Returns: PgInterval

The interval.

Hashes PostgreSQL’s comparison duration so equivalent intervals have equal managed hashes.

public override int GetHashCode()

Returns: int

A managed hash consistent with equality, without requiring backend access.

Reads a floating-point field using PostgreSQL date_part semantics.

public double? GetPart(PgDateTimePart part)

Parameters:

part — PgDateTimePart

The field.

Returns: double?

The field, or null for an undefined field of an infinite value.

Normalizes months, days, hours, and mixed component signs using PostgreSQL justify_interval.

public PgInterval Justify()

Returns: PgInterval

The normalized interval.

Normalizes thirty-day groups into months using PostgreSQL justify_days.

public PgInterval JustifyDays()

Returns: PgInterval

The normalized interval.

Normalizes twenty-four-hour groups into calendar days using PostgreSQL justify_hours.

public PgInterval JustifyHours()

Returns: PgInterval

The normalized interval.

Scales the interval with PostgreSQL’s fractional-month and fractional-day rules.

public PgInterval Multiply(double factor)

Parameters:

factor — double

The multiplier.

Returns: PgInterval

The scaled interval.

Negates all interval components with PostgreSQL overflow and infinity handling.

public PgInterval Negate()

Returns: PgInterval

The negated interval.

Parses PostgreSQL interval syntax on the active backend thread.

public static PgInterval Parse(string text)

Parameters:

text — string

The interval text.

Returns: PgInterval

The interval.

Subtracts interval components using PostgreSQL’s overflow and infinity rules.

public PgInterval Subtract(PgInterval other)

Parameters:

other — PgInterval

The interval to subtract.

Returns: PgInterval

The resulting interval.

Converts finite components to PostgreSQL’s comparison approximation of thirty days per month.

public Int128 ToComparisonMicroseconds()

Returns: Int128

The comparison value, which is not a calendar-aware elapsed duration.

Exceptions:

Formats the interval as PostgreSQL’s ISO 8601 duration, independently of the session’s IntervalStyle. No backend is required.

public string ToIsoString()

Returns: string

The text PostgreSQL produces with IntervalStyle iso_8601, such as P1Y2M-3DT4H5M6.5S or PT0S, or infinity and -infinity for the PostgreSQL 17 infinities.

Years and months share the sign of the month component, and hours, minutes, and seconds share the sign of the microsecond component, so mixed signs and every component remain exact. PostgreSQL interval input reads this form identically under every IntervalStyle. PostgreSQL 13 and 14 reject ISO 8601 hour fields beyond the signed 32-bit range; Ankus JSON reads this form without the backend.

Formats the interval using the session’s IntervalStyle.

public string ToPostgresString()

Returns: string

The PostgreSQL interval text. Use ToIsoString for session-independent text.

public override string ToString()

Returns: string

Gets the time-of-day part of the interval’s time component, wrapped into one day, as PostgreSQL’s interval::time cast does. Months and days are ignored.

public PgTime ToTime()

Returns: PgTime

The time of day.

Exceptions:

  • PgException: The interval is infinite (SQLSTATE 22008).

Converts an interval containing only elapsed time. Calendar months or days require a reference timestamp.

public TimeSpan ToTimeSpan()

Returns: TimeSpan

The fixed duration.

Exceptions:

Truncates an interval to a PostgreSQL field.

public PgInterval Truncate(PgDateTimePart part)

Parameters:

part — PgDateTimePart

The truncation field.

Returns: PgInterval

The truncated interval.

Tries to parse PostgreSQL interval syntax on the active backend thread.

public static bool TryParse(string? text, out PgInterval value)

Parameters:

text — string

The interval text.

value — PgInterval

The parsed interval, or the default value on invalid input.

Returns: bool

Whether the input is valid. Backend-access and operational errors still throw.

Invalid input leaves no error pending: PostgreSQL 16 and later report it as a soft input error, and earlier majors roll back an internal subtransaction. Before PostgreSQL 16, transaction callbacks and parallel operations forbid that rollback; native errors there remain pending and throw PgException.

Gets the calendar-day component, distinct from twenty-four elapsed hours across daylight-saving changes.

public int Days { get; }

Value: int

Gets whether this interval has finite components. Infinity is distinct from any finite component combination.

public bool IsFinite { get; }

Value: bool

Gets the time component in microseconds, which may exceed a day.

public long Microseconds { get; }

Value: long

Gets the month component, without assuming a fixed month length.

public int Months { get; }

Value: int

Gets negative infinity, supported by PostgreSQL 17 and later. Its finite component properties are zero.

public static PgInterval NegativeInfinity { get; }

Value: PgInterval

Gets positive infinity, supported by PostgreSQL 17 and later. Its finite component properties are zero.

public static PgInterval PositiveInfinity { get; }

Value: PgInterval

Gets -1, 0, or 1 by comparing with zero under PostgreSQL’s thirty-day-month comparison, including infinities. Mixed-sign components contribute to one comparison duration. No backend is required.

public int Sign { get; }

Value: int

Adds interval components using PostgreSQL’s rules.

public static PgInterval operator +(PgInterval left, PgInterval right)

Parameters:

left — PgInterval

right — PgInterval

Returns: PgInterval

Divides an interval using PostgreSQL’s fractional-month/day rules.

public static PgInterval operator /(PgInterval interval, double divisor)

Parameters:

interval — PgInterval

divisor — double

Returns: PgInterval

public static bool operator ==(PgInterval left, PgInterval right)

Parameters:

left — PgInterval

right — PgInterval

Returns: bool

Tests whether the left interval sorts after the right interval using PostgreSQL’s comparison approximation.

public static bool operator >(PgInterval left, PgInterval right)

Parameters:

left — PgInterval

right — PgInterval

Returns: bool

Tests whether the left interval sorts after or compares as equivalent to the right interval.

public static bool operator >=(PgInterval left, PgInterval right)

Parameters:

left — PgInterval

right — PgInterval

Returns: bool

public static bool operator !=(PgInterval left, PgInterval right)

Parameters:

left — PgInterval

right — PgInterval

Returns: bool

Tests whether the left interval sorts before the right interval using PostgreSQL’s comparison approximation.

public static bool operator <(PgInterval left, PgInterval right)

Parameters:

left — PgInterval

right — PgInterval

Returns: bool

Tests whether the left interval sorts before or compares as equivalent to the right interval.

public static bool operator <=(PgInterval left, PgInterval right)

Parameters:

left — PgInterval

right — PgInterval

Returns: bool

Scales an interval using PostgreSQL’s fractional-month/day rules.

public static PgInterval operator *(PgInterval interval, double factor)

Parameters:

interval — PgInterval

factor — double

Returns: PgInterval

Scales an interval using PostgreSQL’s fractional-month/day rules.

public static PgInterval operator *(double factor, PgInterval interval)

Parameters:

factor — double

interval — PgInterval

Returns: PgInterval

Subtracts interval components using PostgreSQL’s rules.

public static PgInterval operator -(PgInterval left, PgInterval right)

Parameters:

left — PgInterval

right — PgInterval

Returns: PgInterval

Negates an interval using PostgreSQL’s rules.

public static PgInterval operator -(PgInterval value)

Parameters:

value — PgInterval

Returns: PgInterval