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
impl NanoTimestamp
Sourcepub const MIN: NanoTimestamp
pub const MIN: NanoTimestamp
Smallest representable timestamp (earliest possible time).
Sourcepub const MAX: NanoTimestamp
pub const MAX: NanoTimestamp
Largest representable timestamp (latest possible time).
Sourcepub fn as_nanos(&self) -> u128
pub fn as_nanos(&self) -> u128
Returns the raw nanosecond value as u128.
Saturates to zero for negative timestamps (before the epoch).
Sourcepub fn now() -> Self
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).
Sourcepub fn elapsed(&self) -> i64
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.
Sourcepub fn duration_since(&self, other: NanoTimestamp) -> i64
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.
Sourcepub fn diff_millis(&self, other: NanoTimestamp) -> i64
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).
Sourcepub fn elapsed_nanos_since(&self, other: NanoTimestamp) -> Option<i64>
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.
Sourcepub fn add_nanos(&self, nanos: i64) -> NanoTimestamp
pub fn add_nanos(&self, nanos: i64) -> NanoTimestamp
Returns a new NanoTimestamp offset by nanos (positive = forward in time).
Sourcepub fn add_millis(&self, ms: i64) -> NanoTimestamp
pub fn add_millis(&self, ms: i64) -> NanoTimestamp
Returns a new NanoTimestamp offset by ms milliseconds.
Sourcepub fn add_seconds(&self, secs: i64) -> NanoTimestamp
pub fn add_seconds(&self, secs: i64) -> NanoTimestamp
Returns a new NanoTimestamp offset by secs seconds.
Sourcepub fn add_minutes(&self, minutes: i64) -> NanoTimestamp
pub fn add_minutes(&self, minutes: i64) -> NanoTimestamp
Shifts this timestamp forward by minutes minutes (negative values go backwards).
Sourcepub fn add_hours(&self, hours: i64) -> NanoTimestamp
pub fn add_hours(&self, hours: i64) -> NanoTimestamp
Shifts this timestamp forward by hours hours (negative values go backwards).
Sourcepub fn is_before(&self, other: NanoTimestamp) -> bool
pub fn is_before(&self, other: NanoTimestamp) -> bool
Returns true if self is strictly earlier than other.
Sourcepub fn is_after(&self, other: NanoTimestamp) -> bool
pub fn is_after(&self, other: NanoTimestamp) -> bool
Returns true if self is strictly later than other.
Sourcepub fn is_same_second(&self, other: NanoTimestamp) -> bool
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).
Sourcepub fn is_same_minute(&self, other: NanoTimestamp) -> bool
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).
Sourcepub fn from_millis(ms: i64) -> Self
pub fn from_millis(ms: i64) -> Self
Constructs a NanoTimestamp from milliseconds since the Unix epoch.
Sourcepub fn to_millis(&self) -> i64
pub fn to_millis(&self) -> i64
Returns milliseconds since the Unix epoch (truncates sub-millisecond precision).
Sourcepub fn from_secs(secs: i64) -> Self
pub fn from_secs(secs: i64) -> Self
Constructs a NanoTimestamp from whole seconds since the Unix epoch.
Sourcepub fn to_secs(&self) -> i64
pub fn to_secs(&self) -> i64
Returns whole seconds since the Unix epoch (truncates sub-second precision).
Sourcepub fn from_datetime(dt: DateTime<Utc>) -> Self
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.
Sourcepub fn to_datetime(&self) -> DateTime<Utc>
pub fn to_datetime(&self) -> DateTime<Utc>
Converts this timestamp to a DateTime<Utc>.
Sourcepub fn to_seconds(&self) -> f64
pub fn to_seconds(&self) -> f64
Converts this timestamp to floating-point seconds since the Unix epoch.
Sourcepub fn duration_millis(self, other: NanoTimestamp) -> i64
pub fn duration_millis(self, other: NanoTimestamp) -> i64
Returns the signed millisecond difference self - other.
Positive when self is after other.
Sourcepub fn min(self, other: NanoTimestamp) -> NanoTimestamp
pub fn min(self, other: NanoTimestamp) -> NanoTimestamp
Returns the earlier of self and other.
Sourcepub fn max(self, other: NanoTimestamp) -> NanoTimestamp
pub fn max(self, other: NanoTimestamp) -> NanoTimestamp
Returns the later of self and other.
Sourcepub fn elapsed_since(self, earlier: NanoTimestamp) -> i64
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.
Sourcepub fn seconds_since(self, earlier: NanoTimestamp) -> i64
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.
Sourcepub fn minutes_since(self, earlier: NanoTimestamp) -> i64
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.
Sourcepub fn hours_since(self, earlier: NanoTimestamp) -> i64
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.
Sourcepub fn round_down_to(&self, period_nanos: i64) -> NanoTimestamp
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.
Sourcepub fn to_date_string(&self) -> String
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).
Sourcepub fn is_same_day(&self, other: NanoTimestamp) -> bool
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.
Sourcepub fn floor_to_hour(&self) -> NanoTimestamp
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.
Sourcepub fn hour_of_day(self) -> u8
pub fn hour_of_day(self) -> u8
Returns the UTC hour of day (0–23).
Sourcepub fn minute_of_hour(self) -> u8
pub fn minute_of_hour(self) -> u8
Returns the minute within the current UTC hour (0–59).
Sourcepub fn is_market_hours(self, open_hour: u8, close_hour: u8) -> bool
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.
Sourcepub fn floor_to_day(&self) -> NanoTimestamp
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.
Sourcepub fn floor_to_minute(&self) -> NanoTimestamp
pub fn floor_to_minute(&self) -> NanoTimestamp
Truncates nanoseconds and seconds: returns the timestamp at HH:MM:00.000000000.
Sourcepub fn elapsed_seconds(&self, other: NanoTimestamp) -> f64
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.
Sourcepub fn to_datetime_string(&self) -> String
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.
Sourcepub fn is_between(self, start: NanoTimestamp, end: NanoTimestamp) -> bool
pub fn is_between(self, start: NanoTimestamp, end: NanoTimestamp) -> bool
Returns true if self falls within [start, end] (inclusive on both ends).
Sourcepub fn to_unix_ms(self) -> i64
pub fn to_unix_ms(self) -> i64
Converts this timestamp to Unix milliseconds (truncating sub-millisecond precision).
Sourcepub fn to_unix_seconds(self) -> i64
pub fn to_unix_seconds(self) -> i64
Returns whole seconds since the Unix epoch (truncates sub-second precision).
Sourcepub fn second_of_minute(self) -> u8
pub fn second_of_minute(self) -> u8
Returns the second within the current UTC minute (0–59).
Sourcepub fn day_of_week(self) -> u8
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).
Sourcepub fn sub_minutes(&self, minutes: i64) -> NanoTimestamp
pub fn sub_minutes(&self, minutes: i64) -> NanoTimestamp
Shifts the timestamp backward by minutes minutes.
Equivalent to add_minutes(-minutes).
Sourcepub fn is_weekend(self) -> bool
pub fn is_weekend(self) -> bool
Returns true if this timestamp falls on a Saturday (5) or Sunday (6) in UTC.
Sourcepub fn start_of_week(self) -> NanoTimestamp
pub fn start_of_week(self) -> NanoTimestamp
Returns the timestamp floored to Monday 00:00:00 UTC of the containing week.
Sourcepub fn add_days(&self, days: i64) -> NanoTimestamp
pub fn add_days(&self, days: i64) -> NanoTimestamp
Shifts the timestamp forward by days calendar days (positive or negative).
Sourcepub fn minutes_between(self, other: NanoTimestamp) -> u64
pub fn minutes_between(self, other: NanoTimestamp) -> u64
Returns the absolute number of whole minutes between self and other.
Sourcepub fn seconds_between(self, other: NanoTimestamp) -> u64
pub fn seconds_between(self, other: NanoTimestamp) -> u64
Returns the absolute number of whole seconds between self and other.
Sourcepub fn day_of_year(self) -> u16
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.
Sourcepub fn quarter(self) -> u8
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.
Sourcepub fn week_of_year(self) -> u32
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.
Sourcepub fn is_same_week(self, other: NanoTimestamp) -> bool
pub fn is_same_week(self, other: NanoTimestamp) -> bool
Returns true if self and other fall in the same ISO calendar week and year.
Sourcepub fn is_same_month(self, other: NanoTimestamp) -> bool
pub fn is_same_month(self, other: NanoTimestamp) -> bool
Returns true if self and other fall in the same calendar month and year.
Sourcepub fn floor_to_week(self) -> NanoTimestamp
pub fn floor_to_week(self) -> NanoTimestamp
Snaps this timestamp to the most recent Monday at 00:00:00 UTC (start of ISO week).
Sourcepub fn is_same_year(self, other: NanoTimestamp) -> bool
pub fn is_same_year(self, other: NanoTimestamp) -> bool
Returns true if self and other fall in the same calendar year.
Sourcepub fn days_between(self, other: NanoTimestamp) -> u64
pub fn days_between(self, other: NanoTimestamp) -> u64
Returns the absolute number of calendar days between two timestamps.
Sourcepub fn end_of_day(self) -> NanoTimestamp
pub fn end_of_day(self) -> NanoTimestamp
Returns a NanoTimestamp at 23:59:59.999_999_999 UTC on the same calendar day.
Sourcepub fn start_of_month(self) -> NanoTimestamp
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.
Sourcepub fn end_of_month(self) -> NanoTimestamp
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.
Sourcepub fn floor_to_second(self) -> NanoTimestamp
pub fn floor_to_second(self) -> NanoTimestamp
Truncates the timestamp to the nearest whole second.
Sourcepub fn is_same_hour(self, other: NanoTimestamp) -> bool
pub fn is_same_hour(self, other: NanoTimestamp) -> bool
Returns true if both timestamps fall in the same UTC hour.
Sourcepub fn add_weeks(&self, weeks: i64) -> NanoTimestamp
pub fn add_weeks(&self, weeks: i64) -> NanoTimestamp
Shifts the timestamp forward by weeks weeks (negative shifts backward).
Sourcepub fn sub_hours(&self, hours: i64) -> NanoTimestamp
pub fn sub_hours(&self, hours: i64) -> NanoTimestamp
Shifts the timestamp backward by hours hours.
Sourcepub fn sub_weeks(&self, weeks: i64) -> NanoTimestamp
pub fn sub_weeks(&self, weeks: i64) -> NanoTimestamp
Shifts the timestamp backward by weeks weeks.
Sourcepub fn sub_seconds(&self, secs: i64) -> NanoTimestamp
pub fn sub_seconds(&self, secs: i64) -> NanoTimestamp
Shifts the timestamp backward by secs seconds.
Sourcepub fn to_time_string(&self) -> String
pub fn to_time_string(&self) -> String
Formats the timestamp as "HH:MM:SS" in UTC.
Sourcepub fn elapsed_hours(&self, other: NanoTimestamp) -> f64
pub fn elapsed_hours(&self, other: NanoTimestamp) -> f64
Elapsed time in hours between self and other (always non-negative).
Sourcepub fn is_today(&self, other: NanoTimestamp) -> bool
pub fn is_today(&self, other: NanoTimestamp) -> bool
Returns true if this timestamp falls on the same UTC calendar day as other.
Sourcepub fn nanoseconds_between(self, other: NanoTimestamp) -> u64
pub fn nanoseconds_between(self, other: NanoTimestamp) -> u64
Absolute difference in nanoseconds between self and other.
Sourcepub fn elapsed_minutes(&self, other: NanoTimestamp) -> f64
pub fn elapsed_minutes(&self, other: NanoTimestamp) -> f64
Elapsed time in minutes between self and other (always non-negative).
Sourcepub fn elapsed_days(&self, other: NanoTimestamp) -> f64
pub fn elapsed_days(&self, other: NanoTimestamp) -> f64
Elapsed time in calendar days (as a float) between self and other.
Sourcepub fn sub_nanos(&self, nanos: i64) -> NanoTimestamp
pub fn sub_nanos(&self, nanos: i64) -> NanoTimestamp
Shifts the timestamp backward by nanos nanoseconds.
Sourcepub fn start_of_year(self) -> NanoTimestamp
pub fn start_of_year(self) -> NanoTimestamp
Truncates to the first nanosecond of the UTC year (January 1, 00:00:00.000000000).
Sourcepub fn end_of_year(self) -> NanoTimestamp
pub fn end_of_year(self) -> NanoTimestamp
Returns the last nanosecond of the UTC year containing this timestamp.
Sourcepub fn add_months(&self, months: i32) -> NanoTimestamp
pub fn add_months(&self, months: i32) -> NanoTimestamp
Adds months calendar months, clamping to the last day of the resulting month.
Sourcepub fn start_of_quarter(self) -> NanoTimestamp
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).
Sourcepub fn end_of_quarter(self) -> NanoTimestamp
pub fn end_of_quarter(self) -> NanoTimestamp
Returns the NanoTimestamp at the last nanosecond of the current calendar quarter.
Sourcepub fn is_same_quarter(self, other: NanoTimestamp) -> bool
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.
impl Add<i64> for NanoTimestamp
NanoTimestamp + i64 shifts the timestamp forward by nanos nanoseconds.
Source§type Output = NanoTimestamp
type Output = NanoTimestamp
+ operator.Source§impl Clone for NanoTimestamp
impl Clone for NanoTimestamp
Source§fn clone(&self) -> NanoTimestamp
fn clone(&self) -> NanoTimestamp
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl Debug for NanoTimestamp
impl Debug for NanoTimestamp
Source§impl<'de> Deserialize<'de> for NanoTimestamp
impl<'de> Deserialize<'de> for NanoTimestamp
Source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
Source§impl Display for NanoTimestamp
impl Display for NanoTimestamp
Source§impl Ord for NanoTimestamp
impl Ord for NanoTimestamp
Source§fn cmp(&self, other: &NanoTimestamp) -> Ordering
fn cmp(&self, other: &NanoTimestamp) -> Ordering
1.21.0 · Source§fn max(self, other: Self) -> Selfwhere
Self: Sized,
fn max(self, other: Self) -> Selfwhere
Self: Sized,
Source§impl PartialEq for NanoTimestamp
impl PartialEq for NanoTimestamp
Source§impl PartialOrd for NanoTimestamp
impl PartialOrd for NanoTimestamp
Source§impl Serialize for NanoTimestamp
impl Serialize for NanoTimestamp
Source§impl Sub<i64> for NanoTimestamp
NanoTimestamp - i64 shifts the timestamp backward by nanos nanoseconds.
impl Sub<i64> for NanoTimestamp
NanoTimestamp - i64 shifts the timestamp backward by nanos nanoseconds.
Source§type Output = NanoTimestamp
type Output = NanoTimestamp
- operator.Source§impl Sub for NanoTimestamp
NanoTimestamp - NanoTimestamp returns the signed nanosecond difference.
impl Sub for NanoTimestamp
NanoTimestamp - NanoTimestamp returns the signed nanosecond difference.