Struct NpgsqlTimeSpan
- Namespace
- NpgsqlTypes
- Assembly
- Npgsql.dll
Represents the PostgreSQL interval datatype.
public readonly struct NpgsqlTimeSpan : IComparable, IComparer, IEquatable<NpgsqlTimeSpan>, IComparable<NpgsqlTimeSpan>, IComparer<NpgsqlTimeSpan>
- Implements
- Inherited Members
Remarks
PostgreSQL differs from .NET in how it's interval type doesn't assume 24 hours in a day (to deal with 23- and 25-hour days caused by daylight savings adjustments) and has a concept of months that doesn't exist in .NET's TimeSpan class. (Neither datatype has any concessions for leap-seconds).
For most uses just casting to and from TimeSpan will work correctly — in particular, the results of subtracting one DateTime or the PostgreSQL date, time and timestamp types from another should be the same whether you do so in .NET or PostgreSQL — but if the handling of days and months in PostgreSQL is important to your application then you should use this class instead of TimeSpan.
If you don't know whether these differences are important to your application, they probably arent! Just use TimeSpan and do not use this class directly ☺
To avoid forcing unnecessary provider-specific concerns on users who need not be concerned with them a call to GetValue(int) on a field containing an NpgsqlTimeSpan value will return a TimeSpan rather than an NpgsqlTimeSpan. If you need the extra functionality of NpgsqlTimeSpan then use GetInterval(int).
Constructors
NpgsqlTimeSpan(int, int, int, int)
Initializes a new NpgsqlTimeSpan to the specified number of days, hours, minutes & seconds.
public NpgsqlTimeSpan(int days, int hours, int minutes, int seconds)
Parameters
days
intNumber of days.
hours
intNumber of hours.
minutes
intNumber of minutes.
seconds
intNumber of seconds.
NpgsqlTimeSpan(int, int, int, int, int)
Initializes a new NpgsqlTimeSpan to the specified number of days, hours, minutes, seconds & milliseconds.
public NpgsqlTimeSpan(int days, int hours, int minutes, int seconds, int milliseconds)
Parameters
days
intNumber of days.
hours
intNumber of hours.
minutes
intNumber of minutes.
seconds
intNumber of seconds.
milliseconds
intNumber of milliseconds.
NpgsqlTimeSpan(int, int, int, int, int, int)
Initializes a new NpgsqlTimeSpan to the specified number of months, days, hours, minutes, seconds & milliseconds.
public NpgsqlTimeSpan(int months, int days, int hours, int minutes, int seconds, int milliseconds)
Parameters
months
intNumber of months.
days
intNumber of days.
hours
intNumber of hours.
minutes
intNumber of minutes.
seconds
intNumber of seconds.
milliseconds
intNumber of milliseconds.
NpgsqlTimeSpan(int, int, int, int, int, int, int)
Initializes a new NpgsqlTimeSpan to the specified number of years, months, days, hours, minutes, seconds & milliseconds.
Years are calculated exactly equivalent to 12 months.
public NpgsqlTimeSpan(int years, int months, int days, int hours, int minutes, int seconds, int milliseconds)
Parameters
years
intNumber of years.
months
intNumber of months.
days
intNumber of days.
hours
intNumber of hours.
minutes
intNumber of minutes.
seconds
intNumber of seconds.
milliseconds
intNumber of milliseconds.
NpgsqlTimeSpan(int, int, long)
Initializes a new NpgsqlTimeSpan to the specified number of months, days & ticks.
public NpgsqlTimeSpan(int months, int days, long ticks)
Parameters
NpgsqlTimeSpan(long)
Initializes a new NpgsqlTimeSpan to the specified number of ticks.
public NpgsqlTimeSpan(long ticks)
Parameters
ticks
longA time period expressed in 100ns units.
NpgsqlTimeSpan(TimeSpan)
Initializes a new NpgsqlTimeSpan to hold the same time as a TimeSpan
public NpgsqlTimeSpan(TimeSpan timespan)
Parameters
Fields
DaysPerMonth
Represents the number of days assumed in one month if month justification or unjustifcation is performed. This is set to 30 for consistency with PostgreSQL. Note that this is means that month adjustments cause a year to be taken as 30 × 12 = 360 rather than 356/366 days.
public const int DaysPerMonth = 30
Field Value
HoursPerDay
Represents the number of hours in one day (assuming no daylight savings adjustments). This field is constant.
public const int HoursPerDay = 24
Field Value
MaxValue
Represents the maximum NpgsqlTimeSpan. This field is read-only.
public static readonly NpgsqlTimeSpan MaxValue
Field Value
MinValue
Represents the minimum NpgsqlTimeSpan. This field is read-only.
public static readonly NpgsqlTimeSpan MinValue
Field Value
MonthsPerYear
Represents the number of months in a year. This field is constant.
public const int MonthsPerYear = 12
Field Value
TicksPerDay
Represents the number of ticks (100ns periods) in one day. This field is constant.
public const long TicksPerDay = 864000000000
Field Value
TicksPerHour
Represents the number of ticks (100ns periods) in one hour. This field is constant.
public const long TicksPerHour = 36000000000
Field Value
TicksPerMicrosecond
Represents the number of ticks (100ns periods) in one microsecond. This field is constant.
public const long TicksPerMicrosecond = 10
Field Value
TicksPerMillsecond
Represents the number of ticks (100ns periods) in one millisecond. This field is constant.
public const long TicksPerMillsecond = 10000
Field Value
TicksPerMinute
Represents the number of ticks (100ns periods) in one minute. This field is constant.
public const long TicksPerMinute = 600000000
Field Value
TicksPerMonth
Represents the number of ticks (100ns periods) in one day, assuming 30 days per month. DaysPerMonth
public const long TicksPerMonth = 25920000000000
Field Value
TicksPerSecond
Represents the number of ticks (100ns periods) in one second. This field is constant.
public const long TicksPerSecond = 10000000
Field Value
Zero
Represents the zero NpgsqlTimeSpan. This field is read-only.
public static readonly NpgsqlTimeSpan Zero
Field Value
Properties
Days
Gets the number of days held in the instance.
public int Days { get; }
Property Value
Hours
Gets the number of whole hours held in the instance.
public int Hours { get; }
Property Value
Microseconds
Gets the number of whole microseconds held in the instance.
public int Microseconds { get; }
Property Value
Milliseconds
Gets the number of whole milliseconds held in the instance.
public int Milliseconds { get; }
Property Value
Minutes
Gets the number of whole minutes held in the instance.
public int Minutes { get; }
Property Value
Months
Gets the number of months held in the instance.
public int Months { get; }
Property Value
Seconds
Gets the number of whole seconds held in the instance.
public int Seconds { get; }
Property Value
Ticks
The total number of ticks(100ns units) contained. This is the resolution of the
NpgsqlTimeSpan type. This ignores the number of days and
months held. If you want them included use UnjustifyInterval() first.
- interval(0)resolution of 1s (1 second)
- interval(1)resolution of 100ms = 0.1s (100 milliseconds)
- interval(2)resolution of 10ms = 0.01s (10 milliseconds)
- interval(3)resolution of 1ms = 0.001s (1 millisecond)
- interval(4)resolution of 100µs = 0.0001s (100 microseconds)
- interval(5)resolution of 10µs = 0.00001s (10 microseconds)
- interval(6) or intervalresolution of 1µs = 0.000001s (1 microsecond)
As such, if the 100-nanosecond resolution is significant to an application, a PostgreSQL interval will not suffice for those purposes.
In more frequent cases though, the resolution of the interval suffices. NpgsqlTimeSpan will always suffice to handle the resolution of any interval value, and upon writing to the database, will be rounded to the resolution used.
public long Ticks { get; }
Property Value
Time
Returns a TimeSpan representing the time component of the instance.
public TimeSpan Time { get; }
Property Value
TotalDays
The total number of days in the instance, assuming 24 hours in each day and 30 days in a month.
public double TotalDays { get; }
Property Value
TotalHours
The total number of hours in the instance, assuming 24 hours in each day and 30 days in a month.
public double TotalHours { get; }
Property Value
TotalMicroseconds
The total number of microseconds in the instance, assuming 24 hours in each day and 30 days in a month.
public double TotalMicroseconds { get; }
Property Value
TotalMilliseconds
The total number of milliseconds in the instance, assuming 24 hours in each day and 30 days in a month.
public double TotalMilliseconds { get; }
Property Value
TotalMinutes
The total number of minutes in the instance, assuming 24 hours in each day and 30 days in a month.
public double TotalMinutes { get; }
Property Value
TotalMonths
The total number of months in the instance, assuming 24 hours in each day and 30 days in a month.
public double TotalMonths { get; }
Property Value
TotalSeconds
The total number of seconds in the instance, assuming 24 hours in each day and 30 days in a month.
public double TotalSeconds { get; }
Property Value
TotalTicks
The total number of ticks (100ns units) in the instance, assuming 24 hours in each day and 30 days in a month.
public long TotalTicks { get; }
Property Value
Methods
Add(NpgsqlTimeSpan)
Adds another interval to this instance and returns the result.
public NpgsqlTimeSpan Add(NpgsqlTimeSpan interval)
Parameters
interval
NpgsqlTimeSpanAn NpgsqlTimeSpan to add to this instance.
Returns
- NpgsqlTimeSpan
An NpgsqlTimeSpan whose values are the sums of the two instances.
Canonicalize()
Produces a canonical NpgslInterval with 0 months and hours in the range of [-23, 23].
While the fact that for many purposes, two different NpgsqlTimeSpan instances could be considered
equivalent (e.g. one with 2days, 3hours and one with 1day 27hours) there are different possible canonical forms.
E.g. we could move all excess hours into days and all excess days into months and have the most readable form, or we could move everything into the ticks and have the form that allows for the easiest arithmetic) the form chosen has two important properties that make it the best choice.
First, it is closest two how TimeSpan objects are most often represented. Second, it is compatible with results of many PostgreSQL functions, particularly with age() and the results of subtracting one date, time or timestamp from another.
Note that the results of casting a TimeSpan to NpgsqlTimeSpan is canonicalised.
public NpgsqlTimeSpan Canonicalize()
Returns
- NpgsqlTimeSpan
An NpgsqlTimeSpan based on this one, but with months converted to multiples of ±30days and with any hours outside of the range [-23, 23] converted into days.
Compare(NpgsqlTimeSpan, NpgsqlTimeSpan)
Compares two NpgsqlTimeSpan instances.
public static int Compare(NpgsqlTimeSpan x, NpgsqlTimeSpan y)
Parameters
x
NpgsqlTimeSpanThe first NpgsqlTimeSpan.
y
NpgsqlTimeSpanThe second NpgsqlTimeSpan.
Returns
- int
0 if the two are equal or equivalent. A value greater than zero if x is greater than y, a value less than zero if x is less than y.
CompareTo(NpgsqlTimeSpan)
Compares this instance with another/
public int CompareTo(NpgsqlTimeSpan other)
Parameters
other
NpgsqlTimeSpanAn NpgsqlTimeSpan to compare this with.
Returns
- int
0 if the instances are equal or equivalent. A value less than zero if this instance is less than the argument. A value greater than zero if this instance is greater than the instance.
CompareTo(object)
Compares this instance with another/
public int CompareTo(object other)
Parameters
other
objectAn object to compare this with.
Returns
- int
0 if the argument is an NpgsqlTimeSpan and the instances are equal or equivalent. A value less than zero if the argument is an NpgsqlTimeSpan and this instance is less than the argument. A value greater than zero if the argument is an NpgsqlTimeSpan and this instance is greater than the instance.
Exceptions
- ArgumentException
The argument is not an NpgsqlTimeSpan.
Duration()
This absolute value of this instance. In the case of some, but not all, components being negative, the rules used for justification are used to determine if the instance is positive or negative.
public NpgsqlTimeSpan Duration()
Returns
- NpgsqlTimeSpan
An NpgsqlTimeSpan whose value is the absolute value of this instance.
Equals(NpgsqlTimeSpan)
Returns true if another NpgsqlTimeSpan is exactly the same as this instance.
public bool Equals(NpgsqlTimeSpan other)
Parameters
other
NpgsqlTimeSpanAn NpgsqlTimeSpan for comparison.
Returns
- bool
true if the two NpgsqlTimeSpan instances are exactly the same, false otherwise.
Equals(object)
Returns true if another object is an NpgsqlTimeSpan, that is exactly the same as this instance
public override bool Equals(object obj)
Parameters
Returns
- bool
true if the argument is an NpgsqlTimeSpan and is exactly the same as this one, false otherwise.
FromDays(double)
Creates an NpgsqlTimeSpan from a number of days.
public static NpgsqlTimeSpan FromDays(double days)
Parameters
days
doubleThe number of days in the interval.
Returns
- NpgsqlTimeSpan
A Canonicalize()d NpgsqlTimeSpan with the given number of days.
FromHours(double)
Creates an NpgsqlTimeSpan from a number of hours.
public static NpgsqlTimeSpan FromHours(double hours)
Parameters
hours
doubleThe number of hours in the interval.
Returns
- NpgsqlTimeSpan
A Canonicalize()d NpgsqlTimeSpan with the given number of hours.
FromMicroseconds(double)
Creates an NpgsqlTimeSpan from a number of microseconds.
public static NpgsqlTimeSpan FromMicroseconds(double micro)
Parameters
micro
doubleThe number of microseconds in the interval.
Returns
- NpgsqlTimeSpan
A Canonicalize()d NpgsqlTimeSpan with the given number of microseconds.
FromMilliseconds(double)
Creates an NpgsqlTimeSpan from a number of milliseconds.
public static NpgsqlTimeSpan FromMilliseconds(double milli)
Parameters
milli
doubleThe number of milliseconds in the interval.
Returns
- NpgsqlTimeSpan
A Canonicalize()d NpgsqlTimeSpan with the given number of milliseconds.
FromMinutes(double)
Creates an NpgsqlTimeSpan from a number of minutes.
public static NpgsqlTimeSpan FromMinutes(double minutes)
Parameters
minutes
doubleThe number of minutes in the interval.
Returns
- NpgsqlTimeSpan
A Canonicalize()d NpgsqlTimeSpan with the given number of minutes.
FromMonths(double)
Creates an NpgsqlTimeSpan from a number of months.
public static NpgsqlTimeSpan FromMonths(double months)
Parameters
months
doubleThe number of months in the interval.
Returns
- NpgsqlTimeSpan
A Canonicalize()d NpgsqlTimeSpan with the given number of months.
FromSeconds(double)
Creates an NpgsqlTimeSpan from a number of seconds.
public static NpgsqlTimeSpan FromSeconds(double seconds)
Parameters
seconds
doubleThe number of seconds in the interval.
Returns
- NpgsqlTimeSpan
A Canonicalize()d NpgsqlTimeSpan with the given number of seconds.
FromTicks(long)
Creates an NpgsqlTimeSpan from a number of ticks.
public static NpgsqlTimeSpan FromTicks(long ticks)
Parameters
ticks
longThe number of ticks (100ns units) in the interval.
Returns
- NpgsqlTimeSpan
A Canonicalize()d NpgsqlTimeSpan with the given number of ticks.
GetHashCode()
A hash code suitable for uses with hashing algorithms.
public override int GetHashCode()
Returns
- int
An signed integer.
JustifyDays()
Equivalent to PostgreSQL's justify_days function.
public NpgsqlTimeSpan JustifyDays()
Returns
- NpgsqlTimeSpan
An NpgsqlTimeSpan based on this one, but with any hours outside of the range [-23, 23] converted into days.
JustifyInterval()
Equivalent to PostgreSQL's justify_interval function.
public NpgsqlTimeSpan JustifyInterval()
Returns
- NpgsqlTimeSpan
An NpgsqlTimeSpan based on this one, but with any months converted to multiples of ±30days and then with any days converted to multiples of ±24hours
JustifyMonths()
Equivalent to PostgreSQL's justify_months function.
public NpgsqlTimeSpan JustifyMonths()
Returns
- NpgsqlTimeSpan
An NpgsqlTimeSpan based on this one, but with any days outside of the range [-30, 30] converted into months.
Negate()
Returns an NpgsqlTimeSpan whose value is the negated value of this instance.
public NpgsqlTimeSpan Negate()
Returns
- NpgsqlTimeSpan
An NpgsqlTimeSpan whose value is the negated value of this instance.
Parse(string)
Parses a string and returns a NpgsqlTimeSpan instance. Designed to use the formats generally returned by PostgreSQL.
public static NpgsqlTimeSpan Parse(string str)
Parameters
Returns
- NpgsqlTimeSpan
An NpgsqlTimeSpan represented by the argument.
Exceptions
- ArgumentNullException
The string was null.
- OverflowException
A value obtained from parsing the string exceeded the values allowed for the relevant component.
- FormatException
The string was not in a format that could be parsed to produce an NpgsqlTimeSpan.
Plus(NpgsqlTimeSpan)
Returns the instance.
public static NpgsqlTimeSpan Plus(NpgsqlTimeSpan x)
Parameters
Returns
Subtract(NpgsqlTimeSpan)
Subtracts another interval from this instance and returns the result.
public NpgsqlTimeSpan Subtract(NpgsqlTimeSpan interval)
Parameters
interval
NpgsqlTimeSpanAn NpgsqlTimeSpan to subtract from this instance.
Returns
- NpgsqlTimeSpan
An NpgsqlTimeSpan whose values are the differences of the two instances.
ToNpgsqlTimeSpan(TimeSpan)
Casts a TimeSpan to an NpgsqlTimeSpan.
public static NpgsqlTimeSpan ToNpgsqlTimeSpan(TimeSpan timespan)
Parameters
timespan
TimeSpan
Returns
ToString()
Create a string representation of the NpgsqlTimeSpan instance.
The format returned is of the form:
[M mon[s]] [d day[s]] [HH:mm:ss[.f[f[f[f[f[f[f[f[f]]]]]]]]]]
A zero NpgsqlTimeSpan is represented as 00:00:00
public override string ToString()
Returns
ToTimeSpan(NpgsqlTimeSpan)
Casts an NpgsqlTimeSpan to a TimeSpan.
public static TimeSpan ToTimeSpan(NpgsqlTimeSpan interval)
Parameters
interval
NpgsqlTimeSpan
Returns
TryParse(string, out NpgsqlTimeSpan)
Attempt to parse a string to produce an NpgsqlTimeSpan.
public static bool TryParse(string str, out NpgsqlTimeSpan result)
Parameters
str
stringThe string to parse.
result
NpgsqlTimeSpan(out) The NpgsqlTimeSpan produced, or Zero if the parsing failed.
Returns
- bool
true if the parsing succeeded, false otherwise.
UnjustifyDays()
Opposite to PostgreSQL's justify_days function.
public NpgsqlTimeSpan UnjustifyDays()
Returns
- NpgsqlTimeSpan
An NpgsqlTimeSpan based on this one, but with any days converted to multiples of ±24hours.
UnjustifyInterval()
Opposite to PostgreSQL's justify_interval function.
public NpgsqlTimeSpan UnjustifyInterval()
Returns
- NpgsqlTimeSpan
An NpgsqlTimeSpan based on this one, but with any months converted to multiples of ±30days and then any days converted to multiples of ±24hours;
UnjustifyMonths()
Opposite to PostgreSQL's justify_months function.
public NpgsqlTimeSpan UnjustifyMonths()
Returns
- NpgsqlTimeSpan
An NpgsqlTimeSpan based on this one, but with any months converted to multiples of ±30days.
Operators
operator +(NpgsqlTimeSpan, NpgsqlTimeSpan)
Adds two NpgsqlTimeSpan together.
public static NpgsqlTimeSpan operator +(NpgsqlTimeSpan x, NpgsqlTimeSpan y)
Parameters
x
NpgsqlTimeSpanThe first NpgsqlTimeSpan to add.
y
NpgsqlTimeSpanThe second NpgsqlTimeSpan to add.
Returns
- NpgsqlTimeSpan
An NpgsqlTimeSpan whose values are the sum of the arguments.
operator ==(NpgsqlTimeSpan, NpgsqlTimeSpan)
Returns true if two NpgsqlTimeSpan are exactly the same.
public static bool operator ==(NpgsqlTimeSpan x, NpgsqlTimeSpan y)
Parameters
x
NpgsqlTimeSpanThe first NpgsqlTimeSpan to compare.
y
NpgsqlTimeSpanThe second NpgsqlTimeSpan to compare.
Returns
- bool
true if the two arguments are exactly the same, false otherwise.
explicit operator TimeSpan(NpgsqlTimeSpan)
Explicit cast of an NpgsqlTimeSpan to a TimeSpan.
public static explicit operator TimeSpan(NpgsqlTimeSpan interval)
Parameters
interval
NpgsqlTimeSpan
Returns
operator >(NpgsqlTimeSpan, NpgsqlTimeSpan)
Compares two NpgsqlTimeSpan instances to see if the first is greater than the second
public static bool operator >(NpgsqlTimeSpan x, NpgsqlTimeSpan y)
Parameters
x
NpgsqlTimeSpanThe first NpgsqlTimeSpan to compare.
y
NpgsqlTimeSpanThe second NpgsqlTimeSpan to compare.
Returns
- bool
true if the first NpgsqlTimeSpan is greater than second, false otherwise.
operator >=(NpgsqlTimeSpan, NpgsqlTimeSpan)
Compares two NpgsqlTimeSpan instances to see if the first is greater than or equivalent the second
public static bool operator >=(NpgsqlTimeSpan x, NpgsqlTimeSpan y)
Parameters
x
NpgsqlTimeSpanThe first NpgsqlTimeSpan to compare.
y
NpgsqlTimeSpanThe second NpgsqlTimeSpan to compare.
Returns
- bool
true if the first NpgsqlTimeSpan is greater than or equivalent to the second, false otherwise.
implicit operator NpgsqlTimeSpan(TimeSpan)
Implicit cast of a TimeSpan to an NpgsqlTimeSpan
public static implicit operator NpgsqlTimeSpan(TimeSpan timespan)
Parameters
Returns
- NpgsqlTimeSpan
An eqivalent, canonical, NpgsqlTimeSpan.
operator !=(NpgsqlTimeSpan, NpgsqlTimeSpan)
Returns false if two NpgsqlTimeSpan are exactly the same.
public static bool operator !=(NpgsqlTimeSpan x, NpgsqlTimeSpan y)
Parameters
x
NpgsqlTimeSpanThe first NpgsqlTimeSpan to compare.
y
NpgsqlTimeSpanThe second NpgsqlTimeSpan to compare.
Returns
- bool
false if the two arguments are exactly the same, true otherwise.
operator <(NpgsqlTimeSpan, NpgsqlTimeSpan)
Compares two NpgsqlTimeSpan instances to see if the first is less than the second
public static bool operator <(NpgsqlTimeSpan x, NpgsqlTimeSpan y)
Parameters
x
NpgsqlTimeSpanThe first NpgsqlTimeSpan to compare.
y
NpgsqlTimeSpanThe second NpgsqlTimeSpan to compare.
Returns
- bool
true if the first NpgsqlTimeSpan is less than second, false otherwise.
operator <=(NpgsqlTimeSpan, NpgsqlTimeSpan)
Compares two NpgsqlTimeSpan instances to see if the first is less than or equivalent to the second
public static bool operator <=(NpgsqlTimeSpan x, NpgsqlTimeSpan y)
Parameters
x
NpgsqlTimeSpanThe first NpgsqlTimeSpan to compare.
y
NpgsqlTimeSpanThe second NpgsqlTimeSpan to compare.
Returns
- bool
true if the first NpgsqlTimeSpan is less than or equivalent to second, false otherwise.
operator -(NpgsqlTimeSpan, NpgsqlTimeSpan)
Subtracts one NpgsqlTimeSpan from another.
public static NpgsqlTimeSpan operator -(NpgsqlTimeSpan x, NpgsqlTimeSpan y)
Parameters
x
NpgsqlTimeSpanThe NpgsqlTimeSpan to subtract the other from.
y
NpgsqlTimeSpanThe NpgsqlTimeSpan to subtract from the other.
Returns
- NpgsqlTimeSpan
An NpgsqlTimeSpan whose values are the difference of the arguments
operator -(NpgsqlTimeSpan)
Negates an NpgsqlTimeSpan instance.
public static NpgsqlTimeSpan operator -(NpgsqlTimeSpan x)
Parameters
x
NpgsqlTimeSpanAn NpgsqlTimeSpan.
Returns
- NpgsqlTimeSpan
The negation of the argument.
operator +(NpgsqlTimeSpan)
Returns the instance.
public static NpgsqlTimeSpan operator +(NpgsqlTimeSpan x)