Duration structv0.1.0

Source
A span of time: seconds and a fraction, normalized.
pub struct Duration {
    pub seconds: int64;
    // private fields
}

Copy. The value is seconds + nanoseconds / 1e9, and nanoseconds is always in [0, 1e9).

Fields

NameTypeDescription
secondsint64The whole seconds, carrying the sign.

Associated functions

Zero

pub func Zero() -> Duration

No time at all.

Returns: a span of no time at all

Source

FromSeconds

pub func FromSeconds(seconds: int64) -> Duration

Exactly seconds seconds.

NameTypeDescription
secondsint64how many seconds

Returns: that many seconds

Source

FromMinutes

pub func FromMinutes(minutes: int64) -> Duration?

Exactly minutes minutes, or None past the range.

NameTypeDescription
minutesint64how many minutes

Returns: that many minutes, or None when the count does not fit

Source

FromHours

pub func FromHours(hours: int64) -> Duration?

Exactly hours hours, or None past the range.

NameTypeDescription
hoursint64how many hours

Returns: that many hours, or None when the count does not fit

Source

FromMilliseconds

pub func FromMilliseconds(milliseconds: int64) -> Duration

Exactly milliseconds milliseconds. Cannot overflow: every int64 count of milliseconds fits.

NameTypeDescription
millisecondsint64how many milliseconds

Returns: that many milliseconds

Source

FromMicroseconds

pub func FromMicroseconds(microseconds: int64) -> Duration

Exactly microseconds microseconds.

NameTypeDescription
microsecondsint64how many microseconds

Returns: that many microseconds

Source

FromNanoseconds

pub func FromNanoseconds(nanoseconds: int64) -> Duration

Exactly nanoseconds nanoseconds.

NameTypeDescription
nanosecondsint64how many nanoseconds

Returns: that many nanoseconds

Source

Methods

IsZero

pub func IsZero(self: &Duration) -> bool

Whether this span is exactly nothing.

Returns: whether the span is exactly zero

Source

IsNegative

pub func IsNegative(self: &Duration) -> bool

Whether this span is negative. The sign lives in the seconds alone, which the normalization guarantees.

Returns: whether the span runs backwards

Source

WholeSeconds

pub func WholeSeconds(self: &Duration) -> int64

The whole seconds, rounded toward negative infinity.

Returns: the whole seconds, truncated toward zero

Source

SubsecondNanoseconds

pub func SubsecondNanoseconds(self: &Duration) -> uint32

The fraction beyond the whole seconds, in nanoseconds, always in [0, 1e9).

Returns: the nanoseconds within the second, in [0, 999999999]

Source

TotalMilliseconds

pub func TotalMilliseconds(self: &Duration) -> int64?

This span as milliseconds, truncated toward zero, or None where an int64 of them cannot hold it.

Returns: the whole span in milliseconds, or None when it does not fit

Source

TotalMicroseconds

pub func TotalMicroseconds(self: &Duration) -> int64?

This span as microseconds, truncated toward zero.

Returns: the whole span in microseconds, or None when it does not fit

Source

TotalNanoseconds

pub func TotalNanoseconds(self: &Duration) -> int64?

This span as nanoseconds, or None where an int64 of them cannot hold it — about 292 years.

Returns: the whole span in nanoseconds, or None when it does not fit

Source

Plus

pub func Plus(self: &Duration, other: Duration) -> Duration?

The sum of two spans, or None past the range.

NameTypeDescription
otherDurationthe value to compare against

Returns: the sum, or None when it does not fit

Source

Minus

pub func Minus(
    self: &Duration,
    other: Duration
) -> Duration?

The difference of two spans, or None past the range.

NameTypeDescription
otherDurationthe value to compare against

Returns: the difference, or None when it does not fit

Source

Negated

pub func Negated(self: &Duration) -> Duration?

This span negated, or None for the one value whose negation does not fit.

Returns: the span running the other way, or None when it does not fit

Source

Times

pub func Times(self: &Duration, factor: int64) -> Duration?

This span scaled by a whole factor, or None past the range.

NameTypeDescription
factorint64what to multiply by

Returns: the product, or None when it does not fit

Source

Equals

pub func Equals(self: &Duration, other: Duration) -> bool

Whether two spans are the same time.

NameTypeDescription
otherDurationthe value to compare against

Returns: whether the two spans are the same length

Source

Compare

pub func Compare(
    self: &Duration,
    other: Duration
) -> Ordering

How this span orders against other. Two comparisons, which the normalization pays for: the seconds decide, and the fractions only tie-break.

NameTypeDescription
otherDurationthe value to compare against

Returns: how this span orders against other

Source

Implements Display

Duration conforms to Display.

WriteDisplay

pub func WriteDisplay(
    self: &Duration,
    writer: &var TextWriter,
    spec: FormatSpec
) -> ! FormatError

Writes the span as signed decimal seconds followed by s. The magnitude is recomputed from the normalized representation rather than read off it: a negative duration stores minus one and a half seconds as seconds = -2 with a fraction of 0.5, so printing the fields as they sit would say "-2.5s". A precision names exactly that many fractional digits, 0 through 9, and truncates. Without one the fraction is written to the digits it has with trailing zeros trimmed, and omitted when it is zero, so a whole number of seconds renders as one. Reports whatever the writer reported, and UnsupportedRequest for a style other than seconds or for a precision past nine.

NameTypeDescription
writer&var TextWriterthe destination
specFormatSpecwhat the placeholder asked for

Returns: success when the whole rendering was accepted

Source

Implements Debug

Duration conforms to Debug.

WriteDebug

pub func WriteDebug(
    self: &Duration,
    writer: &var TextWriter,
    spec: FormatSpec
) -> ! FormatError

The same text as Display: a span has one reading. The failures WriteDisplay reports.

NameTypeDescription
writer&var TextWriterthe destination
specFormatSpecwhat the placeholder asked for

Returns: success when the whole rendering was accepted

Source

NanosecondsPerSecond

pub const NanosecondsPerSecond: uint32 = 1000000000;

How many nanoseconds one second holds.

Source

ParseDuration

pub func ParseDuration(text: char8[..]) -> Duration ! TimeParseError

Reads a span written as signed decimal seconds followed by s. Accepts what Display writes: an optional sign, at least one digit, an optional fraction of up to nine digits, and a closing s. A leading + is accepted and means the same as no sign. Reports InvalidSyntax with the byte offset where the shape stopped matching, and ExcessiveFraction at the first digit that a nanosecond cannot hold. A magnitude too large for the representation is InvalidSyntax at the digit that overflowed, since the text is well formed and the value simply is not one.

Source

Parameters

NameTypeDescription
textchar8[..]the text to read

Returns: the span, or why the text is not one