Struct Interval
An interval between two instants in time (start and end).
Since 1.0.x
Availability net6.0, netstandard2.0
Inherited Members
Object.Equals(Object, Object)
Object.GetType()
Object.ReferenceEquals(Object, Object)
Namespace: NodaTime
Assembly: NodaTime.dll
Syntax
public readonly struct Interval : IEquatable<Interval>, IXmlSerializable
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.
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, 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(Nullable<Instant>, Nullable<Instant>)
Since 2.0.x
Availability net6.0, netstandard2.0
Declaration
public Interval(Instant? start, Instant? end)
Parameters
Type | Name | Description |
---|---|---|
Nullable<Instant> | start | The start Instant or null. |
Nullable<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, netstandard2.0
Declaration
public readonly 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 |
---|---|
System.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, netstandard2.0
Declaration
public readonly Instant End { get; }
Property Value
Type | Description |
---|---|
Instant | The end Instant. |
Exceptions
Type | Condition |
---|---|
System.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, netstandard2.0
Declaration
public readonly bool HasEnd { get; }
Property Value
Type | Description |
---|---|
Boolean | 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, netstandard2.0
Declaration
public readonly bool HasStart { get; }
Property Value
Type | Description |
---|---|
Boolean | 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, netstandard2.0
Declaration
public readonly 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 |
---|---|
System.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, netstandard2.0
Declaration
public static XmlQualifiedName AddSchema(XmlSchemaSet xmlSchemaSet)
Parameters
Type | Name | Description |
---|---|---|
XmlSchemaSet | xmlSchemaSet | The XML schema set provided by System.Xml.Serialization.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, netstandard2.0
Declaration
public readonly bool Contains(Instant instant)
Parameters
Type | Name | Description |
---|---|---|
Instant | instant | Instant to test. |
Returns
Type | Description |
---|---|
Boolean | 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 Nullable<Instant>, out Nullable<Instant>)
Deconstruct this value into its components.
Since 2.3.x
Availability net6.0, netstandard2.0
Declaration
public readonly void Deconstruct(out Instant? start, out Instant? end)
Parameters
Type | Name | Description |
---|---|---|
Nullable<Instant> | start | The start of the interval. |
Nullable<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, netstandard2.0
Declaration
public readonly bool Equals(Interval other)
Parameters
Type | Name | Description |
---|---|---|
Interval | other | The value to compare with this instance. |
Returns
Type | Description |
---|---|
Boolean |
true if the value of this instant is equal to the value of the other parameter;
otherwise, false.
|
Equals(Object)
Determines whether the specified System.Object is equal to this instance.
See the type documentation for a description of equality semantics.
Since 1.0.x
Availability net6.0, netstandard2.0
Declaration
public override readonly bool Equals(object obj)
Parameters
Type | Name | Description |
---|---|---|
Object | obj | The System.Object to compare with this instance. |
Returns
Type | Description |
---|---|
Boolean |
true if the specified System.Object is equal to this instance;
otherwise, false .
|
Overrides
System.ValueType.Equals(System.Object)
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, netstandard2.0
Declaration
public override readonly int GetHashCode()
Returns
Type | Description |
---|---|
Int32 | A 32-bit signed integer that is the hash code for this instance. |
Overrides
System.ValueType.GetHashCode()
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, netstandard2.0
Declaration
public override readonly string ToString()
Returns
Type | Description |
---|---|
String | A string representation of this interval. |
Overrides
System.ValueType.ToString()
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
Equality(Interval, Interval)
Implements the operator ==.
See the type documentation for a description of equality semantics.
Since 1.0.x
Availability net6.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 |
---|---|
Boolean | The result of the operator. |
Inequality(Interval, Interval)
Implements the operator !=.
See the type documentation for a description of equality semantics.
Since 1.0.x
Availability net6.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 |
---|---|
Boolean | The result of the operator. |
Explicit Interface Implementations
IXmlSerializable.ReadXml(XmlReader)
Since 1.2.x
Availability net6.0, netstandard2.0
Declaration
readonly void IXmlSerializable.ReadXml(XmlReader reader)
Parameters
Type | Name | Description |
---|---|---|
XmlReader | reader |
IXmlSerializable.WriteXml(XmlWriter)
Since 1.2.x
Availability net6.0, netstandard2.0
Declaration
readonly void IXmlSerializable.WriteXml(XmlWriter writer)
Parameters
Type | Name | Description |
---|---|---|
XmlWriter | writer |
Implements
System.IEquatable<T>
System.Xml.Serialization.IXmlSerializable