Struct Interval
An interval between two instants in time (start and end).
Since 1.0.x
Availability net6.0, net8.0, netstandard2.0
Inherited Members
Namespace: NodaTime
Assembly: NodaTime.dll
Syntax
public readonly struct Interval : IEquatable<Interval>, IXmlSerializable, IEqualityOperators<Interval, Interval, bool>
Remarks
Equality is defined in a component-wise fashion: two intervals are considered equal if their start instants are equal to each other and their end instants are equal to each other. Ordering between intervals is not defined.
The interval includes the start instant and excludes the end instant. However, an interval may be missing its start or end, in which case the interval is deemed to be infinite in that direction.
The end may equal the start (resulting in an empty interval), but will not be before the start.
The default value of this type is an empty interval with a start and end of UnixEpoch.
Constructors
Interval(Instant, Instant)
Initializes a new instance of the Interval struct.
The interval includes the
start
instant and excludes the
end
instant. The end may equal the start (resulting in an empty interval), but must not be before the start.
Since 1.0.x
Availability net6.0, net8.0, netstandard2.0
Declaration
public Interval(Instant start, Instant end)
Parameters
Type | Name | Description |
---|---|---|
Instant | start | The start Instant. |
Instant | end | The end Instant. |
Sample snippet
using NodaTime;
using System;
Instant start = Instant.FromUtc(2019, 1, 1, 15, 25, 48);
Instant end = Instant.FromUtc(2019, 1, 1, 16, 25, 48);
Interval interval = new Interval(start, end);
Console.WriteLine(interval.Start);
Console.WriteLine(interval.End);
Output:
2019-01-01T15:25:48Z
2019-01-01T16:25:48Z
Exceptions
Type | Condition |
---|---|
ArgumentOutOfRangeException | end is earlier than start . |
Interval(Instant?, Instant?)
Since 2.0.x
Availability net6.0, net8.0, netstandard2.0
Declaration
public Interval(Instant? start, Instant? end)
Parameters
Type | Name | Description |
---|---|---|
Instant? | start | The start Instant or null. |
Instant? | end | The end Instant or null. |
Remarks
If the start is null, the interval is deemed to stretch to the start of time. If the end is null,
the interval is deemed to stretch to the end of time.
Sample snippet
using NodaTime;
using System;
Instant end = Instant.FromUtc(2019, 1, 1, 16, 25, 48);
Interval interval = new Interval(null, end);
Console.WriteLine(interval.HasStart);
Console.WriteLine(interval.HasEnd);
Console.WriteLine(interval.End);
Output:
False
True
2019-01-01T16:25:48Z
Exceptions
Type | Condition |
---|---|
ArgumentOutOfRangeException | end is earlier than start ,
if they are both non-null. |
Properties
Duration
Returns the duration of the interval.
Since 1.0.x
Availability net6.0, net8.0, netstandard2.0
Declaration
public Duration Duration { get; }
Property Value
Type | Description |
---|---|
Duration | The duration of the interval. |
Remarks
This will always be a non-negative duration, though it may be zero.
Sample snippet
using NodaTime;
using System;
Instant start = Instant.FromUtc(2019, 1, 1, 3, 10, 20);
Instant end = Instant.FromUtc(2019, 1, 1, 9, 10, 20);
Interval interval = new Interval(start, end);
var duration = interval.Duration;
Console.WriteLine(duration);
Output:
0:06:00:00
Exceptions
Type | Condition |
---|---|
InvalidOperationException | The interval extends to the start or end of time. |
End
Gets the end instant - the exclusive upper bound of the interval.
Since 1.0.x
Availability net6.0, net8.0, netstandard2.0
Declaration
public Instant End { get; }
Property Value
Type | Description |
---|---|
Instant | The end Instant. |
Exceptions
Type | Condition |
---|---|
InvalidOperationException | The interval extends to the end of time. |
See Also
HasEnd
Returns
true
if this interval has a fixed end point, or false
if it
extends to the end of time.
Since 2.0.x
Availability net6.0, net8.0, netstandard2.0
Declaration
public bool HasEnd { get; }
Property Value
Type | Description |
---|---|
bool | true if this interval has a fixed end point, or false if it
extends to the end of time. |
HasStart
Returns
true
if this interval has a fixed start point, or false
if it
extends to the start of time.
Since 2.0.x
Availability net6.0, net8.0, netstandard2.0
Declaration
public bool HasStart { get; }
Property Value
Type | Description |
---|---|
bool | true if this interval has a fixed start point, or false if it
extends to the start of time. |
Start
Gets the start instant - the inclusive lower bound of the interval.
Since 1.0.x
Availability net6.0, net8.0, netstandard2.0
Declaration
public Instant Start { get; }
Property Value
Type | Description |
---|---|
Instant | The start Instant. |
Remarks
This will never be later than End, though it may be equal to it.
Exceptions
Type | Condition |
---|---|
InvalidOperationException | The interval extends to the start of time. |
See Also
Methods
AddSchema(XmlSchemaSet)
Adds the XML schema type describing the structure of the Interval XML serialization to the given
xmlSchemaSet
.
Since 3.0.x
Availability net6.0, net8.0, netstandard2.0
Declaration
public static XmlQualifiedName AddSchema(XmlSchemaSet xmlSchemaSet)
Parameters
Type | Name | Description |
---|---|---|
XmlSchemaSet | xmlSchemaSet | The XML schema set provided by XmlSchemaExporter. |
Returns
Type | Description |
---|---|
XmlQualifiedName | The qualified name of the schema type that was added to the xmlSchemaSet . |
Contains(Instant)
Returns whether or not this interval contains the given instant.
Since 1.2.x
Availability net6.0, net8.0, netstandard2.0
Declaration
public bool Contains(Instant instant)
Parameters
Type | Name | Description |
---|---|---|
Instant | instant | Instant to test. |
Returns
Type | Description |
---|---|
bool | True if this interval contains the given instant; false otherwise. |
Sample snippet
using NodaTime;
using System;
Instant start = Instant.FromUtc(2019, 1, 1, 15, 25, 48);
Instant end = Instant.FromUtc(2019, 1, 1, 16, 25, 48);
Interval interval = new Interval(start, end);
Console.WriteLine(interval.Contains(Instant.FromUtc(2019, 1, 1, 15, 50, 50)));
Output:
True
Deconstruct(out Instant?, out Instant?)
Deconstruct this value into its components.
Since 2.3.x
Availability net6.0, net8.0, netstandard2.0
Declaration
public void Deconstruct(out Instant? start, out Instant? end)
Parameters
Type | Name | Description |
---|---|---|
Instant? | start | The start of the interval. |
Instant? | end | The end of the interval. |
Sample snippet
using NodaTime;
using System;
Instant? start = Instant.FromUtc(2019, 1, 2, 3, 10, 11);
Instant? end = Instant.FromUtc(2020, 4, 5, 6, 12, 13);
Interval interval = new Interval(start, end);
interval.Deconstruct(out start, out end);
Console.WriteLine(start);
Console.WriteLine(end);
Output:
2019-01-02T03:10:11Z
2020-04-05T06:12:13Z
Equals(Interval)
Indicates whether the value of this interval is equal to the value of the specified interval.
See the type documentation for a description of equality semantics.
Since 1.0.x
Availability net6.0, net8.0, netstandard2.0
Declaration
public bool Equals(Interval other)
Parameters
Type | Name | Description |
---|---|---|
Interval | other | The value to compare with this instance. |
Returns
Type | Description |
---|---|
bool | true if the value of this instant is equal to the value of the other parameter;
otherwise, false. |
Equals(object?)
Determines whether the specified object is equal to this instance.
See the type documentation for a description of equality semantics.
Since 1.0.x
Availability net6.0, net8.0, netstandard2.0
Declaration
public override bool Equals(object? obj)
Parameters
Type | Name | Description |
---|---|---|
object | obj | The object to compare with this instance. |
Returns
Type | Description |
---|---|
bool | true if the specified object is equal to this instance;
otherwise, false . |
Overrides
GetHashCode()
Returns the hash code for this instance.
See the type documentation for a description of equality semantics.
Since 1.0.x
Availability net6.0, net8.0, netstandard2.0
Declaration
public override int GetHashCode()
Returns
Type | Description |
---|---|
int | A 32-bit signed integer that is the hash code for this instance. |
Overrides
ToString()
Returns a string representation of this interval, in extended ISO-8601 format: the format
is "start/end" where each instant uses a format of "uuuu'-'MM'-'dd'T'HH':'mm':'ss;FFFFFFFFF'Z'".
If the start or end is infinite, the relevant part uses "StartOfTime" or "EndOfTime" to
represent this.
Since 1.0.x
Availability net6.0, net8.0, netstandard2.0
Declaration
public override string ToString()
Returns
Type | Description |
---|---|
string | A string representation of this interval. |
Overrides
Sample snippet
using NodaTime;
using System;
Instant start = Instant.FromUtc(2019, 1, 2, 10, 11, 12);
Instant end = Instant.FromUtc(2020, 2, 3, 12, 13, 14);
Interval interval = new Interval(start, end);
var stringRepresentation = interval.ToString();
Console.WriteLine(stringRepresentation);
Output:
2019-01-02T10:11:12Z/2020-02-03T12:13:14Z
Operators
operator ==(Interval, Interval)
Implements the operator ==.
See the type documentation for a description of equality semantics.
Since 1.0.x
Availability net6.0, net8.0, netstandard2.0
Declaration
public static bool operator ==(Interval left, Interval right)
Parameters
Type | Name | Description |
---|---|---|
Interval | left | The left. |
Interval | right | The right. |
Returns
Type | Description |
---|---|
bool | The result of the operator. |
operator !=(Interval, Interval)
Implements the operator !=.
See the type documentation for a description of equality semantics.
Since 1.0.x
Availability net6.0, net8.0, netstandard2.0
Declaration
public static bool operator !=(Interval left, Interval right)
Parameters
Type | Name | Description |
---|---|---|
Interval | left | The left. |
Interval | right | The right. |
Returns
Type | Description |
---|---|
bool | The result of the operator. |
Explicit Interface Implementations
IXmlSerializable.GetSchema()
This method is reserved and should not be used. When implementing the
IXmlSerializable
interface, you should return null (Nothing
in Visual Basic) from this method, and instead, if specifying a custom schema is required, apply the XmlSchemaProviderAttribute to the class.
Since 3.2.x
Availability net6.0, net8.0, netstandard2.0
Declaration
XmlSchema IXmlSerializable.GetSchema()
Returns
Type | Description |
---|---|
XmlSchema | An XmlSchema that describes the XML representation of the object that is produced by the WriteXml(XmlWriter) method and consumed by the ReadXml(XmlReader) method. |
IXmlSerializable.ReadXml(XmlReader)
Generates an object from its XML representation.
Since 1.2.x
Availability net6.0, net8.0, netstandard2.0
Declaration
void IXmlSerializable.ReadXml(XmlReader reader)
Parameters
Type | Name | Description |
---|---|---|
XmlReader | reader | The XmlReader stream from which the object is deserialized. |
IXmlSerializable.WriteXml(XmlWriter)
Converts an object into its XML representation.
Since 1.2.x
Availability net6.0, net8.0, netstandard2.0
Declaration
void IXmlSerializable.WriteXml(XmlWriter writer)
Parameters
Type | Name | Description |
---|---|---|
XmlWriter | writer | The XmlWriter stream to which the object is serialized. |