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):
- for TimeUnit::Second: [-262144-01-01 00:00:00, +262143-12-31 23:59:59]
- for TimeUnit::Millisecond: [-262144-01-01 00:00:00.000, +262143-12-31 23:59:59.999]
- for TimeUnit::Microsecond: [-262144-01-01 00:00:00.000000, +262143-12-31 23:59:59.999999]
- for TimeUnit::Nanosecond: [1677-09-21 00:12:43.145224192, 2262-04-11 23:47:16.854775807]
§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
impl Timestamp
sourcepub fn current_millis() -> Self
pub fn current_millis() -> Self
Creates current timestamp in millisecond.
sourcepub fn current_time(unit: TimeUnit) -> Timestamp
pub fn current_time(unit: TimeUnit) -> Timestamp
Creates current timestamp in specific time unit
.
sourcepub fn sub_duration(&self, duration: Duration) -> Result<Self>
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.
sourcepub fn add_duration(&self, duration: Duration) -> Result<Self>
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.
sourcepub fn add_year_month(&self, interval: IntervalYearMonth) -> Option<Timestamp>
pub fn add_year_month(&self, interval: IntervalYearMonth) -> Option<Timestamp>
Adds given IntervalYearMonth to the current timestamp.
sourcepub fn add_day_time(&self, interval: IntervalDayTime) -> Option<Timestamp>
pub fn add_day_time(&self, interval: IntervalDayTime) -> Option<Timestamp>
Adds given IntervalDayTime to the current timestamp.
sourcepub fn add_month_day_nano(
&self,
interval: IntervalMonthDayNano,
) -> Option<Timestamp>
pub fn add_month_day_nano( &self, interval: IntervalMonthDayNano, ) -> Option<Timestamp>
Adds given IntervalMonthDayNano to the current timestamp.
sourcepub fn sub_year_month(&self, interval: IntervalYearMonth) -> Option<Timestamp>
pub fn sub_year_month(&self, interval: IntervalYearMonth) -> Option<Timestamp>
Subtracts given IntervalYearMonth to the current timestamp.
sourcepub fn sub_day_time(&self, interval: IntervalDayTime) -> Option<Timestamp>
pub fn sub_day_time(&self, interval: IntervalDayTime) -> Option<Timestamp>
Subtracts given IntervalDayTime to the current timestamp.
sourcepub fn sub_month_day_nano(
&self,
interval: IntervalMonthDayNano,
) -> Option<Timestamp>
pub fn sub_month_day_nano( &self, interval: IntervalMonthDayNano, ) -> Option<Timestamp>
Subtracts given IntervalMonthDayNano to the current timestamp.
sourcepub fn sub(&self, rhs: &Self) -> Option<Duration>
pub fn sub(&self, rhs: &Self) -> Option<Duration>
Subtracts current timestamp with another timestamp, yielding a duration.
pub fn new(value: i64, unit: TimeUnit) -> Self
pub const fn new_second(value: i64) -> Self
pub const fn new_millisecond(value: i64) -> Self
pub const fn new_microsecond(value: i64) -> Self
pub const fn new_nanosecond(value: i64) -> Self
pub fn unit(&self) -> TimeUnit
pub fn value(&self) -> i64
sourcepub fn convert_to(&self, unit: TimeUnit) -> Option<Timestamp>
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.
sourcepub fn convert_to_ceil(&self, unit: TimeUnit) -> Option<Timestamp>
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.
sourcepub fn split(&self) -> (i64, u32)
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.
sourcefn from_splits(sec: i64, nsec: u32) -> Option<Self>
fn from_splits(sec: i64, nsec: u32) -> Option<Self>
Creates a new Timestamp instance from seconds and nanoseconds parts. Returns None if overflow.
sourcepub fn to_iso8601_string(&self) -> String
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.
sourcepub fn to_local_string(&self) -> String
pub fn to_local_string(&self) -> String
Format timestamp use system timezone.
sourcepub fn to_timezone_aware_string(&self, tz: Option<&Timezone>) -> String
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.
sourcepub fn as_formatted_string(
self,
pattern: &str,
timezone: Option<&Timezone>,
) -> Result<String>
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.
pub fn to_chrono_datetime(&self) -> Option<NaiveDateTime>
pub fn to_chrono_datetime_with_timezone( &self, tz: Option<&Timezone>, ) -> Option<NaiveDateTime>
sourcepub fn to_chrono_date(&self) -> Option<NaiveDate>
pub fn to_chrono_date(&self) -> Option<NaiveDate>
Convert timestamp to chrono date.
sourcepub fn to_chrono_time(&self) -> Option<NaiveTime>
pub fn to_chrono_time(&self) -> Option<NaiveTime>
Convert timestamp to chrono time.
pub fn from_chrono_datetime(ndt: NaiveDateTime) -> Option<Self>
pub fn from_chrono_date(date: NaiveDate) -> Option<Self>
sourcepub fn from_str_utc(s: &str) -> Result<Self>
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.
sourcepub fn from_str(s: &str, timezone: Option<&Timezone>) -> Result<Self>
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)
pub fn negative(self) -> Self
source§impl Timestamp
impl Timestamp
pub const MIN_SECOND: Self = _
pub const MAX_SECOND: Self = _
pub const MIN_MILLISECOND: Self = _
pub const MAX_MILLISECOND: Self = _
pub const MIN_MICROSECOND: Self = _
pub const MAX_MICROSECOND: Self = _
pub const MIN_NANOSECOND: Self = _
pub const MAX_NANOSECOND: Self = _
Trait Implementations§
source§impl BucketAligned for Timestamp
impl BucketAligned for Timestamp
source§fn align_by_bucket(self, bucket_duration: i64) -> Option<Self>
fn align_by_bucket(self, bucket_duration: i64) -> Option<Self>
source§impl<'de> Deserialize<'de> for Timestamp
impl<'de> Deserialize<'de> for Timestamp
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 Ord for Timestamp
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§impl PartialOrd for Timestamp
impl PartialOrd for Timestamp
impl Copy for Timestamp
impl Eq for Timestamp
Auto Trait Implementations§
impl Freeze for Timestamp
impl RefUnwindSafe for Timestamp
impl Send for Timestamp
impl Sync for Timestamp
impl Unpin for Timestamp
impl UnwindSafe for Timestamp
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
§impl<T> CallHasher for T
impl<T> CallHasher for T
source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
source§unsafe fn clone_to_uninit(&self, dst: *mut T)
unsafe fn clone_to_uninit(&self, dst: *mut T)
clone_to_uninit
)§impl<Q, K> Comparable<K> for Q
impl<Q, K> Comparable<K> for Q
§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key
and return true
if they are equal.source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key
and return true
if they are equal.§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
source§impl<T> IntoRequest<T> for T
impl<T> IntoRequest<T> for T
source§fn into_request(self) -> Request<T>
fn into_request(self) -> Request<T>
T
in a tonic::Request