DateTime

Struct DateTime 

Source
#[repr(transparent)]
pub struct DateTime<U: TimeUnitTrait = Nanosecond>(pub i64, _);
Expand description

Represents a date and time with a specific time unit precision.

§Type Parameters

  • U: The time unit precision, defaulting to Nanosecond. Must implement TimeUnitTrait.

§Fields

  • 0: An i64 representing the timestamp in the specified time unit.
  • PhantomData<U>: A zero-sized type used to “mark” the time unit without affecting the struct’s memory layout.

Tuple Fields§

§0: i64

Implementations§

Source§

impl<U: TimeUnitTrait> DateTime<U>

Source

pub const fn new(dt: i64) -> Self

Creates a new DateTime instance with the given timestamp.

§Arguments
  • dt - An i64 representing the timestamp in the specified time unit.
§Returns

A new DateTime<U> instance.

Source

pub const fn is_nat(&self) -> bool

Checks if the DateTime instance represents “Not-a-Time” (NaT).

§Returns

true if the instance is NaT, false otherwise.

Source

pub const fn is_not_nat(&self) -> bool

Checks if the DateTime instance represents a valid time (not NaT).

§Returns

true if the instance is not NaT, false otherwise.

Source

pub const fn nat() -> Self

Creates a new DateTime instance representing “Not-a-Time” (NaT).

§Returns

A new DateTime<U> instance representing NaT.

Source

pub const fn into_i64(self) -> i64

Converts the DateTime instance to its underlying i64 timestamp.

§Returns

The i64 timestamp value.

Source

pub const fn from_opt_i64(v: Option<i64>) -> Self

Creates a DateTime instance from an optional i64 timestamp.

§Arguments
  • v - An Option<i64> representing the timestamp.
§Returns

A new DateTime<U> instance. If v is None, returns NaT.

Source

pub const fn into_opt_i64(self) -> Option<i64>

Converts the DateTime instance to an optional i64 timestamp.

§Returns

Some(i64) if the instance is not NaT, None otherwise.

Source

pub fn to_cr(&self) -> Option<CrDateTime<Utc>>
where Self: TryInto<CrDateTime<Utc>>,

👎Deprecated since 0.5.0: use as_cr instead

Converts the DateTime instance to a chrono::DateTime<Utc>.

§Returns

Some(CrDateTime<Utc>) if the conversion is successful, None if the instance is NaT.

Source

pub fn as_cr(&self) -> Option<CrDateTime<Utc>>
where Self: TryInto<CrDateTime<Utc>>,

Source

pub fn parse(s: &str, fmt: Option<&str>) -> TResult<Self>
where Self: From<CrDateTime<Utc>>,

Parses a string into a DateTime instance.

§Arguments
  • s - The string to parse.
  • fmt - An optional format string. If None, tries multiple common formats.
§Returns

A TResult<Self> containing the parsed DateTime or an error.

Source

pub fn strftime(&self, fmt: Option<&str>) -> String
where Self: TryInto<CrDateTime<Utc>>,

Formats the DateTime instance as a string.

§Arguments
  • fmt - An optional format string. If None, uses “%Y-%m-%d %H:%M:%S.%f”.
§Returns

A formatted string representation of the DateTime.

Source

pub fn duration_trunc(self, duration: TimeDelta) -> Self

Truncates the DateTime to a specified duration.

§Arguments
  • duration - A TimeDelta specifying the truncation interval.
§Returns

A new DateTime<U> instance truncated to the specified duration.

Source§

impl<U: TimeUnitTrait> DateTime<U>
where Self: TryInto<DateTime<Utc>>,

Source

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

Returns the time component of the DateTime as a NaiveTime.

§Returns

Option<NaiveTime>: The time component if the DateTime is valid, or None if it’s NaT.

Source

pub fn year(&self) -> Option<i32>

Returns the year.

§Returns

Option<i32>: The year if the DateTime is valid, or None if it’s NaT.

Source

pub fn day(&self) -> Option<usize>

Returns the day of the month (1-31).

§Returns

Option<usize>: The day of the month if the DateTime is valid, or None if it’s NaT.

Source

pub fn month(&self) -> Option<usize>

Returns the month (1-12).

§Returns

Option<usize>: The month if the DateTime is valid, or None if it’s NaT.

Source

pub fn hour(&self) -> Option<usize>

Returns the hour (0-23).

§Returns

Option<usize>: The hour if the DateTime is valid, or None if it’s NaT.

Source

pub fn minute(&self) -> Option<usize>

Returns the minute (0-59).

§Returns

Option<usize>: The minute if the DateTime is valid, or None if it’s NaT.

Source

pub fn second(&self) -> Option<usize>

Returns the second (0-59).

§Returns

Option<usize>: The second if the DateTime is valid, or None if it’s NaT.

Source§

impl<U: TimeUnitTrait> DateTime<U>

Source

pub fn into_unit<T: TimeUnitTrait>(self) -> DateTime<T>

Trait Implementations§

Source§

impl<U: TimeUnitTrait> Add<TimeDelta> for DateTime<U>
where Self: From<DateTime<Utc>> + TryInto<DateTime<Utc>>,

Source§

type Output = DateTime<U>

The resulting type after applying the + operator.
Source§

