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>
Constructors
Section titled “Constructors”PgInterval(int, int, long)
Section titled “PgInterval(int, int, long)”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.
Methods
Section titled “Methods”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.
Add(PgInterval)
Section titled “Add(PgInterval)”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.
CompareInPostgres(PgInterval)
Section titled “CompareInPostgres(PgInterval)”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.
CompareTo(PgInterval)
Section titled “CompareTo(PgInterval)”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.
Divide(double)
Section titled “Divide(double)”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.
Equals(PgInterval)
Section titled “Equals(PgInterval)”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.
Equals(object)
Section titled “Equals(object)”public override bool Equals(object obj)Parameters:
obj — object
Returns: bool
Extract(PgDateTimePart)
Section titled “Extract(PgDateTimePart)”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.
FromDays(int)
Section titled “FromDays(int)”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.
FromHours(int)
Section titled “FromHours(int)”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.
FromMicroseconds(long)
Section titled “FromMicroseconds(long)”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.
FromMinutes(int)
Section titled “FromMinutes(int)”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.
FromMonths(int)
Section titled “FromMonths(int)”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.
FromSeconds(double)
Section titled “FromSeconds(double)”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.
FromTimeSpan(TimeSpan)
Section titled “FromTimeSpan(TimeSpan)”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:
- ArgumentException: The duration contains sub-microsecond ticks.
FromWeeks(int)
Section titled “FromWeeks(int)”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.
FromYears(int)
Section titled “FromYears(int)”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.
GetHashCode()
Section titled “GetHashCode()”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.
GetPart(PgDateTimePart)
Section titled “GetPart(PgDateTimePart)”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.
Justify()
Section titled “Justify()”Normalizes months, days, hours, and mixed component signs using PostgreSQL justify_interval.
public PgInterval Justify()Returns: PgInterval
The normalized interval.
JustifyDays()
Section titled “JustifyDays()”Normalizes thirty-day groups into months using PostgreSQL justify_days.
public PgInterval JustifyDays()Returns: PgInterval
The normalized interval.
JustifyHours()
Section titled “JustifyHours()”Normalizes twenty-four-hour groups into calendar days using PostgreSQL justify_hours.
public PgInterval JustifyHours()Returns: PgInterval
The normalized interval.
Multiply(double)
Section titled “Multiply(double)”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.
Negate()
Section titled “Negate()”Negates all interval components with PostgreSQL overflow and infinity handling.
public PgInterval Negate()Returns: PgInterval
The negated interval.
Parse(string)
Section titled “Parse(string)”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.
Subtract(PgInterval)
Section titled “Subtract(PgInterval)”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.
ToComparisonMicroseconds()
Section titled “ToComparisonMicroseconds()”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:
- InvalidOperationException: The interval is infinite.
ToIsoString()
Section titled “ToIsoString()”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.
ToPostgresString()
Section titled “ToPostgresString()”Formats the interval using the session’s IntervalStyle.
public string ToPostgresString()Returns: string
The PostgreSQL interval text. Use ToIsoString for session-independent text.
ToString()
Section titled “ToString()”public override string ToString()Returns: string
ToTime()
Section titled “ToTime()”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).
ToTimeSpan()
Section titled “ToTimeSpan()”Converts an interval containing only elapsed time. Calendar months or days require a reference timestamp.
public TimeSpan ToTimeSpan()Returns: TimeSpan
The fixed duration.
Exceptions:
- InvalidOperationException: The interval is infinite or contains calendar months or days.
- OverflowException: The microseconds exceed TimeSpan’s range.
Truncate(PgDateTimePart)
Section titled “Truncate(PgDateTimePart)”Truncates an interval to a PostgreSQL field.
public PgInterval Truncate(PgDateTimePart part)Parameters:
part — PgDateTimePart
The truncation field.
Returns: PgInterval
The truncated interval.
TryParse(string?, out PgInterval)
Section titled “TryParse(string?, out PgInterval)”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.
Properties
Section titled “Properties”Gets the calendar-day component, distinct from twenty-four elapsed hours across daylight-saving changes.
public int Days { get; }Value: int
IsFinite
Section titled “IsFinite”Gets whether this interval has finite components. Infinity is distinct from any finite component combination.
public bool IsFinite { get; }Value: bool
Microseconds
Section titled “Microseconds”Gets the time component in microseconds, which may exceed a day.
public long Microseconds { get; }Value: long
Months
Section titled “Months”Gets the month component, without assuming a fixed month length.
public int Months { get; }Value: int
NegativeInfinity
Section titled “NegativeInfinity”Gets negative infinity, supported by PostgreSQL 17 and later. Its finite component properties are zero.
public static PgInterval NegativeInfinity { get; }Value: PgInterval
PositiveInfinity
Section titled “PositiveInfinity”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
Operators
Section titled “Operators”operator +(PgInterval, PgInterval)
Section titled “operator +(PgInterval, PgInterval)”Adds interval components using PostgreSQL’s rules.
public static PgInterval operator +(PgInterval left, PgInterval right)Parameters:
left — PgInterval
right — PgInterval
Returns: PgInterval
operator /(PgInterval, double)
Section titled “operator /(PgInterval, double)”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
operator ==(PgInterval, PgInterval)
Section titled “operator ==(PgInterval, PgInterval)”public static bool operator ==(PgInterval left, PgInterval right)Parameters:
left — PgInterval
right — PgInterval
Returns: bool
operator >(PgInterval, PgInterval)
Section titled “operator >(PgInterval, PgInterval)”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
operator >=(PgInterval, PgInterval)
Section titled “operator >=(PgInterval, PgInterval)”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
operator !=(PgInterval, PgInterval)
Section titled “operator !=(PgInterval, PgInterval)”public static bool operator !=(PgInterval left, PgInterval right)Parameters:
left — PgInterval
right — PgInterval
Returns: bool
operator <(PgInterval, PgInterval)
Section titled “operator <(PgInterval, PgInterval)”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
operator <=(PgInterval, PgInterval)
Section titled “operator <=(PgInterval, PgInterval)”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
operator *(PgInterval, double)
Section titled “operator *(PgInterval, double)”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
operator *(double, PgInterval)
Section titled “operator *(double, 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
operator -(PgInterval, PgInterval)
Section titled “operator -(PgInterval, PgInterval)”Subtracts interval components using PostgreSQL’s rules.
public static PgInterval operator -(PgInterval left, PgInterval right)Parameters:
left — PgInterval
right — PgInterval
Returns: PgInterval
operator -(PgInterval)
Section titled “operator -(PgInterval)”Negates an interval using PostgreSQL’s rules.
public static PgInterval operator -(PgInterval value)Parameters:
value — PgInterval
Returns: PgInterval