common_time::timestamp

Struct Timestamp

source
pub struct Timestamp {
    value: i64,
    unit: TimeUnit,
}
Expand description

Timestamp represents the value of units(seconds/milliseconds/microseconds/nanoseconds) elapsed since UNIX epoch. The valid value range of Timestamp depends on it’s unit (all in UTC timezone):

§Note:

For values out of range, you can still store these timestamps, but while performing arithmetic or formatting operations, it will return an error or just overflow.

Fields§

§value: i64§unit: TimeUnit

Implementations§

source§

impl Timestamp

source

pub fn current_millis() -> Self

Creates current timestamp in millisecond.

source

pub fn current_time(unit: TimeUnit) -> Timestamp

Creates current timestamp in specific time unit.

source

pub fn sub_duration(&self, duration: Duration) -> Result<Self>

Subtracts a duration from timestamp.

§Note

The result time unit remains unchanged even if duration has a different unit with self. For example, a timestamp with value 1 and time unit second, subtracted by 1 millisecond and the result is still 1 second.

source

pub fn add_duration(&self, duration: Duration) -> Result<Self>

Adds a duration to timestamp.

§Note

The result time unit remains unchanged even if duration has a different unit with self. For example, a timestamp with value 1 and time unit second, subtracted by 1 millisecond and the result is still 1 second.

source

pub fn add_year_month(&self, interval: IntervalYearMonth) -> Option<Timestamp>

Adds given IntervalYearMonth to the current timestamp.

source

pub fn add_day_time(&self, interval: IntervalDayTime) -> Option<Timestamp>

Adds given IntervalDayTime to the current timestamp.

source

pub fn add_month_day_nano( &self, interval: IntervalMonthDayNano, ) -> Option<Timestamp>

Adds given IntervalMonthDayNano to the current timestamp.

source

pub fn sub_year_month(&self, interval: IntervalYearMonth) -> Option<Timestamp>

Subtracts given IntervalYearMonth to the current timestamp.

source

pub fn sub_day_time(&self, interval: IntervalDayTime) -> Option<Timestamp>

Subtracts given IntervalDayTime to the current timestamp.

source

pub fn sub_month_day_nano( &self, interval: IntervalMonthDayNano, ) -> Option<Timestamp>

Subtracts given IntervalMonthDayNano to the current timestamp.

source

pub fn sub(&self, rhs: &Self) -> Option<Duration>

Subtracts current timestamp with another timestamp, yielding a duration.

source

pub fn new(value: i64, unit: TimeUnit) -> Self

source

pub const fn new_second(value: i64) -> Self

source

pub const fn new_millisecond(value: i64) -> Self

source

pub const fn new_microsecond(value: i64) -> Self

source

pub const fn new_nanosecond(value: i64) -> Self

source

pub fn unit(&self) -> TimeUnit

source

pub fn value(&self) -> i64

source

pub fn convert_to(&self, unit: TimeUnit) -> Option<Timestamp>

Convert a timestamp to given time unit. Conversion from a timestamp with smaller unit to a larger unit may cause rounding error. Return None if conversion causes overflow.

source

pub fn convert_to_ceil(&self, unit: TimeUnit) -> Option<Timestamp>

Convert a timestamp to given time unit. Conversion from a timestamp with smaller unit to a larger unit will round the value to ceil (positive infinity). Return None if conversion causes overflow.

source

pub fn split(&self) -> (i64, u32)

Split a Timestamp into seconds part and nanoseconds part. Notice the seconds part of split result is always rounded down to floor.

source

fn from_splits(sec: i64, nsec: u32) -> Option<Self>

Creates a new Timestamp instance from seconds and nanoseconds parts. Returns None if overflow.

source

pub fn to_iso8601_string(&self) -> String

Format timestamp to ISO8601 string. If the timestamp exceeds what chrono timestamp can represent, this function simply print the timestamp unit and value in plain string.

source

pub fn to_local_string(&self) -> String

Format timestamp use system timezone.

source

pub fn to_timezone_aware_string(&self, tz: Option<&Timezone>) -> String

Format timestamp for given timezone. If tz==None, the server default timezone will used.

source

pub fn as_formatted_string( self, pattern: &str, timezone: Option<&Timezone>, ) -> Result<String>

Format timestamp for given format and timezone. If tz==None, the server default timezone will used.

source

pub fn to_chrono_datetime(&self) -> Option<NaiveDateTime>

source

pub fn to_chrono_datetime_with_timezone( &self, tz: Option<&Timezone>, ) -> Option<NaiveDateTime>

source

pub fn to_chrono_date(&self) -> Option<NaiveDate>

Convert timestamp to chrono date.

source

pub fn to_chrono_time(&self) -> Option<NaiveTime>

