Skip to main content

NanoTimestamp

Struct NanoTimestamp 

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

Exchange-epoch timestamp with nanosecond resolution.

Stores nanoseconds since the Unix epoch (UTC). The inner field is private; use NanoTimestamp::new to construct and NanoTimestamp::nanos to read.

Implementations§

Source§

impl NanoTimestamp

Source

pub const MIN: NanoTimestamp

Smallest representable timestamp (earliest possible time).

Source

pub const MAX: NanoTimestamp

Largest representable timestamp (latest possible time).

Source

pub fn new(nanos: i64) -> Self

Constructs a NanoTimestamp from a raw nanosecond integer.

Source

pub fn nanos(&self) -> i64

Returns the raw nanosecond value.

Source

pub fn as_nanos(&self) -> u128

Returns the raw nanosecond value as u128.

Saturates to zero for negative timestamps (before the epoch).

Source

pub fn now() -> Self

Returns the current UTC time as a NanoTimestamp.

Falls back to 0 if the system clock overflows nanosecond range (extremely unlikely).

Source

pub fn elapsed(&self) -> i64

Returns the nanoseconds elapsed since self (i.e. NanoTimestamp::now() - self).

Positive when self is in the past, negative when self is in the future.

Source

pub fn duration_since(&self, other: NanoTimestamp) -> i64

Returns the signed nanosecond difference self - other.

Positive when self is later than other, negative when earlier.

Source

pub fn diff_millis(&self, other: NanoTimestamp) -> i64

Returns the signed millisecond difference self - other.

Positive when self is later than other. Rounds toward zero (truncates sub-millisecond nanoseconds).

Source

pub fn elapsed_nanos_since(&self, other: NanoTimestamp) -> Option<i64>

Returns Some(nanos) if self >= other (non-negative elapsed time), otherwise None.

Use this when you want to measure forward elapsed time and treat a negative difference as “not yet elapsed” rather than a negative value.

Source

pub fn add_nanos(&self, nanos: i64) -> NanoTimestamp

Returns a new NanoTimestamp offset by nanos (positive = forward in time).

Source

pub fn add_millis(&self, ms: i64) -> NanoTimestamp

Returns a new NanoTimestamp offset by ms milliseconds.

Source

pub fn add_seconds(&self, secs: i64) -> NanoTimestamp

Returns a new NanoTimestamp offset by secs seconds.

Source

pub fn add_minutes(&self, minutes: i64) -> NanoTimestamp

Shifts this timestamp forward by minutes minutes (negative values go backwards).

Source

pub fn add_hours(&self, hours: i64) -> NanoTimestamp

Shifts this timestamp forward by hours hours (negative values go backwards).

Source

pub fn is_before(&self, other: NanoTimestamp) -> bool

Returns true if self is strictly earlier than other.

Source

pub fn is_after(&self, other: NanoTimestamp) -> bool

Returns true if self is strictly later than other.

Source

pub fn is_same_second(&self, other: NanoTimestamp) -> bool

Returns true if self and other fall within the same calendar second.

Two timestamps are in the same second when floor(self / 1_000_000_000) == floor(other / 1_000_000_000).

Source

pub fn is_same_minute(&self, other: NanoTimestamp) -> bool

Returns true if self and other fall within the same calendar minute.

Two timestamps are in the same minute when floor(self / 60_000_000_000) == floor(other / 60_000_000_000).

Source

pub fn from_millis(ms: i64) -> Self

Constructs a NanoTimestamp from milliseconds since the Unix epoch.

Source

pub fn to_millis(&self) -> i64

Returns milliseconds since the Unix epoch (truncates sub-millisecond precision).

Source

pub fn from_secs(secs: i64) -> Self

Constructs a NanoTimestamp from whole seconds since the Unix epoch.

Source

pub fn to_secs(&self) -> i64

Returns whole seconds since the Unix epoch (truncates sub-second precision).

Source

pub fn from_datetime(dt: DateTime<Utc>) -> Self

