Timestamp structv0.1.0

Source
A moment in UTC: seconds since the Unix epoch and a fraction, normalized.
pub struct Timestamp {
    pub seconds: int64;
    pub nanoseconds: uint32;
}

Copy. The value is seconds + nanoseconds / 1e9 from 1970-01-01T00:00:00 UTC, and nanoseconds is always in [0, 1e9).

Fields

NameTypeDescription
secondsint64Whole seconds since the epoch, carrying the sign.
nanosecondsuint32The fraction, in nanoseconds, always in [0, 1e9).

Associated functions

Epoch

pub func Epoch() -> Timestamp

The epoch itself: 1970-01-01T00:00:00 UTC exactly.

Returns: 1970-01-01T00:00:00Z

Source

Now

pub func Now() -> Timestamp

The wall clock, read now.

Returns: the wall clock, read now

Source

FromUnixSeconds

pub func FromUnixSeconds(seconds: int64) -> Timestamp

A moment from a plain second count, the shape most protocols carry.

NameTypeDescription
secondsint64how many seconds

Returns: that many seconds after the epoch

Source

Methods

UnixSeconds

pub func UnixSeconds(self: &Timestamp) -> int64

The whole seconds since the epoch, rounded toward negative infinity.

Returns: whole seconds since the epoch, negative before it

Source

SubsecondNanoseconds

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

The fraction beyond the whole seconds, in nanoseconds.

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

Source

Since

pub func Since(
    self: &Timestamp,
    earlier: Timestamp
) -> Duration?

The span from earlier to this moment, or None past a duration's range. May be negative: the wall clock jumps, and two readings taken across a clock-set really can come back reversed. That is information, not an error, which is the difference from Instant::Since.

NameTypeDescription
earlierTimestampthe earlier reading

Returns: the time between the two moments, or None when earlier is not earlier

Source

Plus

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

This moment shifted by span, or None past the range.

NameTypeDescription
spanDurationthe duration

Returns: the moment that far later, or None when it does not fit

Source

Equals

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

Whether two timestamps name the same moment.

NameTypeDescription
otherTimestampthe value to compare against

Returns: whether the two name the same moment

Source

Compare

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

How this moment orders against other.

NameTypeDescription
otherTimestampthe value to compare against

Returns: how this moment orders against other

Source

IsInCalendarRange

pub func IsInCalendarRange(self: &Timestamp) -> bool

Whether this moment falls inside the years the calendar spells, and so has an RFC 3339 text. The bound is years 0000 through 9999, which is the range Date arithmetic already treats as the package's own and the range RFC 3339's four year digits allow. A caller that must not meet a formatting failure asks here first; one that renders through a placeholder learns the same thing as ValueOutOfRange. Borrows the moment and leaves it untouched. Cannot fail; runs in constant time and allocates nothing.

Returns: true when the calendar form exists for this moment

Source

Implements Display

Timestamp conforms to Display.

WriteDisplay

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

Writes the moment as RFC 3339 in UTC, or as the Unix second count under the unix style. The calendar form is the default and the only one an unadorned placeholder produces: 1970-01-01T00:00:00Z rather than 0. rfc3339 and iso8601 name that same form explicitly, and unix asks for the signed second count instead — a value with two readings does not get to guess which one a caller meant. A precision names exactly that many fractional digits, 0 through 9, and truncates, in either form. Without one the fraction is written to the digits it has with trailing zeros trimmed, and omitted when it is zero. Reports whatever the writer reported, UnsupportedRequest for a style other than the three named above or for a precision past nine, and ValueOutOfRange for a moment outside years 0000 through 9999, which RFC 3339's four year digits cannot spell. The range is checked before anything is written, so a refused moment leaves the destination as it was.

NameTypeDescription
writer&var TextWriterthe destination
specFormatSpecwhat the placeholder asked for

Returns: success when the whole moment was accepted

Source

Implements Debug

Timestamp conforms to Debug.

WriteDebug

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

The same text as Display: a moment has one reading, and the style chooses between the two forms here too. The failures WriteDisplay reports, including ValueOutOfRange before any output.

NameTypeDescription
writer&var TextWriterthe destination
specFormatSpecwhat the placeholder asked for

Returns: success when the whole moment was accepted

Source