Struct GoDuration
Implements support for GO Language formatted durations. This class is useful for integrating with GO applications.
Namespace: Neon.Time
Assembly: Neon.Common.dll
Syntax
public struct GoDuration
Remarks
note
GoDuration measures time down 1 nanosecond resolution whereas TimeSpan's resolution is 100ns and both implementations use a signed 64-bit integer as the underlying representation. This means that GoDuration can represent of maximum duration of about 290 years (positive and negative) where TimeSpan can handle about 29,000 years.
This class will throw a ArgumentOutOfRangeException when converting a TimeSpan that is beyound the capability of a GoDuration.
Constructors
GoDuration(long)
Constructs a GoDuration from nanoseconds.
Declaration
public GoDuration(long nanoseconds)
Parameters
| Type | Name | Description |
|---|---|---|
| long | nanoseconds | The duration in nanoseconds. |
GoDuration(TimeSpan)
Constructs an instance from a TimeSpan.
Declaration
public GoDuration(TimeSpan timespan)
Parameters
| Type | Name | Description |
|---|---|---|
| TimeSpan | timespan | The time span. |
Exceptions
| Type | Condition |
|---|---|
| ArgumentOutOfRangeException | Thrown if the input is outside the capability of a GoDuration. |
Fields
PartialRegEx
The partial regular expression that can be used to validate GOLANG duration strings. This does not include the start/end anchors and is suitable for situations where these are implied.
Declaration
public const string PartialRegEx = "0|(((\\d+(\\.\\d+)?h)(\\d+(\\.\\d+)?m)?(\\d+(\\.\\d+)?s)?(\\d+(\\.\\d+)?ms)?((\\d+(\\.\\d+)?us)|(\\d+(.\\d+)?µs))?(\\d+(\\.\\d+)?ns)?)|((\\d+(\\.\\d+)?h)?(\\d+(\\.\\d+)?m)(\\d+(\\.\\d+)?s)?(\\d+(\\.\\d+)?ms)?((\\d+(\\.\\d+)?us)|(\\d+(.\\d+)?µs))?(\\d+(\\.\\d+)?ns)?)|((\\d+(\\.\\d+)?h)?(\\d+(\\.\\d+)?m)?(\\d+(\\.\\d+)?s)(\\d+(\\.\\d+)?ms)?((\\d+(\\.\\d+)?us)|(\\d+(.\\d+)?µs))?(\\d+(\\.\\d+)?ns)?)|((\\d+(\\.\\d+)?h)?(\\d+(\\.\\d+)?m)?(\\d+(\\.\\d+)?s)?(\\d+(\\.\\d+)?ms)((\\d+(\\.\\d+)?us)|(\\d+(.\\d+)?µs))?(\\d+(\\.\\d+)?ns)?)|((\\d+(\\.\\d+)?h)?(\\d+(\\.\\d+)?m)?(\\d+(\\.\\d+)?s)?(\\d+(\\.\\d+)?ms)?((\\d+(\\.\\d+)?us)|(\\d+(.\\d+)?µs))(\\d+(\\.\\d+)?ns)?)|((\\d+(\\.\\d+)?h)?(\\d+(\\.\\d+)?m)?(\\d+(\\.\\d+)?s)?(\\d+(\\.\\d+)?ms)?((\\d+(\\.\\d+)?us)|(\\d+(.\\d+)?µs))?(\\d+(\\.\\d+)?ns)))"
Field Value
| Type | Description |
|---|---|
| string |
RegEx
The full regular expression (including start/end anchors) use to validate GOLANG duration strings.
Declaration
public const string RegEx = "^(0|(((\\d+(\\.\\d+)?h)(\\d+(\\.\\d+)?m)?(\\d+(\\.\\d+)?s)?(\\d+(\\.\\d+)?ms)?((\\d+(\\.\\d+)?us)|(\\d+(.\\d+)?µs))?(\\d+(\\.\\d+)?ns)?)|((\\d+(\\.\\d+)?h)?(\\d+(\\.\\d+)?m)(\\d+(\\.\\d+)?s)?(\\d+(\\.\\d+)?ms)?((\\d+(\\.\\d+)?us)|(\\d+(.\\d+)?µs))?(\\d+(\\.\\d+)?ns)?)|((\\d+(\\.\\d+)?h)?(\\d+(\\.\\d+)?m)?(\\d+(\\.\\d+)?s)(\\d+(\\.\\d+)?ms)?((\\d+(\\.\\d+)?us)|(\\d+(.\\d+)?µs))?(\\d+(\\.\\d+)?ns)?)|((\\d+(\\.\\d+)?h)?(\\d+(\\.\\d+)?m)?(\\d+(\\.\\d+)?s)?(\\d+(\\.\\d+)?ms)((\\d+(\\.\\d+)?us)|(\\d+(.\\d+)?µs))?(\\d+(\\.\\d+)?ns)?)|((\\d+(\\.\\d+)?h)?(\\d+(\\.\\d+)?m)?(\\d+(\\.\\d+)?s)?(\\d+(\\.\\d+)?ms)?((\\d+(\\.\\d+)?us)|(\\d+(.\\d+)?µs))(\\d+(\\.\\d+)?ns)?)|((\\d+(\\.\\d+)?h)?(\\d+(\\.\\d+)?m)?(\\d+(\\.\\d+)?s)?(\\d+(\\.\\d+)?ms)?((\\d+(\\.\\d+)?us)|(\\d+(.\\d+)?µs))?(\\d+(\\.\\d+)?ns))))$"
Field Value
| Type | Description |
|---|---|
| string |
TicksPerHour
The number of nanosecond ticks per minute.
Declaration
public const long TicksPerHour = 3600000000000
Field Value
| Type | Description |
|---|---|
| long |
TicksPerMicrosecond
The number of nanosecond ticks per micrososecond.
Declaration
public const long TicksPerMicrosecond = 1000
Field Value
| Type | Description |
|---|---|
| long |
TicksPerMillisecond
The number of nanosecond ticks per millisecond.
Declaration
public const long TicksPerMillisecond = 1000000
Field Value
| Type | Description |
|---|---|
| long |
TicksPerMinute
The number of nanosecond ticks per minute.
Declaration
public const long TicksPerMinute = 60000000000
Field Value
| Type | Description |
|---|---|
| long |
TicksPerSecond
The number of nanosecond ticks per second;
Declaration
public const long TicksPerSecond = 1000000000
Field Value
| Type | Description |
|---|---|
| long |
Properties
Hours
Returns the hours component of the duration.
Declaration
public long Hours { get; }
Property Value
| Type | Description |
|---|---|
| long |
MaxValue
Returns the maximum possible GoDuration.
Declaration
public static GoDuration MaxValue { get; }
Property Value
| Type | Description |
|---|---|
| GoDuration |
Microseconds
Returns the microsecond component of the duration.
Declaration
public long Microseconds { get; }
Property Value
| Type | Description |
|---|---|
| long |
Milliseconds
Returns the millisecond component of the duration.
Declaration
public long Milliseconds { get; }
Property Value
| Type | Description |
|---|---|
| long |
MinValue
Returns the minimum possible GoDuration.
Declaration
public static GoDuration MinValue { get; }
Property Value
| Type | Description |
|---|---|
| GoDuration |
Minutes
Returns the minutes component of the duration.
Declaration
public long Minutes { get; }
Property Value
| Type | Description |
|---|---|
| long |
Nanoseconds
Returns the nanosecond component of the duration.
Declaration
public long Nanoseconds { get; }
Property Value
| Type | Description |
|---|---|
| long |
Seconds
Returns the second component of the duration.
Declaration
public long Seconds { get; }
Property Value
| Type | Description |
|---|---|
| long |
Ticks
The duration expressed as nanosecond ticks.
Declaration
public readonly long Ticks { get; }
Property Value
| Type | Description |
|---|---|
| long |
TimeSpan
Returns the equivalent TimeSpan.
Declaration
public TimeSpan TimeSpan { get; }
Property Value
| Type | Description |
|---|---|
| TimeSpan |
TotalHours
Returns the total number of hours.
Declaration
public double TotalHours { get; }
Property Value
| Type | Description |
|---|---|
| double |
TotalMicroseconds
Returns the total number of microseconds.
Declaration
public double TotalMicroseconds { get; }
Property Value
| Type | Description |
|---|---|
| double |
TotalMilliseconds
Returns the total number of milliseconds.
Declaration
public double TotalMilliseconds { get; }
Property Value
| Type | Description |
|---|---|
| double |
TotalMinutes
Returns the total number of minutes.
Declaration
public double TotalMinutes { get; }
Property Value
| Type | Description |
|---|---|
| double |
TotalNanoseconds
Returns the duration as nanoseconds.
Declaration
public double TotalNanoseconds { get; }
Property Value
| Type | Description |
|---|---|
| double |
TotalSeconds
Returns the total number of seconds.
Declaration
public double TotalSeconds { get; }
Property Value
| Type | Description |
|---|---|
| double |
Zero
Returns a zero GoDuration .
Declaration
public static GoDuration Zero { get; }
Property Value
| Type | Description |
|---|---|
| GoDuration |
Methods
FromHours(double)
Returns a GoDuration from hours.
Declaration
public static GoDuration FromHours(double hours)
Parameters
| Type | Name | Description |
|---|---|---|
| double | hours | The duration in hours. |
Returns
| Type | Description |
|---|---|
| GoDuration | The new GoDuration. |
Exceptions
| Type | Condition |
|---|---|
| ArgumentOutOfRangeException | Thrown if the input is outside the capability of a GoDuration. |
FromMicroseconds(double)
Returns a GoDuration from microseconds.
Declaration
public static GoDuration FromMicroseconds(double milliseconds)
Parameters
| Type | Name | Description |
|---|---|---|
| double | milliseconds | The duration in microseconds. |
Returns
| Type | Description |
|---|---|
| GoDuration | The new GoDuration. |
Exceptions
| Type | Condition |
|---|---|
| ArgumentOutOfRangeException | Thrown if the input is outside the capability of a GoDuration. |
FromMilliseconds(double)
Returns a GoDuration from milliseconds.
Declaration
public static GoDuration FromMilliseconds(double milliseconds)
Parameters
| Type | Name | Description |
|---|---|---|
| double | milliseconds | The duration in milliseconds. |
Returns
| Type | Description |
|---|---|
| GoDuration | The new GoDuration. |
Exceptions
| Type | Condition |
|---|---|
| ArgumentOutOfRangeException | Thrown if the input is outside the capability of a GoDuration. |
FromMinutes(double)
Returns a GoDuration from minutes.
Declaration
public static GoDuration FromMinutes(double minutes)
Parameters
| Type | Name | Description |
|---|---|---|
| double | minutes | The duration in minutes. |
Returns
| Type | Description |
|---|---|
| GoDuration | The new GoDuration. |
Exceptions
| Type | Condition |
|---|---|
| ArgumentOutOfRangeException | Thrown if the input is outside the capability of a GoDuration. |
FromNanoseconds(long)
Returns a GoDuration from nanoseconds.
Declaration
public static GoDuration FromNanoseconds(long nanoseconds)
Parameters
| Type | Name | Description |
|---|---|---|
| long | nanoseconds | The duration in nanoseconds. |
Returns
| Type | Description |
|---|---|
| GoDuration | The new GoDuration. |
FromSeconds(double)
Returns a GoDuration from seconds.
Declaration
public static GoDuration FromSeconds(double seconds)
Parameters
| Type | Name | Description |
|---|---|---|
| double | seconds | The duration in seconds. |
Returns
| Type | Description |
|---|---|
| GoDuration | The new GoDuration. |
Exceptions
| Type | Condition |
|---|---|
| ArgumentOutOfRangeException | Thrown if the input is outside the capability of a GoDuration. |
FromTimeSpan(TimeSpan)
Creates a GoDuration from a TimeSpan.
Declaration
public static GoDuration FromTimeSpan(TimeSpan timespan)
Parameters
| Type | Name | Description |
|---|---|---|
| TimeSpan | timespan | The input time span. |
Returns
| Type | Description |
|---|---|
| GoDuration | The new GoDuration. |
Parse(string)
Parses a GoDuration from a string.
Declaration
public static GoDuration Parse(string input)
Parameters
| Type | Name | Description |
|---|---|---|
| string | input | The input string. |
Returns
| Type | Description |
|---|---|
| GoDuration | The parsed GoDuration. |
Remarks
The input is a possibly signed sequence of decimal numbers, each with optional fraction and a unit suffix, such as "300ms", "-1.5h" or "2h45m". Valid time units are "ns", "us" (or "µs"), "ms", "s", "m", "h".
note
null or empty strings are parsed as Zero.
note
GO timespans are limited to about 290 years (the maximum number of nanoseconds that can be represented in a signed 64-bit integer).
Exceptions
| Type | Condition |
|---|---|
| FormatException | Thrown if the input is not valid. |
ToPretty()
Renders the duration into a string including hour, minute, and seconds with fractions as required, avoiding millisecond, microsecond, and nanosecond units.
Declaration
public string ToPretty()
Returns
| Type | Description |
|---|---|
| string | The pretty string. |
ToString()
Renders the time span as a GO Duration compatible string.
Declaration
public override string ToString()
Returns
| Type | Description |
|---|---|
| string | The GO duration. |
Overrides
TryParse(string, out GoDuration)
Attempts to parse a GO formatted timespan.
Declaration
public static bool TryParse(string input, out GoDuration goTimeSpan)
Parameters
| Type | Name | Description |
|---|---|---|
| string | input | The input string. |
| GoDuration | goTimeSpan | Returns as the parsed timespan on success. |
Returns
| Type | Description |
|---|---|
| bool |
|
Remarks
The input is a possibly signed sequence of decimal numbers, each with optional fraction and a unit suffix, such as "300ms", "-1.5h" or "2h45m". Valid time units are "ns", "us" (or "µs"), "ms", "s", "m", "h".
note
GO timespans are limited to about 290 years (the maximum number of nanoseconds that can be represented in a signed 64-bit integer).
Operators
implicit operator TimeSpan(GoDuration)
Implicitly converts a GoDuration into a TimeSpan.
Declaration
public static implicit operator TimeSpan(GoDuration goTimeSpan)
Parameters
| Type | Name | Description |
|---|---|---|
| GoDuration | goTimeSpan | The input GoDuration. |
Returns
| Type | Description |
|---|---|
| TimeSpan | The equivalent TimeSpan. |
implicit operator GoDuration(TimeSpan)
Implicitly converts a TimeSpan into a GoDuration.
Declaration
public static implicit operator GoDuration(TimeSpan timespan)
Parameters
| Type | Name | Description |
|---|---|---|
| TimeSpan | timespan | The input TimeSpan. |
Returns
| Type | Description |
|---|---|
| GoDuration | The equivalent GoDuration. |