Convert timestamp to chrono time.

source

pub fn from_chrono_datetime(ndt: NaiveDateTime) -> Option<Self>

source

pub fn from_chrono_date(date: NaiveDate) -> Option<Self>

source

pub fn from_str_utc(s: &str) -> Result<Self>

Accepts a string in RFC3339 / ISO8601 standard format and some variants and converts it to a nanosecond precision timestamp. It no timezone specified in string, it cast to nanosecond epoch timestamp in UTC.

source

pub fn from_str(s: &str, timezone: Option<&Timezone>) -> Result<Self>

Accepts a string in RFC3339 / ISO8601 standard format and some variants and converts it to a nanosecond precision timestamp. This code is copied from arrow-datafusion with some bugfixes. Supported format:

  • 2022-09-20T14:16:43.012345Z (Zulu timezone)
  • 2022-09-20T14:16:43.012345+08:00 (Explicit offset)
  • 2022-09-20T14:16:43.012345 (The given timezone, with T)
  • 2022-09-20T14:16:43 (Zulu timezone, no fractional seconds, with T)
  • 2022-09-20 14:16:43.012345Z (Zulu timezone, without T)
  • 2022-09-20 14:16:43 (The given timezone, without T)
  • 2022-09-20 14:16:43.012345 (The given timezone, without T)
source

pub fn negative(self) -> Self

source§

impl Timestamp

source

pub const MIN_SECOND: Self = _

source

pub const MAX_SECOND: Self = _

source

pub const MIN_MILLISECOND: Self = _

source

pub const MAX_MILLISECOND: Self = _

source

pub const MIN_MICROSECOND: Self = _

source

pub const MAX_MICROSECOND: Self = _

source

pub const MIN_NANOSECOND: Self = _

source

pub const MAX_NANOSECOND: Self = _

Trait Implementations§

source§

impl BucketAligned for Timestamp

source§

fn align_by_bucket(self, bucket_duration: i64) -> Option<Self>

Aligns the value by bucket_duration or None if underflow occurred. Read more
source§

fn align_to_ceil_by_bucket(self, bucket_duration: i64) -> Option<Self>

Aligns the value by bucket_duration to ceil or None if overflow occurred. Read more
source§

impl Clone for Timestamp

source§

fn clone(&self) -> Timestamp

Returns a copy 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 Timestamp

source§

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

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

impl Default for Timestamp

source§

fn default() -> Timestamp

Returns the “default value” for a type. Read more
source§

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

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 From<Timestamp> for Value

source§

fn from(d: Timestamp) -> Self

Converts to this type from the input type.
source§

impl From<Timestamp> for i64

source§

fn from(t: Timestamp) -> Self

Converts to this type from the input type.
source§

impl From<i64> for Timestamp

source§

fn from(v: i64) -> Self

Converts to this type from the input type.
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

A proper implementation of total order requires antisymmetry, reflexivity, transitivity and totality. In this comparison implementation, we map a timestamp uniquely to a (i64, i64) tuple which is respectively total order.

source§

fn cmp(&self, other: &Self) -> 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 Timestamp

source§

fn eq(&self, other: &Self) -> 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 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 · 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 Timestamp

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 Copy for Timestamp

source§

impl Eq 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
§

impl<T> CallHasher for T
where T: Hash + ?Sized,

§

default fn get_hash<H, B>(value: &H, build_hasher: &B) -> u64
where H: Hash + ?Sized, B: BuildHasher,

source§

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

source§

unsafe fn clone_to_uninit(&self, dst: *mut T)

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

impl<Q, K> Comparable<K> for Q
where Q: Ord + ?Sized, K: Borrow<Q> + ?Sized,

§

fn compare(&self, key: &K) -> Ordering

Compare self to key and return their ordering.
§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

§

fn equivalent(&self, key: &K) -> bool

Compare self to key and return true if they are equal.
source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

source§

fn equivalent(&self, key: &K) -> bool

Compare self to key and return true if they are equal.
§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

§

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

§

fn from_ref(input: &T) -> T

Converts to this type from a reference to the input type.
§

impl<T> Instrument for T

§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided [Span], returning an Instrumented wrapper. Read more
§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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> IntoRequest<T> for T

source§

fn into_request(self) -> Request<T>

Wrap the input message T in a tonic::Request
source§

impl<T> Same for T

source§

type Output = T

Should always be Self
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, 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.
§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

§

fn vzip(self) -> V

§

impl<T> WithSubscriber for T

§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a [WithDispatch] wrapper. Read more
§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a [WithDispatch] wrapper. Read more
§

impl<T> Allocation for T
where T: RefUnwindSafe + Send + Sync,

source§

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

source§

impl<N> NodeTrait for N
where N: Copy + Ord + Hash,