Constructs a NanoTimestamp from a DateTime<Utc>.

Falls back to 0 if the datetime is outside the representable nanosecond range.

Source

pub fn to_datetime(&self) -> DateTime<Utc>

Converts this timestamp to a DateTime<Utc>.

Source

pub fn to_seconds(&self) -> f64

Converts this timestamp to floating-point seconds since the Unix epoch.

Source

pub fn duration_millis(self, other: NanoTimestamp) -> i64

Returns the signed millisecond difference self - other.

Positive when self is after other.

Source

pub fn min(self, other: NanoTimestamp) -> NanoTimestamp

Returns the earlier of self and other.

Source

pub fn max(self, other: NanoTimestamp) -> NanoTimestamp

Returns the later of self and other.

Source

pub fn elapsed_since(self, earlier: NanoTimestamp) -> i64

Returns the signed nanosecond difference self - earlier.

Positive when self is after earlier, negative when before. Use for computing durations between two timestamps without assuming ordering.

Source

pub fn seconds_since(self, earlier: NanoTimestamp) -> i64

Returns the difference in whole seconds: (self - earlier) / 1_000_000_000.

Positive when self is after earlier.

Source

pub fn minutes_since(self, earlier: NanoTimestamp) -> i64

Returns the difference in whole minutes: (self - earlier) / 60_000_000_000.

Positive when self is after earlier.

Source

pub fn hours_since(self, earlier: NanoTimestamp) -> i64

Returns the difference in whole hours: (self - earlier) / 3_600_000_000_000.

Positive when self is after earlier.

Source

pub fn round_down_to(&self, period_nanos: i64) -> NanoTimestamp

Snaps this timestamp down to the nearest multiple of period_nanos.

For example, rounding ts=1_500_000_000 down to period_nanos=1_000_000_000 yields 1_000_000_000. Useful for bar-boundary calculations.

Returns self unchanged when period_nanos == 0.

Source

pub fn to_date_string(&self) -> String

Formats this timestamp as a UTC date string "YYYY-MM-DD".

Useful for grouping bars or ticks by calendar date (e.g., daily session boundaries).

Source

pub fn is_same_day(&self, other: NanoTimestamp) -> bool

Returns true if self and other fall on the same UTC calendar day.

Useful for detecting session boundaries when grouping bars by date.

Source

pub fn floor_to_hour(&self) -> NanoTimestamp

Floors this timestamp to the start of the current UTC minute.

Floors this timestamp to the start of the current UTC hour.

Truncates minutes, seconds, and nanoseconds: returns HH:00:00.000000000.

Source

pub fn hour_of_day(self) -> u8

Returns the UTC hour of day (0–23).

Source

pub fn minute_of_hour(self) -> u8

Returns the minute within the current UTC hour (0–59).

Source

pub fn is_market_hours(self, open_hour: u8, close_hour: u8) -> bool

Returns true if the UTC hour falls within [open_hour, close_hour).

Useful for checking whether a timestamp is within a trading session. Both open_hour and close_hour must be in 0–23; if open_hour >= close_hour the function returns false.

Source

pub fn floor_to_day(&self) -> NanoTimestamp

Floors this timestamp to midnight UTC (start of the day).

Truncates hours, minutes, seconds, and nanoseconds: returns 00:00:00.000000000.

Source

pub fn floor_to_minute(&self) -> NanoTimestamp

Truncates nanoseconds and seconds: returns the timestamp at HH:MM:00.000000000.

Source

pub fn elapsed_seconds(&self, other: NanoTimestamp) -> f64

Returns the signed elapsed time between self and other in seconds.

Positive when self is after other. Resolution is nanoseconds.

Source

pub fn to_datetime_string(&self) -> String

Formats this timestamp as a UTC datetime string "YYYY-MM-DD HH:MM:SS".

Useful for logging and display when a full datetime is needed rather than just the date.

Source

pub fn is_between(self, start: NanoTimestamp, end: NanoTimestamp) -> bool

Returns true if self falls within [start, end] (inclusive on both ends).

