Period
Definition
Namespace: Corvus.Text.Json
Assembly: Corvus.Text.Json.dll
Source: Period.cs
Represents a period of time expressed in human chronological terms: hours, days, weeks, months and so on.
public readonly struct Period : IEquatable<Period>
Remarks
A Period contains a set of properties such as Years, Months, and so on that return the number of each unit contained within this period. Note that these properties are not normalized in any way by default, and so a Period may contain values such as "2 hours and 90 minutes". The Normalize method will convert equivalent periods into a standard representation. Periods can contain negative units as well as positive units ("+2 hours, -43 minutes, +10 seconds"), but do not differentiate between properties that are zero and those that are absent (i.e. a period created as "10 years" and one created as "10 years, zero months" are equal periods; the Months property returns zero in both cases). Period equality is implemented by comparing each property's values individually, without any normalization. (For example, a period of "24 hours" is not considered equal to a period of "1 day".) The static NormalizingEqualityComparer comparer provides an equality comparer which performs normalization before comparisons. There is no natural ordering for periods, but CreateComparer can be used to create a comparer which orders periods according to a reference date, by adding each period to that date and comparing the results. Periods operate on calendar-related types such as LocalDateTime whereas Duration operates on instants on the time line. (Note that although ZonedDateTime includes both concepts, it only supports duration-based arithmetic.) The complexity of each method in this type is hard to document precisely, and often depends on the calendar system involved in performing the actual calculations. Operations do not depend on the magnitude of the units in the period, other than for optimizations for values of zero or occasionally for particularly small values. For example, adding 10,000 days to a date does not require greater algorithmic complexity than adding 1,000 days to the same date.
Implements
Properties
| Property | Type | Description |
|---|---|---|
| Days | int |
Gets the number of days within this period. |
| HasDateComponent | bool |
Gets a value indicating whether or not this period contains any non-zero date-based properties (days or higher). |
| HasTimeComponent | bool |
Gets a value indicating whether or not this period contains any non-zero-valued time-based properties (hours or lower). |
| Hours | long |
Gets the number of hours within this period. |
MaxValue static |
Period |
A period containing the maximum value for all properties. |
| Milliseconds | long |
Gets the number of milliseconds within this period. |
| Minutes | long |
Gets the number of minutes within this period. |
MinValue static |
Period |
A period containing the minimum value for all properties. |
| Months | int |
Gets the number of months within this period. |
| Nanoseconds | long |
Gets the number of nanoseconds within this period. |
NormalizingEqualityComparer static |
IEqualityComparer<Period> |
Gets an equality comparer which compares periods by first normalizing them - so 24 hours is deemed equal to 1 day, and so on. Note that as per the... |
| Seconds | long |
Gets the number of seconds within this period. |
| Ticks | long |
Gets the number of ticks within this period. |
| Weeks | int |
Gets the number of weeks within this period. |
| Years | int |
Gets the number of years within this period. |
Zero static |
Period |
Gets a period containing only zero-valued properties. |
Methods
| Method | Description |
|---|---|
Add(Period, Period) static |
Adds two periods together, by simply adding the values for each property. |
CreateComparer(LocalDateTime) static |
Creates an IComparer for periods, using the given "base" local date/time. |
DaysBetween(LocalDate, LocalDate) static |
Returns the number of days between two LocalDate objects. |
| Equals | Compares the given object for equality with this one, as per Equals. See the type documentation for a description of equality semantics. |
FromDays(int) static |
Creates a period representing the specified number of days. |
FromHours(long) static |
Creates a period representing the specified number of hours. |
FromMilliseconds(long) static |
Creates a period representing the specified number of milliseconds. |
FromMinutes(long) static |
Creates a period representing the specified number of minutes. |
FromMonths(int) static |
Creates a period representing the specified number of months. |
FromNanoseconds(long) static |
Creates a period representing the specified number of nanoseconds. |
FromSeconds(long) static |
Creates a period representing the specified number of seconds. |
FromTicks(long) static |
Creates a period representing the specified number of ticks. |
FromWeeks(int) static |
Creates a period representing the specified number of weeks. |
FromYears(int) static |
Creates a period representing the specified number of years. |
| GetHashCode() | Returns the hash code for this period, consistent with Equals. See the type documentation for a description of equality semantics. |
| Normalize() | Returns a normalized version of this period, such that equivalent (but potentially non-equal) periods are changed to the same representation. |
PeriodParser(ReadOnlySpan<byte>, ref PeriodBuilder) static |
A parser for a json period. |
Subtract(Period, Period) static |
Subtracts one period from another, by simply subtracting each property value. |
| ToDuration() | For periods that do not contain a non-zero number of years or months, returns a duration for this period assuming a standard 7-day week, 24-hour day, 60-minute hour etc. |
| ToString() | Returns this string formatted according to the ISO8601 duration specification used by JSON schema. |
TryParse(ReadOnlySpan<byte>, ref Period) static |
Parses a string into a Period. |
Operators
| Operator | Description |
|---|---|
| operator +(Period, Period) | Adds two periods together, by simply adding the values for each property. |
| operator ==(Period, Period) | Implements the operator == (equality). See the type documentation for a description of equality semantics. |
| Implicit | Convert to a NodaTime.Period. |
| operator !=(Period, Period) | Implements the operator != (inequality). See the type documentation for a description of equality semantics. |
| operator -(Period, Period) | Subtracts one period from another, by simply subtracting each property value. |
Applies To
| Product | Versions |
|---|---|
| .NET | 9, 10 |
| .NET Standard | 2.0, 2.1 |
Collaborate with us on GitHub
The source for this content can be found on GitHub, where you can also create and review issues and pull requests.
Open an issue