Class PeriodBuilder
A mutable builder class for Period values. Each property can
be set independently, and then a Period can be created from the result
using the Build() method.
Since 1.0.x
Availability net6.0, net8.0, netstandard2.0
Implements
Inherited Members
Namespace: NodaTime
Assembly: NodaTime.dll
Syntax
public sealed class PeriodBuilder : IXmlSerializable
Constructors
PeriodBuilder()
Creates a new period builder with an initially zero period.
Since 1.0.x
Availability net6.0, net8.0, netstandard2.0
Declaration
public PeriodBuilder()
PeriodBuilder(Period)
Creates a new period builder with the values from an existing
period. Calling this constructor instead of ToBuilder()
allows object initializers to be used.
Since 1.0.x
Availability net6.0, net8.0, netstandard2.0
Declaration
public PeriodBuilder(Period period)
Parameters
| Type | Name | Description |
|---|---|---|
| Period | period | An existing period to copy values from. |
Sample snippet
using NodaTime;
using System;
Period existingPeriod = Period.FromYears(5);
PeriodBuilder periodBuilder = new PeriodBuilder(existingPeriod);
periodBuilder.Months = 6;
Period period = periodBuilder.Build();
Console.WriteLine(period.Years);
Console.WriteLine(period.Months);
Output:
5
6
Properties
Days
Gets or sets the number of days within the period.
Since 1.0.x
Availability net6.0, net8.0, netstandard2.0
Declaration
public int Days { get; set; }
Property Value
| Type | Description |
|---|---|
| int | The number of days within the period. |
Hours
Gets or sets the number of hours within the period.
Since 1.0.x
Availability net6.0, net8.0, netstandard2.0
Declaration
public long Hours { get; set; }
Property Value
| Type | Description |
|---|---|
| long | The number of hours within the period. |
this[PeriodUnits]
Gets or sets the value of a single unit.
Since 1.0.x
Availability net6.0, net8.0, netstandard2.0
Declaration
public long this[PeriodUnits unit] { get; set; }
Parameters
| Type | Name | Description |
|---|---|---|
| PeriodUnits | unit | A single value within the PeriodUnits enumeration. |
Property Value
| Type | Description |
|---|---|
| long | The value of the given unit within this period builder, or zero if the unit is unset. |
Remarks
The type of this indexer is long for uniformity, but any date unit (year, month, week, day) will only ever have a value in the range of int.
For the Nanoseconds unit, the value is converted to Int64 when reading from the indexer, causing it to
fail if the value is out of range (around 250 years). To access the values of very large numbers of nanoseconds, use the Nanoseconds
property directly.
Exceptions
| Type | Condition |
|---|---|
| ArgumentOutOfRangeException | unit is not a single unit, or a value is provided for a date unit which is outside the range of int. |
Milliseconds
Gets or sets the number of milliseconds within the period.
Since 1.0.x
Availability net6.0, net8.0, netstandard2.0
Declaration
public long Milliseconds { get; set; }
Property Value
| Type | Description |
|---|---|
| long | The number of milliseconds within the period. |
Minutes
Gets or sets the number of minutes within the period.
Since 1.0.x
Availability net6.0, net8.0, netstandard2.0
Declaration
public long Minutes { get; set; }
Property Value
| Type | Description |
|---|---|
| long | The number of minutes within the period. |
Months
Gets or sets the number of months within the period.
Since 1.0.x
Availability net6.0, net8.0, netstandard2.0
Declaration
public int Months { get; set; }
Property Value
| Type | Description |
|---|---|
| int | The number of months within the period. |
Nanoseconds
Gets or sets the number of nanoseconds within the period.
Since 2.0.x
Availability net6.0, net8.0, netstandard2.0
Declaration
public long Nanoseconds { get; set; }
Property Value
| Type | Description |
|---|---|
| long | The number of nanoseconds within the period. |
Seconds
Gets or sets the number of seconds within the period.
Since 1.0.x
Availability net6.0, net8.0, netstandard2.0
Declaration
public long Seconds { get; set; }
Property Value
| Type | Description |
|---|---|
| long | The number of seconds within the period. |
Ticks
Gets or sets the number of ticks within the period.
Since 1.0.x
Availability net6.0, net8.0, netstandard2.0
Declaration
public long Ticks { get; set; }
Property Value
| Type | Description |
|---|---|
| long | The number of ticks within the period. |
Weeks
Gets or sets the number of weeks within the period.
Since 1.0.x
Availability net6.0, net8.0, netstandard2.0
Declaration
public int Weeks { get; set; }
Property Value
| Type | Description |
|---|---|
| int | The number of weeks within the period. |
Years
Gets or sets the number of years within the period.
Since 1.0.x
Availability net6.0, net8.0, netstandard2.0
Declaration
public int Years { get; set; }
Property Value
| Type | Description |
|---|---|
| int | The number of years within the period. |
Methods
AddSchema(XmlSchemaSet)
Adds the XML schema type describing the structure of the PeriodBuilder 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. |
Build()
Builds a period from the properties in this builder.
Since 1.0.x
Availability net6.0, net8.0, netstandard2.0
Declaration
public Period Build()
Returns
| Type | Description |
|---|---|
| Period | A period containing the values from this builder. |
Sample snippet
using NodaTime;
using System;
PeriodBuilder periodBuilder = new PeriodBuilder()
{
Years = 2,
Months = 3,
Days = 4
};
Period period = periodBuilder.Build();
Console.WriteLine(period.Years);
Console.WriteLine(period.Months);
Console.WriteLine(period.Days);
Output:
2
3
4
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. |