Skip to main content

Timestamp

Struct Timestamp 

Source
pub struct Timestamp(/* private fields */);
Expand description

Microseconds elapsed since the Unix epoch (January 1, 1970 00:00:00 UTC).

§The unit is microseconds (#254)

Before v0.4.0 this held whole seconds. It now holds microseconds, and microseconds are the one canonical on-disk unit regardless of a field’s declared timestamp(s|ms|us) precision — a timestamp(ms) field simply stores millisecond-aligned microsecond values. Storing each field in its own unit would make a Timestamp value unit-ambiguous at runtime, which is only resolvable with a fatter value (a layout change) or a Timestamp<const U> (generated signature churn everywhere).

i64 microseconds spans ±292,277 years, which is why microseconds rather than nanoseconds are the floor: nanoseconds would cap the type at 1678–2262, putting a birth date or a long-dated bond maturity out of range.

§The wire form is RFC 3339, not a number

[Display], FromStr and serde all speak RFC 3339 — one rendering everywhere, so the create handler’s id.to_string() and a REST path parameter’s parse() agree with no generated codec. Output is fixed (exactly 6 fractional digits, always Z, including for a timestamp(s) field: Display is on the type and cannot see a field’s precision); input is lenient (fewer or more fractional digits, lowercase t/z, any numeric offset).

A number would have been a silent break — a client reading seconds would start reading microseconds and display 1970 forever. A client expecting a number and receiving a string fails immediately and visibly.

§Range

The rendering has no failure mode, so the type’s range is RFC 3339’s year range 0000–9999 (is_rfc3339_representable), enforced by the generated write path rather than here. Values outside it still render and re-parse losslessly (with an extended year), so no data is trapped; they are simply refused at the boundary.

§Examples

use forgedb_types::Timestamp;

let ts = Timestamp::from_micros(1_775_000_000_123_456);
assert_eq!(ts.to_rfc3339(), "2026-03-31T23:33:20.123456Z");
assert_eq!(ts.as_micros(), 1_775_000_000_123_456);
assert_eq!("2026-03-31T23:33:20.123456Z".parse::<Timestamp>().unwrap(), ts);

Implementations§

Source§

impl Timestamp

Source

pub fn from_micros(micros: i64) -> Self

Creates a timestamp from microseconds since the Unix epoch.

use forgedb_types::Timestamp;
assert_eq!(Timestamp::from_micros(1_234_567_890).as_micros(), 1_234_567_890);
Source

pub fn as_micros(&self) -> i64

Returns the timestamp as microseconds since the Unix epoch.

Source

pub fn now() -> Self

Returns the current timestamp, in microseconds.

use forgedb_types::Timestamp;
assert!(Timestamp::now().as_micros() > 1_577_836_800_000_000);
Source

pub fn to_rfc3339(&self) -> String

Renders the instant as RFC 3339: exactly 6 fractional digits, always Z.

Total by construction — this is what [Display] calls, and Display cannot fail. A value outside RFC 3339’s year range renders with an extended year (+10000-… / -0001-…) so the round-trip stays lossless; such a value is refused at the write boundary instead, via is_rfc3339_representable.

Source

pub fn from_rfc3339(s: &str) -> Result<Self, TimestampParseError>

Parses an RFC 3339 instant. Lenient about everything the rendering fixes: any number of fractional digits (sub-microsecond digits are truncated, since the stored unit is the finest thing the value can mean), a lowercase t/z separator, and any numeric offset (±HH:MM / ±HHMM / ±HH).

An offset is applied, not recorded: the stored value is an instant.

Source

pub fn floor_to_micros(&self, quantum_us: i64) -> Self

Floors the value to a multiple of quantum_us (res 5): a user-supplied value finer than a field’s declared precision is recorded coarsely rather than refused, because rejecting a valid measurement for being too precise only exports the flooring to every client.

Floor, never truncate toward zero. Truncation rounds a pre-epoch value forward in time (-0.5s → 0s), which breaks monotonicity across 1970 — two instants an hour apart could quantize to the same value from opposite sides, or swap order.

A quantum_us of 0 or less is the identity (there is nothing to floor to), so this never divides by zero on a caller’s behalf.

Source

pub fn is_rfc3339_representable(&self) -> bool

Whether the value renders inside RFC 3339’s year range (0000–9999).

i64 microseconds spans ±292,277 years, so the wire form is narrower than the storage type. Since [Display] cannot fail, the narrowing is enforced as a validity constraint at the write boundary (a 422) rather than as a rendering error — and this predicate is what the generated write path checks.

Trait Implementations§

Source§

impl Clone for Timestamp

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Copy for Timestamp

Source§

impl Debug for Timestamp

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<'de> Deserialize<'de> for Timestamp

Source§

fn deserialize<D: Deserializer<'de>>(de: D) -> Result<Self, D::Error>

Deserialize this value from the given Serde deserializer. Read more
Source§

impl Display for Timestamp

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Eq for Timestamp

Source§

impl From<Timestamp> for i64

Source§

fn from(ts: Timestamp) -> Self

Converts to this type from the input type.
Source§

impl From<Timestamp> for Value

Source§

fn from(v: Timestamp) -> Self

Converts to this type from the input type.
Source§

impl From<i64> for Timestamp

Source§

fn from(micros: i64) -> Self

Microseconds since the epoch, matching Timestamp::from_micros.

Source§

impl FromStr for Timestamp

Source§

type Err = TimestampParseError

The associated error which can be returned from parsing.
Source§

fn from_str(s: &str) -> Result<Self, Self::Err>

Parses a string s to return a value of this type. Read more
Source§

impl Hash for Timestamp

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl Ord for Timestamp

Source§

fn cmp(&self, other: &Self) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 (const: unstable) · Source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 (const: unstable) · Source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 (const: unstable) · Source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. Read more
Source§

fn clamp_to<R>(self, range: R) -> Self
where Self: Sized, R: ClampBounds<Self>,

🔬This is a nightly-only experimental API. (clamp_to)
Restrict a value to a certain range. Read more
Source§

impl PartialEq for Timestamp

Source§

fn eq(&self, other: &Self) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl PartialOrd for Timestamp

Source§

fn partial_cmp(&self, other: &Self) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 (const: unstable) · Source§

fn lt(&self, other: &Rhs) -> bool

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 (const: unstable) · Source§

fn le(&self, other: &Rhs) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 (const: unstable) · Source§

fn gt(&self, other: &Rhs) -> bool

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 (const: unstable) · Source§

fn ge(&self, other: &Rhs) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl Serialize for Timestamp

Source§

fn serialize<S: Serializer>(&self, ser: S) -> Result<S::Ok, S::Error>

Serialize this value into the given Serde serializer. Read more
Source§

impl StructuralPartialEq for Timestamp

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.