fn add(self, rhs: TimeDelta) -> Self::Output

Performs the + operation. Read more
Source§

impl<U: Clone + TimeUnitTrait> Clone for DateTime<U>

Source§

fn clone(&self) -> DateTime<U>

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<U: TimeUnitTrait> Debug for DateTime<U>
where Self: TryInto<DateTime<Utc>>,

Source§

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

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

impl<U: TimeUnitTrait> Default for DateTime<U>

Source§

fn default() -> Self

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

impl From<DateTime<Utc>> for DateTime<Nanosecond>

Source§

fn from(dt: CrDateTime<Utc>) -> Self

Converts to this type from the input type.
Source§

impl From<DateTime<Utc>> for DateTime<Microsecond>

Source§

fn from(dt: CrDateTime<Utc>) -> Self

Converts to this type from the input type.
Source§

impl From<DateTime<Utc>> for DateTime<Millisecond>

Source§

fn from(dt: CrDateTime<Utc>) -> Self

Converts to this type from the input type.
Source§

impl From<DateTime<Utc>> for DateTime<Second>

Source§

fn from(dt: CrDateTime<Utc>) -> Self

Converts to this type from the input type.
Source§

impl<U: TimeUnitTrait> From<NaiveDate> for DateTime<U>
where Self: From<DateTime<Utc>>,

Source§

fn from(dt: NaiveDate) -> Self

Converts to this type from the input type.
Source§

impl<U: TimeUnitTrait> From<NaiveDateTime> for DateTime<U>
where Self: From<DateTime<Utc>>,

Source§

fn from(dt: NaiveDateTime) -> Self

Converts to this type from the input type.
Source§

impl<U: TimeUnitTrait> From<Option<NaiveDateTime>> for DateTime<U>
where Self: From<DateTime<Utc>>,

Source§

fn from(dt: Option<NaiveDateTime>) -> Self

Converts to this type from the input type.
Source§

impl<U: TimeUnitTrait> From<Option<i64>> for DateTime<U>

Source§

fn from(dt: Option<i64>) -> Self

Converts to this type from the input type.
Source§

impl<U: TimeUnitTrait> From<i64> for DateTime<U>

Source§

fn from(dt: i64) -> Self

Converts to this type from the input type.
Source§

impl<U: TimeUnitTrait> FromStr for DateTime<U>
where Self: From<DateTime<Utc>>,

Source§

type Err = TError

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<U: Hash + TimeUnitTrait> Hash for DateTime<U>

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<U: Ord + TimeUnitTrait> Ord for DateTime<U>

Source§

fn cmp(&self, other: &DateTime<U>) -> 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<U: PartialEq + TimeUnitTrait> PartialEq for DateTime<U>

Source§

fn eq(&self, other: &DateTime<U>) -> 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<U: PartialOrd + TimeUnitTrait> PartialOrd for DateTime<U>

Source§

fn partial_cmp(&self, other: &DateTime<U>) -> 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<U: TimeUnitTrait> Sub<TimeDelta> for DateTime<U>
where Self: From<DateTime<Utc>> + TryInto<DateTime<Utc>>,

Source§

type Output = DateTime<U>

The resulting type after applying the - operator.
Source§

fn sub(self, rhs: TimeDelta) -> Self::Output

Performs the - operation. Read more
Source§

impl<U: TimeUnitTrait> Sub for DateTime<U>
where Self: From<DateTime<Utc>> + TryInto<DateTime<Utc>>,

Source§

type Output = TimeDelta

The resulting type after applying the - operator.
Source§

fn sub(self, rhs: DateTime<U>) -> Self::Output

Performs the - operation. Read more
Source§

impl TryFrom<DateTime<Microsecond>> for DateTime<Utc>

Source§

type Error = TError

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

fn try_from(dt: DateTime<Microsecond>) -> TResult<Self>

Performs the conversion.
Source§

impl TryFrom<DateTime<Millisecond>> for DateTime<Utc>

Source§

type Error = TError

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

fn try_from(dt: DateTime<Millisecond>) -> TResult<Self>

Performs the conversion.
Source§

impl TryFrom<DateTime<Second>> for DateTime<Utc>

Source§

type Error = TError

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

fn try_from(dt: DateTime<Second>) -> TResult<Self>

Performs the conversion.
Source§

impl TryFrom<DateTime> for DateTime<Utc>

Source§

type Error = TError

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

fn try_from(dt: DateTime<Nanosecond>) -> TResult<Self>

Performs the conversion.
Source§

impl<U: Copy + TimeUnitTrait> Copy for DateTime<U>

Source§

impl<U: Eq + TimeUnitTrait> Eq for DateTime<U>

Source§

impl<U: TimeUnitTrait> Send for DateTime<U>

Source§

impl<U: TimeUnitTrait> StructuralPartialEq for DateTime<U>

Source§

impl<U: TimeUnitTrait> Sync for DateTime<U>

Auto Trait Implementations§

§

impl<U> Freeze for DateTime<U>

§

impl<U> RefUnwindSafe for DateTime<U>
where U: RefUnwindSafe,

§

impl<U> Unpin for DateTime<U>
where U: Unpin,

§

impl<U> UnwindSafe for DateTime<U>
where U: UnwindSafe,

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, 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.