Source

pub fn to_unix_ms(self) -> i64

Converts this timestamp to Unix milliseconds (truncating sub-millisecond precision).

Source

pub fn to_unix_seconds(self) -> i64

Returns whole seconds since the Unix epoch (truncates sub-second precision).

Source

pub fn second_of_minute(self) -> u8

Returns the second within the current UTC minute (0–59).

Source

pub fn day_of_week(self) -> u8

Returns the day of the week as u8 where Monday = 0 and Sunday = 6.

Computed from the Unix epoch (1970-01-01 was a Thursday = 3).

Source

pub fn sub_minutes(&self, minutes: i64) -> NanoTimestamp

Shifts the timestamp backward by minutes minutes.

Equivalent to add_minutes(-minutes).

Source

pub fn is_weekend(self) -> bool

Returns true if this timestamp falls on a Saturday (5) or Sunday (6) in UTC.

Source

pub fn start_of_week(self) -> NanoTimestamp

Returns the timestamp floored to Monday 00:00:00 UTC of the containing week.

Source

pub fn add_days(&self, days: i64) -> NanoTimestamp

Shifts the timestamp forward by days calendar days (positive or negative).

Source

pub fn minutes_between(self, other: NanoTimestamp) -> u64

Returns the absolute number of whole minutes between self and other.

Source

pub fn seconds_between(self, other: NanoTimestamp) -> u64

Returns the absolute number of whole seconds between self and other.

Source

pub fn day_of_year(self) -> u16

Returns the day of the year (1 = January 1, 365/366 = December 31).

Uses the UTC calendar. The Unix epoch (1970-01-01) is day 1.

Source

pub fn quarter(self) -> u8

Returns the quarter number: 1 (Jan–Mar), 2 (Apr–Jun), 3 (Jul–Sep), or 4 (Oct–Dec).

Uses the UTC calendar.

Source

pub fn week_of_year(self) -> u32

Returns the ISO 8601 week number (1–53).

Uses the UTC calendar. Week 1 is the first week containing a Thursday.

Source

pub fn is_same_week(self, other: NanoTimestamp) -> bool

Returns true if self and other fall in the same ISO calendar week and year.

Source

pub fn is_same_month(self, other: NanoTimestamp) -> bool

Returns true if self and other fall in the same calendar month and year.

Source

pub fn floor_to_week(self) -> NanoTimestamp

Snaps this timestamp to the most recent Monday at 00:00:00 UTC (start of ISO week).

Source

pub fn is_same_year(self, other: NanoTimestamp) -> bool

Returns true if self and other fall in the same calendar year.

Source

pub fn days_between(self, other: NanoTimestamp) -> u64

Returns the absolute number of calendar days between two timestamps.

Source

pub fn end_of_day(self) -> NanoTimestamp

Returns a NanoTimestamp at 23:59:59.999_999_999 UTC on the same calendar day.

Source

pub fn start_of_month(self) -> NanoTimestamp

Returns a NanoTimestamp at 00:00:00.000_000_000 UTC on the first day of the same month.

Source

pub fn end_of_month(self) -> NanoTimestamp

Returns a NanoTimestamp at 23:59:59.999_999_999 UTC on the last day of the same month.

Source

pub fn floor_to_second(self) -> NanoTimestamp

Truncates the timestamp to the nearest whole second.

Source

pub fn is_same_hour(self, other: NanoTimestamp) -> bool

Returns true if both timestamps fall in the same UTC hour.

Source

pub fn add_weeks(&self, weeks: i64) -> NanoTimestamp

Shifts the timestamp forward by weeks weeks (negative shifts backward).

Source

pub fn sub_hours(&self, hours: i64) -> NanoTimestamp

Shifts the timestamp backward by hours hours.

Source

pub fn sub_weeks(&self, weeks: i64) -> NanoTimestamp

Shifts the timestamp backward by weeks weeks.

Source

pub fn sub_seconds(&self, secs: i64) -> NanoTimestamp

