PgDate
Namespace: Ankus
Assembly: Ankus.Runtime.dll
Represents PostgreSQL date, including BC dates, its full finite range, and both infinities. The default value is 2000-01-01.
[JsonConverter(typeof(PgDateConverter))]public readonly struct PgDate : IComparable<PgDate>, IParsable<PgDate>, IEquatable<PgDate>Implements: IComparable<PgDate>, IParsable<PgDate>, IEquatable<PgDate>
Constructors
Section titled “Constructors”PgDate(int)
Section titled “PgDate(int)”Creates a date from days relative to 2000-01-01. Int32 extremes represent infinities.
public PgDate(int daysSinceEpoch)Parameters:
daysSinceEpoch — int
The signed PostgreSQL day offset.
Exceptions:
- ArgumentOutOfRangeException: The finite offset is outside PostgreSQL’s date range.
Methods
Section titled “Methods”Add(PgInterval)
Section titled “Add(PgInterval)”Adds a calendar interval, yielding a timestamp.
public PgTimestamp Add(PgInterval interval)Parameters:
interval — PgInterval
The interval.
Returns: PgTimestamp
The resulting timestamp.
AddDays(int)
Section titled “AddDays(int)”Adds calendar days with PostgreSQL range and infinity rules.
public PgDate AddDays(int days)Parameters:
days — int
The signed number of days.
Returns: PgDate
The resulting date.
AtTime(PgTime)
Section titled “AtTime(PgTime)”Combines a date and wall-clock time, accepting PostgreSQL’s 24:00 value.
public PgTimestamp AtTime(PgTime time)Parameters:
time — PgTime
The time of day.
Returns: PgTimestamp
The timestamp.
AtTime(PgTimeTz)
Section titled “AtTime(PgTimeTz)”Combines a date and fixed-offset time to produce an instant.
public PgTimestampTz AtTime(PgTimeTz time)Parameters:
time — PgTimeTz
The time and fixed offset.
Returns: PgTimestampTz
The UTC instant.
CompareTo(PgDate)
Section titled “CompareTo(PgDate)”Compares dates, including infinities, without requiring an active backend.
public int CompareTo(PgDate other)Parameters:
other — PgDate
The date to compare.
Returns: int
A negative value, zero, or a positive value for earlier, equal, or later dates.
Create(int, int, int)
Section titled “Create(int, int, int)”Constructs a date using PostgreSQL’s calendar; negative years denote BC and year zero is invalid.
public static PgDate Create(int year, int month, int day)Parameters:
year — int
The signed year.
month — int
The month, one through twelve.
day — int
The day of month.
Returns: PgDate
The date.
Equals(PgDate)
Section titled “Equals(PgDate)”public bool Equals(PgDate other)Parameters:
other — PgDate
Returns: bool
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.
FromDateOnly(DateOnly)
Section titled “FromDateOnly(DateOnly)”Converts a .NET date exactly; its minimum and maximum dates remain finite.
public static PgDate FromDateOnly(DateOnly value)Parameters:
value — DateOnly
The .NET date.
Returns: PgDate
The PostgreSQL date.
FromRawSaturating(int)
Section titled “FromRawSaturating(int)”Creates a date from a raw PostgreSQL day offset, clamping values outside the finite range to infinity.
public static PgDate FromRawSaturating(int daysSinceEpoch)Parameters:
daysSinceEpoch — int
The signed number of days since 2000-01-01, including infinity sentinels.
Returns: PgDate
The finite date, negative infinity below the finite minimum, or positive infinity above the finite maximum.
GetDateParts()
Section titled “GetDateParts()”Reads the full-range Gregorian date without requiring a backend.
public (int Year, int Month, int Day) GetDateParts()Returns: (int Year, int Month, int Day)
The year, month, and day. Negative years denote BC; there is no year zero.
Exceptions:
- InvalidOperationException: The date is infinite.
GetHashCode()
Section titled “GetHashCode()”public override int GetHashCode()Returns: int
GetPart(PgDateTimePart)
Section titled “GetPart(PgDateTimePart)”Reads a field with PostgreSQL date_part semantics, returning floating-point values.
public double? GetPart(PgDateTimePart part)Parameters:
part — PgDateTimePart
The field to read.
Returns: double?
The field, or null for an undefined field of an infinite value.
Parse(string)
Section titled “Parse(string)”Parses PostgreSQL date syntax using the current backend’s DateStyle.
public static PgDate Parse(string text)Parameters:
text — string
The date text, including PostgreSQL special values.
Returns: PgDate
The parsed date.
Subtract(PgDate)
Section titled “Subtract(PgDate)”Computes the signed day difference using PostgreSQL’s infinity rules.
public int Subtract(PgDate other)Parameters:
other — PgDate
The date to subtract.
Returns: int
The number of days.
Subtract(PgInterval)
Section titled “Subtract(PgInterval)”Subtracts a calendar interval, yielding a timestamp.
public PgTimestamp Subtract(PgInterval interval)Parameters:
interval — PgInterval
The interval.
Returns: PgTimestamp
The resulting timestamp.
SubtractDays(int)
Section titled “SubtractDays(int)”Subtracts calendar days, including Int32.MinValue, with PostgreSQL range checks.
public PgDate SubtractDays(int days)Parameters:
days — int
The signed number of days.
Returns: PgDate
The resulting date.
ToDateOnly()
Section titled “ToDateOnly()”Converts a finite date in the .NET range to DateOnly; other values throw InvalidOperationException.
public DateOnly ToDateOnly()Returns: DateOnly
The .NET date.
Exceptions:
- InvalidOperationException: The date is infinite or outside years 1–9999.
ToIsoString()
Section titled “ToIsoString()”Formats the date using PostgreSQL’s ISO JSON date representation, independently of DateStyle.
public string ToIsoString()Returns: string
The ISO date, or PostgreSQL’s infinity spelling.
ToJulianDays()
Section titled “ToJulianDays()”Converts this finite date to its Julian day number without requiring a backend.
public int ToJulianDays()Returns: int
The Julian day number, with zero denoting 4714-11-24 BC in the proleptic Gregorian calendar.
Exceptions:
- InvalidOperationException: The date is infinite.
ToPostgresString()
Section titled “ToPostgresString()”Formats the date using the current backend’s DateStyle.
public string ToPostgresString()Returns: string
The PostgreSQL text.
ToString()
Section titled “ToString()”Formats stored diagnostic values without requiring a backend or reading finite calendar fields.
public override string ToString()Returns: string
The managed record diagnostic representation.
ToTimestamp()
Section titled “ToTimestamp()”Converts the date to midnight without a timezone, checking PostgreSQL’s narrower timestamp range.
public PgTimestamp ToTimestamp()Returns: PgTimestamp
The midnight timestamp.
ToTimestampTz()
Section titled “ToTimestampTz()”Converts the date to midnight in the current session’s timezone.
public PgTimestampTz ToTimestampTz()Returns: PgTimestampTz
The UTC instant.
ToUnixEpochDays()
Section titled “ToUnixEpochDays()”Converts this finite date to the signed number of days since 1970-01-01 without requiring a backend.
public int ToUnixEpochDays()Returns: int
The signed Unix epoch day offset.
Exceptions:
- InvalidOperationException: The date is infinite.
ToUnixTimeSeconds()
Section titled “ToUnixTimeSeconds()”Converts this finite date’s midnight to Unix epoch seconds, treating every day as 86400 seconds.
public long ToUnixTimeSeconds()Returns: long
The signed epoch seconds without any session timezone adjustment or backend access.
Exceptions:
- InvalidOperationException: The date is infinite.
TryParse(string?, out PgDate)
Section titled “TryParse(string?, out PgDate)”Tries to parse PostgreSQL date syntax on the active backend thread.
public static bool TryParse(string? text, out PgDate value)Parameters:
text — string
The date text.
value — PgDate
The parsed date, 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”CurrentDate
Section titled “CurrentDate”Gets the current transaction’s date in the session timezone, like SQL CURRENT_DATE.
public static PgDate CurrentDate { get; }Value: PgDate
Gets the day of the month without requiring a backend.
public int Day { get; }Value: int
Exceptions:
- InvalidOperationException: The date is infinite.
DaysSinceEpoch
Section titled “DaysSinceEpoch”Gets the PostgreSQL day offset, including its infinity sentinels.
public int DaysSinceEpoch { get; }Value: int
IsFinite
Section titled “IsFinite”Gets whether this date is finite.
public bool IsFinite { get; }Value: bool
IsNegativeInfinity
Section titled “IsNegativeInfinity”Gets whether this date is negative infinity.
public bool IsNegativeInfinity { get; }Value: bool
IsPositiveInfinity
Section titled “IsPositiveInfinity”Gets whether this date is positive infinity.
public bool IsPositiveInfinity { get; }Value: bool
Gets the calendar month from one through twelve without requiring a backend.
public int Month { get; }Value: int
Exceptions:
- InvalidOperationException: The date is infinite.
NegativeInfinity
Section titled “NegativeInfinity”Gets negative infinity.
public static PgDate NegativeInfinity { get; }Value: PgDate
PositiveInfinity
Section titled “PositiveInfinity”Gets positive infinity.
public static PgDate PositiveInfinity { get; }Value: PgDate
Gets the calendar year without requiring a backend. Negative years denote BC; there is no year zero.
public int Year { get; }Value: int
Exceptions:
- InvalidOperationException: The date is infinite.
Fields
Section titled “Fields”PostgresEpochJulianDay
Section titled “PostgresEpochJulianDay”The Julian day number of PostgreSQL’s epoch, 2000-01-01.
public const int PostgresEpochJulianDay = 2451545UnixEpochJulianDay
Section titled “UnixEpochJulianDay”The Julian day number of the Unix epoch, 1970-01-01.
public const int UnixEpochJulianDay = 2440588Operators
Section titled “Operators”operator +(PgDate, PgInterval)
Section titled “operator +(PgDate, PgInterval)”Adds a calendar interval to a date.
public static PgTimestamp operator +(PgDate date, PgInterval interval)Parameters:
date — PgDate
interval — PgInterval
Returns: PgTimestamp
operator +(PgDate, PgTime)
Section titled “operator +(PgDate, PgTime)”Combines a date and wall-clock time.
public static PgTimestamp operator +(PgDate date, PgTime time)Parameters:
date — PgDate
time — PgTime
Returns: PgTimestamp
operator +(PgDate, PgTimeTz)
Section titled “operator +(PgDate, PgTimeTz)”Combines a date and fixed-offset time.
public static PgTimestampTz operator +(PgDate date, PgTimeTz time)Parameters:
date — PgDate
time — PgTimeTz
Returns: PgTimestampTz
operator +(PgDate, int)
Section titled “operator +(PgDate, int)”Adds calendar days using PostgreSQL’s rules.
public static PgDate operator +(PgDate date, int days)Parameters:
date — PgDate
days — int
Returns: PgDate
operator +(PgInterval, PgDate)
Section titled “operator +(PgInterval, PgDate)”Adds a calendar interval to a date.
public static PgTimestamp operator +(PgInterval interval, PgDate date)Parameters:
interval — PgInterval
date — PgDate
Returns: PgTimestamp
operator +(PgTime, PgDate)
Section titled “operator +(PgTime, PgDate)”Combines a date and wall-clock time.
public static PgTimestamp operator +(PgTime time, PgDate date)Parameters:
time — PgTime
date — PgDate
Returns: PgTimestamp
operator +(PgTimeTz, PgDate)
Section titled “operator +(PgTimeTz, PgDate)”Combines a date and fixed-offset time.
public static PgTimestampTz operator +(PgTimeTz time, PgDate date)Parameters:
time — PgTimeTz
date — PgDate
Returns: PgTimestampTz
operator +(int, PgDate)
Section titled “operator +(int, PgDate)”Adds calendar days using PostgreSQL’s rules.
public static PgDate operator +(int days, PgDate date)Parameters:
days — int
date — PgDate
Returns: PgDate
operator ==(PgDate, PgDate)
Section titled “operator ==(PgDate, PgDate)”public static bool operator ==(PgDate left, PgDate right)Parameters:
left — PgDate
right — PgDate
Returns: bool
operator >(PgDate, PgDate)
Section titled “operator >(PgDate, PgDate)”Tests whether the left date follows the right date.
public static bool operator >(PgDate left, PgDate right)Parameters:
left — PgDate
right — PgDate
Returns: bool
operator >=(PgDate, PgDate)
Section titled “operator >=(PgDate, PgDate)”Tests whether the left date follows or equals the right date.
public static bool operator >=(PgDate left, PgDate right)Parameters:
left — PgDate
right — PgDate
Returns: bool
operator !=(PgDate, PgDate)
Section titled “operator !=(PgDate, PgDate)”public static bool operator !=(PgDate left, PgDate right)Parameters:
left — PgDate
right — PgDate
Returns: bool
operator <(PgDate, PgDate)
Section titled “operator <(PgDate, PgDate)”Tests whether the left date precedes the right date.
public static bool operator <(PgDate left, PgDate right)Parameters:
left — PgDate
right — PgDate
Returns: bool
operator <=(PgDate, PgDate)
Section titled “operator <=(PgDate, PgDate)”Tests whether the left date precedes or equals the right date.
public static bool operator <=(PgDate left, PgDate right)Parameters:
left — PgDate
right — PgDate
Returns: bool
operator -(PgDate, PgDate)
Section titled “operator -(PgDate, PgDate)”Computes the signed day difference.
public static int operator -(PgDate left, PgDate right)Parameters:
left — PgDate
right — PgDate
Returns: int
operator -(PgDate, PgInterval)
Section titled “operator -(PgDate, PgInterval)”Subtracts a calendar interval from a date.
public static PgTimestamp operator -(PgDate date, PgInterval interval)Parameters:
date — PgDate
interval — PgInterval
Returns: PgTimestamp
operator -(PgDate, int)
Section titled “operator -(PgDate, int)”Subtracts calendar days using PostgreSQL’s rules.
public static PgDate operator -(PgDate date, int days)Parameters:
date — PgDate
days — int
Returns: PgDate