Skip to content

Duration

Namespace: Shadop.Archmage.Sdk

Represents a duration as a signed number of nanoseconds.

public struct Duration

Inheritance ObjectValueTypeDuration
Implements IEquatable, IComparable

Remarks:

Duration provides methods to retrieve duration in various units (nanoseconds, microseconds, milliseconds, seconds, minutes, hours) and supports arithmetic operations, comparisons, and JSON serialization.

The struct uses nanosecond precision internally and includes static readonly constants for common durations. The ToString method formats durations in a human-readable format like “1h30m5s”.

Duration serializes to and from a compact JSON array format for efficient storage.

public static Duration Zero;
public static Duration Nanosecond;
public static Duration Microsecond;
public static Duration Millisecond;
public static Duration Second;
public static Duration Minute;
public static Duration Hour;
Duration(long nanoseconds)

nanoseconds Int64

Returns the absolute value of the duration.

Duration Abs()

Duration

Returns floating-point duration in hours (may lose precision).

double Hours()

Double

Returns floating-point duration in minutes (may lose precision).

double Minutes()

Double

Returns floating-point duration in seconds (may lose precision).

double Seconds()

Double

Returns integer millisecond count (truncates sub-millisecond precision).

long Milliseconds()

Int64

Returns integer microsecond count (truncates sub-microsecond precision).

long Microseconds()

Int64

long Nanoseconds()

Int64

Returns the result of truncating this duration toward zero to the nearest multiple of the specified duration.

Duration Truncate(Duration m)

m Duration
The divisor duration. If zero or negative, this duration is returned unchanged.

Duration
A Duration truncated to a multiple of m.

Returns the result of rounding this duration to the nearest multiple of the specified duration.

Duration Round(Duration m)

m Duration
The divisor duration. If zero or negative, this duration is returned unchanged.

Duration
A Duration rounded to the nearest multiple of m.

Formats the duration as a human-readable string.

string ToString()

String
A formatted duration string.

Remarks:

Examples: “1h30m5s”, “500ms”, “1us”, “-2h15m”.

Converts to TimeSpan (100-nanosecond precision; may lose sub-tick precision).

TimeSpan ToTimeSpan()

TimeSpan

Creates Duration from TimeSpan.

Duration FromTimeSpan(TimeSpan timeSpan)

timeSpan TimeSpan

Duration

int CompareTo(Duration other)

other Duration

Int32

bool Equals(Duration other)

other Duration

Boolean

bool Equals(object obj)

obj Object

Boolean

int GetHashCode()

Int32