Shifts the timestamp backward by secs seconds.

Source

pub fn to_time_string(&self) -> String

Formats the timestamp as "HH:MM:SS" in UTC.

Source

pub fn elapsed_hours(&self, other: NanoTimestamp) -> f64

Elapsed time in hours between self and other (always non-negative).

Source

pub fn is_today(&self, other: NanoTimestamp) -> bool

Returns true if this timestamp falls on the same UTC calendar day as other.

Source

pub fn nanoseconds_between(self, other: NanoTimestamp) -> u64

Absolute difference in nanoseconds between self and other.

Source

pub fn elapsed_minutes(&self, other: NanoTimestamp) -> f64

Elapsed time in minutes between self and other (always non-negative).

Source

pub fn elapsed_days(&self, other: NanoTimestamp) -> f64

Elapsed time in calendar days (as a float) between self and other.

Source

pub fn sub_nanos(&self, nanos: i64) -> NanoTimestamp

Shifts the timestamp backward by nanos nanoseconds.

Source

pub fn start_of_year(self) -> NanoTimestamp

Truncates to the first nanosecond of the UTC year (January 1, 00:00:00.000000000).

Source

pub fn end_of_year(self) -> NanoTimestamp

Returns the last nanosecond of the UTC year containing this timestamp.

Source

pub fn add_months(&self, months: i32) -> NanoTimestamp

Adds months calendar months, clamping to the last day of the resulting month.

Source

pub fn start_of_quarter(self) -> NanoTimestamp

Returns the NanoTimestamp at the start of the current calendar quarter (Jan/Apr/Jul/Oct 1 00:00:00.000000000 UTC).

Source

pub fn end_of_quarter(self) -> NanoTimestamp

Returns the NanoTimestamp at the last nanosecond of the current calendar quarter.

Source

pub fn is_same_quarter(self, other: NanoTimestamp) -> bool

Returns true if self and other fall in the same calendar quarter and year.

Trait Implementations§

Source§

impl Add<i64> for NanoTimestamp

NanoTimestamp + i64 shifts the timestamp forward by nanos nanoseconds.

Source§

type Output = NanoTimestamp

The resulting type after applying the + operator.
Source§

fn add(self, rhs: i64) -> NanoTimestamp

Performs the + operation. Read more
Source§

impl Clone for NanoTimestamp

Source§

fn clone(&self) -> NanoTimestamp

Returns a duplicate of the value. Read more
1.0.0 · Source§

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

Performs copy-assignment from source. Read more
Source§

impl Debug for NanoTimestamp

Source§

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

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

impl<'de> Deserialize<'de> for NanoTimestamp

Source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

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

impl Display for NanoTimestamp

Source§

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

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

impl Ord for NanoTimestamp

Source§

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

This method returns an Ordering between self and other. Read more
1.21.0 · Source§

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

Compares and returns the maximum of two values. Read more
1.21.0 · Source§

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

Compares and returns the minimum of two values. Read more
1.50.0 · Source§

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

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

impl PartialEq for NanoTimestamp

Source§

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

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

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

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl PartialOrd for NanoTimestamp

Source§

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

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · 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 · 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 · 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 · 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 NanoTimestamp

Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

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

impl Sub<i64> for NanoTimestamp

NanoTimestamp - i64 shifts the timestamp backward by nanos nanoseconds.

Source§

type Output = NanoTimestamp

The resulting type after applying the - operator.
Source§

fn sub(self, rhs: i64) -> NanoTimestamp

Performs the - operation. Read more
Source§

impl Sub for NanoTimestamp

NanoTimestamp - NanoTimestamp returns the signed nanosecond difference.

Source§

type Output = i64

The resulting type after applying the - operator.
Source§

fn sub(self, rhs: NanoTimestamp) -> i64

Performs the - operation. Read more
Source§

impl Copy for NanoTimestamp

Source§

impl Eq for NanoTimestamp

Source§

impl StructuralPartialEq for NanoTimestamp

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> 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 = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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.
Source§

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