Duration structv0.1.0
pub struct Duration {
pub seconds: int64;
// private fields
}
Copy. The value is seconds + nanoseconds / 1e9, and nanoseconds is always in [0, 1e9).
Fields
| Name | Type | Description |
|---|---|---|
seconds | int64 | The whole seconds, carrying the sign. |
Associated functions
Zero
pub func Zero() -> Duration
No time at all.
Returns: a span of no time at all
FromSeconds
pub func FromSeconds(seconds: int64) -> Duration
Exactly seconds seconds.
| Name | Type | Description |
|---|---|---|
seconds | int64 | how many seconds |
Returns: that many seconds
FromMinutes
pub func FromMinutes(minutes: int64) -> Duration?
Exactly minutes minutes, or None past the range.
| Name | Type | Description |
|---|---|---|
minutes | int64 | how many minutes |
Returns: that many minutes, or None when the count does not fit
FromHours
pub func FromHours(hours: int64) -> Duration?
Exactly hours hours, or None past the range.
| Name | Type | Description |
|---|---|---|
hours | int64 | how many hours |
Returns: that many hours, or None when the count does not fit
FromMilliseconds
pub func FromMilliseconds(milliseconds: int64) -> Duration
Exactly milliseconds milliseconds.
Cannot overflow: every int64 count of milliseconds fits.
| Name | Type | Description |
|---|---|---|
milliseconds | int64 | how many milliseconds |
Returns: that many milliseconds
FromMicroseconds
pub func FromMicroseconds(microseconds: int64) -> Duration
Exactly microseconds microseconds.
| Name | Type | Description |
|---|---|---|
microseconds | int64 | how many microseconds |
Returns: that many microseconds
FromNanoseconds
pub func FromNanoseconds(nanoseconds: int64) -> Duration
Exactly nanoseconds nanoseconds.
| Name | Type | Description |
|---|---|---|
nanoseconds | int64 | how many nanoseconds |
Returns: that many nanoseconds
Methods
IsZero
pub func IsZero(self: &Duration) -> bool
Whether this span is exactly nothing.
Returns: whether the span is exactly zero
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
WholeSeconds
pub func WholeSeconds(self: &Duration) -> int64
The whole seconds, rounded toward negative infinity.
Returns: the whole seconds, truncated toward zero
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]
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
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
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
Plus
pub func Plus(self: &Duration, other: Duration) -> Duration?
The sum of two spans, or None past the range.
| Name | Type | Description |
|---|---|---|
other | Duration | the value to compare against |
Returns: the sum, or None when it does not fit
Minus
pub func Minus(
self: &Duration,
other: Duration
) -> Duration?
The difference of two spans, or None past the range.
| Name | Type | Description |
|---|---|---|
other | Duration | the value to compare against |
Returns: the difference, or None when it does not fit
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
Times
pub func Times(self: &Duration, factor: int64) -> Duration?
This span scaled by a whole factor, or None past the range.
| Name | Type | Description |
|---|---|---|
factor | int64 | what to multiply by |
Returns: the product, or None when it does not fit
Equals
pub func Equals(self: &Duration, other: Duration) -> bool
Whether two spans are the same time.
| Name | Type | Description |
|---|---|---|
other | Duration | the value to compare against |
Returns: whether the two spans are the same length
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.
| Name | Type | Description |
|---|---|---|
other | Duration | the value to compare against |
Returns: how this span orders against other
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.
| Name | Type | Description |
|---|---|---|
writer | &var TextWriter | the destination |
spec | FormatSpec | what the placeholder asked for |
Returns: success when the whole rendering was accepted
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.
| Name | Type | Description |
|---|---|---|
writer | &var TextWriter | the destination |
spec | FormatSpec | what the placeholder asked for |
Returns: success when the whole rendering was accepted
Related
NanosecondsPerSecond
pub const NanosecondsPerSecond: uint32 = 1000000000;
How many nanoseconds one second holds.
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.
Parameters
| Name | Type | Description |
|---|---|---|
text | char8[..] | the text to read |
Returns: the span, or why the text is not one