Skip to content

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>

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:

Adds a calendar interval, yielding a timestamp.

public PgTimestamp Add(PgInterval interval)

Parameters:

interval — PgInterval

The interval.

Returns: PgTimestamp

The resulting timestamp.

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.

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.

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.

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.

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.

public bool Equals(PgDate other)

Parameters:

other — PgDate

Returns: bool

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.

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.

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.

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:

public override int GetHashCode()

Returns: int

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.

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.

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.

Subtracts a calendar interval, yielding a timestamp.

public PgTimestamp Subtract(PgInterval interval)

Parameters:

interval — PgInterval

The interval.

Returns: PgTimestamp

The resulting timestamp.

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.

Converts a finite date in the .NET range to DateOnly; other values throw InvalidOperationException.

public DateOnly ToDateOnly()

Returns: DateOnly

The .NET date.

Exceptions:

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.

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:

Formats the date using the current backend’s DateStyle.

public string ToPostgresString()

Returns: string

The PostgreSQL text.

Formats stored diagnostic values without requiring a backend or reading finite calendar fields.

public override string ToString()

Returns: string

The managed record diagnostic representation.

Converts the date to midnight without a timezone, checking PostgreSQL’s narrower timestamp range.

public PgTimestamp ToTimestamp()

Returns: PgTimestamp

The midnight timestamp.

Converts the date to midnight in the current session’s timezone.

public PgTimestampTz ToTimestampTz()

Returns: PgTimestampTz

The UTC instant.

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:

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:

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.

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:

Gets the PostgreSQL day offset, including its infinity sentinels.

public int DaysSinceEpoch { get; }

Value: int

Gets whether this date is finite.

public bool IsFinite { get; }

Value: bool

Gets whether this date is negative infinity.

public bool IsNegativeInfinity { get; }

Value: bool

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:

Gets negative infinity.

public static PgDate NegativeInfinity { get; }

Value: PgDate

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:

The Julian day number of PostgreSQL’s epoch, 2000-01-01.

public const int PostgresEpochJulianDay = 2451545

The Julian day number of the Unix epoch, 1970-01-01.

public const int UnixEpochJulianDay = 2440588

Adds a calendar interval to a date.

public static PgTimestamp operator +(PgDate date, PgInterval interval)

Parameters:

date — PgDate

interval — PgInterval

Returns: PgTimestamp

Combines a date and wall-clock time.

public static PgTimestamp operator +(PgDate date, PgTime time)

Parameters:

date — PgDate

time — PgTime

Returns: PgTimestamp

Combines a date and fixed-offset time.

public static PgTimestampTz operator +(PgDate date, PgTimeTz time)

Parameters:

date — PgDate

time — PgTimeTz

Returns: PgTimestampTz

Adds calendar days using PostgreSQL’s rules.

public static PgDate operator +(PgDate date, int days)

Parameters:

date — PgDate

days — int

Returns: PgDate

Adds a calendar interval to a date.

public static PgTimestamp operator +(PgInterval interval, PgDate date)

Parameters:

interval — PgInterval

date — PgDate

Returns: PgTimestamp

Combines a date and wall-clock time.

public static PgTimestamp operator +(PgTime time, PgDate date)

Parameters:

time — PgTime

date — PgDate

Returns: PgTimestamp

Combines a date and fixed-offset time.

public static PgTimestampTz operator +(PgTimeTz time, PgDate date)

Parameters:

time — PgTimeTz

date — PgDate

Returns: PgTimestampTz

Adds calendar days using PostgreSQL’s rules.

public static PgDate operator +(int days, PgDate date)

Parameters:

days — int

date — PgDate

Returns: PgDate

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

Parameters:

left — PgDate

right — PgDate

Returns: bool

Tests whether the left date follows the right date.

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

Parameters:

left — PgDate

right — PgDate

Returns: bool

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

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

Parameters:

left — PgDate

right — PgDate

Returns: bool

Tests whether the left date precedes the right date.

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

Parameters:

left — PgDate

right — PgDate

Returns: bool

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

Computes the signed day difference.

public static int operator -(PgDate left, PgDate right)

Parameters:

left — PgDate

right — PgDate

Returns: int

Subtracts a calendar interval from a date.

public static PgTimestamp operator -(PgDate date, PgInterval interval)

Parameters:

date — PgDate

interval — PgInterval

Returns: PgTimestamp

Subtracts calendar days using PostgreSQL’s rules.

public static PgDate operator -(PgDate date, int days)

Parameters:

date — PgDate

days — int

Returns: PgDate