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, netstandard2.0
Inheritance
Object
PeriodBuilder
Implements
IXmlSerializable
Inherited Members
Object.Equals(Object)
Object.Equals(Object, Object)
Object.GetHashCode()
Object.GetType()
Object.MemberwiseClone()
Object.ReferenceEquals(Object, Object)
Object.ToString()
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, 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, 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, netstandard2.0
Declaration
public int Days { get; set; }
Property Value
Type | Description |
---|---|
Int32 | 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, netstandard2.0
Declaration
public long Hours { get; set; }
Property Value
Type | Description |
---|---|
Int64 | The number of hours within the period. |
Item[PeriodUnits]
Gets or sets the value of a single unit.
Since 1.0.x
Availability net6.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 |
---|---|
Int64 | The value of the given unit within this period builder, or zero if the unit is unset. |
Remarks
The type of this indexer is System.Int64 for uniformity, but any date unit (year, month, week, day) will only ever have a value in the range of System.Int32.
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 System.Int32. |
Milliseconds
Gets or sets the number of milliseconds within the period.
Since 1.0.x
Availability net6.0, netstandard2.0
Declaration
public long Milliseconds { get; set; }
Property Value
Type | Description |
---|---|
Int64 | 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, netstandard2.0
Declaration
public long Minutes { get; set; }
Property Value
Type | Description |
---|---|
Int64 | 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, netstandard2.0
Declaration
public int Months { get; set; }
Property Value
Type | Description |
---|---|
Int32 | 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, netstandard2.0
Declaration
public long Nanoseconds { get; set; }
Property Value
Type | Description |
---|---|
Int64 | 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, netstandard2.0
Declaration
public long Seconds { get; set; }
Property Value
Type | Description |
---|---|
Int64 | 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, netstandard2.0
Declaration
public long Ticks { get; set; }
Property Value
Type | Description |
---|---|
Int64 | 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, netstandard2.0
Declaration
public int Weeks { get; set; }
Property Value
Type | Description |
---|---|
Int32 | 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, netstandard2.0
Declaration
public int Years { get; set; }
Property Value
Type | Description |
---|---|
Int32 | 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, 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 . |
Build()
Builds a period from the properties in this builder.
Since 1.0.x
Availability net6.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.ReadXml(XmlReader)
Since 1.2.x
Availability net6.0, netstandard2.0
Declaration
void IXmlSerializable.ReadXml(XmlReader reader)
Parameters
Type | Name | Description |
---|---|---|
XmlReader | reader |
IXmlSerializable.WriteXml(XmlWriter)
Since 1.2.x
Availability net6.0, netstandard2.0
Declaration
void IXmlSerializable.WriteXml(XmlWriter writer)
Parameters
Type | Name | Description |
---|---|---|
XmlWriter | writer |
Implements
System.Xml.Serialization.IXmlSerializable